Merge pull request #6 from CrystalPointA4/array-rendering
Implement optimised array rendering
This commit is contained in:
@@ -163,6 +163,7 @@
|
|||||||
<ClCompile Include="Model.cpp" />
|
<ClCompile Include="Model.cpp" />
|
||||||
<ClCompile Include="Player.cpp" />
|
<ClCompile Include="Player.cpp" />
|
||||||
<ClCompile Include="Vector.cpp" />
|
<ClCompile Include="Vector.cpp" />
|
||||||
|
<ClCompile Include="Vertex.cpp" />
|
||||||
<ClCompile Include="World.cpp" />
|
<ClCompile Include="World.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -178,6 +179,7 @@
|
|||||||
<ClInclude Include="Singleton.h" />
|
<ClInclude Include="Singleton.h" />
|
||||||
<ClInclude Include="stb_image.h" />
|
<ClInclude Include="stb_image.h" />
|
||||||
<ClInclude Include="vector.h" />
|
<ClInclude Include="vector.h" />
|
||||||
|
<ClInclude Include="Vertex.h" />
|
||||||
<ClInclude Include="World.h" />
|
<ClInclude Include="World.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
|||||||
@@ -51,6 +51,9 @@
|
|||||||
<ClCompile Include="HeightMap.cpp">
|
<ClCompile Include="HeightMap.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Vertex.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="CrystalJohan.h">
|
<ClInclude Include="CrystalJohan.h">
|
||||||
@@ -92,5 +95,8 @@
|
|||||||
<ClInclude Include="HeightMap.h">
|
<ClInclude Include="HeightMap.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Vertex.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
+2
-13
@@ -1,4 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Vertex.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <GL/freeglut.h>
|
#include <GL/freeglut.h>
|
||||||
@@ -19,19 +21,6 @@ public:
|
|||||||
void GetHeigth(float x, float z);
|
void GetHeigth(float x, float z);
|
||||||
void SetTexture(const std::string &file);
|
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<Vertex> vertices;
|
std::vector<Vertex> vertices;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -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 ;)
|
for (size_t i = ii - 3; i < ii; i++) //magische forlus om van quads triangles te maken ;)
|
||||||
{
|
{
|
||||||
Vertex vertex;
|
VertexIndex vertex;
|
||||||
std::vector<std::string> indices = split(params[i == (ii - 3) ? 1 : i], "/");
|
std::vector<std::string> indices = split(params[i == (ii - 3) ? 1 : i], "/");
|
||||||
if (indices.size() >= 1) //er is een positie
|
if (indices.size() >= 1) //er is een positie
|
||||||
vertex.position = atoi(indices[0].c_str()) - 1;
|
vertex.position = atoi(indices[0].c_str()) - 1;
|
||||||
@@ -121,8 +121,42 @@ Model::Model(std::string fileName)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
groups.push_back(currentGroup);
|
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)
|
Model::~Model(void)
|
||||||
{
|
{
|
||||||
@@ -160,36 +194,19 @@ void Model::draw()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
glBegin(GL_TRIANGLES);
|
glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
for (auto &f : g->faces)
|
glEnableClientState(GL_NORMAL_ARRAY);
|
||||||
{
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
for (auto &v : f.vertices)
|
|
||||||
{
|
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), ((float*)g->VertexArray.data()) + 0);
|
||||||
glNormal3f(normals[v.normal].x, normals[v.normal].y, normals[v.normal].z);
|
glNormalPointer(GL_FLOAT, sizeof(Vertex), ((float*)g->VertexArray.data()) + 3);
|
||||||
glTexCoord2f(texcoords[v.texcoord].x, texcoords[v.texcoord].y);
|
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), ((float*)g->VertexArray.data()) + 6);
|
||||||
glVertex3f(vertices[v.position].x, vertices[v.position].y, vertices[v.position].z);
|
glDrawArrays(GL_TRIANGLES, 0, g->VertexArray.size());
|
||||||
}
|
|
||||||
}
|
glDisableClientState(GL_VERTEX_ARRAY);
|
||||||
glEnd();
|
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)
|
void Model::loadMaterialFile(std::string fileName, std::string dirName)
|
||||||
|
|||||||
@@ -5,11 +5,13 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include "Vector.h"
|
#include "Vector.h"
|
||||||
|
#include "Vertex.h"
|
||||||
|
|
||||||
class Model
|
class Model
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
class Vertex
|
|
||||||
|
class VertexIndex
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
int position;
|
int position;
|
||||||
@@ -20,7 +22,7 @@ private:
|
|||||||
class Face
|
class Face
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
std::list<Vertex> vertices;
|
std::list<VertexIndex> vertices;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Texture
|
class Texture
|
||||||
@@ -54,6 +56,7 @@ private:
|
|||||||
std::string name;
|
std::string name;
|
||||||
int materialIndex;
|
int materialIndex;
|
||||||
std::list<Face> faces;
|
std::list<Face> faces;
|
||||||
|
std::vector<Vertex> VertexArray;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<Vec3f> vertices;
|
std::vector<Vec3f> vertices;
|
||||||
@@ -74,6 +77,7 @@ public:
|
|||||||
static void unload(Model* model);
|
static void unload(Model* model);
|
||||||
|
|
||||||
void draw();
|
void draw();
|
||||||
|
void Optimise(ObjGroup *t);
|
||||||
|
|
||||||
Vec3f minVertex;
|
Vec3f minVertex;
|
||||||
Vec3f maxVertex;
|
Vec3f maxVertex;
|
||||||
|
|||||||
+19
@@ -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()
|
||||||
|
{
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user