blob: 5ba7468cc444c874c246b323d1f9f0ed4cd9547c [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>
Keith Whitwellb799af92009-06-29 14:13:58 +010012#include <GL/glew.h>
Stephane Marchesin4ccbee22008-10-07 21:21:20 +020013#include <GL/gl.h>
14#include <GL/glut.h>
15#include <GL/glext.h>
16#include "extfuncs.h"
Brian Paulb99c39e2008-10-07 16:24:43 -060017#include "shaderutil.h"
Stephane Marchesin4ccbee22008-10-07 21:21:20 +020018
19
20static char *FragProgFile = NULL;
21static char *VertProgFile = NULL;
22static GLuint fragShader;
23static GLuint vertShader;
24static GLuint program;
25static GLint win = 0;
Brian Paul0a8590e2008-10-28 18:18:31 -060026static GLboolean anim = GL_FALSE;
Stephane Marchesin4ccbee22008-10-07 21:21:20 +020027static GLfloat xRot = 0.0f, yRot = 0.0f;
28static int w,h;
29
Brian Paulb99c39e2008-10-07 16:24:43 -060030
Stephane Marchesin4ccbee22008-10-07 21:21:20 +020031static void
32Redisplay(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
49static void
50Idle(void)
51{
52 yRot = glutGet(GLUT_ELAPSED_TIME) * 0.1;
53 glutPostRedisplay();
54}
55
56
57static void
58Reshape(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
70static void
71CleanUp(void)
72{
73 glDeleteShader_func(fragShader);
74 glDeleteShader_func(vertShader);
75 glDeleteProgram_func(program);
76 glutDestroyWindow(win);
77}
78
79
80static void
81Key(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
104static void
105SpecialKey(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
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200130static void
131Init(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";
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200141
Brian Paulb99c39e2008-10-07 16:24:43 -0600142 if (!ShadersSupported())
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200143 exit(1);
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200144
145 GetExtensionFuncs();
146
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200147 if (FragProgFile)
Brian Paulb99c39e2008-10-07 16:24:43 -0600148 fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200149 else
Brian Paulb99c39e2008-10-07 16:24:43 -0600150 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200151
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200152 if (VertProgFile)
Brian Paulb99c39e2008-10-07 16:24:43 -0600153 vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200154 else
Brian Paulb99c39e2008-10-07 16:24:43 -0600155 vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200156
Brian Paulb99c39e2008-10-07 16:24:43 -0600157 program = LinkShaders(vertShader, fragShader);
158
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200159 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
176static void
177ParseOptions(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
191int
192main(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]);
Keith Whitwellb799af92009-06-29 14:13:58 +0100199 glewInit();
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200200 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}