blob: 6e86df1dcfad329ec5d7d9a35ea5ccbd0e5356ab [file] [log] [blame]
Brian9110dbd2007-07-24 10:00:29 -06001/* Test fragment shader */
2
3#include <assert.h>
4#include <string.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <math.h>
José Fonseca9aa73cf2009-02-01 12:00:07 +00008#include <GL/glew.h>
Brian9110dbd2007-07-24 10:00:29 -06009#include <GL/glut.h>
Brian9110dbd2007-07-24 10:00:29 -060010
11
12static GLuint fragShader;
13static GLuint vertShader;
14static GLuint program;
15static GLint win = 0;
16static GLfloat xpos = 0, ypos = 0;
17
18
19static void
20Redisplay(void)
21{
22 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
23
24 glPushMatrix();
25 glTranslatef(xpos, ypos, 0);
26
27 glBegin(GL_TRIANGLES);
Brian3af78762007-07-24 12:28:01 -060028 glColor3f(1, 0, 0);
Brian9110dbd2007-07-24 10:00:29 -060029 glVertex2f(-0.9, -0.9);
Brian3af78762007-07-24 12:28:01 -060030 glColor3f(0, 1, 0);
Brian9110dbd2007-07-24 10:00:29 -060031 glVertex2f( 0.9, -0.9);
Brian3af78762007-07-24 12:28:01 -060032 glColor3f(0, 0, 1);
Brian9110dbd2007-07-24 10:00:29 -060033 glVertex2f( 0, 0.9);
34 glEnd();
35
36 glPopMatrix();
37
38 glutSwapBuffers();
39}
40
41
42static void
43Reshape(int width, int height)
44{
45 glViewport(0, 0, width, height);
46 glMatrixMode(GL_PROJECTION);
47 glLoadIdentity();
48 glOrtho(-1, 1, -1, 1, -1, 1);
49 glMatrixMode(GL_MODELVIEW);
50 glLoadIdentity();
51}
52
53
54static void
55CleanUp(void)
56{
José Fonseca9aa73cf2009-02-01 12:00:07 +000057 glDeleteShader(fragShader);
58 glDeleteShader(vertShader);
59 glDeleteProgram(program);
Brian9110dbd2007-07-24 10:00:29 -060060 glutDestroyWindow(win);
61}
62
63
64static void
65Key(unsigned char key, int x, int y)
66{
67 (void) x;
68 (void) y;
69
70 switch(key) {
71 case 27:
72 CleanUp();
73 exit(0);
74 break;
75 }
76 glutPostRedisplay();
77}
78
79
80static void
81SpecialKey(int key, int x, int y)
82{
83 const GLfloat step = 0.1;
84
85 (void) x;
86 (void) y;
87
88 switch(key) {
89 case GLUT_KEY_UP:
90 ypos += step;
91 break;
92 case GLUT_KEY_DOWN:
93 ypos -= step;
94 break;
95 case GLUT_KEY_LEFT:
96 xpos -= step;
97 break;
98 case GLUT_KEY_RIGHT:
99 xpos += step;
100 break;
101 }
102 glutPostRedisplay();
103}
104
105
106static void
107LoadAndCompileShader(GLuint shader, const char *text)
108{
109 GLint stat;
110
José Fonseca9aa73cf2009-02-01 12:00:07 +0000111 glShaderSource(shader, 1, (const GLchar **) &text, NULL);
Brian9110dbd2007-07-24 10:00:29 -0600112
José Fonseca9aa73cf2009-02-01 12:00:07 +0000113 glCompileShader(shader);
Brian9110dbd2007-07-24 10:00:29 -0600114
José Fonseca9aa73cf2009-02-01 12:00:07 +0000115 glGetShaderiv(shader, GL_COMPILE_STATUS, &stat);
Brian9110dbd2007-07-24 10:00:29 -0600116 if (!stat) {
117 GLchar log[1000];
118 GLsizei len;
José Fonseca9aa73cf2009-02-01 12:00:07 +0000119 glGetShaderInfoLog(shader, 1000, &len, log);
Brian9110dbd2007-07-24 10:00:29 -0600120 fprintf(stderr, "fslight: problem compiling shader:\n%s\n", log);
121 exit(1);
122 }
123}
124
125
126static void
127CheckLink(GLuint prog)
128{
129 GLint stat;
José Fonseca9aa73cf2009-02-01 12:00:07 +0000130 glGetProgramiv(prog, GL_LINK_STATUS, &stat);
Brian9110dbd2007-07-24 10:00:29 -0600131 if (!stat) {
132 GLchar log[1000];
133 GLsizei len;
José Fonseca9aa73cf2009-02-01 12:00:07 +0000134 glGetProgramInfoLog(prog, 1000, &len, log);
Brian9110dbd2007-07-24 10:00:29 -0600135 fprintf(stderr, "Linker error:\n%s\n", log);
136 }
137}
138
139
140static void
141Init(void)
142{
143 /* fragment color is a function of fragment position: */
144 static const char *fragShaderText =
145 "void main() {\n"
146 " gl_FragColor = gl_FragCoord * vec4(0.005); \n"
Brian3af78762007-07-24 12:28:01 -0600147 " //gl_FragColor = gl_Color; \n"
148 " //gl_FragColor = vec4(1, 0, 0.5, 0); \n"
Brian9110dbd2007-07-24 10:00:29 -0600149 "}\n";
150#if 0
151 static const char *vertShaderText =
152 "varying vec3 normal;\n"
153 "void main() {\n"
154 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
155 " normal = gl_NormalMatrix * gl_Normal;\n"
156 "}\n";
157#endif
158 const char *version;
159
160 version = (const char *) glGetString(GL_VERSION);
161 if (version[0] != '2' || version[1] != '.') {
162 printf("This program requires OpenGL 2.x, found %s\n", version);
163 exit(1);
164 }
165
José Fonseca9aa73cf2009-02-01 12:00:07 +0000166 fragShader = glCreateShader(GL_FRAGMENT_SHADER);
Brian9110dbd2007-07-24 10:00:29 -0600167 LoadAndCompileShader(fragShader, fragShaderText);
168
169#if 0
José Fonseca9aa73cf2009-02-01 12:00:07 +0000170 vertShader = glCreateShader(GL_VERTEX_SHADER);
Brian9110dbd2007-07-24 10:00:29 -0600171 LoadAndCompileShader(vertShader, vertShaderText);
172#endif
173
José Fonseca9aa73cf2009-02-01 12:00:07 +0000174 program = glCreateProgram();
175 glAttachShader(program, fragShader);
Brian9110dbd2007-07-24 10:00:29 -0600176#if 0
José Fonseca9aa73cf2009-02-01 12:00:07 +0000177 glAttachShader(program, vertShader);
Brian9110dbd2007-07-24 10:00:29 -0600178#endif
José Fonseca9aa73cf2009-02-01 12:00:07 +0000179 glLinkProgram(program);
Brian9110dbd2007-07-24 10:00:29 -0600180 CheckLink(program);
José Fonseca9aa73cf2009-02-01 12:00:07 +0000181 glUseProgram(program);
Brian9110dbd2007-07-24 10:00:29 -0600182
183 assert(glGetError() == 0);
184
185 glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
186
187 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
188}
189
190
191int
192main(int argc, char *argv[])
193{
194 glutInit(&argc, argv);
195 glutInitWindowPosition( 0, 0);
196 glutInitWindowSize(200, 200);
197 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
198 win = glutCreateWindow(argv[0]);
José Fonseca9aa73cf2009-02-01 12:00:07 +0000199 glewInit();
Brian9110dbd2007-07-24 10:00:29 -0600200 glutReshapeFunc(Reshape);
201 glutKeyboardFunc(Key);
202 glutSpecialFunc(SpecialKey);
203 glutDisplayFunc(Redisplay);
204 Init();
205 glutMainLoop();
206 return 0;
207}
208
209