Add drawing skybox

This commit is contained in:
s473577 2024-01-12 23:26:00 +01:00
parent c8d4806f88
commit 8de2bf6a70
9 changed files with 120 additions and 8 deletions

View File

@ -48,6 +48,8 @@
<None Include="shaders\shader_9_1.vert" /> <None Include="shaders\shader_9_1.vert" />
<None Include="shaders\shader_8_sun.frag" /> <None Include="shaders\shader_8_sun.frag" />
<None Include="shaders\shader_8_sun.vert" /> <None Include="shaders\shader_8_sun.vert" />
<None Include="shaders\shader_skybox.frag" />
<None Include="shaders\shader_skybox.vert" />
<None Include="shaders\test.frag" /> <None Include="shaders\test.frag" />
<None Include="shaders\test.vert" /> <None Include="shaders\test.vert" />
</ItemGroup> </ItemGroup>

View File

@ -130,5 +130,11 @@
<None Include="shaders\test.vert"> <None Include="shaders\test.vert">
<Filter>Shader Files</Filter> <Filter>Shader Files</Filter>
</None> </None>
<None Include="shaders\shader_skybox.frag">
<Filter>Shader Files</Filter>
</None>
<None Include="shaders\shader_skybox.vert">
<Filter>Shader Files</Filter>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,12 @@
#version 430 core
uniform samplerCube skybox;
in vec3 texCoord;
out vec4 out_color;
void main()
{
out_color = texture(skybox,texCoord);
}

View File

@ -0,0 +1,13 @@
#version 430 core
layout(location = 0) in vec3 vertexPosition;
uniform mat4 transformation;
out vec3 texCoord;
void main()
{
texCoord = vertexPosition;
gl_Position = transformation * vec4(vertexPosition, 1.0);
}

View File

@ -189,3 +189,16 @@ void Core::drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, gl
glUniform1f(glGetUniformLocation(program, "spotlightPhi"), spaceship->spotlightPhi); glUniform1f(glGetUniformLocation(program, "spotlightPhi"), spaceship->spotlightPhi);
Core::DrawContext(context); Core::DrawContext(context);
} }
void Core::drawSkybox(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint textureID, GLuint program) {
Spaceship* spaceship = Spaceship::getInstance();
glDisable(GL_DEPTH_TEST);
glUseProgram(program);
glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrix();
glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
Core::DrawContext(context);
glEnable(GL_DEPTH_TEST);
}

View File

@ -69,6 +69,7 @@ namespace Core
void DrawVertexArray(const VertexData & data); void DrawVertexArray(const VertexData & data);
void DrawContext(RenderContext& context); void DrawContext(RenderContext& context);
void drawSkybox(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint textureID, GLuint program);
glm::mat4 createPerspectiveMatrix(); glm::mat4 createPerspectiveMatrix();
void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color, float roughness, float metallic, GLuint program); void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color, float roughness, float metallic, GLuint program);

View File

