forked from CIS-566-Fall-2021/hw00-webgl-intro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4ae8b3
commit 22a93bf
Showing
5 changed files
with
94 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,58 @@ | ||
#version 300 es | ||
|
||
uniform mat4 u_Model; | ||
uniform mat4 u_ModelInvTr; | ||
uniform mat4 u_ViewProj; | ||
in vec4 vs_Pos; | ||
in vec4 vs_Nor; | ||
in vec4 vs_Col; | ||
out vec4 fs_Pos; | ||
out vec4 fs_Nor; | ||
out vec4 fs_LightVec; | ||
out vec4 fs_Col; | ||
|
||
const vec4 lightPos = vec4(5, 5, 3, 1); | ||
//This is a vertex shader. While it is called a "shader" due to outdated conventions, this file | ||
//is used to apply matrix transformations to the arrays of vertex data passed to it. | ||
//Since this code is run on your GPU, each vertex is transformed simultaneously. | ||
//If it were run on your CPU, each vertex would have to be processed in a FOR loop, one at a time. | ||
//This simultaneous transformation allows your program to run much faster, especially when rendering | ||
//geometry with millions of vertices. | ||
|
||
uniform mat4 u_Model; // The matrix that defines the transformation of the | ||
// object we're rendering. In this assignment, | ||
// this will be the result of traversing your scene graph. | ||
|
||
uniform mat4 u_ModelInvTr; // The inverse transpose of the model matrix. | ||
// This allows us to transform the object's normals properly | ||
// if the object has been non-uniformly scaled. | ||
|
||
uniform mat4 u_ViewProj; // The matrix that defines the camera's transformation. | ||
// We've written a static matrix for you to use for HW2, | ||
// but in HW3 you'll have to generate one yourself | ||
uniform highp int u_Time; | ||
in vec4 vs_Pos; // The array of vertex positions passed to the shader | ||
|
||
in vec4 vs_Nor; // The array of vertex normals passed to the shader | ||
|
||
in vec4 vs_Col; // The array of vertex colors passed to the shader. | ||
|
||
out vec4 fs_Pos; | ||
out vec4 fs_Nor; // The array of normals that has been transformed by u_ModelInvTr. This is implicitly passed to the fragment shader. | ||
out vec4 fs_LightVec; // The direction in which our virtual light lies, relative to each vertex. This is implicitly passed to the fragment shader. | ||
out vec4 fs_Col; // The color of each vertex. This is implicitly passed to the fragment shader. | ||
|
||
const vec4 lightPos = vec4(5, 5, 5, 1); //The position of our virtual light, which is used to compute the shading of | ||
//the geometry in the fragment shader. | ||
|
||
void main() | ||
{ | ||
fs_Pos = vs_Pos; | ||
fs_Col = vs_Col; // Pass the vertex colors to the fragment shader for interpolation | ||
fs_Col = vs_Col; // Pass the vertex colors to the fragment shader for interpolation | ||
|
||
mat3 invTranspose = mat3(u_ModelInvTr); | ||
fs_Nor = vec4(invTranspose * vec3(vs_Nor), 0); // Pass the vertex normals to the fragment shader for interpolation. | ||
// Transform the geometry's normals by the inverse transpose of the | ||
// model matrix. This is necessary to ensure the normals remain | ||
// perpendicular to the surface after the surface is transformed by | ||
// the model matrix. | ||
mat3 invTranspose = mat3(u_ModelInvTr); | ||
fs_Nor = vec4(invTranspose * vec3(vs_Nor), 0); // Pass the vertex normals to the fragment shader for interpolation. | ||
// Transform the geometry's normals by the inverse transpose of the | ||
// model matrix. This is necessary to ensure the normals remain | ||
// perpendicular to the surface after the surface is transformed by | ||
// the model matrix. | ||
|
||
|
||
vec4 modelposition = u_Model * vs_Pos; // Temporarily store the transformed vertex positions for use below | ||
vec4 modelposition = u_Model * vs_Pos; // Temporarily store the transformed vertex positions for use below | ||
fs_Pos = modelposition; | ||
|
||
modelposition.x += pow(sin((modelposition.y * 20.f + float(u_Time) * 0.05)), 2.0); | ||
modelposition.y += sin((modelposition.y * 10.f + float(u_Time) * 0.05)); | ||
|
||
fs_LightVec = lightPos - modelposition; // Compute the direction in which the light source lies | ||
fs_LightVec = lightPos - modelposition; // Compute the direction in which the light source lies | ||
|
||
gl_Position = u_ViewProj * modelposition;// gl_Position is a built-in variable of OpenGL which is | ||
// used to render the final positions of the geometry's vertices | ||
gl_Position = u_ViewProj * modelposition;// gl_Position is a built-in variable of OpenGL which is | ||
// used to render the final positions of the geometry's vertices | ||
} |