Put the skybox shader out of the skybox

This commit is contained in:
Hugo Willaume 2019-04-03 14:36:03 +09:00
parent 893c355511
commit b129392d88
7 changed files with 60 additions and 34 deletions

View File

@ -23,10 +23,8 @@ public:
glm::mat4 Trans = glm::translate(glm::mat4(1.0f), glm::vec3(x, y, z)); glm::mat4 Trans = glm::translate(glm::mat4(1.0f), glm::vec3(x, y, z));
modelstack.top() = modelstack.top() * Trans; modelstack.top() = modelstack.top() * Trans;
} }
void glRotate(float degree, float x, float y, float z) void glRotate(float degree, float x, float y, float z)
{ {
glm::mat4 rot = glm::rotate(glm::mat4(1.0f), glm::radians(degree), glm::vec3(x, y, z)); glm::mat4 rot = glm::rotate(glm::mat4(1.0f), glm::radians(degree), glm::vec3(x, y, z));
@ -39,7 +37,6 @@ public:
modelstack.top() = modelstack.top() * scale; modelstack.top() = modelstack.top() * scale;
} }
void glPushMatrix() void glPushMatrix()
{ {
glm::mat4 t = getMatrix(); glm::mat4 t = getMatrix();

View File

@ -46,6 +46,7 @@ MyGlWindow::MyGlWindow(int w, int h) :
MyGlWindow::~MyGlWindow() MyGlWindow::~MyGlWindow()
{ {
shaders.clear();
} }
void MyGlWindow::setBgColor(float bgColor[3]) void MyGlWindow::setBgColor(float bgColor[3])
@ -107,6 +108,9 @@ void MyGlWindow::shaderSetup()
//Removing useless specular component //Removing useless specular component
shaders["Toon"]->uniformFlags &= ~ShaderFlags::KS_FLAG; shaders["Toon"]->uniformFlags &= ~ShaderFlags::KS_FLAG;
shaders["Toon"]->uniformFlags &= ~ShaderFlags::SHINE_FLAG; shaders["Toon"]->uniformFlags &= ~ShaderFlags::SHINE_FLAG;
shaders["Skybox"] = new Shader("skybox.vert", "skybox.frag");
} }
void MyGlWindow::lightSetup() void MyGlWindow::lightSetup()
@ -129,9 +133,10 @@ void MyGlWindow::setup()
glEnable(GL_DEPTH_BUFFER); glEnable(GL_DEPTH_BUFFER);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
skybox.initialize("Models/Skybox/");
textureSetup(); textureSetup();
shaderSetup(); shaderSetup();
skybox.initialize("Models/Skybox/", shaders["Skybox"]);
skybox.scale = 10;
lightSetup(); lightSetup();
//meshes.emplace("Ogre", new Mesh("ogre/ogre.obj", shaders["TexNmapLight"])); //meshes.emplace("Ogre", new Mesh("ogre/ogre.obj", shaders["TexNmapLight"]));
@ -207,7 +212,7 @@ void MyGlWindow::draw()
//_scnctx.adjustSpots(); //_scnctx.adjustSpots();
for (auto it = meshes.begin(); it != meshes.end(); it++) for (auto it = meshes.begin(); it != meshes.end(); it++)
(*it).second->draw(_scnctx); (*it).second->draw(_scnctx);
skybox.draw(_scnctx); skybox.draw(shaders["Skybox"], _scnctx);
} }
void MyGlWindow::resize(int w, int h) void MyGlWindow::resize(int w, int h)

View File

