Course list http://www.c-jump.com/bcc/
|
|
All objects (aka models or meshes) are made from triangles
Note: OpenGL does not support polygons with more than 3 vertices (aka quads and others)
|
|
Shader has source code (plain text file):
Cg (C for Graphics) -- shading language developed by Nvidia and Microsoft -- DirectX
HLSL (High-level shader language) -- developed by Microsoft -- Direct3D
GLSL -- OpenGL Shading Language -- very C-like
Your program needs at least two shaders (vertex and fragment), but can use many more
Shaders are compiled into a binary program in memory
Vertex shader
Changes the position of a vertex (translate/rotate/scale/skew)
May determine color of the vertex
Fragment shader
Determines the color of a pixel
Calculates lighting using materials, normals, and textures
Compile a vertex shader -> returns shader ID
Compile a fragment shader -> returns shader ID
Check for compilation errors
Create a shader program -> returns program ID
Retain this program ID in your code
Attach to a vertex and fragemnt shaders to the program
Link those two shaders together
Use program ID before you render triangles
Separate shaders may be used for different objects (see next slide)
Using different shaders yields different results:
Vertex shader:
#version 300
in vec4 s_vPosition;
void main () {
// The value of s_vPosition should be between -1.0 and +1.0
gl_Position = s_vPosition;
}
Fragment shader:
#version 300
out vec4 s_vColor;
void main () {
// All pixels will be red:
fColor = vec4( 1.0, 0.0, 0.0, 1.0 );
}
Each shader has to begin with the version directive:
#version 300
The the version indicates minimal version of GLSL language expected by the shader code. The following table provides correspondence between OpenGL and GLSL versions:
_____________________________________ OpenGL GLSL Directive version version to use -------- -------- ------------- 3.0 1.3 #version 130 3.1 1.4 #version 140 3.2 1.5 #version 150 3.3 3.3 #version 330 4.0 4.0 #version 400 // Since March 2010 versions match 4.1 4.1 #version 410 ... ... ... _____________________________________
These functions are invoked during main initialization:
GLuint makeVertexShader(const char* shaderSource) { GLuint vertexShaderID = glCreateShader( GL_VERTEX_SHADER ); glShaderSource( vertexShaderID, 1, (const GLchar**)&shaderSource, NULL ); glCompileShader( vertexShaderID ); return vertexShaderID; } GLuint makeFragmentShader(const char* shaderSource) { GLuint fragmentShaderID = glCreateShader( GL_FRAGMENT_SHADER ); glShaderSource( fragmentShaderID, 1, (const GLchar**)&shaderSource, NULL ); glCompileShader( fragmentShaderID ); return fragmentShaderID; } GLuint makeShaderProgram (GLuint vertexShaderID, GLuint fragmentShaderID) { GLuint shaderProgramID = glCreateProgram(); glAttachShader( shaderProgramID, vertexShaderID ); glAttachShader( shaderProgramID, fragmentShaderID ); glLinkProgram( shaderProgramID ); return shaderProgramID; }
When rendering (or any time you want to activate the program), call
glUseProgram( shaderProgramID );
const GLchar* VertexShader = { "#version 400\n"\ "layout(location=0) in vec4 in_Position;\n"\ "layout(location=1) in vec4 in_Color;\n"\ "out vec4 ex_Color;\n"\ "void main(void)\n"\ "{\n"\ " gl_Position = in_Position;\n"\ " ex_Color = in_Color;\n"\ "}\n" }; const GLchar* FragmentShader = { "#version 400\n"\ "in vec4 ex_Color;\n"\ "out vec4 out_Color;\n"\ "void main(void)\n"\ "{\n"\ " out_Color = ex_Color;\n"\ "}\n" };