static void TestFunctions(void) { printf("Error 0x%x at line %d\n", glGetError(), __LINE__); { GLfloat pos[3]; printf("Error 0x%x at line %d\n", glGetError(), __LINE__); printf("Light pos %g %g %g\n", pos[0], pos[1], pos[2]); } { GLfloat m[16], result[16]; GLint mPos; int i; for (i = 0; i < 16; i++) m[i] = (float) i; mPos = glGetUniformLocation_func(program, "m"); printf("Error 0x%x at line %d\n", glGetError(), __LINE__); glUniformMatrix4fv_func(mPos, 1, GL_FALSE, m); printf("Error 0x%x at line %d\n", glGetError(), __LINE__); glGetUniformfv_func(program, mPos, result); printf("Error 0x%x at line %d\n", glGetError(), __LINE__); for (i = 0; i < 16; i++) { printf("%8g %8g\n", m[i], result[i]); } } assert(glIsProgram_func(program)); assert(glIsShader_func(fragShader)); assert(glIsShader_func(vertShader)); /* attached shaders */ { GLuint shaders[20]; GLsizei count; int i; glGetAttachedShaders_func(program, 20, &count, shaders); for (i = 0; i < count; i++) { printf("Attached: %u\n", shaders[i]); assert(shaders[i] == fragShader || shaders[i] == vertShader); } } { GLchar log[1000]; GLsizei len; glGetShaderInfoLog_func(vertShader, 1000, &len, log); printf("Vert Shader Info Log: %s\n", log); glGetShaderInfoLog_func(fragShader, 1000, &len, log); printf("Frag Shader Info Log: %s\n", log); glGetProgramInfoLog_func(program, 1000, &len, log); printf("Program Info Log: %s\n", log); } }
void ShaderAPITest::test_uniform_query_matrix(void) { GLuint program; GLfloat data[18]; GLint i, r, c; GLint location; program = make_program("#version 110\nuniform mat3 m[2];\nvoid main() { gl_Position.xyz = m[1][2]; }\n", NULL); location = glGetUniformLocation_func(program, "m"); for (i = 0; i < 9; i++) data[i] = i; for (i = 9; i < 18; i++) data[i] = 321.0; glUniformMatrix3fv_func(location, 1, GL_TRUE, data); for (i = 0; i < 18; i++) data[i] = 123.0; glGetUniformfv_func(program, location, data); for (c = 0; c < 3; c++) for (r = 0; r < 3; r++) assert(data[c * 3 + r] == r * 3 + c); for (i = 9; i < 18; i++) assert(data[i] == 123.0); }
void ShaderAPITest::test_uniform_scalar_count(void) { GLuint program; GLint location; GLfloat data[128]; program = make_program("#version 110\nuniform vec2 x;\nvoid main() { gl_Position.xy = x; }\n", NULL); location = glGetUniformLocation_func(program, "x"); assert_no_error(); glUniform2fv_func(location, 64, data); assert_error(GL_INVALID_OPERATION); }
void ShaderAPITest::test_uniform_array_overflow(void) { GLuint program; GLint location; GLfloat data[128]; program = make_program("#version 120\nuniform vec2 x[10];\nvoid main() { gl_Position.xy = x[9]; }\n", NULL); location = glGetUniformLocation_func(program, "x"); assert_no_error(); glUniform2fv_func(location, 64, data); assert_no_error(); }
void ShaderAPITest::test_uniform_multiple_samplers(void) { GLuint program; GLint location; GLint values[2] = {0, 1}; assert_no_error(); program = make_program(NULL, "uniform sampler2D s[2];\nvoid main() { gl_FragColor = texture2D(s[1], vec2(0.0, 0.0)); }\n"); location = glGetUniformLocation_func(program, "s[0]"); assert(location != -1); assert_no_error(); glUniform1iv_func(location, 2, values); assert_no_error(); }
void ShaderAPITest::test_uniform_bool_conversion(void) { GLuint program; GLint location; GLint value[16]; /* in case glGetUniformiv goes nuts on the stack */ assert_no_error(); program = make_program("uniform bool b;\nvoid main() { gl_Position.x = b ? 1.5 : 0.5; }\n", NULL); location = glGetUniformLocation_func(program, "b"); assert(location != -1); assert_no_error(); glUniform1i_func(location, 5); assert_no_error(); glGetUniformiv_func(program, location, &value[0]); assert_no_error(); assert(value[0] == 1); }
static void Init(void) { const char *version; GLint i; version = (const char *) glGetString(GL_VERSION); if (version[0] != '2' || version[1] != '.') { printf("Warning: this program expects OpenGL 2.0\n"); /*exit(1);*/ } GetExtensionFuncs(); vertShader = glCreateShader_func(GL_VERTEX_SHADER); ReadShader(vertShader, VertProgFile); fragShader = glCreateShader_func(GL_FRAGMENT_SHADER); ReadShader(fragShader, FragProgFile); program = glCreateProgram_func(); glAttachShader_func(program, fragShader); glAttachShader_func(program, vertShader); glLinkProgram_func(program); CheckLink(program); glUseProgram_func(program); for (i = 0; Uniforms[i].name; i++) { Uniforms[i].location = glGetUniformLocation_func(program, Uniforms[i].name); printf("Uniform %s location: %d\n", Uniforms[i].name, Uniforms[i].location); switch (Uniforms[i].size) { case 1: glUniform1fv_func(Uniforms[i].location, 1, Uniforms[i].value); break; case 2: glUniform2fv_func(Uniforms[i].location, 1, Uniforms[i].value); break; case 3: glUniform3fv_func(Uniforms[i].location, 1, Uniforms[i].value); break; case 4: glUniform4fv_func(Uniforms[i].location, 1, Uniforms[i].value); break; default: abort(); } } assert(glGetError() == 0); glClearColor(0.4f, 0.4f, 0.8f, 0.0f); printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); assert(glIsProgram_func(program)); assert(glIsShader_func(fragShader)); assert(glIsShader_func(vertShader)); glColor3f(1, 0, 0); }
static void Init(void) { static const char *fragShaderText = "uniform vec4 diffuse;\n" "uniform vec4 specular;\n" "varying vec3 normal;\n" "void main() {\n" " // Compute dot product of light direction and normal vector\n" " float dotProd = max(dot(gl_LightSource[0].position.xyz, \n" " normalize(normal)), 0.0);\n" " // Compute diffuse and specular contributions\n" " gl_FragColor = diffuse * dotProd + specular * pow(dotProd, 20.0);\n" "}\n"; static const char *vertShaderText = "varying vec3 normal;\n" "void main() {\n" " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" " normal = gl_NormalMatrix * gl_Normal;\n" "}\n"; const char *version; version = (const char *) glGetString(GL_VERSION); if (version[0] != '2' || version[1] != '.') { printf("This program requires OpenGL 2.x, found %s\n", version); exit(1); } GetExtensionFuncs(); fragShader = glCreateShader_func(GL_FRAGMENT_SHADER); if (FragProgFile) ReadShader(fragShader, FragProgFile); else LoadAndCompileShader(fragShader, fragShaderText); vertShader = glCreateShader_func(GL_VERTEX_SHADER); if (VertProgFile) ReadShader(vertShader, VertProgFile); else LoadAndCompileShader(vertShader, vertShaderText); program = glCreateProgram_func(); glAttachShader_func(program, fragShader); glAttachShader_func(program, vertShader); glLinkProgram_func(program); CheckLink(program); glUseProgram_func(program); uDiffuse = glGetUniformLocation_func(program, "diffuse"); uSpecular = glGetUniformLocation_func(program, "specular"); uTexture = glGetUniformLocation_func(program, "texture"); printf("DiffusePos %d SpecularPos %d TexturePos %d\n", uDiffuse, uSpecular, uTexture); glUniform4fv_func(uDiffuse, 1, diffuse); glUniform4fv_func(uSpecular, 1, specular); /* assert(glGetError() == 0);*/ glUniform1i_func(uTexture, 2); /* use texture unit 2 */ /*assert(glGetError() == 0);*/ if (CoordAttrib) { int i; glBindAttribLocation_func(program, CoordAttrib, "coord"); i = glGetAttribLocation_func(program, "coord"); assert(i >= 0); if (i != CoordAttrib) { printf("Hmmm, NVIDIA bug?\n"); CoordAttrib = i; } else { printf("Mesa bind attrib: coord = %d\n", i); } } /*assert(glGetError() == 0);*/ glClearColor(0.3f, 0.3f, 0.3f, 0.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular); glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 20.0f); MakeSphere(); MakeRect(); CurList = SphereList; #if TEXTURE MakeTexture(); #endif printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); printf("Press p to toggle between per-pixel and per-vertex lighting\n"); /* test glGetShaderSource() */ if (0) { GLsizei len = strlen(fragShaderText) + 1; GLsizei lenOut; GLchar *src =(GLchar *) malloc(len * sizeof(GLchar)); glGetShaderSource_func(fragShader, 0, NULL, src); glGetShaderSource_func(fragShader, len, &lenOut, src); assert(len == lenOut + 1); assert(strcmp(src, fragShaderText) == 0); free(src); } assert(glIsProgram_func(program)); assert(glIsShader_func(fragShader)); assert(glIsShader_func(vertShader)); glColor3f(1, 0, 0); /* for testing state vars */ { static GLfloat fc[4] = { 1, 1, 0, 0 }; static GLfloat amb[4] = { 1, 0, 1, 0 }; glFogfv(GL_FOG_COLOR, fc); glLightfv(GL_LIGHT1, GL_AMBIENT, amb); } #if 0 TestFunctions(); #else (void) TestFunctions; #endif }