@ -28,6 +28,43 @@ GLuint Core::LoadTexture( const char * filepath )
return id; return id;
} }
GLuint Core::LoadCubemap(const std::vector<std::string>& faces)
{
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
int width, height;
unsigned char* data;
for (unsigned int i = 0; i < faces.size(); i++)
{
data = SOIL_load_image(("textures/skybox/" + faces[i]).c_str(), &width, &height, 0, SOIL_LOAD_RGBA);
if (data)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
SOIL_free_image_data(data);
}
else
{
std::cerr << "Cubemap tex failed to load at path:" << faces[i] << std::endl;
SOIL_free_image_data(data);
glDeleteTextures(1, &textureID);
return 0;
}
}
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);
return textureID;
}
void Core::SetActiveTexture(GLuint textureID, const char * shaderVariableName, GLuint programID, int textureUnit) void Core::SetActiveTexture(GLuint textureID, const char * shaderVariableName, GLuint programID, int textureUnit)
{ {

View File

@ -2,10 +2,13 @@
#include "glew.h" #include "glew.h"
#include "freeglut.h" #include "freeglut.h"
#include <vector>
#include <string>
namespace Core namespace Core
{ {
GLuint LoadTexture(const char * filepath); GLuint LoadTexture(const char * filepath);
GLuint LoadCubemap(const std::vector<std::string>& faces);
// textureID - identyfikator tekstury otrzymany z funkcji LoadTexture // textureID - identyfikator tekstury otrzymany z funkcji LoadTexture
// shaderVariableName - nazwa zmiennej typu 'sampler2D' w shaderze, z ktora ma zostac powiazana tekstura // shaderVariableName - nazwa zmiennej typu 'sampler2D' w shaderze, z ktora ma zostac powiazana tekstura

View File

@ -5,10 +5,11 @@
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
#include <list> #include <list>
#include <vector>
#include "Shader_Loader.h" #include "Shader_Loader.h"
#include "Render_Utils.h" #include "Render_Utils.h"
//#include "Texture.h" #include "texture.h"
#include "Box.cpp" #include "Box.cpp"
#include <assimp/Importer.hpp> #include <assimp/Importer.hpp>
@ -29,6 +30,10 @@ namespace models {
Core::RenderContext marbleBustContext; Core::RenderContext marbleBustContext;
Core::RenderContext spaceshipContext; Core::RenderContext spaceshipContext;
Core::RenderContext sphereContext; Core::RenderContext sphereContext;
Core::RenderContext cubeContext;
}
namespace texture {
GLuint cubemapTexture;
} }
GLuint depthMapFBO; GLuint depthMapFBO;
@ -38,6 +43,7 @@ GLuint program;
GLuint programSun; GLuint programSun;
GLuint programTest; GLuint programTest;
GLuint programTex; GLuint programTex;
GLuint programCubemap;
Core::Shader_Loader shaderLoader; Core::Shader_Loader shaderLoader;
@ -75,7 +81,6 @@ void renderShadowapSun() {
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, WIDTH, HEIGHT); glViewport(0, 0, WIDTH, HEIGHT);
} }
void renderScene(GLFWwindow* window) void renderScene(GLFWwindow* window)
{ {
glClearColor(0.4f, 0.4f, 0.8f, 1.0f); glClearColor(0.4f, 0.4f, 0.8f, 1.0f);
@ -84,6 +89,9 @@ void renderScene(GLFWwindow* window)
updateDeltaTime(time); updateDeltaTime(time);
renderShadowapSun(); renderShadowapSun();
drawSkybox(models::cubeContext, glm::translate(glm::mat4(), spaceship->cameraPos), texture::cubemapTexture, programCubemap);
//space lamp //space lamp
glUseProgram(programSun); glUseProgram(programSun);
@ -165,7 +173,6 @@ void createSuns() {
suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(200, 150, -100), glm::vec3(0.03633f, 0.151106, 0.103226f), glm::vec3(0.6f, 0.2f, 0.9f), 3.9)); suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(200, 150, -100), glm::vec3(0.03633f, 0.151106, 0.103226f), glm::vec3(0.6f, 0.2f, 0.9f), 3.9));
suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-150, -100, -50), glm::vec3(0.83633f, -0.251106, -0.123226f), glm::vec3(0.7f, 0.5f, 0.8f), 4.1));*/ suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-150, -100, -50), glm::vec3(0.83633f, -0.251106, -0.123226f), glm::vec3(0.7f, 0.5f, 0.8f), 4.1));*/
} }
void init(GLFWwindow* window) void init(GLFWwindow* window)
{ {
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
@ -174,15 +181,33 @@ void init(GLFWwindow* window)
program = shaderLoader.CreateProgram("shaders/shader_9_1.vert", "shaders/shader_9_1.frag"); program = shaderLoader.CreateProgram("shaders/shader_9_1.vert", "shaders/shader_9_1.frag");
programTest = shaderLoader.CreateProgram("shaders/test.vert", "shaders/test.frag"); programTest = shaderLoader.CreateProgram("shaders/test.vert", "shaders/test.frag");
programSun = shaderLoader.CreateProgram("shaders/shader_8_sun.vert", "shaders/shader_8_sun.frag"); programSun = shaderLoader.CreateProgram("shaders/shader_8_sun.vert", "shaders/shader_8_sun.frag");
programCubemap = shaderLoader.CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag");
//loadModelToContext("./models/sphere.obj", sphereContext);
//loadModelToContext("./models/spaceship.obj", spaceship.context);
loadModelToContext("./models/marbleBust.obj", models::marbleBustContext); loadModelToContext("./models/marbleBust.obj", models::marbleBustContext);
loadModelToContext("./models/spaceship.obj", models::spaceshipContext); loadModelToContext("./models/spaceship.obj", models::spaceshipContext);
loadModelToContext("./models/sphere.obj", models::sphereContext); loadModelToContext("./models/sphere.obj", models::sphereContext);
loadModelToContext("./models/cube.obj", models::cubeContext);
/*std::vector<std::string> cubeFaces = {
"space_rt.png",
"space_lf.png",
"space_up.png",
"space_dn.png",
"space_bk.png",
"space_ft.png"
};*/
std::vector<std::string> cubeFaces = {
"bkg2_right1.png",
"bkg2_left2.png",
"bkg2_top3.png",
"bkg2_bottom4.png",
"bkg2_front5.png",
"bkg2_back6.png"
};
texture::cubemapTexture = Core::LoadCubemap(cubeFaces);
createSuns(); createSuns();
} }