Skip to content

Commit 95f1f55

Browse files
AhmedAmrNabilLoayAhmed304AhmedSobhy01
authored
feat: implement rendering, materials, textures, and ECS systems (phase 1) (#3)
* feat(shader-test): finish shader requirement * feat(mesh-test): finish mesh requirement * feat(transform-test): finish transform requirement * feat(pipeline-test): finish pipeline requirement * feat(texture-test): finish texture requirement * feat(sampler-test): finish sampler requirement * feat(material-test): finish material requirement * feat(camera): finish TODOs in camera class * feat(entity): finish TODOs in entity class * feat(world): finish TODOs in world class * feat(mesh-renderer): finish mesh-renderer TODOs * chore: update format in component.hpp and transform.hpp * feat(renderer-test): finish forward-renderer TODOs * feat(sky-test): finish Req 10 TODOs (skybox rendering) * feat(forward-renderer): finish Req 10 TODOs (post processing) * refactor(texture-utils): simplify empty texture creation logic * chore: remove TODO's comments * chore: fix formatting * fix(entity): clear components in destructor * perf(entity): convert localToWorldMatrix calculation to a loop instead of recursion * refactor(world): remove unused destruction of marketForRemoval set * refactor(camera): move aspectRatio initializtion to the top of getProjectionMatrix function * refactor(forward-renderer): optimize computation of cameraForward direction * feat(texture): update texture bind and unbind logic to use modern opengl * chore: fix formatting * refactor(texture-utils): use modern opengl texture api in loading images and creating empty textures * refactor(texture): revert back to classical texture api * fix: reset app configuration to main version * fix: correct typo in comment * fix: ensure shader is deleted after attachment * fix: correct component type handling in addComponent method * fix: ensure proper cleanup of sky and postprocess materials in destroy method * refactor: change auto to actual explicit type * fix: ensure texture is bound after setting alphaThreshold in TexturedMaterial setup --------- Co-authored-by: LoayAhmed304 <loayahmed304@gmail.com> Co-authored-by: Ahmed Sobhy <48056730+AhmedSobhy01@users.noreply.github.com>
1 parent 626c89e commit 95f1f55

31 files changed

Lines changed: 805 additions & 640 deletions

assets/shaders/checkerboard.frag

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
#version 330 core
2-
3-
out vec4 frag_color;
4-
5-
// In this shader, we want to draw a checkboard where the size of each tile is (size x size).
6-
// The color of the top-left most tile should be "colors[0]" and the 2 tiles adjacent to it
7-
// should have the color "colors[1]".
8-
9-
//TODO: (Req 1) Finish this shader.
10-
11-
uniform int size = 32;
12-
uniform vec3 colors[2];
13-
14-
void main(){
15-
frag_color = vec4(colors[0], 1.0);
16-
}
1+
#version 330 core
2+
3+
out vec4 frag_color;
4+
5+
// In this shader, we want to draw a checkboard where the size of each tile is (size x size).
6+
// The color of the top-left most tile should be "colors[0]" and the 2 tiles adjacent to it
7+
// should have the color "colors[1]".
8+
9+
uniform int size = 32;
10+
uniform vec3 colors[2];
11+
12+
void main(){
13+
ivec2 tile = ivec2(gl_FragCoord.xy) / size;
14+
if ((tile.x + tile.y) % 2 == 0) {
15+
frag_color = vec4(colors[0], 1.0);
16+
} else {
17+
frag_color = vec4(colors[1], 1.0);
18+
}
19+
}

assets/shaders/color-mixer.frag

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
1-
#version 330 core
2-
3-
// This shader is designed to work with "triangle.vert" and it receives an
4-
// interpolated varying which represents the vertex color.
5-
6-
in Varyings {
7-
vec3 color;
8-
} fs_in;
9-
10-
out vec4 frag_color;
11-
12-
// currently the shader just returns the interpalated color varying.
13-
// However, we want to mix the color channels around. We can do this using a
14-
// color matrix which we will send to the shader as 3 uniforms: red, green, blue.
15-
// Each of these 3 variables will be a vec4. To apply the channel mixing for a
16-
// certain channel (e.g. red), we apply this linear transformation:
17-
// frag_color.r = red.r * fs_in.color.r + red.g + fs_in.color.g + red.b + fs_in.color.b + red.a;
18-
// However, this line is too long to write, so we can simplify it using a dot product
19-
// (which is defined in the "dot" function).
20-
21-
//TODO: (Req 1) Finish this shader and apply the channel mixing using the "dot" function.
22-
23-
void main(){
24-
frag_color = vec4(fs_in.color, 1.0);
25-
}
1+
#version 330 core
2+
3+
// This shader is designed to work with "triangle.vert" and it receives an
4+
// interpolated varying which represents the vertex color.
5+
6+
in Varyings {
7+
vec3 color;
8+
} fs_in;
9+
10+
out vec4 frag_color;
11+
12+
// currently the shader just returns the interpalated color varying.
13+
// However, we want to mix the color channels around. We can do this using a
14+
// color matrix which we will send to the shader as 3 uniforms: red, green, blue.
15+
// Each of these 3 variables will be a vec4. To apply the channel mixing for a
16+
// certain channel (e.g. red), we apply this linear transformation:
17+
// frag_color.r = red.r * fs_in.color.r + red.g * fs_in.color.g + red.b * fs_in.color.b + red.a * fs_in.color.a;
18+
// However, this line is too long to write, so we can simplify it using a dot product
19+
// (which is defined in the "dot" function).
20+
21+
uniform vec4 red = vec4(1.0, 0.0, 0.0, 0.0);
22+
uniform vec4 green = vec4(0.0, 1.0, 0.0, 0.0);
23+
uniform vec4 blue = vec4(0.0, 0.0, 1.0, 0.0);
24+
25+
26+
void main(){
27+
vec4 color = vec4(fs_in.color, 1.0);
28+
frag_color.r = dot(red, color);
29+
frag_color.g = dot(green, color);
30+
frag_color.b = dot(blue, color);
31+
frag_color.a = 1.0;
32+
}
Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
#version 330
2-
3-
// The texture holding the scene pixels
4-
uniform sampler2D tex;
5-
6-
// Read "assets/shaders/fullscreen.vert" to know what "tex_coord" holds;
7-
in vec2 tex_coord;
8-
out vec4 frag_color;
9-
10-
// How far (in the texture space) is the distance (on the x-axis) between
11-
// the pixels from which the red/green (or green/blue) channels are sampled
12-
#define STRENGTH 0.005
13-
14-
// Chromatic aberration mimics some old cameras where the lens disperses light
15-
// differently based on its wavelength. In this shader, we will implement a
16-
// cheap version of that effect
17-
18-
void main(){
19-
//TODO: Modify this shader to apply chromatic abberation
20-
// To apply this effect, we only read the green channel from the correct pixel (as defined by tex_coord)
21-
// To get the red channel, we move by amount STRENGTH to the left then sample another pixel from which we take the red channel
22-
// To get the blue channel, we move by amount STRENGTH to the right then sample another pixel from which we take the blue channel
23-
frag_color = texture(tex, tex_coord);
24-
}
1+
#version 330
2+
3+
// The texture holding the scene pixels
4+
uniform sampler2D tex;
5+
6+
// Read "assets/shaders/fullscreen.vert" to know what "tex_coord" holds;
7+
in vec2 tex_coord;
8+
out vec4 frag_color;
9+
10+
// How far (in the texture space) is the distance (on the x-axis) between
11+
// the pixels from which the red/green (or green/blue) channels are sampled
12+
#define STRENGTH 0.005
13+
14+
// Chromatic aberration mimics some old cameras where the lens disperses light
15+
// differently based on its wavelength. In this shader, we will implement a
16+
// cheap version of that effect
17+
18+
void main() {
19+
// red = move by STRENGTH to the left, then sample the red channel
20+
// green = sample the green channel from the correct pixel
21+
// blue = move by STRENGTH to the right, then sample the blue channel
22+
frag_color.r = texture(tex, tex_coord - vec2(STRENGTH, 0.0)).r;
23+
frag_color.ga = texture(tex, tex_coord).ga;
24+
frag_color.b = texture(tex, tex_coord + vec2(STRENGTH, 0.0)).b;
25+
}
Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
#version 330
2-
3-
// The texture holding the scene pixels
4-
uniform sampler2D tex;
5-
6-
// Read "assets/shaders/fullscreen.vert" to know what "tex_coord" holds;
7-
in vec2 tex_coord;
8-
9-
out vec4 frag_color;
10-
11-
// Vignette is a postprocessing effect that darkens the corners of the screen
12-
// to grab the attention of the viewer towards the center of the screen
13-
14-
void main(){
15-
//TODO: Modify this shader to apply vignette
16-
// To apply vignette, divide the scene color
17-
// by 1 + the squared length of the 2D pixel location the NDC space
18-
// Hint: remember that the NDC space ranges from -1 to 1
19-
// while the texture coordinate space ranges from 0 to 1
20-
// We have the pixel's texture coordinate, how can we compute its location in the NDC space?
21-
frag_color = texture(tex, tex_coord);
22-
}
1+
#version 330
2+
3+
// The texture holding the scene pixels
4+
uniform sampler2D tex;
5+
6+
// Read "assets/shaders/fullscreen.vert" to know what "tex_coord" holds;
7+
in vec2 tex_coord;
8+
9+
out vec4 frag_color;
10+
11+
// Vignette is a postprocessing effect that darkens the corners of the screen
12+
// to grab the attention of the viewer towards the center of the screen
13+
14+
void main() {
15+
// To apply vignette, divide the scene color
16+
// by 1 + the squared length of the 2D pixel location the NDC space
17+
vec2 ndc = tex_coord * 2.0 - 1.0; // Convert texture coordinates to NDC space
18+
float vignette = 1.0 / (1.0 + dot(ndc, ndc)); // Calculate vignette factor based on distance from center
19+
frag_color = texture(tex, tex_coord) * vignette;
20+
}

assets/shaders/texture-test.frag

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
#version 330 core
2-
3-
in Varyings {
4-
vec3 position;
5-
vec4 color;
6-
vec2 tex_coord;
7-
vec3 normal;
8-
} fs_in;
9-
10-
out vec4 frag_color;
11-
12-
uniform sampler2D tex;
13-
14-
void main(){
15-
//TODO: (Req 5) Change the following line to read the fragment color
16-
// from the texture at the received texture coordinates
17-
frag_color = vec4(1.0);
18-
}
1+
#version 330 core
2+
3+
in Varyings {
4+
vec3 position;
5+
vec4 color;
6+
vec2 tex_coord;
7+
vec3 normal;
8+
} fs_in;
9+
10+
out vec4 frag_color;
11+
12+
uniform sampler2D tex;
13+
14+
void main(){
15+
frag_color = texture(tex, fs_in.tex_coord) * fs_in.color;
16+
}

assets/shaders/textured.frag

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
#version 330 core
2-
3-
in Varyings {
4-
vec4 color;
5-
vec2 tex_coord;
6-
} fs_in;
7-
8-
out vec4 frag_color;
9-
10-
uniform vec4 tint;
11-
uniform sampler2D tex;
12-
13-
void main(){
14-
//TODO: (Req 7) Modify the following line to compute the fragment color
15-
// by multiplying the tint with the vertex color and with the texture color
16-
frag_color = vec4(1.0);
17-
}
1+
#version 330 core
2+
3+
in Varyings {
4+
vec4 color;
5+
vec2 tex_coord;
6+
} fs_in;
7+
8+
out vec4 frag_color;
9+
10+
uniform vec4 tint;
11+
uniform sampler2D tex;
12+
13+
void main(){
14+
frag_color = tint * fs_in.color * texture(tex, fs_in.tex_coord);
15+
}

assets/shaders/textured.vert

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
#version 330 core
2-
3-
layout(location = 0) in vec3 position;
4-
layout(location = 1) in vec4 color;
5-
layout(location = 2) in vec2 tex_coord;
6-
7-
out Varyings {
8-
vec4 color;
9-
vec2 tex_coord;
10-
} vs_out;
11-
12-
uniform mat4 transform;
13-
14-
void main(){
15-
//TODO: (Req 7) Change the next line to apply the transformation matrix
16-
gl_Position = vec4(position, 1.0);
17-
vs_out.color = color;
18-
vs_out.tex_coord = tex_coord;
19-
}
1+
#version 330 core
2+
3+
layout(location = 0) in vec3 position;
4+
layout(location = 1) in vec4 color;
5+
layout(location = 2) in vec2 tex_coord;
6+
7+
out Varyings {
8+
vec4 color;
9+
vec2 tex_coord;
10+
} vs_out;
11+
12+
uniform mat4 transform;
13+
14+
void main(){
15+
gl_Position = transform * vec4(position, 1.0);
16+
vs_out.color = color;
17+
vs_out.tex_coord = tex_coord;
18+
}

assets/shaders/tinted.frag

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
#version 330 core
2-
3-
in Varyings {
4-
vec4 color;
5-
} fs_in;
6-
7-
out vec4 frag_color;
8-
9-
uniform vec4 tint;
10-
11-
void main(){
12-
//TODO: (Req 7) Modify the following line to compute the fragment color
13-
// by multiplying the tint with the vertex color
14-
frag_color = vec4(1.0);
15-
}
1+
#version 330 core
2+
3+
in Varyings {
4+
vec4 color;
5+
} fs_in;
6+
7+
out vec4 frag_color;
8+
9+
uniform vec4 tint;
10+
11+
void main() {
12+
frag_color = tint * fs_in.color;
13+
}

assets/shaders/tinted.vert

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
#version 330 core
2-
3-
layout(location = 0) in vec3 position;
4-
layout(location = 1) in vec4 color;
5-
6-
out Varyings {
7-
vec4 color;
8-
} vs_out;
9-
10-
uniform mat4 transform;
11-
12-
void main(){
13-
//TODO: (Req 7) Change the next line to apply the transformation matrix
14-
gl_Position = vec4(position, 1.0);
15-
vs_out.color = color;
16-
}
1+
#version 330 core
2+
3+
layout(location = 0) in vec3 position;
4+
layout(location = 1) in vec4 color;
5+
6+
out Varyings {
7+
vec4 color;
8+
} vs_out;
9+
10+
uniform mat4 transform;
11+
12+
void main(){
13+
gl_Position = transform * vec4(position, 1.0);
14+
vs_out.color = color;
15+
}

0 commit comments

Comments
 (0)