GLSL II Objectives Coupling GLSL to Applications Example

GLSL II

Objectives • Coupling GLSL to Applications • Example applications GLSL 2 2

Linking Shaders to Open. GL • Open. GL Extensions ARB_shader_objects ARB_vertex_shader ARB_fragment_shader • Open. GL 2. 0 Almost identical to using extensions Avoids extension suffixes on function names GLSL 2 3

Program Object • Container for shaders Can contain multiple shaders Other GLSL functions GLuint my. Prog. Obj; my. Prog. Obj = gl. Create. Program(); /* define shader objects here */ gl. Use. Program(my. Prog. Obj); gl. Link. Program(my. Prog. Obj); GLSL 2 4

Reading a Shader • Shader added to program object and compiled • Usual method of passing shader null terminated string using function gl. Shader. Source • If shader in file can write reader to convert file to string GLSL 2 5

Shader Reader #include <stdio. h> char* read. Shader. Source(const char* shader. File) { FILE* fp = fopen(shader. File, "r"); char* buf; long size; if(fp == NULL) return(NULL); fseek(fp, 0 L, SEEK_END); /* end of file */ size = ftell(fp); fseek(fp, )L, SEEK_SET); /* start of file */ GLSL 2 6

Shader Reader (cont) • buf = (char*) malloc(stat. Buf. st_size + 1 * sizeof(char)); fread(buf, 1, size, fp); buf[size] = '