Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Test very basic glsl functionality (identity vertex and fragment shaders). |
Brian Paul | b99c39e | 2008-10-07 16:24:43 -0600 | [diff] [blame] | 3 | * Brian Paul & Stephane Marchesin |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <string.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <math.h> |
Keith Whitwell | b799af9 | 2009-06-29 14:13:58 +0100 | [diff] [blame] | 12 | #include <GL/glew.h> |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 13 | #include <GL/glut.h> |
Brian Paul | b99c39e | 2008-10-07 16:24:43 -0600 | [diff] [blame] | 14 | #include "shaderutil.h" |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 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; |
Brian Paul | 0a8590e | 2008-10-28 18:18:31 -0600 | [diff] [blame] | 23 | static GLboolean anim = GL_FALSE; |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 24 | static GLfloat xRot = 0.0f, yRot = 0.0f; |
| 25 | static int w,h; |
| 26 | |
Brian Paul | b99c39e | 2008-10-07 16:24:43 -0600 | [diff] [blame] | 27 | |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 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 | { |
Brian Paul | ee0b1bc | 2009-07-17 13:23:11 -0600 | [diff] [blame^] | 70 | glDeleteShader(fragShader); |
| 71 | glDeleteShader(vertShader); |
| 72 | glDeleteProgram(program); |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 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 | |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 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"; |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 138 | |
Brian Paul | b99c39e | 2008-10-07 16:24:43 -0600 | [diff] [blame] | 139 | if (!ShadersSupported()) |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 140 | exit(1); |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 141 | |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 142 | if (FragProgFile) |
Brian Paul | b99c39e | 2008-10-07 16:24:43 -0600 | [diff] [blame] | 143 | fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile); |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 144 | else |
Brian Paul | b99c39e | 2008-10-07 16:24:43 -0600 | [diff] [blame] | 145 | fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText); |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 146 | |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 147 | if (VertProgFile) |
Brian Paul | b99c39e | 2008-10-07 16:24:43 -0600 | [diff] [blame] | 148 | vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile); |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 149 | else |
Brian Paul | b99c39e | 2008-10-07 16:24:43 -0600 | [diff] [blame] | 150 | vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText); |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 151 | |
Brian Paul | b99c39e | 2008-10-07 16:24:43 -0600 | [diff] [blame] | 152 | program = LinkShaders(vertShader, fragShader); |
| 153 | |
Brian Paul | ee0b1bc | 2009-07-17 13:23:11 -0600 | [diff] [blame^] | 154 | glUseProgram(program); |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 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 | |
Brian Paul | ee0b1bc | 2009-07-17 13:23:11 -0600 | [diff] [blame^] | 163 | assert(glIsProgram(program)); |
| 164 | assert(glIsShader(fragShader)); |
| 165 | assert(glIsShader(vertShader)); |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 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 | glutInitWindowPosition( 0, 0); |
| 191 | glutInitWindowSize(200, 200); |
| 192 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); |
| 193 | win = glutCreateWindow(argv[0]); |
Keith Whitwell | b799af9 | 2009-06-29 14:13:58 +0100 | [diff] [blame] | 194 | glewInit(); |
Stephane Marchesin | 4ccbee2 | 2008-10-07 21:21:20 +0200 | [diff] [blame] | 195 | 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 | } |