diff --git a/CrystalJohan.cpp b/CrystalJohan.cpp
index 3e68723..dd6672a 100644
--- a/CrystalJohan.cpp
+++ b/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;
diff --git a/CrystalJohan.vcxproj b/CrystalJohan.vcxproj
index ad34863..eb8f65c 100644
--- a/CrystalJohan.vcxproj
+++ b/CrystalJohan.vcxproj
@@ -156,18 +156,21 @@
+
+
+
@@ -176,6 +179,7 @@
+
diff --git a/CrystalJohan.vcxproj.filters b/CrystalJohan.vcxproj.filters
index 02225e5..4b5393a 100644
--- a/CrystalJohan.vcxproj.filters
+++ b/CrystalJohan.vcxproj.filters
@@ -48,6 +48,12 @@
Source Files
+
+ Source Files
+
+
+ Source Files
+
@@ -86,6 +92,12 @@
Header Files
+
+ Header Files
+
+
+ Header Files
+
diff --git a/HeightMap.cpp b/HeightMap.cpp
new file mode 100644
index 0000000..f68e36f
--- /dev/null
+++ b/HeightMap.cpp
@@ -0,0 +1,99 @@
+#include "HeightMap.h"
+#include "stb_image.h"
+
+#include
+#include
+#include
+
+
+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);
+}
\ No newline at end of file
diff --git a/HeightMap.h b/HeightMap.h
new file mode 100644
index 0000000..0cb4feb
--- /dev/null
+++ b/HeightMap.h
@@ -0,0 +1,26 @@
+#pragma once
+#include "Vertex.h"
+
+#include
+#include
+#include
+
+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 vertices;
+};
+
diff --git a/Model.cpp b/Model.cpp
index c599a80..9a86b2e 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