Merge pull request #15 from CrystalPointA4/feature/Skybox

Feature/skybox
This commit is contained in:
2016-06-09 13:04:19 +02:00
29 changed files with 203 additions and 6 deletions
+2
View File
@@ -168,6 +168,7 @@
<ClCompile Include="Sound.cpp" />
<ClCompile Include="SoundSystem.cpp" />
<ClCompile Include="Player.cpp" />
<ClCompile Include="Skybox.cpp" />
<ClCompile Include="Vector.cpp" />
<ClCompile Include="Vertex.cpp" />
<ClCompile Include="World.cpp" />
@@ -188,6 +189,7 @@
<ClInclude Include="Sound.h" />
<ClInclude Include="SoundSystem.h" />
<ClInclude Include="Player.h" />
<ClInclude Include="Skybox.h" />
<ClInclude Include="stb_image.h" />
<ClInclude Include="vector.h" />
<ClInclude Include="Vertex.h" />
+9
View File
@@ -69,6 +69,12 @@
<ClCompile Include="Interface.cpp">
<Filter>Source Files\World</Filter>
</ClCompile>
<ClCompile Include="Skybox.cpp">
<Filter>Source Files\World</Filter>
</ClCompile>
<ClCompile Include="Crystal.cpp">
<Filter>Source Files\Object</Filter>
</ClCompile>
<ClCompile Include="SoundSystem.cpp">
<Filter>Source Files</Filter>
</ClCompile>
@@ -137,6 +143,9 @@
<ClInclude Include="Crystal.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Skybox.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="worlds\worlds.json">
+6
View File
@@ -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;
+1
View File
@@ -20,6 +20,7 @@ public:
void Draw();
float GetHeight(float x, float y);
int GetSize();
void SetTexture(const std::string &file);
std::vector<Vertex> vertices;
+3 -2
View File
@@ -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)
+1
View File
@@ -93,6 +93,7 @@ void configureOpenGL()
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
//glEnable(GL_COLOR_MATERIAL);
glutSetCursor(GLUT_CURSOR_NONE);
}
+144
View File
@@ -0,0 +1,144 @@
#include "cmath"
#include <GL/freeglut.h>
#include "stb_image.h"
#include "Skybox.h"
#include <string>
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;
}
+16
View File
@@ -0,0 +1,16 @@
#pragma once
#include <string>
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);
};
+17 -4
View File
@@ -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<std::string, bool> 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)
+3
View File
@@ -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<Entity*> entities;
Binary file not shown.

After

Width:  |  Height:  |  Size: 136 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 118 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 132 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 136 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 123 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 113 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 132 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 720 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 378 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 742 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 496 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 768 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 KiB

+1
View File
@@ -1,6 +1,7 @@
{
"world": {
"heightmap": "worlds/small.png",
"skybox": "skyboxes/water/",
"object-templates": [
{
"color": 100,