TextureMesh.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "TextureMesh.h"
  2. #include <cstring>
  3. #include <iostream>
  4. using namespace std;
  5. TextureMesh::TextureMesh()
  6. {
  7. data = new float[dataSize * 5];
  8. }
  9. TextureMesh::TextureMesh(const TextureMesh& orig)
  10. {
  11. }
  12. TextureMesh::~TextureMesh()
  13. {
  14. delete[] data;
  15. glDeleteVertexArrays(1, &vba);
  16. glDeleteBuffers(1, &vbo);
  17. }
  18. void TextureMesh::init()
  19. {
  20. glGenVertexArrays(1, &vba);
  21. glBindVertexArray(vba);
  22. glGenBuffers(1, &vbo);
  23. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  24. glVertexAttribPointer(0, 3, GL_FLOAT, 0, 20, (GLvoid*) 0);
  25. glEnableVertexAttribArray(0);
  26. glVertexAttribPointer(2, 2, GL_FLOAT, 0, 20, (GLvoid*) 12);
  27. glEnableVertexAttribArray(2);
  28. }
  29. void TextureMesh::addPoint(float x, float y, float z, float tx, float ty)
  30. {
  31. if(vertices >= dataSize)
  32. {
  33. float* newData = new float[dataSize * 2 * 5];
  34. memcpy(newData, data, sizeof(float) * dataSize * 5);
  35. delete[] data;
  36. data = newData;
  37. dataSize *= 2;
  38. }
  39. unsigned int index = vertices * 5;
  40. data[index] = x;
  41. data[index + 1] = y;
  42. data[index + 2] = z;
  43. data[index + 3] = tx;
  44. data[index + 4] = ty;
  45. vertices++;
  46. }
  47. void TextureMesh::build()
  48. {
  49. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  50. /*for(int i = 0; i < vertices; i++)
  51. {
  52. int index = i * 7;
  53. cout << data[index] << " " << data[index + 1] << " " << data[index + 2]
  54. << " " << data[index + 3] << " " << data[index + 4] << " "
  55. << data[index + 5] << " " << data[index + 6] << endl;
  56. }*/
  57. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 5 * vertices, data, GL_STATIC_DRAW);
  58. }
  59. void TextureMesh::draw()
  60. {
  61. glBindVertexArray(vba);
  62. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  63. glDrawArrays(GL_TRIANGLES, 0, vertices);
  64. }