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 #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; +}; + diff --git a/World.cpp b/World.cpp index f8d2eb6..98e8a7d 100644 --- a/World.cpp +++ b/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<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(); diff --git a/World.h b/World.h index 57d84d3..91ad3e6 100644 --- a/World.h +++ b/World.h @@ -1,6 +1,7 @@ #pragma once #include +#include "HeightMap.h" #include "Player.h" #include "Enemy.h" @@ -15,7 +16,11 @@ public: Player& player; std::vector entities; + std::vector enemies; + + HeightMap* heightmap; + void draw(); void update(float elapsedTime); bool isPlayerPositionValid(); diff --git a/worlds/hell.png b/worlds/hell.png new file mode 100644 index 0000000..6d4789b Binary files /dev/null and b/worlds/hell.png differ diff --git a/worlds/helltexture.png b/worlds/helltexture.png new file mode 100644 index 0000000..49d490a Binary files /dev/null and b/worlds/helltexture.png differ diff --git a/worlds/hmcs.png b/worlds/hmcs.png new file mode 100644 index 0000000..4666651 Binary files /dev/null and b/worlds/hmcs.png differ diff --git a/worlds/hmcstexture.png b/worlds/hmcstexture.png new file mode 100644 index 0000000..0dbff01 Binary files /dev/null and b/worlds/hmcstexture.png differ diff --git a/worlds/world1.json b/worlds/world1.json index ad02c89..7eff300 100644 --- a/worlds/world1.json +++ b/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": [ {