From 167548dfc2e94cb9aad5d3a9c8f0131886d16544 Mon Sep 17 00:00:00 2001
From: Marc-Eric Martel <1205697@etu.cegepsth.qc.ca>
Date: Tue, 12 Oct 2021 15:58:54 -0400
Subject: [PATCH] TP3 + classe Skybox
---
SQCSim2021/SQCSim2021.vcxproj | 2 +
SQCSim2021/SQCSim2021.vcxproj.filters | 6 ++
SQCSim2021/engine.cpp | 107 +++++---------------------
SQCSim2021/engine.h | 2 +
SQCSim2021/media/shaders/skybox.frag | 12 +++
SQCSim2021/media/shaders/skybox.vert | 8 ++
SQCSim2021/player.cpp | 4 +-
SQCSim2021/skybox.cpp | 58 ++++++++++++++
SQCSim2021/skybox.h | 25 ++++++
9 files changed, 135 insertions(+), 89 deletions(-)
create mode 100644 SQCSim2021/media/shaders/skybox.frag
create mode 100644 SQCSim2021/media/shaders/skybox.vert
create mode 100644 SQCSim2021/skybox.cpp
create mode 100644 SQCSim2021/skybox.h
diff --git a/SQCSim2021/SQCSim2021.vcxproj b/SQCSim2021/SQCSim2021.vcxproj
index f162677..457adbb 100644
--- a/SQCSim2021/SQCSim2021.vcxproj
+++ b/SQCSim2021/SQCSim2021.vcxproj
@@ -29,6 +29,7 @@
+
@@ -43,6 +44,7 @@
+
diff --git a/SQCSim2021/SQCSim2021.vcxproj.filters b/SQCSim2021/SQCSim2021.vcxproj.filters
index 04f30e9..16bfb28 100644
--- a/SQCSim2021/SQCSim2021.vcxproj.filters
+++ b/SQCSim2021/SQCSim2021.vcxproj.filters
@@ -56,6 +56,9 @@
Fichiers d%27en-tête
+
+ Fichiers d%27en-tête
+
@@ -91,6 +94,9 @@
Fichiers sources
+
+ Fichiers sources
+
diff --git a/SQCSim2021/engine.cpp b/SQCSim2021/engine.cpp
index d18bc47..a2d54b1 100644
--- a/SQCSim2021/engine.cpp
+++ b/SQCSim2021/engine.cpp
@@ -11,8 +11,7 @@ Engine::~Engine() { }
void Engine::Init()
{
GLenum glewErr = glewInit();
- if (glewErr != GLEW_OK)
- {
+ if (glewErr != GLEW_OK) {
std::cerr << " ERREUR GLEW : " << glewGetErrorString(glewErr) << std::endl;
abort();
}
@@ -29,6 +28,8 @@ void Engine::Init()
glEnable(GL_LIGHTING);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_CULL_FACE);
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Light
GLfloat light0Pos[4] = { 0.0f, CHUNK_SIZE_Y, 0.0f, 1.0f };
@@ -42,16 +43,8 @@ void Engine::Init()
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0Diff);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0Spec);
- // Lumière de Skybox.
- GLfloat light1Amb[4] = { 1.5f, 1.5f, 1.5f, 1.5f };
- GLfloat light1Diff[4] = { 1.f, 1.f, 1.f, 1.f };
- GLfloat light1Emi[4] = { 2.f, 2.f, 2.f, 2.f };
- glLightfv(GL_LIGHT1, GL_AMBIENT, light1Amb);
- glLightfv(GL_LIGHT1, GL_DIFFUSE, light1Diff);
- glLightfv(GL_LIGHT1, GL_EMISSION, light1Emi);
-
- // Blend
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ // Objet de skybox avec sa propre texture et son propre shader!
+ m_skybox.Init(0.00013f);
// Init testChunk
for (int x = 0; x < CHUNK_SIZE_X; ++x) {
@@ -71,23 +64,25 @@ void Engine::DeInit() { }
void Engine::LoadResource() {
LoadTexture(m_textureFloor, TEXTURE_PATH "grass.png");
- LoadTexture(m_textureSkybox, TEXTURE_PATH "skybox.png");
+ LoadTexture(m_skybox.GetTexture(), TEXTURE_PATH "skybox.png");
LoadTexture(m_textureCube1, TEXTURE_PATH "metal1.png");
LoadTexture(m_textureCube2, TEXTURE_PATH "metal2.png");
LoadTexture(m_textureCube3, TEXTURE_PATH "metal3.png");
LoadTexture(m_textureCube4, TEXTURE_PATH "metal4.png");
std::cout << " Loading and compiling shaders ..." << std::endl;
- if (!m_shader01.Load(SHADER_PATH "shader01.vert", SHADER_PATH "shader01.frag", true))
- {
+ if (!m_shader01.Load(SHADER_PATH "shader01.vert", SHADER_PATH "shader01.frag", true)) {
+ std::cout << " Failed to load shader " << std::endl;
+ exit(1);
+ }
+
+ if (!m_skybox.GetShader().Load(SHADER_PATH "skybox.vert", SHADER_PATH "skybox.frag", true)) {
std::cout << " Failed to load shader " << std::endl;
exit(1);
}
}
-void Engine::UnloadResource()
-{
-}
+void Engine::UnloadResource(){}
void Engine::Render(float elapsedTime)
{
@@ -108,77 +103,13 @@ void Engine::Render(float elapsedTime)
m_player.ApplyTransformation(all);
m_player.ApplyTransformation(skybox, false); // Version d'ApplyTransformation qui ne tient compte que de la rotation
// (donc l'objet ne bouge pas relativement au joueur, ce qui est pratique pour une skybox!).
-
- // Skybox
- skybox.Use();
- m_textureSkybox.Bind();
- glDisable(GL_DEPTH_TEST); // Pour que tout ce qui est dessiné après soit dessiné par-dessus la skybox.
- glDisable(GL_LIGHT0);
- glEnable(GL_LIGHT1);
- glBegin(GL_QUADS);
-
- glTexCoord2f(0, .5f);
- glVertex3f(5.f, -5.f, 5.f);
- glTexCoord2f(.25f, .5f);
- glVertex3f(-5.f, -5.f, 5.f);
- glTexCoord2f(.25f, .75f);
- glVertex3f(-5.f, 5.f, 5.f);
- glTexCoord2f(0, .75f);
- glVertex3f(5.f, 5.f, 5.f);
-
- glTexCoord2f(.25f, .5f);
- glVertex3f(-5.f, -5.f, 5.f);
- glTexCoord2f(.50f, .5f);
- glVertex3f(-5.f, -5.f, -5.f);
- glTexCoord2f(.50f, .75f);
- glVertex3f(-5.f, 5.f, -5.f);
- glTexCoord2f(.25f, .75f);
- glVertex3f(-5.f, 5.f, 5.f);
-
- glTexCoord2f(.50f, .5f);
- glVertex3f(-5.f, -5.f, -5.f);
- glTexCoord2f(.75f, .5f);
- glVertex3f(5.f, -5.f, -5.f);
- glTexCoord2f(.75f, .75f);
- glVertex3f(5.f, 5.f, -5.f);
- glTexCoord2f(.50f, .75f);
- glVertex3f(-5.f, 5.f, -5.f);
-
- glTexCoord2f(.75f, .5f);
- glVertex3f(5.f, -5.f, -5.f);
- glTexCoord2f(1, .5f);
- glVertex3f(5.f, -5.f, 5.f);
- glTexCoord2f(1, .75f);
- glVertex3f(5.f, 5.f, 5.f);
- glTexCoord2f(.75f, .75f);
- glVertex3f(5.f, 5.f, -5.f);
-
- glTexCoord2f(.25f, 1);
- glVertex3f(5.f, 5.f, -5.f);
- glTexCoord2f(.50f, 1);
- glVertex3f(5.f, 5.f, 5.f);
- glTexCoord2f(.50f, .75f);
- glVertex3f(-5.f, 5.f, 5.f);
- glTexCoord2f(.25f, .75f);
- glVertex3f(-5.f, 5.f, -5.f);
-
- glTexCoord2f(.50f, .25f);
- glVertex3f(-5.f, -5.f, 5.f);
- glTexCoord2f(.75f, .25f);
- glVertex3f(5.f, -5.f, 5.f);
- glTexCoord2f(.75f, .5f);
- glVertex3f(5.f, -5.f, -5.f);
- glTexCoord2f(.50f, .5f);
- glVertex3f(-5.f, -5.f, -5.f);
-
- glEnd();
- glEnable(GL_DEPTH_TEST);
- glDisable(GL_LIGHT1);
- glEnable(GL_LIGHT0);
-
+
+ m_skybox.Render(skybox);
+
// Plancher
// Les vertex doivent etre affiches dans le sens anti-horaire (CCW)
all.Use();
+ m_shader01.Use();
m_textureFloor.Bind();
float nbRep = 5.f;
glBegin(GL_QUADS);
@@ -206,9 +137,9 @@ void Engine::Render(float elapsedTime)
}
if (m_testChunk.IsDirty())
m_testChunk.Update();
- m_shader01.Use();
+
m_testChunk.Render();
- Shader::Disable();
+ //Shader::Disable();
}
diff --git a/SQCSim2021/engine.h b/SQCSim2021/engine.h
index 275a435..bc462ec 100644
--- a/SQCSim2021/engine.h
+++ b/SQCSim2021/engine.h
@@ -7,6 +7,7 @@
#include "shader.h""
#include "player.h"
#include "chunk.h"
+#include "skybox.h"
class Engine : public OpenglContext
{
@@ -37,6 +38,7 @@ private:
Texture m_textureCube3;
Texture m_textureCube4;
+ Skybox m_skybox;
Shader m_shader01;
Chunk m_testChunk;
Player m_player = Player(Vector3f(0, 0, 0));
diff --git a/SQCSim2021/media/shaders/skybox.frag b/SQCSim2021/media/shaders/skybox.frag
new file mode 100644
index 0000000..c540d70
--- /dev/null
+++ b/SQCSim2021/media/shaders/skybox.frag
@@ -0,0 +1,12 @@
+uniform sampler2D tex;
+varying vec4 light;
+
+void main()
+{
+ vec4 texel;
+ texel = texture2D(tex,gl_TexCoord[0].st);
+
+ texel *= light;
+
+ gl_FragColor = texel;
+}
diff --git a/SQCSim2021/media/shaders/skybox.vert b/SQCSim2021/media/shaders/skybox.vert
new file mode 100644
index 0000000..6c5c7ca
--- /dev/null
+++ b/SQCSim2021/media/shaders/skybox.vert
@@ -0,0 +1,8 @@
+varying vec4 light;
+
+void main()
+{
+ light = gl_Color;
+ gl_TexCoord[0] = gl_MultiTexCoord0;
+ gl_Position = ftransform();
+}
diff --git a/SQCSim2021/player.cpp b/SQCSim2021/player.cpp
index 49967ac..1fdd124 100644
--- a/SQCSim2021/player.cpp
+++ b/SQCSim2021/player.cpp
@@ -37,7 +37,9 @@ void Player::Move(bool front, bool back, bool left, bool right, bool jump, bool
if (dash) dashtimeout = 2;
- if ((jump || dash) && dbljump < 2 && !jumped ) {
+ if (dbljump >= 2) jump = false;
+
+ if ((jump || dash) && !jumped ) {
accjmp += jump? m_jumpforce: 0.1f;
jumped = true;
dbljump++;
diff --git a/SQCSim2021/skybox.cpp b/SQCSim2021/skybox.cpp
new file mode 100644
index 0000000..10cb6d5
--- /dev/null
+++ b/SQCSim2021/skybox.cpp
@@ -0,0 +1,58 @@
+#include "skybox.h"
+
+Skybox::Skybox() {}
+
+Skybox::~Skybox() {}
+
+void Skybox::Init(float size){
+ int count = 0;
+ VertexBuffer::VertexData* vd = new VertexBuffer::VertexData[24];
+
+ vd[count++] = VertexBuffer::VertexData(size, -size, size, 1.f, 1.f, 1.f, 0.f, .5f);
+ vd[count++] = VertexBuffer::VertexData(-size, -size, size, 1.f, 1.f, 1.f, .25f, .5f);
+ vd[count++] = VertexBuffer::VertexData(-size, size, size, 1.f, 1.f, 1.f, .25f, .75f);
+ vd[count++] = VertexBuffer::VertexData(size, size, size, 1.f, 1.f, 1.f, 0.f, .75f);
+
+ vd[count++] = VertexBuffer::VertexData(-size, -size, size, 1.f, 1.f, 1.f, .25f, .5f);
+ vd[count++] = VertexBuffer::VertexData(-size, -size, -size, 1.f, 1.f, 1.f, .5f, .5f);
+ vd[count++] = VertexBuffer::VertexData(-size, size, -size, 1.f, 1.f, 1.f, .5f, .75f);
+ vd[count++] = VertexBuffer::VertexData(-size, size, size, 1.f, 1.f, 1.f, .25f, .75f);
+
+ vd[count++] = VertexBuffer::VertexData(-size, -size, -size, 1.f, 1.f, 1.f, .5f, .5f);
+ vd[count++] = VertexBuffer::VertexData(size, -size, -size, 1.f, 1.f, 1.f, .75f, .5f);
+ vd[count++] = VertexBuffer::VertexData(size, size, -size, 1.f, 1.f, 1.f, .75f, .75f);
+ vd[count++] = VertexBuffer::VertexData(-size, size, -size, 1.f, 1.f, 1.f, .5f, .75f);
+
+ vd[count++] = VertexBuffer::VertexData(size, -size, -size, 1.f, 1.f, 1.f, .75f, .5f);
+ vd[count++] = VertexBuffer::VertexData(size, -size, size, 1.f, 1.f, 1.f, 1.f, .5f);
+ vd[count++] = VertexBuffer::VertexData(size, size, size, 1.f, 1.f, 1.f, 1.f, .75f);
+ vd[count++] = VertexBuffer::VertexData(size, size, -size, 1.f, 1.f, 1.f, .75f, .75f);
+
+ vd[count++] = VertexBuffer::VertexData(size, size, -size, 1.f, 1.f, 1.f, .25f, 1.f);
+ vd[count++] = VertexBuffer::VertexData(size, size, size, 1.f, 1.f, 1.f, .5f, 1.f);
+ vd[count++] = VertexBuffer::VertexData(-size, size, size, 1.f, 1.f, 1.f, .5f, .75f);
+ vd[count++] = VertexBuffer::VertexData(-size, size, -size, 1.f, 1.f, 1.f, .25f, .75f);
+
+ vd[count++] = VertexBuffer::VertexData(-size, -size, size, 1.f, 1.f, 1.f, .5f, .25f);
+ vd[count++] = VertexBuffer::VertexData(size, -size, size, 1.f, 1.f, 1.f, .75f, .25f);
+ vd[count++] = VertexBuffer::VertexData(size, -size, -size, 1.f, 1.f, 1.f, .75f, .5f);
+ vd[count++] = VertexBuffer::VertexData(-size, -size, -size, 1.f, 1.f, 1.f, .5f, .5f);
+
+ m_vertexBuffer.SetMeshData(vd, count);
+ delete[] vd;
+}
+
+void Skybox::Render(Transformation tran) const {
+ glDisable(GL_DEPTH_TEST);
+ tran.Use();
+ m_texture.Bind();
+ m_shader.Use();
+ m_vertexBuffer.Render();
+ glEnable(GL_DEPTH_TEST);
+}
+
+Texture& Skybox::GetTexture() { return m_texture; }
+
+Shader& Skybox::GetShader() { return m_shader; }
+
+
diff --git a/SQCSim2021/skybox.h b/SQCSim2021/skybox.h
new file mode 100644
index 0000000..a596416
--- /dev/null
+++ b/SQCSim2021/skybox.h
@@ -0,0 +1,25 @@
+#ifndef SKYBOX_H__
+#define SKYBOX_H__
+
+#include "define.h"
+#include "vertexbuffer.h"
+#include "texture.h"
+#include "transformation.h"
+#include "shader.h"
+
+class Skybox {
+private:
+ VertexBuffer m_vertexBuffer;
+ Texture m_texture;
+ Shader m_shader;
+
+public:
+ Skybox();
+ ~Skybox();
+ void Init(float size);
+ void Render(Transformation trans) const;
+ Texture& GetTexture();
+ Shader& GetShader();
+};
+
+#endif // SKYBOX_H__