diff --git a/CrystalPoint.vcxproj b/CrystalPoint.vcxproj
index 867d173..a14f55a 100644
--- a/CrystalPoint.vcxproj
+++ b/CrystalPoint.vcxproj
@@ -168,6 +168,7 @@
+
@@ -188,6 +189,7 @@
+
diff --git a/CrystalPoint.vcxproj.filters b/CrystalPoint.vcxproj.filters
index faa1d7c..df8bece 100644
--- a/CrystalPoint.vcxproj.filters
+++ b/CrystalPoint.vcxproj.filters
@@ -69,6 +69,12 @@
Source Files\World
+
+ Source Files\World
+
+
+ Source Files\Object
+
Source Files
@@ -137,6 +143,9 @@
Header Files
+
+ Header Files
+
diff --git a/HeightMap.cpp b/HeightMap.cpp
index e93fc7a..99e4924 100644
--- a/HeightMap.cpp
+++ b/HeightMap.cpp
@@ -131,6 +131,12 @@ float HeightMap::GetHeight(float x, float y)
return z.y;
}
+
+int HeightMap::GetSize()
+{
+ return height >= width ? height : width;
+}
+
void HeightMap::SetTexture(const std::string &file)
{
int bpp, width2, height2;
diff --git a/HeightMap.h b/HeightMap.h
index fe2e963..7ea9178 100644
--- a/HeightMap.h
+++ b/HeightMap.h
@@ -20,6 +20,7 @@ public:
void Draw();
float GetHeight(float x, float y);
+ int GetSize();
void SetTexture(const std::string &file);
std::vector vertices;
diff --git a/Interface.cpp b/Interface.cpp
index 9a3d498..4a89725 100644
--- a/Interface.cpp
+++ b/Interface.cpp
@@ -77,8 +77,6 @@ void Interface::draw()
glColor4f(1.0f, 1.0f, 0.1f, 1.0);
glutBitmapString("Level: " + std::to_string(player->level), 490, 900);
-
-
int cw, ch, offset;
cw = 20;
ch = 50;
@@ -94,6 +92,9 @@ void Interface::draw()
glVertex2f(975 , ch / 2 + offset*i + ch*i);
glEnd();
}
+
+ glEnable(GL_LIGHTING);
+ glEnable(GL_DEPTH_TEST);
}
void Interface::update(float deltaTime)
diff --git a/Main.cpp b/Main.cpp
index f911678..2660258 100644
--- a/Main.cpp
+++ b/Main.cpp
@@ -93,6 +93,7 @@ void configureOpenGL()
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
+ //glEnable(GL_COLOR_MATERIAL);
glutSetCursor(GLUT_CURSOR_NONE);
}
\ No newline at end of file
diff --git a/Skybox.cpp b/Skybox.cpp
new file mode 100644
index 0000000..1d268ab
--- /dev/null
+++ b/Skybox.cpp
@@ -0,0 +1,144 @@
+#include "cmath"
+#include
+
+#include "stb_image.h"
+#include "Skybox.h"
+#include
+
+enum{SKY_LEFT=0,SKY_BACK,SKY_RIGHT,SKY_FRONT,SKY_TOP,SKY_BOTTOM};
+GLuint skybox[6];
+
+Skybox::Skybox(const float &size, const std::string &folder)
+{
+ this->size = size;
+ this->folder = folder;
+}
+
+Skybox::~Skybox()
+{
+ glDeleteTextures(6, &skybox[0]);
+}
+
+void Skybox::init()
+{
+ skybox[SKY_LEFT] = loadTexture(folder + "left.png");
+ skybox[SKY_BACK] = loadTexture(folder + "back.png");
+ skybox[SKY_RIGHT] = loadTexture(folder + "right.png");
+ skybox[SKY_FRONT] = loadTexture(folder + "front.png");
+ skybox[SKY_TOP] = loadTexture(folder + "top.png");
+ skybox[SKY_BOTTOM] = loadTexture(folder + "bottom.png");
+}
+
+void Skybox::draw()
+{
+ bool b1 = glIsEnabled(GL_TEXTURE_2D);
+ glDisable(GL_LIGHTING);
+ glDisable(GL_DEPTH_TEST);
+ glEnable(GL_TEXTURE_2D);
+ glDisable(GL_COLOR_MATERIAL);
+
+ glBindTexture(GL_TEXTURE_2D, skybox[SKY_BACK]);
+ glBegin(GL_QUADS);
+ glTexCoord2f(1,1);
+ glVertex3f(size / 2, size / 2, size / 2);
+ glTexCoord2f(0,1);
+ glVertex3f(-size / 2, size / 2, size / 2);
+ glTexCoord2f(0,0);
+ glVertex3f(-size / 2, -size / 2, size / 2);
+ glTexCoord2f(1,0);
+ glVertex3f(size / 2, -size / 2, size / 2);
+ glEnd();
+
+ glBindTexture(GL_TEXTURE_2D, skybox[SKY_LEFT]);
+ glBegin(GL_QUADS);
+ //left face
+ glTexCoord2f(1,1);
+ glVertex3f(-size / 2, size / 2, size / 2);
+ glTexCoord2f(0,1);
+ glVertex3f(-size / 2, size / 2, -size / 2);
+ glTexCoord2f(0,0);
+ glVertex3f(-size / 2, -size / 2, -size / 2);
+ glTexCoord2f(1,0);
+ glVertex3f(-size / 2, -size / 2, size / 2);
+ glEnd();
+ glBindTexture(GL_TEXTURE_2D, skybox[SKY_FRONT]);
+ glBegin(GL_QUADS);
+ //front face
+ glTexCoord2f(0, 1);
+ glVertex3f(size / 2, size / 2, -size / 2);
+ glTexCoord2f(1, 1);
+ glVertex3f(-size / 2, size / 2, -size / 2);
+ glTexCoord2f(1, 0);
+ glVertex3f(-size / 2, -size / 2, -size / 2);
+ glTexCoord2f(0, 0);
+ glVertex3f(size / 2, -size / 2, -size / 2);
+ glEnd();
+ glBindTexture(GL_TEXTURE_2D, skybox[SKY_RIGHT]);
+ glBegin(GL_QUADS);
+ //right face
+ glTexCoord2f(1, 1);
+ glVertex3f(size / 2, size / 2, -size / 2);
+ glTexCoord2f(0,1);
+ glVertex3f(size / 2, size / 2, size / 2);
+ glTexCoord2f(0,0);
+ glVertex3f(size / 2, -size / 2, size / 2);
+ glTexCoord2f(1, 0);
+ glVertex3f(size / 2, -size / 2, -size / 2);
+ glEnd();
+ glBindTexture(GL_TEXTURE_2D, skybox[SKY_TOP]);
+ glBegin(GL_QUADS); //top face
+ glTexCoord2f(0,0);
+ glVertex3f(size / 2, size / 2, size / 2);
+ glTexCoord2f(0,1);
+ glVertex3f(-size / 2, size / 2, size / 2);
+ glTexCoord2f(1,1);
+ glVertex3f(-size / 2, size / 2, -size / 2);
+ glTexCoord2f(1,0);
+ glVertex3f(size / 2, size / 2, -size / 2);
+ glEnd();
+ glBindTexture(GL_TEXTURE_2D, skybox[SKY_BOTTOM]);
+ glBegin(GL_QUADS);
+ //bottom face
+ glTexCoord2f(0,1);
+ glVertex3f(size / 2, -size / 2, size / 2);
+ glTexCoord2f(0,0);
+ glVertex3f(-size / 2, -size / 2, size / 2);
+ glTexCoord2f(1,0);
+ glVertex3f(-size / 2, -size / 2, -size / 2);
+ glTexCoord2f(1,1);
+ glVertex3f(size / 2, -size / 2, -size / 2);
+ glEnd();
+ glEnable(GL_LIGHTING); //turn everything back, which we turned on, and turn everything off, which we have turned on.
+ glEnable(GL_DEPTH_TEST);
+ if (!b1)
+ glDisable(GL_TEXTURE_2D);
+}
+
+GLuint Skybox::loadTexture(const std::string & fileName) //load the filename named texture
+{
+ int width, height, bpp;
+
+ stbi_set_flip_vertically_on_load(true);
+ unsigned char* imgData = stbi_load(fileName.c_str(), &width, &height, &bpp, 4);
+ GLuint num;
+ glGenTextures(1, &num);
+ glBindTexture(GL_TEXTURE_2D, num);
+
+ 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);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
+
+ stbi_image_free(imgData);
+ return num;
+}
+
diff --git a/Skybox.h b/Skybox.h
new file mode 100644
index 0000000..5b51fbb
--- /dev/null
+++ b/Skybox.h
@@ -0,0 +1,16 @@
+#pragma once
+#include
+
+class Skybox
+{
+private:
+ float size;
+ std::string folder;
+public:
+ Skybox(const float &size, const std::string &folder);
+ ~Skybox();
+
+ void init();
+ void draw();
+ GLuint loadTexture(const std::string &fileName);
+};
diff --git a/World.cpp b/World.cpp
index 849db00..869de65 100644
--- a/World.cpp
+++ b/World.cpp
@@ -26,14 +26,18 @@ World::World(const std::string &fileName):
file.close();
//Check file
- if(v["world"].isNull() || v["world"]["heightmap"].isNull())
+ if(v["world"].isNull() || v["world"]["heightmap"].isNull() || v["world"]["skybox"].isNull())
std::cout << "Invalid world file: world - " << fileName << "\n";
+ if (v["world"]["object-templates"].isNull())
+ std::cout << "Invalid world file: object templates - " << fileName << "\n";
if (v["player"].isNull() || v["player"]["startposition"].isNull())
std::cout << "Invalid world file: player - " << fileName << "\n";
if (v["objects"].isNull())
std::cout << "Invalid world file: objects - " << fileName << "\n";
- if (v["world"]["object-templates"].isNull())
- std::cout << "Invalid world file: object templates - " << fileName << "\n";
+ if (v["enemies"].isNull())
+ std::cout << "Invalid world file: enemies - " << fileName << "\n";
+ if (v["crystals"].isNull())
+ std::cout << "Invalid world file: crystals - " << fileName << "\n";
//Load object templates
for (auto objt : v["world"]["object-templates"])
@@ -49,6 +53,10 @@ World::World(const std::string &fileName):
//Generate heightmap for this world
heightmap = new HeightMap(v["world"]["heightmap"].asString(), this);
+ //Load skybox
+ skybox = new Skybox(15000.0f, v["world"]["skybox"].asString());
+ skybox->init();
+
//Map different texture to heightmap if available
if(!v["world"]["texture"].isNull())
heightmap->SetTexture(v["world"]["texture"].asString());
@@ -159,7 +167,8 @@ World::World(const std::string &fileName):
World::~World()
{
- //delete heightmap;
+ delete heightmap;
+ delete skybox;
}
std::pair World::getObjectFromValue(int val)
@@ -187,6 +196,10 @@ void World::draw()
float lightAmbient[4] = { 0.2, 0.2, 0.2, 1 };
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
+ glColor4f(1, 1, 1, 1);
+
+ skybox->draw();
+
heightmap->Draw();
for (auto &enemy : enemies)
diff --git a/World.h b/World.h
index 6152f31..8a8f946 100644
--- a/World.h
+++ b/World.h
@@ -7,6 +7,7 @@
#include "LevelObject.h"
#include "Interface.h"
#include "Crystal.h"
+#include "Skybox.h"
class Entity;
@@ -18,6 +19,8 @@ private:
Player* player;
HeightMap* heightmap;
Interface* interface;
+ Skybox* skybox;
+
int music_id;
std::vector entities;
diff --git a/skyboxes/peaceful/back.png b/skyboxes/peaceful/back.png
new file mode 100644
index 0000000..d45a424
Binary files /dev/null and b/skyboxes/peaceful/back.png differ
diff --git a/skyboxes/peaceful/bottom.png b/skyboxes/peaceful/bottom.png
new file mode 100644
index 0000000..5db7212
Binary files /dev/null and b/skyboxes/peaceful/bottom.png differ
diff --git a/skyboxes/peaceful/front.png b/skyboxes/peaceful/front.png
new file mode 100644
index 0000000..85f1c79
Binary files /dev/null and b/skyboxes/peaceful/front.png differ
diff --git a/skyboxes/peaceful/left.png b/skyboxes/peaceful/left.png
new file mode 100644
index 0000000..eee545d
Binary files /dev/null and b/skyboxes/peaceful/left.png differ
diff --git a/skyboxes/peaceful/right.png b/skyboxes/peaceful/right.png
new file mode 100644
index 0000000..5ff5a2e
Binary files /dev/null and b/skyboxes/peaceful/right.png differ
diff --git a/skyboxes/peaceful/top.png b/skyboxes/peaceful/top.png
new file mode 100644
index 0000000..8e799f0
Binary files /dev/null and b/skyboxes/peaceful/top.png differ
diff --git a/skyboxes/test/back.png b/skyboxes/test/back.png
new file mode 100644
index 0000000..7cc0a30
Binary files /dev/null and b/skyboxes/test/back.png differ
diff --git a/skyboxes/test/bottom.png b/skyboxes/test/bottom.png
new file mode 100644
index 0000000..f742d17
Binary files /dev/null and b/skyboxes/test/bottom.png differ
diff --git a/skyboxes/test/front.png b/skyboxes/test/front.png
new file mode 100644
index 0000000..af353ce
Binary files /dev/null and b/skyboxes/test/front.png differ
diff --git a/skyboxes/test/left.png b/skyboxes/test/left.png
new file mode 100644
index 0000000..568e58c
Binary files /dev/null and b/skyboxes/test/left.png differ
diff --git a/skyboxes/test/right.png b/skyboxes/test/right.png
new file mode 100644
index 0000000..9443f8c
Binary files /dev/null and b/skyboxes/test/right.png differ
diff --git a/skyboxes/test/top.png b/skyboxes/test/top.png
new file mode 100644
index 0000000..0dbc574
Binary files /dev/null and b/skyboxes/test/top.png differ
diff --git a/skyboxes/water/back.png b/skyboxes/water/back.png
new file mode 100644
index 0000000..d813596
Binary files /dev/null and b/skyboxes/water/back.png differ
diff --git a/skyboxes/water/bottom.png b/skyboxes/water/bottom.png
new file mode 100644
index 0000000..66436b9
Binary files /dev/null and b/skyboxes/water/bottom.png differ
diff --git a/skyboxes/water/front.png b/skyboxes/water/front.png
new file mode 100644
index 0000000..4e24463
Binary files /dev/null and b/skyboxes/water/front.png differ
diff --git a/skyboxes/water/left.png b/skyboxes/water/left.png
new file mode 100644
index 0000000..46cc0db
Binary files /dev/null and b/skyboxes/water/left.png differ
diff --git a/skyboxes/water/right.png b/skyboxes/water/right.png
new file mode 100644
index 0000000..70a00f1
Binary files /dev/null and b/skyboxes/water/right.png differ
diff --git a/skyboxes/water/top.png b/skyboxes/water/top.png
new file mode 100644
index 0000000..970b815
Binary files /dev/null and b/skyboxes/water/top.png differ
diff --git a/worlds/small.json b/worlds/small.json
index 34c9513..42e51a8 100644
--- a/worlds/small.json
+++ b/worlds/small.json
@@ -1,6 +1,7 @@
{
"world": {
"heightmap": "worlds/small.png",
+ "skybox": "skyboxes/water/",
"object-templates": [
{
"color": 100,