OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
Mesh.cpp
Go to the documentation of this file.
1 
8 #include "global.h"
9 #include "TextureManager.h"
10 #include "Mesh.h"
11 
12 Mesh::Mesh(const std::vector<glm::vec3>& vert,
13  const std::vector<IndexedRectangle>& rect,
14  const std::vector<IndexedRectangle>& tri,
15  const std::vector<IndexedColoredRectangle>& coloredRect,
16  const std::vector<IndexedColoredRectangle>& coloredTri) {
17  for (auto& t : rect) {
18  indicesBuff.push_back(0);
19  verticesBuff.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
20  verticesBuff.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
21  verticesBuff.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
22  verticesBuff.push_back(glm::vec3(vert.at(t.v4).x, vert.at(t.v4).y, vert.at(t.v4).z));
23  texturesBuff.push_back(t.texture);
24  }
25 
26  for (auto& t : tri) {
27  indicesBuff.push_back(1);
28  verticesBuff.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
29  verticesBuff.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
30  verticesBuff.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
31  texturesBuff.push_back(t.texture);
32  }
33 
34  for (auto& t : coloredRect) {
35  indicesColorBuff.push_back(0);
36  verticesColorBuff.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
37  verticesColorBuff.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
38  verticesColorBuff.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
39  verticesColorBuff.push_back(glm::vec3(vert.at(t.v4).x, vert.at(t.v4).y, vert.at(t.v4).z));
40  colorsBuff.push_back(t.index);
41  }
42 
43  for (auto& t : coloredTri) {
44  indicesColorBuff.push_back(1);
45  verticesColorBuff.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
46  verticesColorBuff.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
47  verticesColorBuff.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
48  colorsBuff.push_back(t.index);
49  }
50 }
51 
52 void Mesh::prepare() {
53  std::vector<unsigned short> ind;
54  std::vector<glm::vec3> vert;
55  std::vector<glm::vec2> uvBuff;
56  std::vector<unsigned int> tex;
57 
58  int vertIndex = 0;
59  for (int i = 0; i < indicesBuff.size(); i++) {
60  unsigned int texture = TextureManager::getTile(texturesBuff.at(i)).getTexture();
61  for (int x = 0; x < ((indicesBuff.at(i) == 0) ? 4 : 3); x++) {
62  int v = x;
63  if (v == 0)
64  v = 2;
65  else if (v == 2)
66  v = 0;
67  ind.push_back(vert.size());
68  vert.push_back(verticesBuff.at(vertIndex + v));
69  uvBuff.push_back(TextureManager::getTile(texturesBuff.at(i)).getUV(v));
70  tex.push_back(texture);
71  }
72 
73  if (indicesBuff.at(i) == 0) {
74  ind.push_back(ind.at(ind.size() - 4));
75  ind.push_back(ind.at(ind.size() - 3));
76  }
77 
78  vertIndex += (indicesBuff.at(i) == 0) ? 4 : 3;
79  }
80 
81  assertEqual(ind.size() % 3, 0);
82  assertEqual(vert.size(), tex.size());
83  assertEqual(vert.size(), uvBuff.size());
84 
85  indicesBuff = std::move(ind);
86  vertices.bufferData(vert);
87  uvs.bufferData(uvBuff);
88  texturesBuff = std::move(tex);
89 
90  std::vector<unsigned short> indCol;
91  std::vector<glm::vec3> vertCol;
92  std::vector<glm::vec3> cols;
93 
94  vertIndex = 0;
95  for (int i = 0; i < indicesColorBuff.size(); i++) {
96  for (int x = 0; x < ((indicesColorBuff.at(i) == 0) ? 4 : 3); x++) {
97  int v = x;
98  if (v == 0)
99  v = 2;
100  else if (v == 2)
101  v = 0;
102  indCol.push_back(vertCol.size());
103  vertCol.push_back(verticesColorBuff.at(vertIndex + v));
104  glm::vec4 c = TextureManager::getPalette(colorsBuff.at(i));
105  cols.push_back(glm::vec3(c.x, c.y, c.z));
106  }
107 
108  if (indicesColorBuff.at(i) == 0) {
109  indCol.push_back(indCol.at(indCol.size() - 4));
110  indCol.push_back(indCol.at(indCol.size() - 3));
111  }
112 
113  vertIndex += (indicesColorBuff.at(i) == 0) ? 4 : 3;
114  }
115 
116  assertEqual(indCol.size() % 3, 0);
117  assertEqual(vertCol.size(), cols.size());
118 
119  indicesColor.bufferData(indCol);
120  verticesColor.bufferData(vertCol);
121  colors.bufferData(cols);
122 
123  verticesBuff.clear();
124  indicesColorBuff.clear();
125  verticesColorBuff.clear();
126  colorsBuff.clear();
127 }
128 
129 void Mesh::display(glm::mat4 MVP, ShaderTexture* shaderTexture) {
130  if (indicesBuff.size() > 0) {
131  unsigned int indexStart = 0;
132  unsigned int indexPos = 1;
133  unsigned int texture = texturesBuff.at(indicesBuff.at(0));
134 
135  while ((indexStart != indexPos) && (indexPos < indicesBuff.size())) {
136  while ((indexPos < indicesBuff.size())
137  && (texturesBuff.at(indicesBuff.at(indexPos)) == texture)) {
138  indexPos++;
139  }
140 
141  std::vector<unsigned short> ind(indicesBuff.begin() + indexStart,
142  indicesBuff.begin() + indexPos);
143  indices.bufferData(ind);
144  Shader::drawGL(vertices, uvs, indices, texture, MVP, TextureStorage::GAME, shaderTexture);
145 
146  if (indexPos < indicesBuff.size()) {
147  indexStart = indexPos;
148  indexPos += 1;
149  texture = texturesBuff.at(indicesBuff.at(indexStart));
150  }
151  }
152  }
153 
154  if (indicesColor.getSize() > 0)
155  Shader::drawGL(verticesColor, colors, indicesColor, MVP, GL_TRIANGLES, shaderTexture);
156 }
157 
void bufferData(int elem, int size, void *data)
Definition: Shader.cpp:18
ShaderBuffer vertices
Definition: Mesh.h:54
ShaderBuffer colors
Definition: Mesh.h:55
Mesh(const std::vector< glm::vec3 > &vertices, const std::vector< IndexedRectangle > &rectangles, const std::vector< IndexedRectangle > &triangles, const std::vector< IndexedColoredRectangle > &coloredRectangles, const std::vector< IndexedColoredRectangle > &coloredTriangles)
Definition: Mesh.cpp:12
ShaderBuffer uvs
Definition: Mesh.h:54
static TextureTile & getTile(int index)
int getSize()
Definition: Shader.h:31
std::vector< glm::vec3 > verticesColorBuff
Definition: Mesh.h:51
Included everywhere.
std::vector< unsigned int > colorsBuff
Definition: Mesh.h:52
ShaderBuffer indices
Definition: Mesh.h:54
static void drawGL(ShaderBuffer &vertices, ShaderBuffer &uvs, glm::vec4 color, unsigned int texture, TextureStorage store=TextureStorage::SYSTEM, unsigned int mode=GL_TRIANGLES, ShaderTexture *target=nullptr, Shader &shader=textShader)
Definition: Shader.cpp:278
void display(glm::mat4 MVP, ShaderTexture *shaderTexture=nullptr)
Definition: Mesh.cpp:129
std::vector< unsigned short > indicesColorBuff
Definition: Mesh.h:50
#define assertEqual(x, y)
Definition: global.h:130
Textured/Colored Mesh.
Texture Registry.
ShaderBuffer verticesColor
Definition: Mesh.h:55
static glm::vec4 getPalette(int index)
std::vector< unsigned int > texturesBuff
Definition: Mesh.h:48
ShaderBuffer indicesColor
Definition: Mesh.h:55
void prepare()
Definition: Mesh.cpp:52
std::vector< glm::vec3 > verticesBuff
Definition: Mesh.h:47
std::vector< unsigned short > indicesBuff
Definition: Mesh.h:46