Mesh.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include <fstream>
  2. #include <boost/lexical_cast.hpp>
  3. #include "Mesh.h"
  4. #include "Material.h"
  5. #include "MeshData.h"
  6. #include "Vbo.h"
  7. //======================================================================================================================
  8. // Constructor =
  9. //======================================================================================================================
  10. Mesh::Mesh():
  11. Resource(RT_MESH),
  12. Object(NULL)
  13. {
  14. memset(&vbos[0], NULL, sizeof(vbos));
  15. }
  16. //======================================================================================================================
  17. // load =
  18. //======================================================================================================================
  19. void Mesh::load(const char* filename)
  20. {
  21. MeshData meshData(filename);
  22. vertIdsNum = meshData.getVertIndeces().size();
  23. try
  24. {
  25. //
  26. // Sanity checks
  27. //
  28. if(meshData.getVertIndeces().size() < 1 || meshData.getVertCoords().size() < 1 ||
  29. meshData.getVertNormals().size() < 1 || meshData.getVertTangents().size() < 1)
  30. {
  31. throw EXCEPTION("Empty one of the required vectors");
  32. }
  33. createVbos(meshData);
  34. }
  35. catch(std::exception& e)
  36. {
  37. throw EXCEPTION("Mesh \"" + filename + "\": " + e.what());
  38. }
  39. }
  40. //======================================================================================================================
  41. // createVbos =
  42. //======================================================================================================================
  43. void Mesh::createVbos(const MeshData& meshData)
  44. {
  45. vbos[VBO_VERT_INDECES] = new Vbo(GL_ELEMENT_ARRAY_BUFFER, meshData.getVertIndeces().getSizeInBytes(),
  46. &meshData.getVertIndeces()[0], GL_STATIC_DRAW, this);
  47. vbos[VBO_VERT_POSITIONS] = new Vbo(GL_ARRAY_BUFFER, meshData.getVertCoords().getSizeInBytes(),
  48. &meshData.getVertCoords()[0], GL_STATIC_DRAW, this);
  49. vbos[VBO_VERT_NORMALS] = new Vbo(GL_ARRAY_BUFFER, meshData.getVertNormals().getSizeInBytes(),
  50. &meshData.getVertNormals()[0], GL_STATIC_DRAW, this);
  51. if(meshData.getVertTangents().size() > 1)
  52. {
  53. vbos[VBO_VERT_TANGENTS] = new Vbo(GL_ARRAY_BUFFER, meshData.getVertTangents().getSizeInBytes(),
  54. &meshData.getVertTangents()[0], GL_STATIC_DRAW, this);
  55. }
  56. else
  57. {
  58. vbos[VBO_VERT_TANGENTS] = NULL;
  59. }
  60. if(meshData.getTexCoords().size() > 1)
  61. {
  62. vbos[VBO_TEX_COORDS] = new Vbo(GL_ARRAY_BUFFER, meshData.getTexCoords().getSizeInBytes(),
  63. &meshData.getTexCoords()[0], GL_STATIC_DRAW, this);
  64. }
  65. else
  66. {
  67. vbos[VBO_TEX_COORDS] = NULL;
  68. }
  69. if(meshData.getVertWeights().size() > 1)
  70. {
  71. vbos[VBO_VERT_WEIGHTS] = new Vbo(GL_ARRAY_BUFFER, meshData.getVertWeights().getSizeInBytes(),
  72. &meshData.getVertWeights()[0], GL_STATIC_DRAW, this);
  73. }
  74. else
  75. {
  76. vbos[VBO_VERT_WEIGHTS] = NULL;
  77. }
  78. }