Skip to content

Commit

Permalink
Changes to lab 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Manni-99 committed Sep 26, 2024
1 parent 8a6a881 commit fe787ca
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 235 deletions.
68 changes: 68 additions & 0 deletions shaders/EDAF80/phong.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#version 410 core

// Inputs from vertex shader
in vec3 FragPos; // Position in world space
in vec3 Normal; // Normal in world space
in vec3 ViewDir; // View direction
in vec3 textCoords; //Texture
in vec3 Tangent; //Tangent
in vec3 Bitangent; //Bitangent
// Uniforms
uniform vec3 light_position;
uniform vec3 diffuse_colour;
uniform vec3 specular_colour;
uniform vec3 ambient_colour;
uniform float shininess_value;
uniform bool use_normal_mapping;

uniform sampler2D demo_diffuse_texture;
uniform sampler2D demo_specular_texture;
uniform sampler2D demo_normal_texture;
// Output color
out vec4 FragColor;

void main()
{
vec3 ambient = ambient_colour;

if(use_normal_mapping){
vec3 T = normalize(Tangent);
vec3 B = normalize(Bitangent);
vec3 N = normalize(Normal);
mat3 TBN = mat3(T, B, N);

vec3 normalMap = texture(demo_normal_texture, textCoords.xy).rgb;
normalMap = normalize(normalMap * 2.0 - 1.0);
vec3 normal = normalize(TBN * normalMap);

vec3 light_dir = normalize(light_position - FragPos); // Light direction
float diff = max(dot(normal, light_dir), 0.0); // Angle between light and normal
vec3 diffuse = diff * texture(demo_diffuse_texture, textCoords.xy).rgb;

// Specular lighting (Phong reflectance)
vec3 reflect_dir = reflect(-light_dir, normal); // Reflect the light direction around the normal
float spec = pow(max(dot(ViewDir, reflect_dir), 0.0), shininess_value); // Specular intensity
vec3 specular = spec * texture(demo_specular_texture, textCoords.xy).rgb;
// Combine all three components (ambient + diffuse + specular)
vec3 result = (ambient + diffuse + specular);
FragColor = vec4(result, 1.0);
} else
{
vec3 normal = normalize(Normal);

vec3 light_dir = normalize(light_position - FragPos); // Light direction
float diff = max(dot(normal, light_dir), 0.0); // Angle between light and normal
vec3 diffuse = diff * texture(demo_diffuse_texture, textCoords.xy).rgb;

// Specular lighting (Phong reflectance)
vec3 reflect_dir = reflect(-light_dir, normal); // Reflect the light direction around the normal
float spec = pow(max(dot(ViewDir, reflect_dir), 0.0), shininess_value); // Specular intensity
vec3 specular = spec * texture(demo_specular_texture, textCoords.xy).rgb;

// Combine all three components (ambient + diffuse + specular)
vec3 result = (ambient + diffuse + specular);

// Output the final color (with alpha value of 1.0)
FragColor = vec4(result, 1.0);
}
}
41 changes: 41 additions & 0 deletions shaders/EDAF80/phong.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#version 410 core

// Vertex attributes
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec3 textures;
layout (location = 3) in vec3 tangent;
layout (location = 4) in vec3 bitangent;

// Uniforms
uniform mat4 normal_model_to_world;
uniform mat4 vertex_model_to_world;
uniform mat4 vertex_world_to_clip;
uniform vec3 camera_position; // Camera position (for specular reflections)
// Outputs to fragment shader
out vec3 FragPos; // Position in world space
out vec3 Normal; // Normal in world space
out vec3 ViewDir; // View direction (camera position - fragment position)
out vec3 textCoords;
out vec3 Tangent;
out vec3 Bitangent;
void main()
{
// Transform the vertex position to world space
FragPos = vec3(vertex_model_to_world * vec4(position, 1.0));

// Pass the normal in world space
Normal = (normal_model_to_world * vec4(normal, 0.0)).xyz;

Tangent = (normal_model_to_world * vec4(tangent, 0.0)).xyz;

Bitangent = (normal_model_to_world * vec4(bitangent, 0.0)).xyz;

// Calculate view direction (camera position - fragment position)
ViewDir = normalize(camera_position - FragPos);

textCoords = textures;

// Transform the position to clip space for rasterization
gl_Position = vertex_world_to_clip * vec4(FragPos, 1.0);
}
1 change: 0 additions & 1 deletion shaders/EDAF80/skybox.frag
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ uniform samplerCube skybox;
void main()
{
FragColor = texture(skybox, textCoords);
// FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
3 changes: 2 additions & 1 deletion shaders/EDAF80/skybox.vert
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ uniform mat4 vertex_world_to_clip;

void main()
{
textCoords = vec3(aPos.x, aPos.y, -aPos.z); //Test
vec4 pos = vertex_world_to_clip * vertex_model_to_world * vec4(aPos, 1.0f);
gl_Position = vec4(pos.x, pos.y, pos.w, pos.w);
textCoords = vec3(aPos.x, aPos.y, -aPos.z); //Test

}
72 changes: 24 additions & 48 deletions src/EDAF80/assignment3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ void edaf80::Assignment3::run()
skybox_shader);
if (skybox_shader == 0u)
LogError("Failed to load skybox shader");

