This commit is contained in:
s464971 2024-02-04 10:46:06 +01:00
parent 80db86ab11
commit d150694eff
11 changed files with 227 additions and 111 deletions

View File

@ -56,7 +56,7 @@
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset> <PlatformToolset>v142</PlatformToolset>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>

12
cw_8/models/cube.mtl Normal file
View File

@ -0,0 +1,12 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl _PBR
Ns 225.000000
Ka 1.000000 1.000000 1.000000
Kd 0.053704 0.053704 0.053704
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 3

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

@ -9,10 +9,12 @@
#include "Render_Utils.h" #include "Render_Utils.h"
#include "Texture.h" #include "Texture.h"
#include "SOIL/SOIL.h"
GLuint program; GLuint program;
GLuint programSun; GLuint programSun;
GLuint programParticle; GLuint programParticle;
GLuint programSkybox;
Core::Shader_Loader shaderLoader; Core::Shader_Loader shaderLoader;
@ -21,6 +23,8 @@ Core::RenderContext sunContext;
Core::RenderContext planetContext; Core::RenderContext planetContext;
Core::RenderContext stationContext; Core::RenderContext stationContext;
Core::RenderContext cubeContext;
namespace texture { namespace texture {
GLuint sun; GLuint sun;
@ -72,6 +76,8 @@ namespace texture {
GLuint station_ao; GLuint station_ao;
GLuint station_roughness; GLuint station_roughness;
GLuint station_metallic; GLuint station_metallic;
GLuint cube;
} }
float aspectRatio = 1.f; float aspectRatio = 1.f;
@ -100,116 +106,6 @@ glm::mat4 createCameraMatrix();
void drawObjectSun(Core::RenderContext& context, glm::mat4 modelMatrix); void drawObjectSun(Core::RenderContext& context, glm::mat4 modelMatrix);
void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint albedo, GLuint ao, GLuint normal, GLuint roughness, GLuint metallic); void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint albedo, GLuint ao, GLuint normal, GLuint roughness, GLuint metallic);
void init(GLFWwindow* window)
{
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glEnable(GL_DEPTH_TEST);
program = shaderLoader.CreateProgram("shaders/pbr.vert", "shaders/pbr.frag");
programSun = shaderLoader.CreateProgram("shaders/sun.vert", "shaders/sun.frag");
//programParticle = shaderLoader.CreateProgram("shaders/part.vert", "shaders/part.frag");
//load models here
loadModelToContext("./models/spaceship.fbx", shipContext);
loadModelToContext("./models/sun.glb", sunContext);
loadModelToContext("./models/sphere.obj", planetContext);
loadModelToContext("./models/station.obj", stationContext);
//load textures here
texture::sun = Core::LoadTexture("textures/sun/sun.png");
texture::planet_albedo = Core::LoadTexture("textures/planet/albedo.jpg");
texture::planet_ao = Core::LoadTexture("textures/planet/ao.jpg");
texture::planet_normal = Core::LoadTexture("textures/planet/normal.jpg");
texture::planet_roughness = Core::LoadTexture("textures/planet/roughness.jpg");
texture::planet_metallic = Core::LoadTexture("textures/planet/metallic.png");
texture::mars_albedo = Core::LoadTexture("textures/mars/albedo.jpg");
texture::mars_normal = Core::LoadTexture("textures/mars/normal.png");
texture::mars_ao = Core::LoadTexture("textures/mars/ao.png");
texture::mars_roughness = Core::LoadTexture("textures/mars/roughness.png");
texture::mars_metallic = Core::LoadTexture("textures/mars/metallic.png");
texture::ship_albedo = Core::LoadTexture("textures/ship/albedo.png");
texture::ship_ao = Core::LoadTexture("textures/ship/ao.png");
texture::ship_normal = Core::LoadTexture("textures/ship/normal.png");
texture::ship_roughness = Core::LoadTexture("textures/ship/roughness.png");
texture::ship_metallic = Core::LoadTexture("textures/ship/metallic.jpg");
texture::jupiter_albedo = Core::LoadTexture("textures/jupiter/albedo.png");
texture::jupiter_normal = Core::LoadTexture("textures/jupiter/normal.png");
texture::jupiter_ao = Core::LoadTexture("textures/jupiter/ao.png");
texture::jupiter_roughness = Core::LoadTexture("textures/jupiter/roughness.png");
texture::jupiter_metallic = Core::LoadTexture("textures/jupiter/metallic.png");
texture::earth_albedo = Core::LoadTexture("textures/earth/albedo.png");
texture::earth_normal = Core::LoadTexture("textures/earth/normal.png");
texture::earth_roughness = Core::LoadTexture("textures/earth/roughness.png");
texture::earth_ao = Core::LoadTexture("textures/earth/ao.png");
texture::earth_metallic = Core::LoadTexture("textures/earth/metallic.png");
texture::moon_albedo = Core::LoadTexture("textures/moon/albedo.jpg");
texture::moon_normal = Core::LoadTexture("textures/moon/normal.jpg");
texture::moon_roughness = Core::LoadTexture("textures/moon/roughness.jpg");
texture::moon_ao = Core::LoadTexture("textures/moon/ao.jpg");
texture::moon_metallic = Core::LoadTexture("textures/moon/metallic.png");
texture::station_albedo = Core::LoadTexture("textures/station/albedo.jpg");
texture::station_normal = Core::LoadTexture("textures/station/normal.png");
texture::station_roughness = Core::LoadTexture("textures/station/roughness.jpg");
texture::station_ao = Core::LoadTexture("textures/station/ao.jpg");
texture::station_metallic = Core::LoadTexture("textures/station/metallic.jpg");
}
void renderScene(GLFWwindow* window){
glClearColor(0.0f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
float time = glfwGetTime();
updateDeltaTime(time);
//desired objects go here
drawObjectSun(sunContext, glm::scale(glm::vec3(0.1f)) * glm::eulerAngleY(time/10));
glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f)));
glm::vec3 spaceshipUp = glm::normalize(glm::cross(spaceshipSide, spaceshipDir));
glm::mat4 spaceshipRotationMatrix = glm::mat4({
spaceshipSide.x,spaceshipSide.y,spaceshipSide.z,0,
spaceshipUp.x,spaceshipUp.y,spaceshipUp.z ,0,
-spaceshipDir.x,-spaceshipDir.y,-spaceshipDir.z,0,
0.,0.,0.,1.,
});
drawObjectPBR(shipContext,
glm::translate(spaceshipPos)
* spaceshipRotationMatrix
* glm::translate(glm::vec3(0.f, -0.1f, 0.1f))
* glm::eulerAngleX(glm::radians(80.f))
* glm::eulerAngleY(glm::radians(180.f))
* glm::scale(glm::vec3(0.005f)),
texture::ship_albedo, texture::ship_normal, texture::ship_ao, texture::ship_roughness, texture::ship_metallic);
drawObjectPBR(planetContext, glm::eulerAngleY(time/3) * glm::translate(glm::vec3(4.f, 0, 0)) * glm::eulerAngleY(-0.5f*time), texture::planet_albedo, texture::planet_normal, texture::planet_ao, texture::planet_roughness, texture::planet_metallic);
drawObjectPBR(planetContext, glm::eulerAngleY(time/3) * glm::translate(glm::vec3(-4.f, 0, 0)) * glm::eulerAngleY(-0.5f*time), texture::mars_albedo, texture::mars_normal, texture::mars_ao, texture::mars_roughness, texture::mars_metallic);
drawObjectPBR(planetContext, glm::eulerAngleY(time/3) * glm::translate(glm::vec3(0, 0, 4.f)) * glm::eulerAngleY(-0.5f*time), texture::jupiter_albedo, texture::jupiter_normal, texture::jupiter_ao, texture::jupiter_roughness, texture::jupiter_metallic);
drawObjectPBR(planetContext, glm::eulerAngleY(time/3) * glm::translate(glm::vec3(0, 0, -4.f)) * glm::eulerAngleY(-0.5f*time), texture::earth_albedo, texture::earth_normal, texture::earth_ao, texture::earth_roughness, texture::earth_metallic);
drawObjectPBR(planetContext, glm::translate(glm::vec3(4.f, 0, 4.f)) * glm::eulerAngleY(-0.5f * time), texture::moon_albedo, texture::moon_normal, texture::moon_ao, texture::moon_roughness, texture::moon_metallic);
drawObjectPBR(stationContext, glm::translate(glm::vec3(0.f, 3.f, 0.f)) * glm::scale(glm::vec3(0.1f)) * glm::eulerAngleY(time), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic);
//desired objects end here
glUseProgram(0);
glfwSwapBuffers(window);
}
void processInput(GLFWwindow* window) void processInput(GLFWwindow* window)
{ {
@ -283,12 +179,195 @@ void loadModelToContext(std::string path, Core::RenderContext& context)
context.initFromAssimpMesh(scene->mMeshes[0]); context.initFromAssimpMesh(scene->mMeshes[0]);
} }
void drawSkybox(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint textureID) {
glDisable(GL_DEPTH_TEST);
glUseProgram(programSkybox);
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(programSkybox, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(programSkybox, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
glUniform3f(glGetUniformLocation(programSkybox, "lightPos"), 0, 0, 0);
Core::SetActiveTexture(textureID, "colorTexture", programSkybox, 0);
Core::DrawContext(context);
glEnable(GL_DEPTH_TEST);
}
unsigned int loadCubemap(std::vector<std::string> faces)
{
unsigned int textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
int width, height, nrChannels;
for (unsigned int i = 0; i < faces.size(); i++)
{
unsigned char* data = SOIL_load_image(faces[i].c_str(), &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data
);
}
else
{
std::cout << "Cubemap tex failed to load at path: " << faces[i] << std::endl;
}
}
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 renderScene(GLFWwindow* window) {
glClearColor(0.0f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
float time = glfwGetTime();
updateDeltaTime(time);
drawSkybox(cubeContext, glm::translate(cameraPos), texture::cube);
//desired objects go here
drawObjectSun(sunContext, glm::scale(glm::vec3(0.1f)) * glm::eulerAngleY(time / 10));
glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f)));
glm::vec3 spaceshipUp = glm::normalize(glm::cross(spaceshipSide, spaceshipDir));
glm::mat4 spaceshipRotationMatrix = glm::mat4({
spaceshipSide.x,spaceshipSide.y,spaceshipSide.z,0,
spaceshipUp.x,spaceshipUp.y,spaceshipUp.z ,0,
-spaceshipDir.x,-spaceshipDir.y,-spaceshipDir.z,0,
0.,0.,0.,1.,
});
drawObjectPBR(shipContext,
glm::translate(spaceshipPos)
* spaceshipRotationMatrix
* glm::translate(glm::vec3(0.f, -0.1f, 0.1f))
* glm::eulerAngleX(glm::radians(80.f))
* glm::eulerAngleY(glm::radians(180.f))
* glm::scale(glm::vec3(0.005f)),
texture::ship_albedo, texture::ship_normal, texture::ship_ao, texture::ship_roughness, texture::ship_metallic);
drawObjectPBR(planetContext, glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(4.f, 0, 0)) * glm::eulerAngleY(-0.5f * time), texture::planet_albedo, texture::planet_normal, texture::planet_ao, texture::planet_roughness, texture::planet_metallic);
drawObjectPBR(planetContext, glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(-4.f, 0, 0)) * glm::eulerAngleY(-0.5f * time), texture::mars_albedo, texture::mars_normal, texture::mars_ao, texture::mars_roughness, texture::mars_metallic);
drawObjectPBR(planetContext, glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(0, 0, 4.f)) * glm::eulerAngleY(-0.5f * time), texture::jupiter_albedo, texture::jupiter_normal, texture::jupiter_ao, texture::jupiter_roughness, texture::jupiter_metallic);
drawObjectPBR(planetContext, glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(0, 0, -4.f)) * glm::eulerAngleY(-0.5f * time), texture::earth_albedo, texture::earth_normal, texture::earth_ao, texture::earth_roughness, texture::earth_metallic);
drawObjectPBR(planetContext, glm::translate(glm::vec3(4.f, 0, 4.f)) * glm::eulerAngleY(-0.5f * time), texture::moon_albedo, texture::moon_normal, texture::moon_ao, texture::moon_roughness, texture::moon_metallic);
drawObjectPBR(stationContext, glm::translate(glm::vec3(0.f, 3.f, 0.f)) * glm::scale(glm::vec3(0.1f)) * glm::eulerAngleY(time), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic);
//desired objects end here
glUseProgram(0);
glfwSwapBuffers(window);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{ {
aspectRatio = width / float(height); aspectRatio = width / float(height);
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
} }
void init(GLFWwindow* window)
{
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glEnable(GL_DEPTH_TEST);
program = shaderLoader.CreateProgram("shaders/pbr.vert", "shaders/pbr.frag");
programSun = shaderLoader.CreateProgram("shaders/sun.vert", "shaders/sun.frag");
//programParticle = shaderLoader.CreateProgram("shaders/part.vert", "shaders/part.frag");
//load models here
loadModelToContext("./models/spaceship.fbx", shipContext);
loadModelToContext("./models/sun.glb", sunContext);
loadModelToContext("./models/sphere.obj", planetContext);
loadModelToContext("./models/station.obj", stationContext);
loadModelToContext("./models/cube.obj", cubeContext);
//load skybox here
programSkybox = shaderLoader.CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag");
std::vector<std::string> x = {
"textures/skybox/space_bk.png",
"textures/skybox/space_dn.png",
"textures/skybox/space_ft.png",
"textures/skybox/space_lf.png",
"textures/skybox/space_rt.png",
"textures/skybox/space_up.png"
};
texture::cube = loadCubemap(x);
//load textures here
texture::sun = Core::LoadTexture("textures/sun/sun.png");
texture::planet_albedo = Core::LoadTexture("textures/planet/albedo.jpg");
texture::planet_ao = Core::LoadTexture("textures/planet/ao.jpg");
texture::planet_normal = Core::LoadTexture("textures/planet/normal.jpg");
texture::planet_roughness = Core::LoadTexture("textures/planet/roughness.jpg");
texture::planet_metallic = Core::LoadTexture("textures/planet/metallic.png");
texture::mars_albedo = Core::LoadTexture("textures/mars/albedo.jpg");
texture::mars_normal = Core::LoadTexture("textures/mars/normal.png");
texture::mars_ao = Core::LoadTexture("textures/mars/ao.png");
texture::mars_roughness = Core::LoadTexture("textures/mars/roughness.png");
texture::mars_metallic = Core::LoadTexture("textures/mars/metallic.png");
texture::ship_albedo = Core::LoadTexture("textures/ship/albedo.png");
texture::ship_ao = Core::LoadTexture("textures/ship/ao.png");
texture::ship_normal = Core::LoadTexture("textures/ship/normal.png");
texture::ship_roughness = Core::LoadTexture("textures/ship/roughness.png");
texture::ship_metallic = Core::LoadTexture("textures/ship/metallic.jpg");
texture::jupiter_albedo = Core::LoadTexture("textures/jupiter/albedo.png");
texture::jupiter_normal = Core::LoadTexture("textures/jupiter/normal.png");
texture::jupiter_ao = Core::LoadTexture("textures/jupiter/ao.png");
texture::jupiter_roughness = Core::LoadTexture("textures/jupiter/roughness.png");
texture::jupiter_metallic = Core::LoadTexture("textures/jupiter/metallic.png");
texture::earth_albedo = Core::LoadTexture("textures/earth/albedo.png");
texture::earth_normal = Core::LoadTexture("textures/earth/normal.png");
texture::earth_roughness = Core::LoadTexture("textures/earth/roughness.png");
texture::earth_ao = Core::LoadTexture("textures/earth/ao.png");
texture::earth_metallic = Core::LoadTexture("textures/earth/metallic.png");
texture::moon_albedo = Core::LoadTexture("textures/moon/albedo.jpg");
texture::moon_normal = Core::LoadTexture("textures/moon/normal.jpg");
texture::moon_roughness = Core::LoadTexture("textures/moon/roughness.jpg");
texture::moon_ao = Core::LoadTexture("textures/moon/ao.jpg");
texture::moon_metallic = Core::LoadTexture("textures/moon/metallic.png");
texture::station_albedo = Core::LoadTexture("textures/station/albedo.jpg");
texture::station_normal = Core::LoadTexture("textures/station/normal.png");
texture::station_roughness = Core::LoadTexture("textures/station/roughness.jpg");
texture::station_ao = Core::LoadTexture("textures/station/ao.jpg");
texture::station_metallic = Core::LoadTexture("textures/station/metallic.jpg");
}
void shutdown(GLFWwindow* window) void shutdown(GLFWwindow* window)
{ {
shaderLoader.DeleteProgram(program); shaderLoader.DeleteProgram(program);

Binary file not shown.

After

Width:  |  Height:  |  Size: 299 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 636 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 545 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 508 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 326 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 507 KiB