37579eb3469c41de9dd3ef32c58a861fd3426361
[mesa.git] / progs / glsl / identity.c
1 /**
2 * Test very basic glsl functionality (identity vertex and fragment shaders).
3 * Brian Paul & Stephane Marchesin
4 */
5
6
7 #include <assert.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <math.h>
12 #include <GL/gl.h>
13 #include <GL/glut.h>
14 #include <GL/glext.h>
15 #include "extfuncs.h"
16 #include "shaderutil.h"
17
18
19 static char *FragProgFile = NULL;
20 static char *VertProgFile = NULL;
21 static GLuint fragShader;
22 static GLuint vertShader;
23 static GLuint program;
24 static GLint win = 0;
25 static GLboolean anim = GL_FALSE;
26 static GLfloat xRot = 0.0f, yRot = 0.0f;
27 static int w,h;
28
29
30 static void
31 Redisplay(void)
32 {
33 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
34
35 glBegin(GL_TRIANGLES);
36 glColor3f(.8,0,0);
37 glVertex3f(-0.9, -0.9, 0.0);
38 glColor3f(0,.9,0);
39 glVertex3f( 0.9, -0.9, 0.0);
40 glColor3f(0,0,.7);
41 glVertex3f( 0.0, 0.9, 0.0);
42 glEnd();
43
44 glutSwapBuffers();
45 }
46
47
48 static void
49 Idle(void)
50 {
51 yRot = glutGet(GLUT_ELAPSED_TIME) * 0.1;
52 glutPostRedisplay();
53 }
54
55
56 static void
57 Reshape(int width, int height)
58 {
59 glViewport(0, 0, width, height);
60 glMatrixMode(GL_PROJECTION);
61 glLoadIdentity();
62 glMatrixMode(GL_MODELVIEW);
63 glLoadIdentity();
64 w = width;
65 h = height;
66 }
67
68
69 static void
70 CleanUp(void)
71 {
72 glDeleteShader_func(fragShader);
73 glDeleteShader_func(vertShader);
74 glDeleteProgram_func(program);
75 glutDestroyWindow(win);
76 }
77
78
79 static void
80 Key(unsigned char key, int x, int y)
81 {
82 (void) x;
83 (void) y;
84
85 switch(key) {
86 case ' ':
87 case 'a':
88 anim = !anim;
89 if (anim)
90 glutIdleFunc(Idle);
91 else
92 glutIdleFunc(NULL);
93 break;
94 case 27:
95 CleanUp();
96 exit(0);
97 break;
98 }
99 glutPostRedisplay();
100 }
101
102
103 static void
104 SpecialKey(int key, int x, int y)
105 {
106 const GLfloat step = 3.0f;
107
108 (void) x;
109 (void) y;
110
111 switch(key) {
112 case GLUT_KEY_UP:
113 xRot -= step;
114 break;
115 case GLUT_KEY_DOWN:
116 xRot += step;
117 break;
118 case GLUT_KEY_LEFT:
119 yRot -= step;
120 break;
121 case GLUT_KEY_RIGHT:
122 yRot += step;
123 break;
124 }
125 glutPostRedisplay();
126 }
127
128
129 static void
130 Init(void)
131 {
132 static const char *fragShaderText =
133 "void main() {\n"
134 " gl_FragColor = vec4(1.0,0.0,0.0,1.0);\n"
135 "}\n";
136 static const char *vertShaderText =
137 "void main() {\n"
138 " gl_Position = gl_Vertex;\n"
139 "}\n";
140
141 if (!ShadersSupported())
142 exit(1);
143
144 GetExtensionFuncs();
145
146 if (FragProgFile)
147 fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
148 else
149 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
150
151 if (VertProgFile)
152 vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
153 else
154 vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
155
156 program = LinkShaders(vertShader, fragShader);
157
158 glUseProgram_func(program);
159
160 /*assert(glGetError() == 0);*/
161
162 glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
163 glEnable(GL_DEPTH_TEST);
164
165 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
166
167 assert(glIsProgram_func(program));
168 assert(glIsShader_func(fragShader));
169 assert(glIsShader_func(vertShader));
170
171 glColor3f(1, 0, 0);
172 }
173
174
175 static void
176 ParseOptions(int argc, char *argv[])
177 {
178 int i;
179 for (i = 1; i < argc; i++) {
180 if (strcmp(argv[i], "-fs") == 0) {
181 FragProgFile = argv[i+1];
182 }
183 else if (strcmp(argv[i], "-vs") == 0) {
184 VertProgFile = argv[i+1];
185 }
186 }
187 }
188
189
190 int
191 main(int argc, char *argv[])
192 {
193 glutInit(&argc, argv);
194 glutInitWindowPosition( 0, 0);
195 glutInitWindowSize(200, 200);
196 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
197 win = glutCreateWindow(argv[0]);
198 glutReshapeFunc(Reshape);
199 glutKeyboardFunc(Key);
200 glutSpecialFunc(SpecialKey);
201 glutDisplayFunc(Redisplay);
202 if (anim)
203 glutIdleFunc(Idle);
204 ParseOptions(argc, argv);
205 Init();
206 glutMainLoop();
207 return 0;
208 }