Pārlūkot izejas kodu

Implement optimised array rendering

Kenneth van Ewijk 9 gadi atpakaļ
vecāks
revīzija
84fcc346a9
7 mainītis faili ar 100 papildinājumiem un 44 dzēšanām
  1. 2 0
      CrystalJohan.vcxproj
  2. 6 0
      CrystalJohan.vcxproj.filters
  3. 2 13
      HeightMap.h
  4. 46 29
      Model.cpp
  5. 6 2
      Model.h
  6. 19 0
      Vertex.cpp
  7. 19 0
      Vertex.h

+ 2 - 0
CrystalJohan.vcxproj

@@ -163,6 +163,7 @@
     <ClCompile Include="Model.cpp" />
     <ClCompile Include="Player.cpp" />
     <ClCompile Include="Vector.cpp" />
+    <ClCompile Include="Vertex.cpp" />
     <ClCompile Include="World.cpp" />
   </ItemGroup>
   <ItemGroup>
@@ -178,6 +179,7 @@
     <ClInclude Include="Singleton.h" />
     <ClInclude Include="stb_image.h" />
     <ClInclude Include="vector.h" />
+    <ClInclude Include="Vertex.h" />
     <ClInclude Include="World.h" />
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

+ 6 - 0
CrystalJohan.vcxproj.filters

@@ -51,6 +51,9 @@
     <ClCompile Include="HeightMap.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="Vertex.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="CrystalJohan.h">
@@ -92,5 +95,8 @@
     <ClInclude Include="HeightMap.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="Vertex.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>

+ 2 - 13
HeightMap.h

@@ -1,4 +1,6 @@
 #pragma once
+#include "Vertex.h"
+
 #include <string>
 #include <vector>
 #include <GL/freeglut.h>
@@ -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<Vertex> vertices;
 };
 

+ 46 - 29
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<std::string> 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();
-	}
-
-	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);
+		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);
+	}
 }
 
 void Model::loadMaterialFile(std::string fileName, std::string dirName)

+ 6 - 2
Model.h

@@ -5,11 +5,13 @@
 #include <vector>
 #include <map>
 #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<Vertex> vertices;
+		std::list<VertexIndex> vertices;
 	};
 
 	class Texture
@@ -54,6 +56,7 @@ private:
 		std::string name;
 		int materialIndex;
 		std::list<Face> faces;
+		std::vector<Vertex> VertexArray;
 	};
 
 	std::vector<Vec3f>	vertices;
@@ -74,6 +77,7 @@ public:
 	static void unload(Model* model);
 
 	void draw();
+	void Optimise(ObjGroup *t);
 
 	Vec3f minVertex;
 	Vec3f maxVertex;

+ 19 - 0
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()
+{
+}

+ 19 - 0
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;
+};
+