mesh.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include <GL/glew.h>
  2. #include <GL/glut.h>
  3. #include <vector>
  4. #include "mesh.h"
  5. #include "loadModel.h"
  6. #include "vec3.h"
  7. #include <stdio.h>
  8. bool Mesh::load(const char *path, char mirror)
  9. {
  10. return loadPLY(path, v, c, &bound, mirror);
  11. }
  12. void Mesh::draw()
  13. {
  14. //Local matrix
  15. glPushMatrix();
  16. //Local transform
  17. glTranslatef(loc.x, loc.y, loc.z);
  18. //Start drawinig
  19. glBegin(GL_TRIANGLES);
  20. //For each vertex
  21. unsigned int i;
  22. for (i = 0; i < v.size(); i++)
  23. {
  24. //Vertex data
  25. glColor3f (c[i].x, c[i].y, c[i].z);
  26. glVertex3f(v[i].x, v[i].y, v[i].z);
  27. }
  28. //Stop drawing
  29. glEnd();
  30. //Cancel matrix
  31. glPopMatrix();
  32. }
  33. void Mesh::add(Mesh m)
  34. {
  35. for (unsigned int i = 0; i < m.v.size(); i++)
  36. {
  37. m.v[i].x += m.loc.x;
  38. m.v[i].y += m.loc.y;
  39. m.v[i].z += m.loc.z;
  40. v.push_back(m.v[i]);
  41. c.push_back(m.c[i]);
  42. }
  43. }
  44. /* Experiments */
  45. //Constructor
  46. Mesh2::Mesh2()
  47. {
  48. //Init everything
  49. vbo = 0;
  50. vertices = 0;
  51. colors = 0;
  52. vcount = 0;
  53. }
  54. //Destructor
  55. Mesh2::~Mesh2()
  56. {
  57. //Comment out for now, as they are deleted on loading
  58. //delete [] vertices;
  59. //delete [] colors;
  60. }
  61. //Set vertices number
  62. void Mesh2::set_vertex_count(int gvcount)
  63. {
  64. //Delete previous vertices (if exist)
  65. delete [] vertices;
  66. delete [] colors;
  67. //Set new count
  68. vcount = gvcount;
  69. //Not zero
  70. if (vcount)
  71. {
  72. //Alocate space for position and colors
  73. vertices = new float[vcount*3];
  74. colors = new float[vcount*3];
  75. }
  76. //Zero vertices
  77. else
  78. {
  79. //Nullify
  80. vertices = 0;
  81. colors = 0;
  82. }
  83. }
  84. //Load mesh from file (STILL EXPERIMENTING)
  85. bool Mesh2::load(const char *path, char mirror)
  86. {
  87. /*std::vector<vec3> vvertices, vcolors;
  88. loadPLY(path, vvertices, vcolors, 0, mirror);
  89. set_vertex_count(vvertices.size());
  90. for (int i = 0; i < vcount; i++)
  91. {
  92. vertices[i] = vvertices[i];
  93. colors[i] = vcolors[i];
  94. }*/
  95. // if (loadPly(path, 0) == 0)
  96. // printf( "error loading file!\n" );
  97. std::vector<vec3> vert, col;
  98. loadPLY(path, vert, col, NULL, mirror);
  99. //loadPly(path, 0);
  100. //This is the old way to "convert" old PLY parser to mesh (I plan to change it)
  101. set_vertex_count(vert.size());
  102. //Move all data to pointers
  103. int i;
  104. for (i = 0; i < vcount; i++)
  105. {
  106. vertices[i*3 ] = vert[i].x;
  107. vertices[i*3+1] = vert[i].y;
  108. vertices[i*3+2] = vert[i].z;
  109. colors[i*3 ] = col[i].r;
  110. colors[i*3+1] = col[i].g;
  111. colors[i*3+2] = col[i].b;
  112. //Calculate positive and negative bounds
  113. if (vert[i].x > pbound.x) pbound.x = vert[i].x; else
  114. if (vert[i].x < nbound.x) nbound.x = vert[i].x;
  115. if (vert[i].y > pbound.y) pbound.y = vert[i].y; else
  116. if (vert[i].y < nbound.y) nbound.y = vert[i].y;
  117. if (vert[i].z > pbound.z) pbound.z = vert[i].z; else
  118. if (vert[i].z < nbound.z) nbound.z = vert[i].z;
  119. }
  120. //Create new VBO
  121. glGenBuffers(1, &vbo);
  122. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  123. //Create buffer data
  124. glBufferData(GL_ARRAY_BUFFER, sizeof(float) *vcount *3 *2,
  125. NULL, GL_STATIC_DRAW);
  126. //Add vertex positions
  127. glBufferSubData(GL_ARRAY_BUFFER,
  128. 0,
  129. sizeof(float) *vcount *3,
  130. vertices);
  131. //Add vertex colors
  132. glBufferSubData(GL_ARRAY_BUFFER,
  133. sizeof(float) *vcount *3,
  134. sizeof(float) *vcount *3,
  135. colors);
  136. //Free pointers (ideally the ply parser could save data to a buffer)
  137. delete [] vertices;
  138. delete [] colors;
  139. //Load complete
  140. return true;
  141. }
  142. void Mesh2::draw() const
  143. {
  144. /* Draw mesh using arrays (I might re-use this later) */
  145. //Enable arrays
  146. /*glEnableClientState(GL_VERTEX_ARRAY);
  147. glEnableClientState(GL_COLOR_ARRAY);
  148. //Pass data to arrays
  149. glVertexPointer(3, GL_FLOAT, 0, vertices);
  150. glColorPointer (3, GL_FLOAT, 0, colors );
  151. //Draw arrays
  152. glDrawArrays(GL_TRIANGLES, 0, vcount);
  153. //Disable arrays
  154. glDisableClientState(GL_VERTEX_ARRAY);
  155. glDisableClientState(GL_COLOR_ARRAY);*/
  156. /* Draw mesh using buffer objects */
  157. //Make new VBO active.
  158. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  159. //Activate vertex positions and colors
  160. glEnableClientState(GL_VERTEX_ARRAY);
  161. glEnableClientState(GL_COLOR_ARRAY);
  162. //How to draw them
  163. glVertexPointer(3, GL_FLOAT, 0, NULL);
  164. glColorPointer (3, GL_FLOAT, 0, (void*) (sizeof(float) *vcount *3));
  165. //Actual draw
  166. glDrawArrays(GL_TRIANGLES, 0, vcount);
  167. //Disable
  168. glDisableClientState(GL_VERTEX_ARRAY);
  169. glDisableClientState(GL_COLOR_ARRAY);
  170. }