Programmable Shaders II - Surface Rendering - 12 th Week, 2008 Sun-Jeong Kim Downloading 2
GLEW OpenGL Extension Wrangler Library http://glew.sourceforge.net/ Cross-platform open-source C/C++ extension loading library Windows, Linux, Mac OS, FreeBSD, Irix, and Solaris Support for OpenGL core and extensions 3DFX_*, APPLE_*, ARB_*, ATI_*, EXT_*, MESA_*, NV_*, SGI_*, SUN_*, WIN_* 3 glew.zip C: Program Files Microsoft Visual Studio 8 VC PlatformSDK C: WINDOWS 4
glewinfo.txt 5 visualinfo.txt 6
Linking Shaders to OpenGL OpenGL Extensions ARB_shader_objects ARB_vertex_shader ARB_fragment_shader OpenGL 2.0 Almost identical to using extensions Avoid extension suffixes on function names 7 Container for shaders Program Object Can contain multiple shaders Other GLSL functions GLuint myprogobj; myprogobj = glcreateprogram(); // define shader objects here gluseprogram( g myprogobj ); gllinkprogram( myprogobj ); 8
Reading a Shader Shaders are added to the program object and compiled Usual method of passing a shader is as a nullterminated string using the function glshadersource If the shader is in a file, we can write a reader to convert the file to a string 9 Shader Reader char *ReadTextFile( char *filename ) { FILE *fp; char *content = NULL; int count = 0; if( filename!= NULL ) { fp = fopen( filename, "rt" ); if( fp!= NULL ) { fseek( fp, 0, SEEK_END END ); count = ftell( fp ); rewind( fp ); } if( count > 0 ) { content = (char *)malloc( sizeof(char) * (count+1) ); count = fread( content, sizeof(char), count, fp ); content[count] = '\0'; } fclose(fp); } } return content; 10
Adding Shaders GLuint vshader = glcreateshader( GL_VERTEX_SHADER ); GLuint fshader = glcreateshader( GL_FRAGMENT_SHADER ); char *vsource = ReadTextFile( "simple.vert" ); char *fsource = ReadTextFile( "simple.frag" ); const char *vv = vsource; const char *ff = fsource; glshadersource( vshader, 1, &vv, NULL ); glshadersource( fshader, 1, &ff, NULL ); free( vsource ); free( fsource ); glcompileshader( vshader ); glcompileshader( fshader ); glattachshader( myprogobj, vshader ); glattachshader( myprogobj, fshader ); 11 New Project 12
Header Files & Global Variables 13 Initialization 14
Creating Shaders 15 Reading Shader Files 16
Vertex Shader Fixed functionality 17 Fragment Shader 18
Result Fixed Pipeline 19 Setting Shaders 20
Result Programmable Pipeline 21 Using Shaders 22
Result Using Shaders 23 Specifying Vertex Attributes & Uniform Variables Vertex attributes are named in shaders gl_vertex, gl_color, gl_normal, Linker forms a table Application can get index from table and tie it to an application variable Similar process to uniform variables 24
Flatten Shader (1) 25 Flatten Shader (2) 26
Flatten Shader (3) 27 Flatten Shader (4) 28
Result Flatten Shader (1) 29 Flatten Shader (5) 30
Result Flatten Shader (2) 31 Flatten Shader (6) 32
Result Flatten Shader (3) 33 Flatten Shader (7) 34
Result Flatten Shader (4) 35 Flatten Shader (8) 36
Result Flatten Shader (5) 37 Phong Shading Directional Light (1) 38
Phong Shading Directional Light (2) Add New Item 39 Phong Shading Directional Light (3) 40
Phong Shading Directional Light (4) Add New Item 41 Phong Shading Directional Light (5) 42
Phong Shading Directional Light (6) 43 Result Phong Shading DL 44
Phong Shading Point Light (1) 45 Phong Shading Point Light (2) 46
Phong Shading Point Light (3) 47 Phong Shading Point Light (4) 48
Result Phong Shading PL 49 Exercises Spotlight 를이용한 Phong Shading 을구현하시오. 50