Mesh.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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()
  43. {
  44. delete[] data;
  45. glDeleteVertexArrays(1, &vba);
  46. glDeleteBuffers(1, &vbo);
  47. }
  48. void Mesh::ensureCapacity(unsigned int index)
  49. {
  50. if(index >= dataSize)
  51. {
  52. float* newData = new float[dataSize * 2 * vertexSize];
  53. memcpy(newData, data, sizeof(float) * dataSize * vertexSize);
  54. delete[] data;
  55. data = newData;
  56. dataSize *= 2;
  57. }
  58. }
  59. void Mesh::addPosition(float x, float y, float z)
  60. {
  61. ensureCapacity(positionIndex);
  62. unsigned int index = positionStartIndex + positionIndex * vertexSize;
  63. data[index] = x;
  64. data[index + 1] = y;
  65. data[index + 2] = z;
  66. positionIndex++;
  67. }
  68. void Mesh::addColor(float r, float g, float b, float a)
  69. {
  70. ensureCapacity(colorIndex);
  71. unsigned int index = colorStartIndex + colorIndex * vertexSize;
  72. data[index] = r;
  73. data[index + 1] = g;
  74. data[index + 2] = b;
  75. data[index + 3] = a;
  76. colorIndex++;
  77. }
  78. void Mesh::addTexture(float tx, float ty)
  79. {
  80. ensureCapacity(textureIndex);
  81. unsigned int index = textureStartIndex + textureIndex * vertexSize;
  82. data[index] = tx;
  83. data[index + 1] = ty;
  84. textureIndex++;
  85. }
  86. void Mesh::addNormal(float nx, float ny, float nz)
  87. {
  88. ensureCapacity(normalIndex);
  89. unsigned int index = normalStartIndex + normalIndex * vertexSize;
  90. data[index] = nx;
  91. data[index + 1] = ny;
  92. data[index + 2] = nz;
  93. normalIndex++;
  94. }
  95. void Mesh::build(bool print)
  96. {
  97. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  98. vertices = positionIndex;
  99. if(print)
  100. {
  101. cout << "______________" << vertices << " " << vertexSize << endl;
  102. for(int i = 0; i < vertices; i++)
  103. {
  104. cout << "----------vert " << i << endl;
  105. int index = i * vertexSize;
  106. for(int j = 0; j < vertexSize; j++)
  107. {
  108. cout << data[index + j] << " " << endl;
  109. }
  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. }
  128. void Mesh::draw(int start, int count)
  129. {
  130. if(vertices == 0 || start < 0 || start >= vertices || start + count > vertices)
  131. {
  132. return;
  133. }
  134. glBindVertexArray(vba);
  135. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  136. glDrawArrays(GL_TRIANGLES, start, count);
  137. }