TP3 + classe Skybox

This commit is contained in:
Marc-Eric Martel 2021-10-12 15:58:54 -04:00
parent 6f3dd99ab7
commit 167548dfc2
9 changed files with 135 additions and 89 deletions

View File

@ -29,6 +29,7 @@
<ClInclude Include="openglcontext.h" />
<ClInclude Include="player.h" />
<ClInclude Include="shader.h" />
<ClInclude Include="skybox.h" />
<ClInclude Include="texture.h" />
<ClInclude Include="tool.h" />
<ClInclude Include="transformation.h" />
@ -43,6 +44,7 @@
<ClCompile Include="openglcontext.cpp" />
<ClCompile Include="player.cpp" />
<ClCompile Include="shader.cpp" />
<ClCompile Include="skybox.cpp" />
<ClCompile Include="texture.cpp" />
<ClCompile Include="tool.cpp" />
<ClCompile Include="transformation.cpp" />

View File

@ -56,6 +56,9 @@
<ClInclude Include="array2d.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="skybox.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="blockinfo.cpp">
@ -91,6 +94,9 @@
<ClCompile Include="vertexbuffer.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="skybox.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="notes.txt" />

View File

@ -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();
}

View File

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

View File

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

View File

@ -0,0 +1,8 @@
varying vec4 light;
void main()
{
light = gl_Color;
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}

View File

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

58
SQCSim2021/skybox.cpp Normal file
View File

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

25
SQCSim2021/skybox.h Normal file
View File

@ -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__