diff --git a/CrystalJohan.cpp b/CrystalJohan.cpp index 4997c6f..07b0be5 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 2c3970d..20344e7 100644 --- a/CrystalJohan.vcxproj +++ b/CrystalJohan.vcxproj @@ -156,6 +156,7 @@ + @@ -168,6 +169,7 @@ + diff --git a/CrystalJohan.vcxproj.filters b/CrystalJohan.vcxproj.filters index 7339905..5772419 100644 --- a/CrystalJohan.vcxproj.filters +++ b/CrystalJohan.vcxproj.filters @@ -48,6 +48,9 @@ Source Files + + Source Files + @@ -86,5 +89,8 @@ Header Files + + Header Files + \ No newline at end of file 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..0857248 --- /dev/null +++ b/HeightMap.h @@ -0,0 +1,24 @@ +#pragma once +#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/World.cpp b/World.cpp index b31b697..62f1b4f 100644 --- a/World.cpp +++ b/World.cpp @@ -11,9 +11,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]; @@ -51,14 +55,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 e : entities) e->draw(); diff --git a/World.h b/World.h index b37377f..ebe239e 100644 --- a/World.h +++ b/World.h @@ -1,6 +1,7 @@ #pragma once #include +#include "HeightMap.h" #include "Player.h" class Entity; @@ -14,6 +15,7 @@ public: Player& player; std::vector entities; + HeightMap* heightmap; void draw(); void update(float elapsedTime); 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 a2aa577..c3c16d4 100644 --- a/worlds/world1.json +++ b/worlds/world1.json @@ -1,12 +1,15 @@ { + "world": { + "heightmap": "worlds/hmcs.png", + "texture": "worlds/hmcstexture.png" + }, "player": { - "startposition": [ 0, 1.7, 0 ] + "startposition": [ -100, 1.7, -100 ] }, "objects": [ { "file": "models/boom/Boom.obj", - "pos": [ 0, 0, -4 ], - "rot": [ 0, 20, 0 ] + "pos": [ 0, 0, -4 ] }, { "file": "models/boom/Boom.obj",