Put the skybox shader out of the skybox
This commit is contained in:
parent
893c355511
commit
b129392d88
@ -23,10 +23,8 @@ public:
|
||||
|
||||
glm::mat4 Trans = glm::translate(glm::mat4(1.0f), glm::vec3(x, y, z));
|
||||
modelstack.top() = modelstack.top() * Trans;
|
||||
|
||||
}
|
||||
|
||||
|
||||
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));
|
||||
@ -39,7 +37,6 @@ public:
|
||||
modelstack.top() = modelstack.top() * scale;
|
||||
}
|
||||
|
||||
|
||||
void glPushMatrix()
|
||||
{
|
||||
glm::mat4 t = getMatrix();
|
||||
|
||||
@ -46,6 +46,7 @@ MyGlWindow::MyGlWindow(int w, int h) :
|
||||
|
||||
MyGlWindow::~MyGlWindow()
|
||||
{
|
||||
shaders.clear();
|
||||
}
|
||||
|
||||
void MyGlWindow::setBgColor(float bgColor[3])
|
||||
@ -107,6 +108,9 @@ void MyGlWindow::shaderSetup()
|
||||
//Removing useless specular component
|
||||
shaders["Toon"]->uniformFlags &= ~ShaderFlags::KS_FLAG;
|
||||
shaders["Toon"]->uniformFlags &= ~ShaderFlags::SHINE_FLAG;
|
||||
|
||||
shaders["Skybox"] = new Shader("skybox.vert", "skybox.frag");
|
||||
|
||||
}
|
||||
|
||||
void MyGlWindow::lightSetup()
|
||||
@ -128,10 +132,11 @@ void MyGlWindow::setup()
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_DEPTH_BUFFER);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
|
||||
skybox.initialize("Models/Skybox/");
|
||||
|
||||
textureSetup();
|
||||
shaderSetup();
|
||||
skybox.initialize("Models/Skybox/", shaders["Skybox"]);
|
||||
skybox.scale = 10;
|
||||
lightSetup();
|
||||
|
||||
//meshes.emplace("Ogre", new Mesh("ogre/ogre.obj", shaders["TexNmapLight"]));
|
||||
@ -207,7 +212,7 @@ void MyGlWindow::draw()
|
||||
//_scnctx.adjustSpots();
|
||||
for (auto it = meshes.begin(); it != meshes.end(); it++)
|
||||
(*it).second->draw(_scnctx);
|
||||
skybox.draw(_scnctx);
|
||||
skybox.draw(shaders["Skybox"], _scnctx);
|
||||
}
|
||||
|
||||
void MyGlWindow::resize(int w, int h)
|
||||
|
||||
@ -40,20 +40,28 @@ public:
|
||||
|
||||
|
||||
public:
|
||||
void setUniforms(SceneContext ctx);
|
||||
void setUniforms(SceneContext ctx);
|
||||
|
||||
void addUniform(const std::string name, glm::vec3 data)
|
||||
{
|
||||
_program.use(); glUniform3fv(_program.addUniform(name), 1, glm::value_ptr(data));
|
||||
}
|
||||
|
||||
void addUniform(const std::string name, int data)
|
||||
{
|
||||
_program.use(); glUniform1i(_program.addUniform(name), data);
|
||||
}
|
||||
|
||||
void addUniform(const std::string name, float 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)
|
||||
{
|
||||
if (mat.enabled)
|
||||
|
||||
@ -58,13 +58,12 @@ Skybox::Skybox()
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(GLuint), el, GL_STATIC_DRAW);
|
||||
|
||||
//glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
||||
//glEnableVertexAttribArray(4);
|
||||
|
||||
shader.initFromFiles("skybox.vert", "skybox.frag");
|
||||
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
||||
glEnableVertexAttribArray(4);
|
||||
|
||||
}
|
||||
|
||||
void Skybox::initialize(std::string skybox_dir)
|
||||
void Skybox::initialize(std::string skybox_dir, Shader *sky)
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glGenTextures(1, &texID); //set the texID as a member variable
|
||||
@ -96,7 +95,10 @@ void Skybox::initialize(std::string skybox_dir)
|
||||
stbi_image_free(image);
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
||||
sky->enable();
|
||||
sky->addUniform("DrawSkyBox", (int)GL_FALSE);
|
||||
sky->disable();
|
||||
}
|
||||
|
||||
Skybox::~Skybox()
|
||||
@ -106,23 +108,28 @@ Skybox::~Skybox()
|
||||
glDeleteVertexArrays(1, &vao);
|
||||
}
|
||||
|
||||
void Skybox::draw(SceneContext &ctx)
|
||||
void Skybox::draw(Shader *sky_shader, SceneContext &ctx)
|
||||
{
|
||||
shader.use();
|
||||
|
||||
glm::mat4x4 mvpMatrix = ctx.projectionMatrix * ctx.viewMatrix * glm::mat4(1.0);
|
||||
glUniformMatrix4fv(shader.addUniform("mvp"), 1, GL_FALSE, glm::value_ptr(mvpMatrix));
|
||||
glUniform1i(shader.addUniform("DrawSkyBox"), GL_TRUE);
|
||||
sky_shader->enable();
|
||||
glEnable(GL_CULL_FACE);
|
||||
glCullFace(GL_BACK);
|
||||
glm::mat4x4 modelMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(scale));
|
||||
glm::mat4x4 mvpMatrix = ctx.projectionMatrix * ctx.viewMatrix * modelMatrix;
|
||||
sky_shader->addUniform("mvp", mvpMatrix);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, texID);
|
||||
glUniform1i(shader.addUniform("CubeMapTex"), 0);
|
||||
sky_shader->addUniform("CubeMapTex", (int)0);
|
||||
glBindVertexArray(vao);
|
||||
|
||||
sky_shader->addUniform("DrawSkyBox", (int)GL_TRUE);
|
||||
|
||||
|
||||
int size;
|
||||
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
|
||||
glDrawElements(GL_TRIANGLES, size / sizeof(GLuint), GL_UNSIGNED_INT, 0);
|
||||
|
||||
sky_shader->addUniform("DrawSkyBox", (int)GL_FALSE);
|
||||
glBindVertexArray(0);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
||||
shader.disable();
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
||||
glDisable(GL_CULL_FACE);
|
||||
sky_shader->disable();
|
||||
}
|
||||
|
||||
@ -9,7 +9,7 @@
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/matrix_inverse.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include "Loader.h"
|
||||
#include "Shader.h"
|
||||
#include "SceneContext.h"
|
||||
|
||||
class Skybox
|
||||
@ -19,11 +19,12 @@ private:
|
||||
unsigned int vector_vbo;
|
||||
unsigned int ibo;
|
||||
GLuint texID;
|
||||
ShaderProgram shader;
|
||||
public:
|
||||
Skybox();
|
||||
void initialize(std::string skybox_dir);
|
||||
float scale = 1;
|
||||
void initialize(std::string skybox_dir, Shader *sky);
|
||||
GLuint getTexID() { return texID; }
|
||||
~Skybox();
|
||||
void draw(SceneContext &ctx);
|
||||
void draw(Shader *sky_shader, SceneContext &ctx);
|
||||
};
|
||||
|
||||
|
||||
@ -4,6 +4,8 @@ out vec4 FragColors;
|
||||
|
||||
in vec3 reflectDir;
|
||||
|
||||
uniform vec3 MaterialColor;
|
||||
uniform float ReflectFactor;
|
||||
uniform bool DrawSkyBox;
|
||||
uniform samplerCube CubeMapTex;
|
||||
|
||||
@ -16,6 +18,6 @@ void main()
|
||||
vec4 texColor = texture(CubeMapTex, reflectDir);
|
||||
if (DrawSkyBox == true)
|
||||
FragColors = texColor;
|
||||
else
|
||||
FragColors = vec4(1, 1, rand(reflectDir.xy), 1);
|
||||
else
|
||||
FragColors = vec4(mix(texColor.xyz,MaterialColor,ReflectFactor), 1);
|
||||
}
|
||||
|
||||
@ -7,19 +7,25 @@ layout(location=3) in vec2 v_texmap;
|
||||
|
||||
out vec3 reflectDir;
|
||||
|
||||
uniform mat4 ModelMatrix;
|
||||
uniform mat4 mvp;
|
||||
uniform bool DrawSkyBox;
|
||||
|
||||
uniform vec3 WorldCamPos;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
|
||||
|
||||
//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)
|
||||
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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user