From 84fcc346a9ef7f9c3b744836e598648838df8e9e Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Wed, 25 May 2016 12:44:24 +0200 Subject: [PATCH] Implement optimised array rendering --- CrystalJohan.vcxproj | 2 + CrystalJohan.vcxproj.filters | 6 +++ HeightMap.h | 15 +------ Model.cpp | 77 ++++++++++++++++++++++-------------- Model.h | 8 +++- Vertex.cpp | 19 +++++++++ Vertex.h | 19 +++++++++ 7 files changed, 101 insertions(+), 45 deletions(-) create mode 100644 Vertex.cpp create mode 100644 Vertex.h diff --git a/CrystalJohan.vcxproj b/CrystalJohan.vcxproj index 20344e7..5078850 100644 --- a/CrystalJohan.vcxproj +++ b/CrystalJohan.vcxproj @@ -163,6 +163,7 @@ + @@ -178,6 +179,7 @@ + diff --git a/CrystalJohan.vcxproj.filters b/CrystalJohan.vcxproj.filters index 5772419..aefe928 100644 --- a/CrystalJohan.vcxproj.filters +++ b/CrystalJohan.vcxproj.filters @@ -51,6 +51,9 @@ Source Files + + Source Files + @@ -92,5 +95,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/HeightMap.h b/HeightMap.h index ede7cc8..0cb4feb 100644 --- a/HeightMap.h +++ b/HeightMap.h @@ -1,4 +1,6 @@ #pragma once +#include "Vertex.h" + #include #include #include @@ -19,19 +21,6 @@ public: void GetHeigth(float x, float z); void SetTexture(const std::string &file); - struct Vertex { - float x; - float y; - float z; - - float normalX; - float normalY; - float normalZ; - - float texX; - float texY; - }; - std::vector vertices; }; diff --git a/Model.cpp b/Model.cpp index a8549a6..72c6cab 100644 --- a/Model.cpp +++ b/Model.cpp @@ -76,7 +76,7 @@ Model::Model(std::string fileName) for (size_t i = ii - 3; i < ii; i++) //magische forlus om van quads triangles te maken ;) { - Vertex vertex; + VertexIndex vertex; std::vector indices = split(params[i == (ii - 3) ? 1 : i], "/"); if (indices.size() >= 1) //er is een positie vertex.position = atoi(indices[0].c_str()) - 1; @@ -121,8 +121,42 @@ Model::Model(std::string fileName) } } groups.push_back(currentGroup); + + minVertex = vertices[0]; + maxVertex = vertices[0]; + for (auto v : vertices) + { + for (int i = 0; i < 3; i++) + { + minVertex[i] = fmin(minVertex[i], v[i]); + maxVertex[i] = fmax(maxVertex[i], v[i]); + } + } + center = (minVertex + maxVertex) / 2.0f; + radius = 0; + for (auto v : vertices) + radius = fmax(radius, (center.x - v.x) * (center.x - v.x) + (center.z - v.z) * (center.z - v.z)); + radius = sqrt(radius); + + for each(ObjGroup *group in groups) + { + Optimise(group); + } } +void Model::Optimise(ObjGroup *t) +{ + for (Face &face : t->faces) + { + for each(auto &vertex in face.vertices) + { + t->VertexArray.push_back(Vertex(vertices[vertex.position].x, vertices[vertex.position].y, vertices[vertex.position].z, + normals[vertex.normal].x, normals[vertex.normal].y, normals[vertex.normal].z, + texcoords[vertex.texcoord].x, texcoords[vertex.texcoord].y)); + + } + } +} Model::~Model(void) { @@ -160,36 +194,19 @@ void Model::draw() } } - glBegin(GL_TRIANGLES); - for (auto &f : g->faces) - { - for (auto &v : f.vertices) - { - glNormal3f(normals[v.normal].x, normals[v.normal].y, normals[v.normal].z); - glTexCoord2f(texcoords[v.texcoord].x, texcoords[v.texcoord].y); - glVertex3f(vertices[v.position].x, vertices[v.position].y, vertices[v.position].z); - } - } - glEnd(); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + + glVertexPointer(3, GL_FLOAT, sizeof(Vertex), ((float*)g->VertexArray.data()) + 0); + glNormalPointer(GL_FLOAT, sizeof(Vertex), ((float*)g->VertexArray.data()) + 3); + glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), ((float*)g->VertexArray.data()) + 6); + glDrawArrays(GL_TRIANGLES, 0, g->VertexArray.size()); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); } - - minVertex = vertices[0]; - maxVertex = vertices[0]; - for (auto v : vertices) - { - for (int i = 0; i < 3; i++) - { - minVertex[i] = fmin(minVertex[i], v[i]); - maxVertex[i] = fmax(maxVertex[i], v[i]); - } - } - center = (minVertex + maxVertex) / 2.0f; - radius = 0; - for (auto v : vertices) - radius = fmax(radius, (center.x - v.x) * (center.x - v.x) + (center.z - v.z) * (center.z - v.z)); - radius = sqrt(radius); - - } void Model::loadMaterialFile(std::string fileName, std::string dirName) diff --git a/Model.h b/Model.h index a16b45f..e2778c3 100644 --- a/Model.h +++ b/Model.h @@ -5,11 +5,13 @@ #include #include #include "Vector.h" +#include "Vertex.h" class Model { private: - class Vertex + + class VertexIndex { public: int position; @@ -20,7 +22,7 @@ private: class Face { public: - std::list vertices; + std::list vertices; }; class Texture @@ -54,6 +56,7 @@ private: std::string name; int materialIndex; std::list faces; + std::vector VertexArray; }; std::vector vertices; @@ -74,6 +77,7 @@ public: static void unload(Model* model); void draw(); + void Optimise(ObjGroup *t); Vec3f minVertex; Vec3f maxVertex; diff --git a/Vertex.cpp b/Vertex.cpp new file mode 100644 index 0000000..9e4aa85 --- /dev/null +++ b/Vertex.cpp @@ -0,0 +1,19 @@ +#include "Vertex.h" + +Vertex::Vertex(float x, float y, float z, float nx, float ny, float nz, float tx, float ty) +{ + this->x = x; + this->y = y; + this->z = z; + + this->normalX = nx; + this->normalY = ny; + this->normalZ = nz; + + this->texX = tx; + this->texY = ty; +} + +Vertex::~Vertex() +{ +} diff --git a/Vertex.h b/Vertex.h new file mode 100644 index 0000000..7edbc50 --- /dev/null +++ b/Vertex.h @@ -0,0 +1,19 @@ +#pragma once +class Vertex +{ +public: + Vertex(float x, float y, float z, float nx, float ny, float nz, float tx, float ty); + ~Vertex(); + + float x; + float y; + float z; + + float normalX; + float normalY; + float normalZ; + + float texX; + float texY; +}; +