@ -41,19 +41,27 @@ public:
public: public:
void setUniforms(SceneContext ctx); void setUniforms(SceneContext ctx);
void addUniform(const std::string name, glm::vec3 data) void addUniform(const std::string name, glm::vec3 data)
{ {
_program.use(); glUniform3fv(_program.addUniform(name), 1, glm::value_ptr(data)); _program.use(); glUniform3fv(_program.addUniform(name), 1, glm::value_ptr(data));
} }
void addUniform(const std::string name, int data) void addUniform(const std::string name, int data)
{ {
_program.use(); glUniform1i(_program.addUniform(name), data); _program.use(); glUniform1i(_program.addUniform(name), data);
} }
void addUniform(const std::string name, float data) void addUniform(const std::string name, float data)
{ {
_program.use(); glUniform1fv(_program.addUniform(name), 1, &data); _program.use(); glUniform1fv(_program.addUniform(name), 1, &data);
} }
void addUniform(const std::string name, glm::mat4x4 &data)
{
_program.use(); glUniformMatrix4fv(_program.addUniform(name), 1, GL_FALSE, glm::value_ptr(data));
}
void applyTextureMaterial(Texture &tex) void applyTextureMaterial(Texture &tex)
{ {
if (mat.enabled) if (mat.enabled)

View File

@ -58,13 +58,12 @@ Skybox::Skybox()
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(GLuint), el, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(GLuint), el, GL_STATIC_DRAW);
//glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 0, NULL); glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 0, NULL);
//glEnableVertexAttribArray(4); glEnableVertexAttribArray(4);
shader.initFromFiles("skybox.vert", "skybox.frag");
} }
void Skybox::initialize(std::string skybox_dir) void Skybox::initialize(std::string skybox_dir, Shader *sky)
{ {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texID); //set the texID as a member variable glGenTextures(1, &texID); //set the texID as a member variable
@ -97,6 +96,9 @@ void Skybox::initialize(std::string skybox_dir)
} }
glBindTexture(GL_TEXTURE_CUBE_MAP, 0); glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
sky->enable();
sky->addUniform("DrawSkyBox", (int)GL_FALSE);
sky->disable();
} }
Skybox::~Skybox() Skybox::~Skybox()
@ -106,23 +108,28 @@ Skybox::~Skybox()
glDeleteVertexArrays(1, &vao); glDeleteVertexArrays(1, &vao);
} }
void Skybox::draw(SceneContext &ctx) void Skybox::draw(Shader *sky_shader, SceneContext &ctx)
{ {
shader.use(); sky_shader->enable();
glEnable(GL_CULL_FACE);
glm::mat4x4 mvpMatrix = ctx.projectionMatrix * ctx.viewMatrix * glm::mat4(1.0); glCullFace(GL_BACK);
glUniformMatrix4fv(shader.addUniform("mvp"), 1, GL_FALSE, glm::value_ptr(mvpMatrix)); glm::mat4x4 modelMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(scale));
glUniform1i(shader.addUniform("DrawSkyBox"), GL_TRUE); glm::mat4x4 mvpMatrix = ctx.projectionMatrix * ctx.viewMatrix * modelMatrix;
sky_shader->addUniform("mvp", mvpMatrix);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, texID); glBindTexture(GL_TEXTURE_CUBE_MAP, texID);
glUniform1i(shader.addUniform("CubeMapTex"), 0); sky_shader->addUniform("CubeMapTex", (int)0);
glBindVertexArray(vao); glBindVertexArray(vao);
sky_shader->addUniform("DrawSkyBox", (int)GL_TRUE);
int size; int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size); glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size / sizeof(GLuint), GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, size / sizeof(GLuint), GL_UNSIGNED_INT, 0);
sky_shader->addUniform("DrawSkyBox", (int)GL_FALSE);
glBindVertexArray(0); glBindVertexArray(0);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0); glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
shader.disable(); glDisable(GL_CULL_FACE);
sky_shader->disable();
} }

View File

@ -9,7 +9,7 @@
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/matrix_inverse.hpp> #include <glm/gtc/matrix_inverse.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include "Loader.h" #include "Shader.h"
#include "SceneContext.h" #include "SceneContext.h"
class Skybox class Skybox
@ -19,11 +19,12 @@ private:
unsigned int vector_vbo; unsigned int vector_vbo;
unsigned int ibo; unsigned int ibo;
GLuint texID; GLuint texID;
ShaderProgram shader;
public: public:
Skybox(); Skybox();
void initialize(std::string skybox_dir); float scale = 1;
void initialize(std::string skybox_dir, Shader *sky);
GLuint getTexID() { return texID; }
~Skybox(); ~Skybox();
void draw(SceneContext &ctx); void draw(Shader *sky_shader, SceneContext &ctx);
}; };

View File

@ -4,6 +4,8 @@ out vec4 FragColors;
in vec3 reflectDir; in vec3 reflectDir;
uniform vec3 MaterialColor;
uniform float ReflectFactor;
uniform bool DrawSkyBox; uniform bool DrawSkyBox;
uniform samplerCube CubeMapTex; uniform samplerCube CubeMapTex;
@ -17,5 +19,5 @@ void main()
if (DrawSkyBox == true) if (DrawSkyBox == true)
FragColors = texColor; FragColors = texColor;
else else
FragColors = vec4(1, 1, rand(reflectDir.xy), 1); FragColors = vec4(mix(texColor.xyz,MaterialColor,ReflectFactor), 1);
} }

View File

@ -7,19 +7,25 @@ layout(location=3) in vec2 v_texmap;
out vec3 reflectDir; out vec3 reflectDir;
uniform mat4 ModelMatrix;
uniform mat4 mvp; uniform mat4 mvp;
uniform bool DrawSkyBox; uniform bool DrawSkyBox;
uniform vec3 WorldCamPos;
void main(void) void main(void)
{ {
//For drawing a mesh with reflected skybox //For drawing a mesh with reflected skybox
//P = (vec3(ModelMAtrix * vec4(Vertexpos, 1)
// N = normalize(ModelMatrix * VertexNormals);
//V = normalize(WorldCamPos - worldPos);
//ReflectDir = reflect(-worldview, worldNorm);
if (DrawSkyBox == true) if (DrawSkyBox == true)
reflectDir = coord3d; reflectDir = coord3d;
else
{
vec3 P = (ModelMatrix * vec4(coord3d, 1)).xyz;
vec3 N = normalize(ModelMatrix * vec4(v_normal, 1)).xyz;
vec3 V = normalize(WorldCamPos - coord3d);
reflectDir = reflect(-V, N);
}
gl_Position = mvp * vec4(coord3d, 1.0f); gl_Position = mvp * vec4(coord3d, 1.0f);
} }