Mesh.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include <cstring>
  2. #include <iostream>
  3. #include "client/engine/Mesh.h"
  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(unsigned int i = 0; i < vertices; i++)
  103. {
  104. cout << "----------vert " << i << endl;
  105. unsigned int index = i * vertexSize;
  106. for(unsigned 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(unsigned int start, unsigned 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. }
  138. NormalTextureMesh::NormalTextureMesh() : Mesh(Mesh::NORMAL | Mesh::TEXTURE)
  139. {
  140. }
  141. void NormalTextureMesh::addTriangle(
  142. float p1x, float p1y, float p1z, float p1nx, float p1ny, float p1nz, float p1texX, float p1texY,
  143. float p2x, float p2y, float p2z, float p2nx, float p2ny, float p2nz, float p2texX, float p2texY,
  144. float p3x, float p3y, float p3z, float p3nx, float p3ny, float p3nz, float p3texX, float p3texY)
  145. {
  146. addPosition(p1x, p1y, p1z);
  147. addPosition(p2x, p2y, p2z);
  148. addPosition(p3x, p3y, p3z);
  149. addNormal(p1nx, p1ny, p1nz);
  150. addNormal(p2nx, p2ny, p2nz);
  151. addNormal(p3nx, p3ny, p3nz);
  152. addTexture(p1texX, p1texY);
  153. addTexture(p2texX, p2texY);
  154. addTexture(p3texX, p3texY);
  155. }
  156. void NormalTextureMesh::addCuboid(float sx, float sy, float sz, float ex, float ey, float ez,
  157. float topTexStartX, float topTexStartY, float topTexEndX, float topTexEndY,
  158. float bottomTexStartX, float bottomTexStartY, float bottomTexEndX, float bottomTexEndY,
  159. float northTexStartX, float northTexStartY, float northTexEndX, float northTexEndY,
  160. float southTexStartX, float southTexStartY, float southTexEndX, float southTexEndY,
  161. float eastTexStartX, float eastTexStartY, float eastTexEndX, float eastTexEndY,
  162. float westTexStartX, float westTexStartY, float westTexEndX, float westTexEndY)
  163. {
  164. // bottom side
  165. addTriangle(
  166. sx, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexStartY,
  167. ex, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexStartY,
  168. sx, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexEndY);
  169. addTriangle(
  170. ex, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexStartY,
  171. ex, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexEndY,
  172. sx, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexEndY);
  173. // top side
  174. addTriangle(
  175. sx, ey, sz, 0.0f, 1.0f, 0.0f, topTexStartX, topTexStartY,
  176. sx, ey, ez, 0.0f, 1.0f, 0.0f, topTexStartX, topTexEndY,
  177. ex, ey, sz, 0.0f, 1.0f, 0.0f, topTexEndX, topTexStartY);
  178. addTriangle(
  179. ex, ey, sz, 0.0f, 1.0f, 0.0f, topTexEndX, topTexStartY,
  180. sx, ey, ez, 0.0f, 1.0f, 0.0f, topTexStartX, topTexEndY,
  181. ex, ey, ez, 0.0f, 1.0f, 0.0f, topTexEndX, topTexEndY);
  182. // north side
  183. addTriangle(
  184. ex, sy, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexEndY,
  185. ex, ey, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexStartY,
  186. ex, sy, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexEndY);
  187. addTriangle(
  188. ex, sy, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexEndY,
  189. ex, ey, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexStartY,
  190. ex, ey, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexStartY);
  191. // south side
  192. addTriangle(
  193. sx, sy, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexEndY,
  194. sx, sy, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexEndY,
  195. sx, ey, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexStartY);
  196. addTriangle(
  197. sx, sy, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexEndY,
  198. sx, ey, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexStartY,
  199. sx, ey, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexStartY);
  200. // east side
  201. addTriangle(
  202. sx, sy, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexEndY,
  203. ex, sy, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexEndY,
  204. ex, ey, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexStartY);
  205. addTriangle(
  206. sx, sy, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexEndY,
  207. ex, ey, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexStartY,
  208. sx, ey, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexStartY);
  209. // west side
  210. addTriangle(
  211. sx, sy, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexEndY,
  212. ex, ey, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexStartY,
  213. ex, sy, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexEndY);
  214. addTriangle(
  215. sx, sy, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexEndY,
  216. sx, ey, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexStartY,
  217. ex, ey, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexStartY);
  218. }