132001f49Smrg/* Test fragment shader */ 232001f49Smrg 332001f49Smrg#include <assert.h> 432001f49Smrg#include <string.h> 532001f49Smrg#include <stdio.h> 632001f49Smrg#include <stdlib.h> 732001f49Smrg#include <math.h> 832001f49Smrg#include <GL/glew.h> 932001f49Smrg#include "glut_wrap.h" 1032001f49Smrg 1132001f49Smrg 1232001f49Smrgstatic GLuint fragShader; 1332001f49Smrgstatic GLuint vertShader; 1432001f49Smrgstatic GLuint program; 1532001f49Smrgstatic GLint win = 0; 1632001f49Smrgstatic GLfloat xpos = 0, ypos = 0; 1732001f49Smrg 1832001f49Smrg 1932001f49Smrgstatic void 2032001f49SmrgRedisplay(void) 2132001f49Smrg{ 2232001f49Smrg glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 2332001f49Smrg 2432001f49Smrg glPushMatrix(); 2532001f49Smrg glTranslatef(xpos, ypos, 0); 2632001f49Smrg 2732001f49Smrg glBegin(GL_TRIANGLES); 2832001f49Smrg glColor3f(1, 0, 0); 2932001f49Smrg glVertex2f(-0.9, -0.9); 3032001f49Smrg glColor3f(0, 1, 0); 3132001f49Smrg glVertex2f( 0.9, -0.9); 3232001f49Smrg glColor3f(0, 0, 1); 3332001f49Smrg glVertex2f( 0, 0.9); 3432001f49Smrg glEnd(); 3532001f49Smrg 3632001f49Smrg glPopMatrix(); 3732001f49Smrg 3832001f49Smrg glutSwapBuffers(); 3932001f49Smrg} 4032001f49Smrg 4132001f49Smrg 4232001f49Smrgstatic void 4332001f49SmrgReshape(int width, int height) 4432001f49Smrg{ 4532001f49Smrg glViewport(0, 0, width, height); 4632001f49Smrg glMatrixMode(GL_PROJECTION); 4732001f49Smrg glLoadIdentity(); 4832001f49Smrg glOrtho(-1, 1, -1, 1, -1, 1); 4932001f49Smrg glMatrixMode(GL_MODELVIEW); 5032001f49Smrg glLoadIdentity(); 5132001f49Smrg} 5232001f49Smrg 5332001f49Smrg 5432001f49Smrgstatic void 5532001f49SmrgCleanUp(void) 5632001f49Smrg{ 5732001f49Smrg glDeleteShader(fragShader); 5832001f49Smrg glDeleteShader(vertShader); 5932001f49Smrg glDeleteProgram(program); 6032001f49Smrg glutDestroyWindow(win); 6132001f49Smrg} 6232001f49Smrg 6332001f49Smrg 6432001f49Smrgstatic void 6532001f49SmrgKey(unsigned char key, int x, int y) 6632001f49Smrg{ 6732001f49Smrg (void) x; 6832001f49Smrg (void) y; 6932001f49Smrg 7032001f49Smrg switch(key) { 7132001f49Smrg case 27: 7232001f49Smrg CleanUp(); 7332001f49Smrg exit(0); 7432001f49Smrg break; 7532001f49Smrg } 7632001f49Smrg glutPostRedisplay(); 7732001f49Smrg} 7832001f49Smrg 7932001f49Smrg 8032001f49Smrgstatic void 8132001f49SmrgSpecialKey(int key, int x, int y) 8232001f49Smrg{ 8332001f49Smrg const GLfloat step = 0.1; 8432001f49Smrg 8532001f49Smrg (void) x; 8632001f49Smrg (void) y; 8732001f49Smrg 8832001f49Smrg switch(key) { 8932001f49Smrg case GLUT_KEY_UP: 9032001f49Smrg ypos += step; 9132001f49Smrg break; 9232001f49Smrg case GLUT_KEY_DOWN: 9332001f49Smrg ypos -= step; 9432001f49Smrg break; 9532001f49Smrg case GLUT_KEY_LEFT: 9632001f49Smrg xpos -= step; 9732001f49Smrg break; 9832001f49Smrg case GLUT_KEY_RIGHT: 9932001f49Smrg xpos += step; 10032001f49Smrg break; 10132001f49Smrg } 10232001f49Smrg glutPostRedisplay(); 10332001f49Smrg} 10432001f49Smrg 10532001f49Smrg 10632001f49Smrgstatic void 10732001f49SmrgLoadAndCompileShader(GLuint shader, const char *text) 10832001f49Smrg{ 10932001f49Smrg GLint stat; 11032001f49Smrg 11132001f49Smrg glShaderSource(shader, 1, (const GLchar **) &text, NULL); 11232001f49Smrg 11332001f49Smrg glCompileShader(shader); 11432001f49Smrg 11532001f49Smrg glGetShaderiv(shader, GL_COMPILE_STATUS, &stat); 11632001f49Smrg if (!stat) { 11732001f49Smrg GLchar log[1000]; 11832001f49Smrg GLsizei len; 11932001f49Smrg glGetShaderInfoLog(shader, 1000, &len, log); 12032001f49Smrg fprintf(stderr, "fslight: problem compiling shader:\n%s\n", log); 12132001f49Smrg exit(1); 12232001f49Smrg } 12332001f49Smrg} 12432001f49Smrg 12532001f49Smrg 12632001f49Smrgstatic void 12732001f49SmrgCheckLink(GLuint prog) 12832001f49Smrg{ 12932001f49Smrg GLint stat; 13032001f49Smrg glGetProgramiv(prog, GL_LINK_STATUS, &stat); 13132001f49Smrg if (!stat) { 13232001f49Smrg GLchar log[1000]; 13332001f49Smrg GLsizei len; 13432001f49Smrg glGetProgramInfoLog(prog, 1000, &len, log); 13532001f49Smrg fprintf(stderr, "Linker error:\n%s\n", log); 13632001f49Smrg } 13732001f49Smrg} 13832001f49Smrg 13932001f49Smrg 14032001f49Smrgstatic void 14132001f49SmrgInit(void) 14232001f49Smrg{ 14332001f49Smrg /* fragment color is a function of fragment position: */ 14432001f49Smrg static const char *fragShaderText = 14532001f49Smrg "void main() {\n" 14632001f49Smrg " gl_FragColor = gl_FragCoord * vec4(0.005); \n" 14732001f49Smrg " //gl_FragColor = gl_Color; \n" 14832001f49Smrg " //gl_FragColor = vec4(1, 0, 0.5, 0); \n" 14932001f49Smrg "}\n"; 15032001f49Smrg#if 0 15132001f49Smrg static const char *vertShaderText = 15232001f49Smrg "varying vec3 normal;\n" 15332001f49Smrg "void main() {\n" 15432001f49Smrg " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" 15532001f49Smrg " normal = gl_NormalMatrix * gl_Normal;\n" 15632001f49Smrg "}\n"; 15732001f49Smrg#endif 15832001f49Smrg 15932001f49Smrg if (!GLEW_VERSION_2_0) { 16032001f49Smrg printf("This program requires OpenGL 2.x\n"); 16132001f49Smrg exit(1); 16232001f49Smrg } 16332001f49Smrg 16432001f49Smrg fragShader = glCreateShader(GL_FRAGMENT_SHADER); 16532001f49Smrg LoadAndCompileShader(fragShader, fragShaderText); 16632001f49Smrg 16732001f49Smrg#if 0 16832001f49Smrg vertShader = glCreateShader(GL_VERTEX_SHADER); 16932001f49Smrg LoadAndCompileShader(vertShader, vertShaderText); 17032001f49Smrg#endif 17132001f49Smrg 17232001f49Smrg program = glCreateProgram(); 17332001f49Smrg glAttachShader(program, fragShader); 17432001f49Smrg#if 0 17532001f49Smrg glAttachShader(program, vertShader); 17632001f49Smrg#endif 17732001f49Smrg glLinkProgram(program); 17832001f49Smrg CheckLink(program); 17932001f49Smrg glUseProgram(program); 18032001f49Smrg 18132001f49Smrg assert(glGetError() == 0); 18232001f49Smrg 18332001f49Smrg glClearColor(0.3f, 0.3f, 0.3f, 0.0f); 18432001f49Smrg 18532001f49Smrg printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); 18632001f49Smrg} 18732001f49Smrg 18832001f49Smrg 18932001f49Smrgint 19032001f49Smrgmain(int argc, char *argv[]) 19132001f49Smrg{ 19232001f49Smrg glutInit(&argc, argv); 19332001f49Smrg glutInitWindowPosition( 0, 0); 19432001f49Smrg glutInitWindowSize(200, 200); 19532001f49Smrg glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); 19632001f49Smrg win = glutCreateWindow(argv[0]); 19732001f49Smrg glewInit(); 19832001f49Smrg glutReshapeFunc(Reshape); 19932001f49Smrg glutKeyboardFunc(Key); 20032001f49Smrg glutSpecialFunc(SpecialKey); 20132001f49Smrg glutDisplayFunc(Redisplay); 20232001f49Smrg Init(); 20332001f49Smrg glutMainLoop(); 20432001f49Smrg return 0; 20532001f49Smrg} 20632001f49Smrg 20732001f49Smrg 208