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