VertexArray.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #include "VertexArray.h"
  9. #include <GL/glew.h>
  10. VertexArray::VertexArray(const void* verts, unsigned int numVerts, Layout layout,
  11. const unsigned int* indices, unsigned int numIndices)
  12. :mNumVerts(numVerts)
  13. ,mNumIndices(numIndices)
  14. {
  15. // Create vertex array
  16. glGenVertexArrays(1, &mVertexArray);
  17. glBindVertexArray(mVertexArray);
  18. unsigned vertexSize = 8 * sizeof(float);
  19. if (layout == PosNormSkinTex)
  20. {
  21. vertexSize = 8 * sizeof(float) + 8 * sizeof(char);
  22. }
  23. // Create vertex buffer
  24. glGenBuffers(1, &mVertexBuffer);
  25. glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
  26. glBufferData(GL_ARRAY_BUFFER, numVerts * vertexSize, verts, GL_STATIC_DRAW);
  27. // Create index buffer
  28. glGenBuffers(1, &mIndexBuffer);
  29. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
  30. glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(unsigned int), indices, GL_STATIC_DRAW);
  31. // Specify the vertex attributes
  32. if (layout == PosNormTex)
  33. {
  34. // Position is 3 floats
  35. glEnableVertexAttribArray(0);
  36. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertexSize, 0);
  37. // Normal is 3 floats
  38. glEnableVertexAttribArray(1);
  39. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, vertexSize,
  40. reinterpret_cast<void*>(sizeof(float) * 3));
  41. // Texture coordinates is 2 floats
  42. glEnableVertexAttribArray(2);
  43. glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, vertexSize,
  44. reinterpret_cast<void*>(sizeof(float) * 6));
  45. }
  46. else if (layout == PosNormSkinTex)
  47. {
  48. // Position is 3 floats
  49. glEnableVertexAttribArray(0);
  50. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertexSize, 0);
  51. // Normal is 3 floats
  52. glEnableVertexAttribArray(1);
  53. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, vertexSize,
  54. reinterpret_cast<void*>(sizeof(float) * 3));
  55. // Skinning indices (keep as ints)
  56. glEnableVertexAttribArray(2);
  57. glVertexAttribIPointer(2, 4, GL_UNSIGNED_BYTE, vertexSize,
  58. reinterpret_cast<void*>(sizeof(float) * 6));
  59. // Skinning weights (convert to floats)
  60. glEnableVertexAttribArray(3);
  61. glVertexAttribPointer(3, 4, GL_UNSIGNED_BYTE, GL_TRUE, vertexSize,
  62. reinterpret_cast<void*>(sizeof(float) * 6 + sizeof(char) * 4));
  63. // Texture coordinates
  64. glEnableVertexAttribArray(4);
  65. glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, vertexSize,
  66. reinterpret_cast<void*>(sizeof(float) * 6 + sizeof(char) * 8));
  67. }
  68. }
  69. VertexArray::~VertexArray()
  70. {
  71. glDeleteBuffers(1, &mVertexBuffer);
  72. glDeleteBuffers(1, &mIndexBuffer);
  73. glDeleteVertexArrays(1, &mVertexArray);
  74. }
  75. void VertexArray::SetActive()
  76. {
  77. glBindVertexArray(mVertexArray);
  78. }