2
0

VertexArray.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 = GetVertexSize(layout);
  19. // Create vertex buffer
  20. glGenBuffers(1, &mVertexBuffer);
  21. glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
  22. glBufferData(GL_ARRAY_BUFFER, numVerts * vertexSize, verts, GL_STATIC_DRAW);
  23. // Create index buffer
  24. glGenBuffers(1, &mIndexBuffer);
  25. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
  26. glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(unsigned int), indices, GL_STATIC_DRAW);
  27. // Specify the vertex attributes
  28. if (layout == PosNormTex)
  29. {
  30. // Position is 3 floats
  31. glEnableVertexAttribArray(0);
  32. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertexSize, 0);
  33. // Normal is 3 floats
  34. glEnableVertexAttribArray(1);
  35. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, vertexSize,
  36. reinterpret_cast<void*>(sizeof(float) * 3));
  37. // Texture coordinates is 2 floats
  38. glEnableVertexAttribArray(2);
  39. glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, vertexSize,
  40. reinterpret_cast<void*>(sizeof(float) * 6));
  41. }
  42. else if (layout == PosNormSkinTex)
  43. {
  44. // Position is 3 floats
  45. glEnableVertexAttribArray(0);
  46. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertexSize, 0);
  47. // Normal is 3 floats
  48. glEnableVertexAttribArray(1);
  49. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, vertexSize,
  50. reinterpret_cast<void*>(sizeof(float) * 3));
  51. // Skinning indices (keep as ints)
  52. glEnableVertexAttribArray(2);
  53. glVertexAttribIPointer(2, 4, GL_UNSIGNED_BYTE, vertexSize,
  54. reinterpret_cast<void*>(sizeof(float) * 6));
  55. // Skinning weights (convert to floats)
  56. glEnableVertexAttribArray(3);
  57. glVertexAttribPointer(3, 4, GL_UNSIGNED_BYTE, GL_TRUE, vertexSize,
  58. reinterpret_cast<void*>(sizeof(float) * 6 + sizeof(char) * 4));
  59. // Texture coordinates
  60. glEnableVertexAttribArray(4);
  61. glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, vertexSize,
  62. reinterpret_cast<void*>(sizeof(float) * 6 + sizeof(char) * 8));
  63. }
  64. }
  65. VertexArray::~VertexArray()
  66. {
  67. glDeleteBuffers(1, &mVertexBuffer);
  68. glDeleteBuffers(1, &mIndexBuffer);
  69. glDeleteVertexArrays(1, &mVertexArray);
  70. }
  71. void VertexArray::SetActive()
  72. {
  73. glBindVertexArray(mVertexArray);
  74. }
  75. unsigned int VertexArray::GetVertexSize(VertexArray::Layout layout)
  76. {
  77. unsigned vertexSize = 8 * sizeof(float);
  78. if (layout == PosNormSkinTex)
  79. {
  80. vertexSize = 8 * sizeof(float) + 8 * sizeof(char);
  81. }
  82. return vertexSize;
  83. }