fs-tri.c revision 32001f49
1/* Test fragment shader */
2
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"
10
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);
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();
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{
57   glDeleteShader(fragShader);
58   glDeleteShader(vertShader);
59   glDeleteProgram(program);
60   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
111   glShaderSource(shader, 1, (const GLchar **) &text, NULL);
112
113   glCompileShader(shader);
114
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);
122   }
123}
124
125
126static void
127CheckLink(GLuint prog)
128{
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);
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"
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
158
159   if (!GLEW_VERSION_2_0) {
160      printf("This program requires OpenGL 2.x\n");
161      exit(1);
162   }
163
164   fragShader = glCreateShader(GL_FRAGMENT_SHADER);
165   LoadAndCompileShader(fragShader, fragShaderText);
166
167#if 0
168   vertShader = glCreateShader(GL_VERTEX_SHADER);
169   LoadAndCompileShader(vertShader, vertShaderText);
170#endif
171
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);
180
181   assert(glGetError() == 0);
182
183   glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
184
185   printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
186}
187
188
189int
190main(int argc, char *argv[])
191{
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;
205}
206
207
208