blob: dce140fc640e6ec9369193d41d3aa894d1ecd89c [file] [log] [blame]
Stephane Marchesin4ccbee22008-10-07 21:21:20 +02001/**
2 * Test very basic glsl functionality (identity vertex and fragment shaders).
Brian Paulb99c39e2008-10-07 16:24:43 -06003 * Brian Paul & Stephane Marchesin
Stephane Marchesin4ccbee22008-10-07 21:21:20 +02004 */
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"
Brian Paulb99c39e2008-10-07 16:24:43 -060016#include "shaderutil.h"
Stephane Marchesin4ccbee22008-10-07 21:21:20 +020017
18
19static char *FragProgFile = NULL;
20static char *VertProgFile = NULL;
21static GLuint fragShader;
22static GLuint vertShader;
23static GLuint program;
24static GLint win = 0;
25static GLboolean anim = GL_TRUE;
26static GLfloat xRot = 0.0f, yRot = 0.0f;
27static int w,h;
28
Brian Paulb99c39e2008-10-07 16:24:43 -060029
Stephane Marchesin4ccbee22008-10-07 21:21:20 +020030static void
31Redisplay(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
48static void
49Idle(void)
50{
51 yRot = glutGet(GLUT_ELAPSED_TIME) * 0.1;
52 glutPostRedisplay();
53}
54
55
56static void
57Reshape(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
69static void
70CleanUp(void)
71{
72 glDeleteShader_func(fragShader);
73 glDeleteShader_func(vertShader);
74 glDeleteProgram_func(program);
75 glutDestroyWindow(win);
76}
77
78
79static void
80Key(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
103static void
104SpecialKey(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
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200129static void
130Init(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";
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200140
Brian Paulb99c39e2008-10-07 16:24:43 -0600141 if (!ShadersSupported())
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200142 exit(1);
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200143
144 GetExtensionFuncs();
145
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200146 if (FragProgFile)
Brian Paulb99c39e2008-10-07 16:24:43 -0600147 fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200148 else
Brian Paulb99c39e2008-10-07 16:24:43 -0600149 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200150
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200151 if (VertProgFile)
Brian Paulb99c39e2008-10-07 16:24:43 -0600152 vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200153 else
Brian Paulb99c39e2008-10-07 16:24:43 -0600154 vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200155
Brian Paulb99c39e2008-10-07 16:24:43 -0600156 program = LinkShaders(vertShader, fragShader);
157
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200158 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
175static void
176ParseOptions(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
190int
191main(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}