diff --git a/tutorial19_Geometry_Normal_Shader/Readme.txt b/tutorial19_Geometry_Normal_Shader/Readme.txt new file mode 100644 index 000000000..2aca29908 --- /dev/null +++ b/tutorial19_Geometry_Normal_Shader/Readme.txt @@ -0,0 +1,5 @@ +The tutorial contains geometry shaders aimed to generate and verify the correct disposition of the normals of a uv wrapped texture on a 3d object. +It contains 3 shaders +vertex shader +geometry shader +fragement shader \ No newline at end of file diff --git a/tutorial19_Geometry_Normal_Shader/model-geometry-fragment-normal.frag b/tutorial19_Geometry_Normal_Shader/model-geometry-fragment-normal.frag new file mode 100644 index 000000000..acd9b06c3 --- /dev/null +++ b/tutorial19_Geometry_Normal_Shader/model-geometry-fragment-normal.frag @@ -0,0 +1,16 @@ + +// Fragment shader for simple coloured rendering of the normals + +#version 330 core +//declaring the vec4 color variable + +out vec4 FragColor; + + + +void main() +{ + //assigning the fragment shader with the vec4 color. + + FragColor = vec4(1.0,1.0,0.5,1.0); +} \ No newline at end of file diff --git a/tutorial19_Geometry_Normal_Shader/model-geometry-shader-normal.gs b/tutorial19_Geometry_Normal_Shader/model-geometry-shader-normal.gs new file mode 100644 index 000000000..aad6ae922 --- /dev/null +++ b/tutorial19_Geometry_Normal_Shader/model-geometry-shader-normal.gs @@ -0,0 +1,43 @@ +//geometry shader to draw the normals + +#version 330 core + +//Since the normals are to be drawn at the 3 vertices of the raster triangle,the vertices have an upper bound of 3 +//line_strip specifies the normals to be drawn adjacent to each other on each of the 3 vertices of a triangle + + +layout(triangles) in; +layout(line_strip,max_vertices=3) out; + +//interface block inherited from the vertex shader + +in VS_OUT{ + +vec3 normal; + +}gs_in[]; + +//magnitude of the length of the normals +float magnitude=0.4; + +//generator function which will assign the normals based on the 3 indices (0,1,2) of each triangle +void generateLine(int index) +{ +//gets the position and draws the normal from the vertices + gl_Position=gl_in[index].gl_Position; + + + EmitVertex(); + gl_Position=gl_in[index].gl_Position + vec4(gs_in[index].normal,0.0)*magnitude; + EmitVertex(); + EndPrimitive(); + +} + +void main() +{ +//line is generated from the vertices + generateLine(0); + generateLine(1); + generateLine(2); +} diff --git a/tutorial19_Geometry_Normal_Shader/model-geometry-vertex-normal.vs b/tutorial19_Geometry_Normal_Shader/model-geometry-vertex-normal.vs new file mode 100644 index 000000000..08577ce22 --- /dev/null +++ b/tutorial19_Geometry_Normal_Shader/model-geometry-vertex-normal.vs @@ -0,0 +1,38 @@ +//geometry shader used for debugging the general position of the normals of the texture wrapping. +//Used to decide whether the normals have been loaded properly. + +#version 330 core + +//vertex attributes position and normals + +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aNormal; + +//interface block to be passed to the geometry shader +//passing only the normal attribute to the geometry shader + + +out VS_OUT{ + + vec3 normal; +}vs_out; + +//MVP matrix loading + + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void main() +{ + //generate normal matrix to convert to NDC (Normalized Device Coordinates) + + mat3 normalMatrix= mat3(transpose(inverse(view*model))); + +//normalizing the normals of the vertices + + vs_out.normal=normalize(vec3(projection * vec4(normalMatrix * aNormal,0.0))); + + gl_Position = projection * view * model * vec4(aPos, 1.0); +} \ No newline at end of file