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