Mesh.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * Copyright (c) 2006-2013 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "Mesh.h"
  22. #include "common/Matrix.h"
  23. #include "common/Exception.h"
  24. namespace love
  25. {
  26. namespace graphics
  27. {
  28. namespace opengl
  29. {
  30. Mesh::Mesh(const std::vector<Vertex> &verts, Mesh::DrawMode mode)
  31. : draw_mode(mode)
  32. , image(0)
  33. {
  34. setVertices(verts);
  35. }
  36. void Mesh::setVertices(const std::vector<Vertex> &verts)
  37. {
  38. if (verts.size() < 3)
  39. throw love::Exception("At least 3 vertices are required.");
  40. vertices = verts;
  41. }
  42. const std::vector<Vertex> &Mesh::getVertices() const
  43. {
  44. return vertices;
  45. }
  46. void Mesh::setVertex(size_t i, Vertex v)
  47. {
  48. if (i >= vertices.size())
  49. throw love::Exception("Invalid index.");
  50. vertices[i] = v;
  51. }
  52. Vertex Mesh::getVertex(size_t i) const
  53. {
  54. if (i >= vertices.size())
  55. throw love::Exception("Invalid index.");
  56. return vertices[i];
  57. }
  58. size_t Mesh::getVertexCount() const
  59. {
  60. return vertices.size();
  61. }
  62. void Mesh::setVertexMap(const std::vector<uint16> &map)
  63. {
  64. for (size_t i = 0; i < map.size(); i++)
  65. {
  66. if (map[i] >= vertices.size())
  67. throw love::Exception("Invalid vertex map value: %d", map[i]);
  68. }
  69. vertex_map = map;
  70. }
  71. const std::vector<uint16> &Mesh::getVertexMap() const
  72. {
  73. return vertex_map;
  74. }
  75. Mesh::~Mesh()
  76. {
  77. }
  78. void Mesh::setImage(Image *img)
  79. {
  80. img->retain();
  81. if (image)
  82. image->release();
  83. image = img;
  84. }
  85. void Mesh::setImage()
  86. {
  87. if (image)
  88. image->release();
  89. image = 0;
  90. }
  91. Image *Mesh::getImage() const
  92. {
  93. return image;
  94. }
  95. void Mesh::setDrawMode(Mesh::DrawMode mode)
  96. {
  97. draw_mode = mode;
  98. }
  99. Mesh::DrawMode Mesh::getDrawMode() const
  100. {
  101. return draw_mode;
  102. }
  103. void Mesh::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const
  104. {
  105. if (vertices.size() == 0)
  106. return;
  107. if (image)
  108. image->bind();
  109. else
  110. gl.bindTexture(0);
  111. Matrix m;
  112. m.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
  113. glPushMatrix();
  114. glMultMatrixf(m.getElements());
  115. glEnableClientState(GL_VERTEX_ARRAY);
  116. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  117. glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &(vertices[0].x));
  118. glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &(vertices[0].s));
  119. {
  120. glEnableClientState(GL_COLOR_ARRAY);
  121. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &(vertices[0].r));
  122. }
  123. GLenum gl_draw_mode = getGLDrawMode(draw_mode);
  124. if (vertex_map.size() > 0)
  125. glDrawElements(gl_draw_mode, vertex_map.size(), GL_UNSIGNED_SHORT, &vertex_map[0]);
  126. else
  127. glDrawArrays(gl_draw_mode, 0, vertices.size());
  128. glDisableClientState(GL_VERTEX_ARRAY);
  129. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  130. {
  131. glDisableClientState(GL_COLOR_ARRAY);
  132. gl.setColor(gl.getColor());
  133. }
  134. glPopMatrix();
  135. }
  136. GLenum Mesh::getGLDrawMode(Mesh::DrawMode mode) const
  137. {
  138. switch (mode)
  139. {
  140. case DRAW_MODE_FAN:
  141. return GL_TRIANGLE_FAN;
  142. case DRAW_MODE_STRIP:
  143. return GL_TRIANGLE_STRIP;
  144. case DRAW_MODE_TRIANGLES:
  145. return GL_TRIANGLES;
  146. case DRAW_MODE_POINTS:
  147. return GL_POINTS;
  148. default:
  149. break;
  150. }
  151. return GL_TRIANGLES;
  152. }
  153. bool Mesh::getConstant(const char *in, Mesh::DrawMode &out)
  154. {
  155. return drawModes.find(in, out);
  156. }
  157. bool Mesh::getConstant(Mesh::DrawMode in, const char *&out)
  158. {
  159. return drawModes.find(in, out);
  160. }
  161. StringMap<Mesh::DrawMode, Mesh::DRAW_MODE_MAX_ENUM>::Entry Mesh::drawModeEntries[] =
  162. {
  163. {"fan", Mesh::DRAW_MODE_FAN},
  164. {"strip", Mesh::DRAW_MODE_STRIP},
  165. {"triangles", Mesh::DRAW_MODE_TRIANGLES},
  166. {"points", Mesh::DRAW_MODE_POINTS},
  167. };
  168. StringMap<Mesh::DrawMode, Mesh::DRAW_MODE_MAX_ENUM> Mesh::drawModes(Mesh::drawModeEntries, sizeof(Mesh::drawModeEntries));
  169. } // opengl
  170. } // graphics
  171. } // love