Open Source Tomb Raider Engine
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*!
  2. * \file include/Mesh.h
  3. * \brief OpenGL Mesh
  4. *
  5. * \author xythobuz
  6. */
  7. #ifndef _MESH_H_
  8. #define _MESH_H_
  9. #include <map>
  10. #include <vector>
  11. #include <glm/mat4x4.hpp>
  12. #include <glm/vec2.hpp>
  13. #include <glm/vec3.hpp>
  14. struct IndexedRectangle {
  15. unsigned int v1, v2, v3, v4; // Vertex list indices
  16. unsigned int texture; // Index into object-texture list
  17. IndexedRectangle(unsigned int t, unsigned int _v1,
  18. unsigned int _v2, unsigned int _v3, unsigned int _v4 = 0)
  19. : v1(_v1), v2(_v2), v3(_v3), v4(_v4), texture(t) { }
  20. };
  21. struct IndexedColoredRectangle {
  22. unsigned int v1, v2, v3, v4;
  23. unsigned char r, g, b;
  24. IndexedColoredRectangle(unsigned char _r, unsigned char _g, unsigned char _b,
  25. unsigned int _v1, unsigned int _v2,
  26. unsigned int _v3, unsigned int _v4 = 0)
  27. : v1(_v1), v2(_v2), v3(_v3), v4(_v4), r(_r), g(_g), b(_b) { }
  28. };
  29. // --------------------------------------
  30. class Mesh {
  31. public:
  32. Mesh(const std::vector<glm::vec3>& vertices,
  33. const std::vector<IndexedRectangle>& rectangles,
  34. const std::vector<IndexedRectangle>& triangles,
  35. const std::vector<IndexedColoredRectangle>& coloredRectangles,
  36. const std::vector<IndexedColoredRectangle>& coloredTriangles);
  37. void prepare();
  38. void display(glm::mat4 model, glm::mat4 view, glm::mat4 projection);
  39. private:
  40. std::vector<unsigned short> indices;
  41. std::vector<glm::vec3> vertices;
  42. std::vector<glm::vec2> uvs;
  43. std::vector<unsigned int> textures;
  44. std::vector<unsigned short> indicesColor;
  45. std::vector<glm::vec3> verticesColor;
  46. std::vector<glm::vec3> colors;
  47. };
  48. // --------------------------------------
  49. struct PackedVertex {
  50. glm::vec3 pos;
  51. glm::vec2 uv;
  52. unsigned int tex;
  53. PackedVertex(glm::vec3 p, glm::vec2 u, unsigned int t) : pos(p), uv(u), tex(t) { }
  54. bool operator<(const PackedVertex& v) const {
  55. return memcmp(this, &v, sizeof(PackedVertex)) > 0;
  56. }
  57. };
  58. struct PackedColoredVertex {
  59. glm::vec3 pos;
  60. glm::vec3 col;
  61. PackedColoredVertex(glm::vec3 p, glm::vec3 c) : pos(p), col(c) { }
  62. bool operator<(const PackedColoredVertex& v) const {
  63. return memcmp(this, &v, sizeof(PackedColoredVertex)) > 0;
  64. }
  65. };
  66. template <typename T>
  67. bool findSimilarVertex(T& v,
  68. std::map<T, unsigned short> m,
  69. unsigned short& s) {
  70. auto it = m.find(v);
  71. if (it == m.end())
  72. return false;
  73. else {
  74. s = it->second;
  75. return true;
  76. }
  77. }
  78. #endif