|
Vertex Shaders
In "Modern" OpenGL |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
layout statement in the shader
(e.g., layout(location = 0) in vec4 position;)
in int gl_VertexID:
in int gl_InstanceID:
glGetUniformLocation() to get a handle/name/IDglUniform____() to initializeuniform type
identifier;
out type
identifier
statementvec4 gl_Position - The clip-space position
of the vertexfloat gl_PointSize - The width and height of a
pixel (only meaningful for point primitives)float gl_ClipDistance - Distance to the clipping
plane (negative for outside)
#version 300 es
/**
* A vertex shader that simply outputs the positions that
* are passed to it as input.
*
* @author Prof. David Bernstein, James Madison University
*/
/**
* Inputs that are different for each invocation of this shader.
*/
layout(location = 0) in vec3 position_modelspace; // The vertex in model space
/**
* Process the inputs and generate the outputs.
* In this case, the only output is the pre-defined
* variable gl_Position.
*/
void main(){
gl_Position.xyz = position_modelspace;
gl_Position.w = 1.0;
// Another way to accomplish the same thing
//gl_Position = vec4(position_modelspace, 1);
}
#version 300 es
/**
* A vertex shader that simply outputs the positions and
* colors that are passed to it as inputs.
*
* @author Prof. David Bernstein, James Madison University
*/
/**
* Inputs that are different for each invocation of this shader.
*/
layout(location = 0) in vec3 position_modelspace; // The position in model space
layout(location = 1) in vec3 color; // The color
/**
* Outputs.
*/
out vec3 fragmentColor;
/**
* Process the inputs and generate the outputs.
* In this case, the only output is the pre-defined
* variable gl_Position.
*/
void main(){
// Output the position of the vertex
gl_Position = vec4(position_modelspace, 1);
// Output the vertex color (which will be interpolated with the
// other vertex colors before it is given to the fragment)
fragmentColor = color;
}
#version 300 es
/**
* A vertex shader that applies the MVP transform to the
* position.
*
* @author Prof. David Bernstein, James Madison University
*/
/**
* Inputs that are different for each invocation of this shader.
*/
layout(location = 0) in vec3 position_modelspace; // The position in model space
/**
* Inputs that (may) stay constant for all vertices.
*/
uniform mat4 MVP; // The model-view-projection matrix
/**
* Process the inputs and generate the outputs.
* In this case, apply the MVP matrix.
*/
void main(){
gl_Position = MVP * vec4(position_modelspace, 1);
}
#version 300 es
/**
* A vertex shader that can be used with a fragment shader models lighting.
*
* Note: This vertex shader assumes that M is the identity matrix.
*
* @author Prof. David Bernstein, James Madison University
*/
precision mediump float;
/**
* Inputs that are different for each invocation of this shader.
*/
layout(location = 0) in vec3 position_modelspace; // The position in model space
layout(location = 1) in vec3 color; // The color
layout(location = 2) in vec3 normal_modelspace; // The normal at the vertex
/**
* Output data (which will be interpolated for each fragment)
*/
out vec3 vertexColor;
out vec3 position_worldspace;
out vec3 normal_cameraspace;
out vec3 eyeDirection_cameraspace;
out vec3 lightDirection_cameraspace;
/**
* Inputs that (may) stay constant for all vertices.
*/
uniform mat4 M; // The model matrix
uniform mat4 V; // The view matrix
uniform mat4 MVP; // The model-view-projection matrix
uniform vec3 lightPosition_worldspace; // The light position in world space
/**
* Process the inputs and generate the outputs.
*/
void main(){
// M is assumed to be the identity matrix!
// The transformed position of the vertex
gl_Position = MVP * vec4(position_modelspace, 1);
// Position of the vertex in worldspace
position_worldspace = (M * vec4(position_modelspace, 1)).xyz;
vec3 cameraposition_cameraspace = vec3(0,0,0);
vec3 position_cameraspace = ( V * M * vec4(position_modelspace,1)).xyz;
eyeDirection_cameraspace = cameraposition_cameraspace - position_cameraspace;
vec3 lightPosition_cameraspace = ( V * vec4(lightPosition_worldspace,1)).xyz;
lightDirection_cameraspace = lightPosition_cameraspace + eyeDirection_cameraspace;
// Note: This is only correct because M does not scale the model.
// Use its inverse transpose if M scales the model.
normal_cameraspace = ( V * M * vec4(normal_modelspace,0)).xyz;
vertexColor = color;
}