Jelajahi Sumber

Merge pull request #15 from CrystalPointA4/feature/Skybox

Feature/skybox
Kenneth van Ewijk 9 tahun lalu
induk
melakukan
6bcd9b1426
29 mengubah file dengan 203 tambahan dan 6 penghapusan
  1. 2 0
      CrystalPoint.vcxproj
  2. 9 0
      CrystalPoint.vcxproj.filters
  3. 6 0
      HeightMap.cpp
  4. 1 0
      HeightMap.h
  5. 3 2
      Interface.cpp
  6. 1 0
      Main.cpp
  7. 144 0
      Skybox.cpp
  8. 16 0
      Skybox.h
  9. 17 4
      World.cpp
  10. 3 0
      World.h
  11. TEMPAT SAMPAH
      skyboxes/peaceful/back.png
  12. TEMPAT SAMPAH
      skyboxes/peaceful/bottom.png
  13. TEMPAT SAMPAH
      skyboxes/peaceful/front.png
  14. TEMPAT SAMPAH
      skyboxes/peaceful/left.png
  15. TEMPAT SAMPAH
      skyboxes/peaceful/right.png
  16. TEMPAT SAMPAH
      skyboxes/peaceful/top.png
  17. TEMPAT SAMPAH
      skyboxes/test/back.png
  18. TEMPAT SAMPAH
      skyboxes/test/bottom.png
  19. TEMPAT SAMPAH
      skyboxes/test/front.png
  20. TEMPAT SAMPAH
      skyboxes/test/left.png
  21. TEMPAT SAMPAH
      skyboxes/test/right.png
  22. TEMPAT SAMPAH
      skyboxes/test/top.png
  23. TEMPAT SAMPAH
      skyboxes/water/back.png
  24. TEMPAT SAMPAH
      skyboxes/water/bottom.png
  25. TEMPAT SAMPAH
      skyboxes/water/front.png
  26. TEMPAT SAMPAH
      skyboxes/water/left.png
  27. TEMPAT SAMPAH
      skyboxes/water/right.png
  28. TEMPAT SAMPAH
      skyboxes/water/top.png
  29. 1 0
      worlds/small.json

+ 2 - 0
CrystalPoint.vcxproj

@@ -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 - 0
CrystalPoint.vcxproj.filters

@@ -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 - 0
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;

+ 1 - 0
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<Vertex> vertices;

+ 3 - 2
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)

+ 1 - 0
Main.cpp

@@ -93,6 +93,7 @@ void configureOpenGL()
 
 	glEnable(GL_LIGHTING);
 	glEnable(GL_LIGHT0);
+	//glEnable(GL_COLOR_MATERIAL);
 
 	glutSetCursor(GLUT_CURSOR_NONE);
 }

+ 144 - 0
Skybox.cpp

@@ -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 - 0
Skybox.h

@@ -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
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<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 - 0
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<Entity*> entities;

TEMPAT SAMPAH
skyboxes/peaceful/back.png


TEMPAT SAMPAH
skyboxes/peaceful/bottom.png


TEMPAT SAMPAH
skyboxes/peaceful/front.png


TEMPAT SAMPAH
skyboxes/peaceful/left.png


TEMPAT SAMPAH
skyboxes/peaceful/right.png


TEMPAT SAMPAH
skyboxes/peaceful/top.png


TEMPAT SAMPAH
skyboxes/test/back.png


TEMPAT SAMPAH
skyboxes/test/bottom.png


TEMPAT SAMPAH
skyboxes/test/front.png


TEMPAT SAMPAH
skyboxes/test/left.png


TEMPAT SAMPAH
skyboxes/test/right.png


TEMPAT SAMPAH
skyboxes/test/top.png


TEMPAT SAMPAH
skyboxes/water/back.png


TEMPAT SAMPAH
skyboxes/water/bottom.png


TEMPAT SAMPAH
skyboxes/water/front.png


TEMPAT SAMPAH
skyboxes/water/left.png


TEMPAT SAMPAH
skyboxes/water/right.png


TEMPAT SAMPAH
skyboxes/water/top.png


+ 1 - 0
worlds/small.json

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