GLuint phong_shader = 0u;
program_manager.CreateAndRegisterProgram("Phong Shading",
{{ShaderType::vertex, "EDAF80/phong.vert"},
{ShaderType::fragment, "EDAF80/phong.frag"}},
phong_shader);
if (phong_shader == 0u)
LogError("Failed to load phong shader");


auto light_position = glm::vec3(-2.0f, 4.0f, 2.0f);
auto const set_uniforms = [&light_position](GLuint program)
Expand All @@ -111,57 +120,12 @@ void edaf80::Assignment3::run()
//
// Set up the two spheres used.
//
auto skybox_shape = parametric_shapes::createSphere(20.0f, 100u, 100u);
auto skybox_shape = parametric_shapes::createSphere(200.0f, 1000u, 1000u); //Increased value to give the illusion of an infinite skybox
if (skybox_shape.vao == 0u)
{
LogError("Failed to retrieve the mesh for the skybox");
return;
}
/*float skyboxVertices[] =
{
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f};
unsigned int skyboxIndices[] =
{
// Right
1, 2, 6,
6, 5, 1,
// Left
0, 4, 7,
7, 3, 0,
// Top
4, 5, 6,
6, 7, 4,
// Bottom
0, 3, 2,
2, 1, 0,
// Back
0, 1, 5,
5, 4, 0,
// Front
3, 7, 6,
6, 2, 3};
unsigned int skyboxVAO, skyboxVBO, skyboxEBO;
glGenVertexArrays(1, &skyboxVAO);
glGenBuffers(1, &skyboxVBO);
glGenBuffers(1, &skyboxEBO);
glBindVertexArray(skyboxVAO);
glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, skyboxEBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxIndices), &skyboxIndices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);*/

GLuint cubeMap = bonobo::loadTextureCubeMap("/home/manni/LTH/Year3/Datorgrafik/AAA_Programing/CG_Labs/res/cubemaps/LarnacaCastle/posx.jpg", "/home/manni/LTH/Year3/Datorgrafik/AAA_Programing/CG_Labs/res/cubemaps/LarnacaCastle/negx.jpg", "/home/manni/LTH/Year3/Datorgrafik/AAA_Programing/CG_Labs/res/cubemaps/LarnacaCastle/posy.jpg",
"/home/manni/LTH/Year3/Datorgrafik/AAA_Programing/CG_Labs/res/cubemaps/LarnacaCastle/negy.jpg", "/home/manni/LTH/Year3/Datorgrafik/AAA_Programing/CG_Labs/res/cubemaps/LarnacaCastle/posz.jpg", "/home/manni/LTH/Year3/Datorgrafik/AAA_Programing/CG_Labs/res/cubemaps/LarnacaCastle/negz.jpg",
Expand Down Expand Up @@ -190,11 +154,21 @@ void edaf80::Assignment3::run()
demo_material.specular = glm::vec3(1.0f, 1.0f, 1.0f);
demo_material.shininess = 10.0f;

GLuint demo_diffuse_texture = bonobo::loadTexture2D("/home/manni/LTH/Year3/Datorgrafik/AAA_Programing/CG_Labs/res/textures/leather_red_02_coll1_2k.jpg", true);
GLuint demo_specular_texture = bonobo::loadTexture2D("/home/manni/LTH/Year3/Datorgrafik/AAA_Programing/CG_Labs/res/textures/leather_red_02_rough_2k.jpg", true);
GLuint demo_normal_texture = bonobo::loadTexture2D("/home/manni/LTH/Year3/Datorgrafik/AAA_Programing/CG_Labs/res/textures/leather_red_02_nor_2k.jpg", true);

Log("demoTexture value: %d\n", demo_diffuse_texture);
Node demo_sphere;
demo_sphere.set_geometry(demo_shape);
demo_sphere.set_material_constants(demo_material);
demo_sphere.set_program(&fallback_shader, phong_set_uniforms);
demo_sphere.set_program(&phong_shader, phong_set_uniforms);
demo_sphere.add_texture("demo_diffuse_texture", demo_diffuse_texture, GL_TEXTURE_2D);
demo_sphere.add_texture("demo_specular_texture", demo_specular_texture, GL_TEXTURE_2D);
demo_sphere.add_texture("demo_normal_texture", demo_normal_texture, GL_TEXTURE_2D);


// Here we should implement the smaller sphere to change its color, phong shading
glClearDepthf(1.0f);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glEnable(GL_DEPTH_TEST);
Expand Down Expand Up @@ -263,9 +237,11 @@ void edaf80::Assignment3::run()

glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
bonobo::changePolygonMode(polygon_mode);

glDepthFunc(GL_LEQUAL);
skybox.render(mCamera.GetWorldToClipMatrix());
glDepthFunc(GL_LESS);
glDepthFunc(GL_LESS);

demo_sphere.render(mCamera.GetWorldToClipMatrix());

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
Expand Down
Loading

0 comments on commit fe787ca

Please sign in to comment.