Просмотр исходного кода

Merge branch 'developer' into enemy

# Conflicts:
#	World.h
#	worlds/world1.json
Remco 9 лет назад
Родитель
Сommit
b2fa0bc028
16 измененных файлов с 256 добавлено и 52 удалено
  1. 8 11
      CrystalJohan.cpp
  2. 4 0
      CrystalJohan.vcxproj
  3. 12 0
      CrystalJohan.vcxproj.filters
  4. 99 0
      HeightMap.cpp
  5. 26 0
      HeightMap.h
  6. 46 29
      Model.cpp
  7. 6 2
      Model.h
  8. 19 0
      Vertex.cpp
  9. 19 0
      Vertex.h
  10. 7 9
      World.cpp
  11. 5 0
      World.h
  12. BIN
      worlds/hell.png
  13. BIN
      worlds/helltexture.png
  14. BIN
      worlds/hmcs.png
  15. BIN
      worlds/hmcstexture.png
  16. 5 1
      worlds/world1.json

+ 8 - 11
CrystalJohan.cpp

@@ -29,7 +29,7 @@ void CrystalJohan::draw()
 
 	glMatrixMode(GL_PROJECTION);
 	glLoadIdentity();
-	gluPerspective(70, width / (float)height, 0.1f, 100);
+	gluPerspective(70, width / (float)height, 0.1f, 15000);
 	glMatrixMode(GL_MODELVIEW);
 	glLoadIdentity();
 
@@ -52,11 +52,7 @@ void CrystalJohan::draw()
 
 	glEnd();*/
 
-
-
 	glutSwapBuffers();
-
-
 }
 
 
@@ -78,14 +74,15 @@ void CrystalJohan::update()
 	if (world->player.rotation.x < -90)
 		world->player.rotation.x = -90;
 
+	float speed = 20;
 
 	Vec3f oldPosition = world->player.position;
-	if (keyboardState.keys['a']) world->player.setPosition(0, deltaTime, false);
-	if (keyboardState.keys['d']) world->player.setPosition(180, deltaTime, false);
-	if (keyboardState.keys['w']) world->player.setPosition(90, deltaTime, false);
-	if (keyboardState.keys['s']) world->player.setPosition(270, deltaTime, false);
-	if (keyboardState.keys['q']) world->player.setPosition(1, deltaTime, true);
-	if (keyboardState.keys['e']) world->player.setPosition(-1, deltaTime, true);
+	if (keyboardState.keys['a']) world->player.setPosition(0, deltaTime*speed, false);
+	if (keyboardState.keys['d']) world->player.setPosition(180, deltaTime*speed, false);
+	if (keyboardState.keys['w']) world->player.setPosition(90, deltaTime*speed, false);
+	if (keyboardState.keys['s']) world->player.setPosition(270, deltaTime*speed, false);
+	if (keyboardState.keys['q']) world->player.setPosition(1, deltaTime*speed, true);
+	if (keyboardState.keys['e']) world->player.setPosition(-1, deltaTime*speed, true);
 	if (!world->isPlayerPositionValid())
 		world->player.position = oldPosition;
 

+ 4 - 0
CrystalJohan.vcxproj

@@ -156,18 +156,21 @@
     <ClCompile Include="CrystalJohan.cpp" />
     <ClCompile Include="Enemy.cpp" />
     <ClCompile Include="Entity.cpp" />
+    <ClCompile Include="HeightMap.cpp" />
     <ClCompile Include="json.cpp" />
     <ClCompile Include="LevelObject.cpp" />
     <ClCompile Include="Main.cpp" />
     <ClCompile Include="Model.cpp" />
     <ClCompile Include="Player.cpp" />
     <ClCompile Include="Vector.cpp" />
+    <ClCompile Include="Vertex.cpp" />
     <ClCompile Include="World.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="CrystalJohan.h" />
     <ClInclude Include="Enemy.h" />
     <ClInclude Include="Entity.h" />
+    <ClInclude Include="HeightMap.h" />
     <ClInclude Include="json.h" />
     <ClInclude Include="LevelObject.h" />
     <ClInclude Include="Main.h" />
@@ -176,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>
   <ItemGroup>

+ 12 - 0
CrystalJohan.vcxproj.filters

@@ -48,6 +48,12 @@
     <ClCompile Include="json.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="HeightMap.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="Vertex.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="CrystalJohan.h">
@@ -86,6 +92,12 @@
     <ClInclude Include="json.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="HeightMap.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="Vertex.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <None Include="worlds\world1.json" />

+ 99 - 0
HeightMap.cpp

