Lecture 31 Sections 9.1, 9.2, 9.3 Hampden-Sydney College Mon, Nov 23, 2009
Outline 1 2 3 4 5
Outline 1 2 3 4 5
The extension wrangler (glew) will check to see which extensions are supported by the installed OpenGL library. The vertex and fragment shaders were built-in (not extensions) in OpenGL 2.0 and higher. For earlier versions, the extension wrangler is needed to Check whether vertex and fragment shader extensions are available. Provide an interface for the function calls. All function names in the extension end with ARB.
Initialize the Extension Initialize the Extension GLenum err = glewinit(); First, initialize glew.
Check the Initialization Check the Initialization if (GLEW_OK!= err) cout << ": " << glewgetstring(err) << endl; else cout << "Status: Using GLEW " << glewgetstring(glew_version) << endl; Check whether the initialization was successful.
Check for Extensions Check for Extensions if (GLEW_ARB_vertex_shader GLEW_ARB_fragment_shader) cout << "Ready for GLSL" << endl; else cout << "Not ready for GLSL" << endl; Check whether the initialization was successful.
Check for OpenGL 2.0 Support Check for OpenGL 2.0 Support if (glewissupported("gl_version_2_0")) cout << "Ready for OpenGL 2.0" << endl; else cout << "OpenGL 2.0 not supported" << endl; Check whether the initialization was successful.
Outline 1 2 3 4 5
GLSL Data Types Vertex glcreateobject() glattachobject() glattachobject() glcreateobject() readsource() glsource() glcompile() Fragment glcreateobject() void main() { gl_position =... } gllink() readsource() void main() { gl_fragcolor =... } gluse() glsource() glcompile()
Creating a The shader programs are written as text files. The application program will read and compile the shader programs. First, store the source code in a text file. Example ( ) int main() { gl_position = gl_modelviewprojectionmatrix*gl_vertex; }
Creating a Read the source code from the text file. Store it in an array of strings. Read the char* readsource(char* file_name); file_name Name of the text file that contains the source code. Return value A pointer the string of source code.
Creating a Example (Read the ) char* vsource = readsource(vfile);
Create a Then, create a shader object. glcreateobjectarb() unsigned int glcreateobjectarb(int shader_type); shader_type GL_VERTEX_SHADER or GL_FRAGMENT_SHADER. Return value A reference number of that shader object.
Create a Example (Create a ) char* vsource = readsource(vfile); unsigned int v = glcreateobjectarb(gl_vertex_shader);
Compiling a Next, pass the source code to the shader object. glsourcearb() glsourcearb(int shader, int num_strs, const char** source, int* len); shader Reference number of the shader object. num_strs Number of strings in the string array of source code. source Array of strings of source code. len Lengths of the string. If len is NULL, then the strings are null-terminated.
Create a Example (Create a ) char* vsource = readsource(vfile); unsigned int v = glcreateobjectarb(gl_vertex_shader); glsourcearb(v, 1, (const char**) &vsource, NULL);
Compiling a Then compile the source code. glcompilearb() glcompilearb(int shader); shader Reference number for the shader object.
Compiling a Example (Compiling a ) char* vsource = readsource(vfile); unsigned int v = glcreateobjectarb(gl_vertex_shader); glsourcearb(v, 1, (const char**) &vsource, NULL); glcompilearb(v);
Creating a Fragment Do all of these steps for both the vertex shader and the fragment shader.
Compiling a Example (Compiling a ) // Compile vertex shader char* vsource = readsource(vfile); unsigned int v = glcreateobjectarb(gl_vertex_shader); glsourcearb(v, 1, (const char**) &vsource, NULL); glcompilearb(v); // Compile fragment shader char* fsource = readsource(ffile); unsigned int f = glcreateobjectarb(gl_fragment_shader); glsourcearb(f, 1, (const char**) &fsource, NULL); glcompilearb(f);
Outline 1 2 3 4 5
Finally, create a program object. Attach the compiled shader program to the program object. Link the program object to the application program. Make the program object active.
Create a Object Create a Object unsigned int glcreateobjectarb(); Return value A reference number for the program object.
Attach a to a Object Attach a to a Object glattachobjectarb(int prog_obj, int shader_obj); prog_obj The reference number of the program object. shader_obj The reference number of the shader object.
Object Object gllinkarb(progobj); prog_obj The reference number of the program object.
Using a Object Using a Object gluseobjectarb(progobj); prog_obj The reference number of the program object.
Compiling a Example (Compiling a ) char* vsource = readsource(vfile); unsigned int v = glcreateobjectarb(gl_vertex_shader); glsourcearb(v, 1, (const char**) &vsource, NULL); glcompilearb(v); char* fsource = readsource(ffile); unsigned int f = glcreateobjectarb(gl_fragment_shader); glsourcearb(f, 1, (const char**) &fsource, NULL); glcompilearb(f); myprogobj = glcreateobjectarb(); glattachobjectarb(myprogobj, v); glattachobjectarb(myprogobj, f); gllinkarb(myprogobj); gluseobjectarb(myprogobj);
The vertex shader. The fragment shader. The application code. The executable.
Outline 1 2 3 4 5
If there are compilation errors, the application program will run, but OpenGL will revert to the fixed pipeline. If there are errors, it is extremely helpful to get error messages. To get the error messages, we must use the function glgetinfologarb.
glgetinfologarb(unsigned int shader, int buf_size, int* msg_size, char* buffer); shader The reference number of the shader object. buf_size The size of the message buffer. msg_size The size of the message (returned). buffer The message (returned).
Example (Compiling a ) char* vsource = readsource(vfile); unsigned int v = glcreateobjectarb(gl_vertex_shader); glsourcearb(v, 1, (const char**) &vsource, NULL); glcompilearb(v); const int size = 1000; char msgbuffer[size]; int msglen; glgetinfologarb(v, size - 1, &msglen, msgbuffer); msgbuffer[msglen] = 0; if (msglen > 0) { cout << "Vertex shader:" << endl; cout << msgbuffer << endl; }
The vertex shader. The fragment shader. The application code. The executable.
Outline 1 2 3 4 5
Homework Homework Read Section 9.1 programmable graphics cards. Read Section 9.2 shading languages. Read Section 9.3 extensions of OpenGL.