Mesh.cpp 3.5 KB

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