OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
RoomMesh.cpp
Go to the documentation of this file.
1 
8 #include "global.h"
9 #include "TextureManager.h"
10 #include "RoomMesh.h"
11 
12 RoomMesh::RoomMesh(const std::vector<RoomVertexTR2>& vert,
13  const std::vector<IndexedRectangle>& rect,
14  const std::vector<IndexedRectangle>& tri) {
15  for (auto& t : rect) {
16  indicesBuff.push_back(0);
17  verticesBuff.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
18  verticesBuff.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
19  verticesBuff.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
20  verticesBuff.push_back(glm::vec3(vert.at(t.v4).x, vert.at(t.v4).y, vert.at(t.v4).z));
21  texturesBuff.push_back(t.texture);
22  }
23 
24  for (auto& t : tri) {
25  indicesBuff.push_back(1);
26  verticesBuff.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
27  verticesBuff.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
28  verticesBuff.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
29  texturesBuff.push_back(t.texture);
30  }
31 }
32 
34  std::vector<unsigned short> ind;
35  std::vector<glm::vec3> vert;
36  std::vector<glm::vec2> uvBuff;
37  std::vector<unsigned int> tex;
38 
39  int vertIndex = 0;
40  for (int i = 0; i < indicesBuff.size(); i++) {
41  unsigned int texture = TextureManager::getTile(texturesBuff.at(i)).getTexture();
42  for (int x = 0; x < ((indicesBuff.at(i) == 0) ? 4 : 3); x++) {
43  int v = x;
44  if (v == 0)
45  v = 2;
46  else if (v == 2)
47  v = 0;
48  ind.push_back(vert.size());
49  vert.push_back(verticesBuff.at(vertIndex + v));
50  uvBuff.push_back(TextureManager::getTile(texturesBuff.at(i)).getUV(v));
51  tex.push_back(texture);
52  }
53 
54  if (indicesBuff.at(i) == 0) {
55  ind.push_back(ind.at(ind.size() - 4));
56  ind.push_back(ind.at(ind.size() - 3));
57  }
58 
59  vertIndex += (indicesBuff.at(i) == 0) ? 4 : 3;
60  }
61 
62  assertEqual(ind.size() % 3, 0);
63  assertEqual(vert.size(), tex.size());
64  assertEqual(vert.size(), uvBuff.size());
65 
66  indicesBuff = std::move(ind);
67  vertices.bufferData(vert);
68  uvs.bufferData(uvBuff);
69  texturesBuff = std::move(tex);
70 }
71 
72 void RoomMesh::display(glm::mat4 MVP) {
73  if (indicesBuff.size() > 0) {
74  unsigned int indexStart = 0;
75  unsigned int indexPos = 1;
76  unsigned int texture = texturesBuff.at(indicesBuff.at(0));
77 
78  while ((indexStart != indexPos) && (indexPos < indicesBuff.size())) {
79  while ((indexPos < indicesBuff.size())
80  && (texturesBuff.at(indicesBuff.at(indexPos)) == texture)) {
81  indexPos++;
82  }
83 
84  std::vector<unsigned short> ind(indicesBuff.begin() + indexStart,
85  indicesBuff.begin() + indexPos);
86  indices.bufferData(ind);
87  Shader::drawGL(vertices, uvs, indices, texture, MVP);
88 
89  if (indexPos < indicesBuff.size()) {
90  indexStart = indexPos;
91  indexPos += 1;
92  texture = texturesBuff.at(indicesBuff.at(indexStart));
93  }
94  }
95  }
96 }
97 
void bufferData(int elem, int size, void *data)
Definition: Shader.cpp:18
static TextureTile & getTile(int index)
std::vector< unsigned short > indicesBuff
Definition: RoomMesh.h:37
Included everywhere.
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
#define assertEqual(x, y)
Definition: global.h:130
RoomMesh(const std::vector< RoomVertexTR2 > &vertices, const std::vector< IndexedRectangle > &rectangles, const std::vector< IndexedRectangle > &triangles)
Definition: RoomMesh.cpp:12
void display(glm::mat4 MVP)
Definition: RoomMesh.cpp:72
Room Mesh Geometry.
ShaderBuffer vertices
Definition: RoomMesh.h:40
Texture Registry.
std::vector< glm::vec3 > verticesBuff
Definition: RoomMesh.h:38
std::vector< unsigned int > texturesBuff
Definition: RoomMesh.h:39
ShaderBuffer indices
Definition: RoomMesh.h:40
void prepare()
Definition: RoomMesh.cpp:33
ShaderBuffer uvs
Definition: RoomMesh.h:40