Displaying a skybox
This commit is contained in:
parent
2457231dbd
commit
893c355511
@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
<ItemGroup Label="ProjectConfigurations">
|
<ItemGroup Label="ProjectConfigurations">
|
||||||
<ProjectConfiguration Include="Debug|Win32">
|
<ProjectConfiguration Include="Debug|Win32">
|
||||||
@ -157,6 +157,7 @@
|
|||||||
<ClCompile Include="Models\Mesh.cpp" />
|
<ClCompile Include="Models\Mesh.cpp" />
|
||||||
<ClCompile Include="MyGLWindow.cpp" />
|
<ClCompile Include="MyGLWindow.cpp" />
|
||||||
<ClCompile Include="Shader.cpp" />
|
<ClCompile Include="Shader.cpp" />
|
||||||
|
<ClCompile Include="Skybox.cpp" />
|
||||||
<ClCompile Include="Source.cpp" />
|
<ClCompile Include="Source.cpp" />
|
||||||
<ClCompile Include="Texture.cpp" />
|
<ClCompile Include="Texture.cpp" />
|
||||||
<ClCompile Include="Viewer.cpp" />
|
<ClCompile Include="Viewer.cpp" />
|
||||||
@ -179,6 +180,7 @@
|
|||||||
<ClInclude Include="MyGLWindow.h" />
|
<ClInclude Include="MyGLWindow.h" />
|
||||||
<ClInclude Include="SceneContext.h" />
|
<ClInclude Include="SceneContext.h" />
|
||||||
<ClInclude Include="Shader.h" />
|
<ClInclude Include="Shader.h" />
|
||||||
|
<ClInclude Include="Skybox.h" />
|
||||||
<ClInclude Include="Texture.h" />
|
<ClInclude Include="Texture.h" />
|
||||||
<ClInclude Include="Viewer.h" />
|
<ClInclude Include="Viewer.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
@ -189,6 +191,8 @@
|
|||||||
<None Include="base_light.vert" />
|
<None Include="base_light.vert" />
|
||||||
<None Include="nmap.frag" />
|
<None Include="nmap.frag" />
|
||||||
<None Include="nmap.vert" />
|
<None Include="nmap.vert" />
|
||||||
|
<None Include="skybox.frag" />
|
||||||
|
<None Include="skybox.vert" />
|
||||||
<None Include="spotlight.frag" />
|
<None Include="spotlight.frag" />
|
||||||
<None Include="spotlight.vert" />
|
<None Include="spotlight.vert" />
|
||||||
<None Include="tex_base_light.frag" />
|
<None Include="tex_base_light.frag" />
|
||||||
|
|||||||
@ -66,6 +66,9 @@
|
|||||||
<ClCompile Include="Texture.cpp">
|
<ClCompile Include="Texture.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Skybox.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="ModelView.h">
|
<ClInclude Include="ModelView.h">
|
||||||
@ -125,6 +128,9 @@
|
|||||||
<ClInclude Include="Texture.h">
|
<ClInclude Include="Texture.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Skybox.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="simple.frag">
|
<None Include="simple.frag">
|
||||||
@ -178,6 +184,12 @@
|
|||||||
<None Include="nmap.frag">
|
<None Include="nmap.frag">
|
||||||
<Filter>Shaders</Filter>
|
<Filter>Shaders</Filter>
|
||||||
</None>
|
</None>
|
||||||
|
<None Include="skybox.vert">
|
||||||
|
<Filter>Shaders</Filter>
|
||||||
|
</None>
|
||||||
|
<None Include="skybox.frag">
|
||||||
|
<Filter>Shaders</Filter>
|
||||||
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Image Include="brick1.jpg">
|
<Image Include="brick1.jpg">
|
||||||
|
|||||||
@ -129,6 +129,7 @@ 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();
|
||||||
lightSetup();
|
lightSetup();
|
||||||
@ -160,7 +161,7 @@ void MyGlWindow::setup()
|
|||||||
|
|
||||||
//meshes.emplace("Mountain", new Mesh("mountain/mount.blend1.obj", shaders["TexBaseLight"]));
|
//meshes.emplace("Mountain", new Mesh("mountain/mount.blend1.obj", shaders["TexBaseLight"]));
|
||||||
|
|
||||||
meshes.emplace("Sponza", new Mesh("sponza/sponza.obj", shaders["TexBaseLight"]));
|
//meshes.emplace("Sponza", new Mesh("sponza/sponza.obj", shaders["TexBaseLight"]));
|
||||||
//meshes["Sponza"]->addTranslation(glm::vec4(0, -200, 0, 1));
|
//meshes["Sponza"]->addTranslation(glm::vec4(0, -200, 0, 1));
|
||||||
|
|
||||||
|
|
||||||
@ -206,6 +207,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);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MyGlWindow::resize(int w, int h)
|
void MyGlWindow::resize(int w, int h)
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
#include "GL/glew.h"
|
#include "GL/glew.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "Skybox.h"
|
||||||
#include "Shader.h"
|
#include "Shader.h"
|
||||||
#include "ModelView.h"
|
#include "ModelView.h"
|
||||||
#include "Viewer.h"
|
#include "Viewer.h"
|
||||||
@ -22,7 +23,7 @@ public:
|
|||||||
MyGlWindow(int w, int h);
|
MyGlWindow(int w, int h);
|
||||||
~MyGlWindow();
|
~MyGlWindow();
|
||||||
|
|
||||||
void draw();
|
void draw();
|
||||||
void setBgColor(float bgColor[3]);
|
void setBgColor(float bgColor[3]);
|
||||||
|
|
||||||
std::map<std::string, Shader *> shaders;
|
std::map<std::string, Shader *> shaders;
|
||||||
@ -38,6 +39,7 @@ private:
|
|||||||
|
|
||||||
float _bgColor[3];
|
float _bgColor[3];
|
||||||
|
|
||||||
|
Skybox skybox;
|
||||||
GLuint _vaoHandle;
|
GLuint _vaoHandle;
|
||||||
GLuint _iboHandle;
|
GLuint _iboHandle;
|
||||||
|
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
#include <glm/mat4x4.hpp>
|
#include <glm/mat4x4.hpp>
|
||||||
#include <glm/vec3.hpp>
|
#include <glm/vec3.hpp>
|
||||||
#include "Texture.h"
|
#include "Texture.h"
|
||||||
|
#include "Light.h"
|
||||||
|
|
||||||
struct SceneContext
|
struct SceneContext
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
#include "Skybox.h"
|
#include "Skybox.h"
|
||||||
|
#include "imgui/stb_image.h"
|
||||||
|
|
||||||
Skybox::Skybox()
|
Skybox::Skybox()
|
||||||
{
|
{
|
||||||
@ -57,40 +58,71 @@ 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)
|
||||||
|
{
|
||||||
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
|
||||||
glBindTexture(GL_TEXTURE_CUBE_MAP, texID);
|
glBindTexture(GL_TEXTURE_CUBE_MAP, texID);
|
||||||
|
|
||||||
const char * suffixes[] = { "left", "right", "top", "down", "back", "front" };
|
|
||||||
|
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||||
|
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||||
|
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
||||||
|
|
||||||
|
const char * suffixes[] = { "left", "right", "top", "down", "back", "front"};
|
||||||
|
|
||||||
GLuint targets[] = {
|
GLuint targets[] = {
|
||||||
GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
|
GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
|
||||||
GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
|
GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
|
||||||
GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
|
GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
|
||||||
};
|
};
|
||||||
|
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
int channel;
|
int channel, width, height;
|
||||||
int width, height;
|
unsigned char *image;
|
||||||
std::string filename;
|
std::string filename;
|
||||||
filename = std::string("Models/skybox/" + suffixes[i]) + string(".jpg");
|
filename = std::string(skybox_dir + suffixes[i]) + std::string(".JPG");
|
||||||
unsigned char * image = SOIL_load_image(filename.c_str(), &width, &height, &channel, SOIL_LOAD_RGB);
|
image = stbi_load(filename.c_str(), &width, &height, &channel, 0);
|
||||||
glTexImage2D(targets[i], 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
|
glTexImage2D(targets[i], 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
|
||||||
glGenerateMipmap(GL_TEXTURE_2D);
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
SOIL_free_image_data(image);
|
stbi_image_free(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Skybox::~Skybox()
|
Skybox::~Skybox()
|
||||||
{
|
{
|
||||||
|
glDeleteBuffers(1, &vector_vbo);
|
||||||
|
glDeleteBuffers(1, &ibo);
|
||||||
|
glDeleteVertexArrays(1, &vao);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Skybox::draw()
|
void Skybox::draw(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);
|
||||||
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
glBindTexture(GL_TEXTURE_CUBE_MAP, texID);
|
||||||
|
glUniform1i(shader.addUniform("CubeMapTex"), 0);
|
||||||
|
glBindVertexArray(vao);
|
||||||
|
|
||||||
|
int size;
|
||||||
|
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
|
||||||
|
glDrawElements(GL_TRIANGLES, size / sizeof(GLuint), GL_UNSIGNED_INT, 0);
|
||||||
|
|
||||||
|
glBindVertexArray(0);
|
||||||
|
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
||||||
|
shader.disable();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,18 +8,22 @@
|
|||||||
#include <glm/mat4x4.hpp>
|
#include <glm/mat4x4.hpp>
|
||||||
#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 "Loader.h"
|
#include "Loader.h"
|
||||||
|
#include "SceneContext.h"
|
||||||
|
|
||||||
class Skybox
|
class Skybox
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
unsigned int vao;
|
unsigned int vao;
|
||||||
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);
|
||||||
~Skybox();
|
~Skybox();
|
||||||
void draw();
|
void draw(SceneContext &ctx);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
21
BaseGLProject/skybox.frag
Normal file
21
BaseGLProject/skybox.frag
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#version 440
|
||||||
|
|
||||||
|
out vec4 FragColors;
|
||||||
|
|
||||||
|
in vec3 reflectDir;
|
||||||
|
|
||||||
|
uniform bool DrawSkyBox;
|
||||||
|
uniform samplerCube CubeMapTex;
|
||||||
|
|
||||||
|
float rand(vec2 co){
|
||||||
|
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
|
||||||
|
}
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec4 texColor = texture(CubeMapTex, reflectDir);
|
||||||
|
if (DrawSkyBox == true)
|
||||||
|
FragColors = texColor;
|
||||||
|
else
|
||||||
|
FragColors = vec4(1, 1, rand(reflectDir.xy), 1);
|
||||||
|
}
|
||||||
25
BaseGLProject/skybox.vert
Normal file
25
BaseGLProject/skybox.vert
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#version 440
|
||||||
|
|
||||||
|
layout(location=0) in vec3 coord3d;
|
||||||
|
layout(location=1) in vec3 v_normal;
|
||||||
|
layout(location=2) in vec3 v_color;
|
||||||
|
layout(location=3) in vec2 v_texmap;
|
||||||
|
|
||||||
|
out vec3 reflectDir;
|
||||||
|
|
||||||
|
uniform mat4 mvp;
|
||||||
|
uniform bool DrawSkyBox;
|
||||||
|
|
||||||
|
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;
|
||||||
|
gl_Position = mvp * vec4(coord3d, 1.0f);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user