Revert "wglgears: show stereo-option in usage"
[mesa-demos.git] / src / trivial / fs-tri.c
blob3022b09709a0e753599cade0a0ba7a12d01e4692
1 /* Test fragment shader */
3 #include <assert.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <math.h>
8 #include <GL/glew.h>
9 #include "glut_wrap.h"
12 static GLuint fragShader;
13 static GLuint vertShader;
14 static GLuint program;
15 static GLint win = 0;
16 static GLfloat xpos = 0, ypos = 0;
19 static void
20 Redisplay(void)
22 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
24 glPushMatrix();
25 glTranslatef(xpos, ypos, 0);
27 glBegin(GL_TRIANGLES);
28 glColor3f(1, 0, 0);
29 glVertex2f(-0.9, -0.9);
30 glColor3f(0, 1, 0);
31 glVertex2f( 0.9, -0.9);
32 glColor3f(0, 0, 1);
33 glVertex2f( 0, 0.9);
34 glEnd();
36 glPopMatrix();
38 glutSwapBuffers();
42 static void
43 Reshape(int width, int height)
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();
54 static void
55 CleanUp(void)
57 glDeleteShader(fragShader);
58 glDeleteShader(vertShader);
59 glDeleteProgram(program);
60 glutDestroyWindow(win);
64 static void
65 Key(unsigned char key, int x, int y)
67 (void) x;
68 (void) y;
70 switch(key) {
71 case 27:
72 CleanUp();
73 exit(0);
74 break;
76 glutPostRedisplay();
80 static void
81 SpecialKey(int key, int x, int y)
83 const GLfloat step = 0.1;
85 (void) x;
86 (void) y;
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;
102 glutPostRedisplay();
106 static void
107 LoadAndCompileShader(GLuint shader, const char *text)
109 GLint stat;
111 glShaderSource(shader, 1, (const GLchar **) &text, NULL);
113 glCompileShader(shader);
115 glGetShaderiv(shader, GL_COMPILE_STATUS, &stat);
116 if (!stat) {
117 GLchar log[1000];
118 GLsizei len;
119 glGetShaderInfoLog(shader, 1000, &len, log);
120 fprintf(stderr, "fslight: problem compiling shader:\n%s\n", log);
121 exit(1);
126 static void
127 CheckLink(GLuint prog)
129 GLint stat;
130 glGetProgramiv(prog, GL_LINK_STATUS, &stat);
131 if (!stat) {
132 GLchar log[1000];
133 GLsizei len;
134 glGetProgramInfoLog(prog, 1000, &len, log);
135 fprintf(stderr, "Linker error:\n%s\n", log);
140 static void
141 Init(void)
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"
147 " //gl_FragColor = gl_Color; \n"
148 " //gl_FragColor = vec4(1, 0, 0.5, 0); \n"
149 "}\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
159 if (!GLEW_VERSION_2_0) {
160 printf("This program requires OpenGL 2.x\n");
161 exit(1);
164 fragShader = glCreateShader(GL_FRAGMENT_SHADER);
165 LoadAndCompileShader(fragShader, fragShaderText);
167 #if 0
168 vertShader = glCreateShader(GL_VERTEX_SHADER);
169 LoadAndCompileShader(vertShader, vertShaderText);
170 #endif
172 program = glCreateProgram();
173 glAttachShader(program, fragShader);
174 #if 0
175 glAttachShader(program, vertShader);
176 #endif
177 glLinkProgram(program);
178 CheckLink(program);
179 glUseProgram(program);
181 assert(glGetError() == 0);
183 glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
185 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
190 main(int argc, char *argv[])
192 glutInit(&argc, argv);
193 glutInitWindowPosition( 0, 0);
194 glutInitWindowSize(200, 200);
195 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
196 win = glutCreateWindow(argv[0]);
197 glewInit();
198 glutReshapeFunc(Reshape);
199 glutKeyboardFunc(Key);
200 glutSpecialFunc(SpecialKey);
201 glutDisplayFunc(Redisplay);
202 Init();
203 glutMainLoop();
204 return 0;