Mesh.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * Copyright (c) 2006-2017 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/Exception.h"
  23. #include "Shader.h"
  24. #include "graphics/Graphics.h"
  25. // C++
  26. #include <algorithm>
  27. #include <limits>
  28. namespace love
  29. {
  30. namespace graphics
  31. {
  32. namespace opengl
  33. {
  34. Mesh::Mesh(graphics::Graphics *gfx, const std::vector<AttribFormat> &vertexformat, const void *data, size_t datasize, DrawMode drawmode, vertex::Usage usage)
  35. : love::graphics::Mesh(gfx, vertexformat, data, datasize, drawmode, usage)
  36. {
  37. }
  38. Mesh::Mesh(graphics::Graphics *gfx, const std::vector<AttribFormat> &vertexformat, int vertexcount, DrawMode drawmode, vertex::Usage usage)
  39. : love::graphics::Mesh(gfx, vertexformat, vertexcount, drawmode, usage)
  40. {
  41. }
  42. Mesh::Mesh(graphics::Graphics *gfx, const std::vector<Vertex> &vertices, DrawMode drawmode, vertex::Usage usage)
  43. : Mesh(gfx, getDefaultVertexFormat(), &vertices[0], vertices.size() * sizeof(Vertex), drawmode, usage)
  44. {
  45. }
  46. Mesh::Mesh(graphics::Graphics *gfx, int vertexcount, DrawMode drawmode, vertex::Usage usage)
  47. : Mesh(gfx, getDefaultVertexFormat(), vertexcount, drawmode, usage)
  48. {
  49. }
  50. Mesh::~Mesh()
  51. {
  52. }
  53. int Mesh::bindAttributeToShaderInput(int attributeindex, const std::string &inputname)
  54. {
  55. const AttribFormat &format = vertexFormat[attributeindex];
  56. GLint attriblocation = -1;
  57. // If the attribute is one of the LOVE-defined ones, use the constant
  58. // attribute index for it, otherwise query the index from the shader.
  59. VertexAttribID builtinattrib;
  60. if (vertex::getConstant(inputname.c_str(), builtinattrib))
  61. attriblocation = (GLint) builtinattrib;
  62. else if (Shader::current)
  63. attriblocation = ((Shader *) Shader::current)->getAttribLocation(inputname);
  64. // The active shader might not use this vertex attribute name.
  65. if (attriblocation < 0)
  66. return attriblocation;
  67. // Make sure the buffer isn't mapped (sends data to GPU if needed.)
  68. vbo->unmap();
  69. gl.bindBuffer(BUFFER_VERTEX, (GLuint) vbo->getHandle());
  70. const void *gloffset = BUFFER_OFFSET(getAttributeOffset(attributeindex));
  71. GLenum datatype = getGLDataType(format.type);
  72. GLboolean normalized = (datatype == GL_UNSIGNED_BYTE);
  73. glVertexAttribPointer(attriblocation, format.components, datatype, normalized, (GLsizei) vertexStride, gloffset);
  74. return attriblocation;
  75. }
  76. void Mesh::drawInternal(int start, int count, int instancecount, bool useindexbuffer, uint32 attribflags, uint32 instancedattribflags) const
  77. {
  78. OpenGL::TempDebugGroup debuggroup("Mesh draw");
  79. gl.useVertexAttribArrays(attribflags, instancedattribflags);
  80. gl.bindTextureToUnit(texture, 0, false);
  81. gl.prepareDraw();
  82. GLenum gldrawmode = getGLDrawMode(drawMode);
  83. if (useindexbuffer)
  84. {
  85. size_t elementsize = vertex::getIndexDataSize(elementDataType);
  86. const void *indices = BUFFER_OFFSET(start * elementsize);
  87. GLenum type = OpenGL::getGLIndexDataType(elementDataType);
  88. gl.bindBuffer(BUFFER_INDEX, (GLuint) ibo->getHandle());
  89. gl.drawElements(gldrawmode, count, type, indices, instancecount);
  90. }
  91. else
  92. {
  93. gl.drawArrays(gldrawmode, start, count, instancecount);
  94. }
  95. }
  96. GLenum Mesh::getGLDrawMode(DrawMode mode)
  97. {
  98. switch (mode)
  99. {
  100. case DRAWMODE_FAN:
  101. return GL_TRIANGLE_FAN;
  102. case DRAWMODE_STRIP:
  103. return GL_TRIANGLE_STRIP;
  104. case DRAWMODE_TRIANGLES:
  105. default:
  106. return GL_TRIANGLES;
  107. case DRAWMODE_POINTS:
  108. return GL_POINTS;
  109. }
  110. }
  111. GLenum Mesh::getGLDataType(DataType type)
  112. {
  113. switch (type)
  114. {
  115. case DATA_BYTE:
  116. return GL_UNSIGNED_BYTE;
  117. case DATA_FLOAT:
  118. return GL_FLOAT;
  119. default:
  120. return 0;
  121. }
  122. }
  123. } // opengl
  124. } // graphics
  125. } // love