@@ -0,0 +1,99 @@
+#include "HeightMap.h"
+#include "stb_image.h"
+
+#include <GL/freeglut.h>
+#include <iostream>
+#include <string>
+
+
+HeightMap::HeightMap(const std::string &file)
+{
+	int bpp;
+	unsigned char* imgData = stbi_load(file.c_str(), &width, &height, &bpp, 4);
+
+	for (int h = 0; h < height; h++)
+	{
+		for (int w = 0; w < width; w++)
+		{
+			int offsets[4][2] = { { 0, 0 },{ 1, 0 },{ 1, 1 },{ 0, 1 } };
+			for (int i = 0; i < 4; i++)
+			{
+				float y = ((float)imgData[((h + offsets[i][0]) + (w + offsets[i][1]) * width) * 4]);
+				y = (y / 256.0f) * 100.0f;
+
+				vertices.push_back(Vertex{ (float)(h + offsets[i][0])*scale, y*scale, (float)(w + offsets[i][1])*scale,
+									0, 1, 0,
+									(h + offsets[i][0]) / (float)height, (w + offsets[i][1]) / (float)width } );
+			}
+		}
+	}
+
+	glGenTextures(1, &imageIndex);
+	glBindTexture(GL_TEXTURE_2D, imageIndex);
+
+	glTexImage2D(GL_TEXTURE_2D,
+		0,					//level
+		GL_RGBA,			//internal format
+		width,				//width
+		height,				//height
+		0,					//border
+		GL_RGBA,			//data format
+		GL_UNSIGNED_BYTE,	//data type
+		imgData);			//data
+	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+
+	stbi_image_free(imgData);
+}
+
+HeightMap::~HeightMap()
+{
+}
+
+void HeightMap::Draw()
+{
+	glEnable(GL_TEXTURE_2D);
+	glBindTexture(GL_TEXTURE_2D, imageIndex);
+
+	glEnableClientState(GL_VERTEX_ARRAY);
+	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+	//glEnableClientState(GL_COLOR_ARRAY);
+	//glEnableClientState(GL_NORMAL_ARRAY);
+
+	glVertexPointer(3, GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 0);
+	glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 6);
+	//glNormalPointer(GL_FLOAT, sizeof(Vertex), ((float*)cubeVertices.data()) + 3);
+	glDrawArrays(GL_QUADS, 0, vertices.size());
+
+	glDisableClientState(GL_VERTEX_ARRAY);
+	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+	//glDisableClientState(GL_COLOR_ARRAY);
+	//glDisableClientState(GL_NORMAL_ARRAY);
+}
+
+void HeightMap::GetHeigth(float x, float z)
+{
+
+}
+
+void HeightMap::SetTexture(const std::string &file)
+{
+	int bpp, width2, height2;
+	unsigned char* imgData = stbi_load(file.c_str(), &width2, &height2, &bpp, 4);
+
+	glBindTexture(GL_TEXTURE_2D, imageIndex);
+
+	glTexImage2D(GL_TEXTURE_2D,
+		0,		//level
+		GL_RGBA,		//internal format
+		width2,		//width
+		height2,		//height
+		0,		//border
+		GL_RGBA,		//data format
+		GL_UNSIGNED_BYTE,	//data type
+		imgData);		//data
+	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+
+	stbi_image_free(imgData);
+}

+ 26 - 0
HeightMap.h

@@ -0,0 +1,26 @@
+#pragma once
+#include "Vertex.h"
+
+#include <string>
+#include <vector>
+#include <GL/freeglut.h>
+
+class HeightMap
+{
+private:
+	int height;
+	int width;
+
+	GLuint imageIndex;
+	int scale = 1;
+public:
+	HeightMap(const std::string &file);
+	~HeightMap();
+
+	void Draw();
+	void GetHeigth(float x, float z);
+	void SetTexture(const std::string &file);
+
+	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;
+};
+

+ 7 - 9
World.cpp

@@ -12,9 +12,13 @@ World::World() : player(Player::getInstance())
 	std::ifstream file("worlds/world1.json");
 	if(!file.is_open())
 		std::cout<<"Uhoh, can't open file\n";
+
 	json::Value v = json::readJson(file);
-	std::cout<<v;
 	file.close();
+
+	heightmap = new HeightMap(v["world"]["heightmap"].asString());
+	heightmap->SetTexture(v["world"]["texture"].asString());
+
 	player.position.x = v["player"]["startposition"][0];
 	player.position.y = v["player"]["startposition"][1];
 	player.position.z = v["player"]["startposition"][2];
@@ -96,14 +100,8 @@ void World::draw()
 	float lightAmbient[4] = { 0.5, 0.5, 0.5, 1 };
 	glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
 
-	glColor3f(0.5f, 0.9f, 0.5f);
-	glNormal3f(0, 1, 0);
-	glBegin(GL_QUADS);
-	glVertex3f(-50, 0, -50);
-	glVertex3f(-50, 0, 50);
-	glVertex3f(50, 0, 50);
-	glVertex3f(50, 0, -50);
-	glEnd();
+	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
+	heightmap->Draw();
 
 	for (auto &enemy : enemies)
 		enemy->draw();

+ 5 - 0
World.h

@@ -1,6 +1,7 @@
 #pragma once
 
 #include <vector>
+#include "HeightMap.h"
 #include "Player.h"
 #include "Enemy.h"
 
@@ -15,7 +16,11 @@ public:
 
 	Player& player;
 	std::vector<Entity*> entities;
+
 	std::vector<Enemy*> enemies;
+
+	HeightMap* heightmap;
+
 	void draw();
 	void update(float elapsedTime);
 	bool isPlayerPositionValid();

BIN
worlds/hell.png


BIN
worlds/helltexture.png


BIN
worlds/hmcs.png


BIN
worlds/hmcstexture.png


+ 5 - 1
worlds/world1.json

@@ -1,6 +1,10 @@
 {
+   "world": {
+      "heightmap": "worlds/hmcs.png",
+	  "texture": "worlds/hmcstexture.png"
+   },
   "player": {
-    "startposition": [ 0, 1.7, 0 ]
+    "startposition": [ -100, 1.7, -100 ]
   },
   "objects": [
     {