Displaying a skybox

This commit is contained in:
Hugo Willaume 2019-04-03 11:44:35 +09:00
parent 2457231dbd
commit 893c355511
9 changed files with 121 additions and 18 deletions

View File

@ -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">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
@ -157,6 +157,7 @@
<ClCompile Include="Models\Mesh.cpp" />
<ClCompile Include="MyGLWindow.cpp" />
<ClCompile Include="Shader.cpp" />
<ClCompile Include="Skybox.cpp" />
<ClCompile Include="Source.cpp" />
<ClCompile Include="Texture.cpp" />
<ClCompile Include="Viewer.cpp" />
@ -179,6 +180,7 @@
<ClInclude Include="MyGLWindow.h" />
<ClInclude Include="SceneContext.h" />
<ClInclude Include="Shader.h" />
<ClInclude Include="Skybox.h" />
<ClInclude Include="Texture.h" />
<ClInclude Include="Viewer.h" />
</ItemGroup>
@ -189,6 +191,8 @@
<None Include="base_light.vert" />
<None Include="nmap.frag" />
<None Include="nmap.vert" />
<None Include="skybox.frag" />
<None Include="skybox.vert" />
<None Include="spotlight.frag" />
<None Include="spotlight.vert" />
<None Include="tex_base_light.frag" />

View File

@ -66,6 +66,9 @@
<ClCompile Include="Texture.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Skybox.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ModelView.h">
@ -125,6 +128,9 @@
<ClInclude Include="Texture.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Skybox.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="simple.frag">
@ -178,6 +184,12 @@
<None Include="nmap.frag">
<Filter>Shaders</Filter>
</None>
<None Include="skybox.vert">
<Filter>Shaders</Filter>
</None>
<None Include="skybox.frag">
<Filter>Shaders</Filter>
</None>
</ItemGroup>
<ItemGroup>
<Image Include="brick1.jpg">

View File

@ -129,6 +129,7 @@ void MyGlWindow::setup()
glEnable(GL_DEPTH_BUFFER);
glEnable(GL_TEXTURE_2D);
skybox.initialize("Models/Skybox/");
textureSetup();
shaderSetup();
lightSetup();
@ -160,7 +161,7 @@ void MyGlWindow::setup()
//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));
@ -206,6 +207,7 @@ void MyGlWindow::draw()
//_scnctx.adjustSpots();
for (auto it = meshes.begin(); it != meshes.end(); it++)
(*it).second->draw(_scnctx);
skybox.draw(_scnctx);
}
void MyGlWindow::resize(int w, int h)

View File

@ -4,6 +4,7 @@
#include "GL/glew.h"
#include <string>
#include <vector>
#include "Skybox.h"
#include "Shader.h"
#include "ModelView.h"
#include "Viewer.h"
@ -38,6 +39,7 @@ private:
float _bgColor[3];
Skybox skybox;
GLuint _vaoHandle;
GLuint _iboHandle;

View File

@ -4,6 +4,7 @@
#include <glm/mat4x4.hpp>
#include <glm/vec3.hpp>
#include "Texture.h"
#include "Light.h"
struct SceneContext
{

View File

@ -1,4 +1,5 @@
#include "Skybox.h"
#include "imgui/stb_image.h"
Skybox::Skybox()
{
@ -57,13 +58,25 @@ 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);
//glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 0, NULL);
//glEnableVertexAttribArray(4);
shader.initFromFiles("skybox.vert", "skybox.frag");
}
void Skybox::initialize(std::string skybox_dir)
{
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texID); //set the texID as a member variable
glBindTexture(GL_TEXTURE_CUBE_MAP, texID);
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[] = {
@ -73,24 +86,43 @@ Skybox::Skybox()
};
for (int i = 0; i < 6; i++) {
int channel;
int width, height;
int channel, width, height;
unsigned char *image;
std::string filename;
filename = std::string("Models/skybox/" + suffixes[i]) + string(".jpg");
unsigned char * image = SOIL_load_image(filename.c_str(), &width, &height, &channel, SOIL_LOAD_RGB);
filename = std::string(skybox_dir + suffixes[i]) + std::string(".JPG");
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);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
stbi_image_free(image);
}
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}
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();
}

View File

@ -8,7 +8,9 @@
#include <glm/mat4x4.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/matrix_inverse.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "Loader.h"
#include "SceneContext.h"
class Skybox
{
@ -17,9 +19,11 @@ private:
unsigned int vector_vbo;
unsigned int ibo;
GLuint texID;
ShaderProgram shader;
public:
Skybox();
void initialize(std::string skybox_dir);
~Skybox();
void draw();
void draw(SceneContext &ctx);
};

21
BaseGLProject/skybox.frag Normal file
View 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
View 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);
}