Mesh.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "Mesh.h"
  2. #include <cstring>
  3. #include <iostream>
  4. using namespace std;
  5. Mesh::Mesh(int mode)
  6. {
  7. // position
  8. positionStartIndex = vertexSize;
  9. vertexSize += 3;
  10. // color
  11. if(mode & COLOR)
  12. {
  13. colorStartIndex = vertexSize;
  14. vertexSize += 4;
  15. }
  16. // texture
  17. if(mode & TEXTURE)
  18. {
  19. textureStartIndex = vertexSize;
  20. vertexSize += 2;
  21. }
  22. // normal
  23. if(mode & NORMAL)
  24. {
  25. normalStartIndex = vertexSize;
  26. vertexSize += 3;
  27. }
  28. data = new float[dataSize * vertexSize];
  29. glGenVertexArrays(1, &vba);
  30. glBindVertexArray(vba);
  31. glGenBuffers(1, &vbo);
  32. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  33. glVertexAttribPointer(0, 3, GL_FLOAT, 0, sizeof(float) * vertexSize, (GLvoid*) (sizeof(float) * positionStartIndex));
  34. glEnableVertexAttribArray(0);
  35. glVertexAttribPointer(1, 4, GL_FLOAT, 0, sizeof(float) * vertexSize, (GLvoid*) (sizeof(float) * colorStartIndex));
  36. glEnableVertexAttribArray(1);
  37. glVertexAttribPointer(2, 2, GL_FLOAT, 0, sizeof(float) * vertexSize, (GLvoid*) (sizeof(float) * textureStartIndex));
  38. glEnableVertexAttribArray(2);
  39. glVertexAttribPointer(3, 3, GL_FLOAT, 0, sizeof(float) * vertexSize, (GLvoid*) (sizeof(float) * normalStartIndex));
  40. glEnableVertexAttribArray(3);
  41. }
  42. Mesh::Mesh(const Mesh& orig)
  43. {
  44. }
  45. Mesh::~Mesh()
  46. {
  47. delete[] data;
  48. glDeleteVertexArrays(1, &vba);
  49. glDeleteBuffers(1, &vbo);
  50. }
  51. void Mesh::ensureCapacity(unsigned int index)
  52. {
  53. if(index >= dataSize)
  54. {
  55. float* newData = new float[dataSize * 2 * vertexSize];
  56. memcpy(newData, data, sizeof(float) * dataSize * vertexSize);
  57. delete[] data;
  58. data = newData;
  59. dataSize *= 2;
  60. }
  61. }
  62. void Mesh::addPosition(float x, float y, float z)
  63. {
  64. ensureCapacity(positionIndex);
  65. unsigned int index = positionStartIndex + positionIndex * vertexSize;
  66. data[index] = x;
  67. data[index + 1] = y;
  68. data[index + 2] = z;
  69. positionIndex++;
  70. }
  71. void Mesh::addColor(float r, float g, float b, float a)
  72. {
  73. ensureCapacity(colorIndex);
  74. unsigned int index = colorStartIndex + colorIndex * vertexSize;
  75. data[index] = r;
  76. data[index + 1] = g;
  77. data[index + 2] = b;
  78. data[index + 3] = a;
  79. colorIndex++;
  80. }
  81. void Mesh::addTexture(float tx, float ty)
  82. {
  83. ensureCapacity(textureIndex);
  84. unsigned int index = textureStartIndex + textureIndex * vertexSize;
  85. data[index] = tx;
  86. data[index + 1] = ty;
  87. textureIndex++;
  88. }
  89. void Mesh::addNormal(float nx, float ny, float nz)
  90. {
  91. ensureCapacity(normalIndex);
  92. unsigned int index = normalStartIndex + normalIndex * vertexSize;
  93. data[index] = nx;
  94. data[index + 1] = ny;
  95. data[index + 2] = nz;
  96. normalIndex++;
  97. }
  98. void Mesh::build()
  99. {
  100. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  101. vertices = positionIndex;
  102. /*cout << "______________" << vertices << " " << vertexSize << endl;
  103. for(int i = 0; i < vertices; i++)
  104. {
  105. cout << "----------vert " << i << endl;
  106. int index = i * vertexSize;
  107. for(int j = 0; j < vertexSize; j++)
  108. {
  109. cout << data[index + j] << " " << endl;
  110. }
  111. }*/
  112. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertexSize * vertices, data, GL_STATIC_DRAW);
  113. positionIndex = 0;
  114. colorIndex = 0;
  115. textureIndex = 0;
  116. normalIndex = 0;
  117. }
  118. void Mesh::draw()
  119. {
  120. if(vertices == 0)
  121. {
  122. return;
  123. }
  124. glBindVertexArray(vba);
  125. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  126. glDrawArrays(GL_TRIANGLES, 0, vertices);
  127. }