1 /* Test fragment shader */
14 static GLuint fragShader
;
15 static GLuint vertShader
;
16 static GLuint program
;
18 static GLfloat xpos
= 0, ypos
= 0;
24 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
27 glTranslatef(xpos
, ypos
, 0);
29 glBegin(GL_TRIANGLES
);
31 glVertex2f(-0.9, -0.9);
33 glVertex2f( 0.9, -0.9);
45 Reshape(int width
, int height
)
47 glViewport(0, 0, width
, height
);
48 glMatrixMode(GL_PROJECTION
);
50 glOrtho(-1, 1, -1, 1, -1, 1);
51 glMatrixMode(GL_MODELVIEW
);
59 glDeleteShader_func(fragShader
);
60 glDeleteShader_func(vertShader
);
61 glDeleteProgram_func(program
);
62 glutDestroyWindow(win
);
67 Key(unsigned char key
, int x
, int y
)
83 SpecialKey(int key
, int x
, int y
)
85 const GLfloat step
= 0.1;
109 LoadAndCompileShader(GLuint shader
, const char *text
)
113 glShaderSource_func(shader
, 1, (const GLchar
**) &text
, NULL
);
115 glCompileShader_func(shader
);
117 glGetShaderiv_func(shader
, GL_COMPILE_STATUS
, &stat
);
121 glGetShaderInfoLog_func(shader
, 1000, &len
, log
);
122 fprintf(stderr
, "fslight: problem compiling shader:\n%s\n", log
);
129 CheckLink(GLuint prog
)
132 glGetProgramiv_func(prog
, GL_LINK_STATUS
, &stat
);
136 glGetProgramInfoLog_func(prog
, 1000, &len
, log
);
137 fprintf(stderr
, "Linker error:\n%s\n", log
);
145 /* fragment color is a function of fragment position: */
146 static const char *fragShaderText
=
148 " gl_FragColor = gl_FragCoord * vec4(0.005); \n"
149 " //gl_FragColor = gl_Color; \n"
150 " //gl_FragColor = vec4(1, 0, 0.5, 0); \n"
153 static const char *vertShaderText
=
154 "varying vec3 normal;\n"
156 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
157 " normal = gl_NormalMatrix * gl_Normal;\n"
162 version
= (const char *) glGetString(GL_VERSION
);
163 if (version
[0] != '2' || version
[1] != '.') {
164 printf("This program requires OpenGL 2.x, found %s\n", version
);
170 fragShader
= glCreateShader_func(GL_FRAGMENT_SHADER
);
171 LoadAndCompileShader(fragShader
, fragShaderText
);
174 vertShader
= glCreateShader_func(GL_VERTEX_SHADER
);
175 LoadAndCompileShader(vertShader
, vertShaderText
);
178 program
= glCreateProgram_func();
179 glAttachShader_func(program
, fragShader
);
181 glAttachShader_func(program
, vertShader
);
183 glLinkProgram_func(program
);
185 glUseProgram_func(program
);
187 assert(glGetError() == 0);
189 glClearColor(0.3f
, 0.3f
, 0.3f
, 0.0f
);
191 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
196 main(int argc
, char *argv
[])
198 glutInit(&argc
, argv
);
199 glutInitWindowPosition( 0, 0);
200 glutInitWindowSize(200, 200);
201 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
202 win
= glutCreateWindow(argv
[0]);
203 glutReshapeFunc(Reshape
);
204 glutKeyboardFunc(Key
);
205 glutSpecialFunc(SpecialKey
);
206 glutDisplayFunc(Redisplay
);