blob: a772ccd716c5e6b181435c064fefcfad7fc95f29 [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/glut.h>
Brian Paulb99c39e2008-10-07 16:24:43 -060014#include "shaderutil.h"
Stephane Marchesin4ccbee22008-10-07 21:21:20 +020015
16
17static char *FragProgFile = NULL;
18static char *VertProgFile = NULL;
19static GLuint fragShader;
20static GLuint vertShader;
21static GLuint program;
22static GLint win = 0;
Brian Paul0a8590e2008-10-28 18:18:31 -060023static GLboolean anim = GL_FALSE;
Stephane Marchesin4ccbee22008-10-07 21:21:20 +020024static GLfloat xRot = 0.0f, yRot = 0.0f;
25static int w,h;
26
Brian Paulb99c39e2008-10-07 16:24:43 -060027
Stephane Marchesin4ccbee22008-10-07 21:21:20 +020028static void
29Redisplay(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
46static void
47Idle(void)
48{
49 yRot = glutGet(GLUT_ELAPSED_TIME) * 0.1;
50 glutPostRedisplay();
51}
52
53
54static void
55Reshape(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
67static void
68CleanUp(void)
69{
Brian Paulee0b1bc2009-07-17 13:23:11 -060070 glDeleteShader(fragShader);
71 glDeleteShader(vertShader);
72 glDeleteProgram(program);
Stephane Marchesin4ccbee22008-10-07 21:21:20 +020073 glutDestroyWindow(win);
74}
75
76
77static void
78Key(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
101static void
102SpecialKey(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
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200127static void
128Init(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";
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200138
Brian Paulb99c39e2008-10-07 16:24:43 -0600139 if (!ShadersSupported())
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200140 exit(1);
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200141
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200142 if (FragProgFile)
Brian Paulb99c39e2008-10-07 16:24:43 -0600143 fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200144 else
Brian Paulb99c39e2008-10-07 16:24:43 -0600145 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200146
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200147 if (VertProgFile)
Brian Paulb99c39e2008-10-07 16:24:43 -0600148 vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200149 else
Brian Paulb99c39e2008-10-07 16:24:43 -0600150 vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200151
Brian Paulb99c39e2008-10-07 16:24:43 -0600152 program = LinkShaders(vertShader, fragShader);
153
Brian Paulee0b1bc2009-07-17 13:23:11 -0600154 glUseProgram(program);
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200155
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
Brian Paulee0b1bc2009-07-17 13:23:11 -0600163 assert(glIsProgram(program));
164 assert(glIsShader(fragShader));
165 assert(glIsShader(vertShader));
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200166
167 glColor3f(1, 0, 0);
168}
169
170
171static void
172ParseOptions(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
186int
187main(int argc, char *argv[])
188{
189 glutInit(&argc, argv);
190 glutInitWindowPosition( 0, 0);
191 glutInitWindowSize(200, 200);
192 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
193 win = glutCreateWindow(argv[0]);
Keith Whitwellb799af92009-06-29 14:13:58 +0100194 glewInit();
Stephane Marchesin4ccbee22008-10-07 21:21:20 +0200195 glutReshapeFunc(Reshape);
196 glutKeyboardFunc(Key);
197 glutSpecialFunc(SpecialKey);
198 glutDisplayFunc(Redisplay);
199 if (anim)
200 glutIdleFunc(Idle);
201 ParseOptions(argc, argv);
202 Init();
203 glutMainLoop();
204 return 0;
205}