Brian | 9110dbd | 2007-07-24 10:00:29 -0600 | [diff] [blame] | 1 | /* Test fragment shader */ |
| 2 | |
| 3 | #include <assert.h> |
| 4 | #include <string.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <math.h> |
| 8 | #include <GL/gl.h> |
| 9 | #include <GL/glut.h> |
| 10 | #include <GL/glext.h> |
| 11 | #include "extfuncs.h" |
| 12 | |
| 13 | |
| 14 | static GLuint fragShader; |
| 15 | static GLuint vertShader; |
| 16 | static GLuint program; |
| 17 | static GLint win = 0; |
| 18 | static GLfloat xpos = 0, ypos = 0; |
| 19 | |
| 20 | |
| 21 | static void |
| 22 | Redisplay(void) |
| 23 | { |
| 24 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 25 | |
| 26 | glPushMatrix(); |
| 27 | glTranslatef(xpos, ypos, 0); |
| 28 | |
| 29 | glBegin(GL_TRIANGLES); |
Brian | 3af7876 | 2007-07-24 12:28:01 -0600 | [diff] [blame^] | 30 | glColor3f(1, 0, 0); |
Brian | 9110dbd | 2007-07-24 10:00:29 -0600 | [diff] [blame] | 31 | glVertex2f(-0.9, -0.9); |
Brian | 3af7876 | 2007-07-24 12:28:01 -0600 | [diff] [blame^] | 32 | glColor3f(0, 1, 0); |
Brian | 9110dbd | 2007-07-24 10:00:29 -0600 | [diff] [blame] | 33 | glVertex2f( 0.9, -0.9); |
Brian | 3af7876 | 2007-07-24 12:28:01 -0600 | [diff] [blame^] | 34 | glColor3f(0, 0, 1); |
Brian | 9110dbd | 2007-07-24 10:00:29 -0600 | [diff] [blame] | 35 | glVertex2f( 0, 0.9); |
| 36 | glEnd(); |
| 37 | |
| 38 | glPopMatrix(); |
| 39 | |
| 40 | glutSwapBuffers(); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | static void |
| 45 | Reshape(int width, int height) |
| 46 | { |
| 47 | glViewport(0, 0, width, height); |
| 48 | glMatrixMode(GL_PROJECTION); |
| 49 | glLoadIdentity(); |
| 50 | glOrtho(-1, 1, -1, 1, -1, 1); |
| 51 | glMatrixMode(GL_MODELVIEW); |
| 52 | glLoadIdentity(); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | static void |
| 57 | CleanUp(void) |
| 58 | { |
| 59 | glDeleteShader_func(fragShader); |
| 60 | glDeleteShader_func(vertShader); |
| 61 | glDeleteProgram_func(program); |
| 62 | glutDestroyWindow(win); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | static void |
| 67 | Key(unsigned char key, int x, int y) |
| 68 | { |
| 69 | (void) x; |
| 70 | (void) y; |
| 71 | |
| 72 | switch(key) { |
| 73 | case 27: |
| 74 | CleanUp(); |
| 75 | exit(0); |
| 76 | break; |
| 77 | } |
| 78 | glutPostRedisplay(); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | static void |
| 83 | SpecialKey(int key, int x, int y) |
| 84 | { |
| 85 | const GLfloat step = 0.1; |
| 86 | |
| 87 | (void) x; |
| 88 | (void) y; |
| 89 | |
| 90 | switch(key) { |
| 91 | case GLUT_KEY_UP: |
| 92 | ypos += step; |
| 93 | break; |
| 94 | case GLUT_KEY_DOWN: |
| 95 | ypos -= step; |
| 96 | break; |
| 97 | case GLUT_KEY_LEFT: |
| 98 | xpos -= step; |
| 99 | break; |
| 100 | case GLUT_KEY_RIGHT: |
| 101 | xpos += step; |
| 102 | break; |
| 103 | } |
| 104 | glutPostRedisplay(); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | static void |
| 109 | LoadAndCompileShader(GLuint shader, const char *text) |
| 110 | { |
| 111 | GLint stat; |
| 112 | |
| 113 | glShaderSource_func(shader, 1, (const GLchar **) &text, NULL); |
| 114 | |
| 115 | glCompileShader_func(shader); |
| 116 | |
| 117 | glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat); |
| 118 | if (!stat) { |
| 119 | GLchar log[1000]; |
| 120 | GLsizei len; |
| 121 | glGetShaderInfoLog_func(shader, 1000, &len, log); |
| 122 | fprintf(stderr, "fslight: problem compiling shader:\n%s\n", log); |
| 123 | exit(1); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | |
| 128 | static void |
| 129 | CheckLink(GLuint prog) |
| 130 | { |
| 131 | GLint stat; |
| 132 | glGetProgramiv_func(prog, GL_LINK_STATUS, &stat); |
| 133 | if (!stat) { |
| 134 | GLchar log[1000]; |
| 135 | GLsizei len; |
| 136 | glGetProgramInfoLog_func(prog, 1000, &len, log); |
| 137 | fprintf(stderr, "Linker error:\n%s\n", log); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | |
| 142 | static void |
| 143 | Init(void) |
| 144 | { |
| 145 | /* fragment color is a function of fragment position: */ |
| 146 | static const char *fragShaderText = |
| 147 | "void main() {\n" |
| 148 | " gl_FragColor = gl_FragCoord * vec4(0.005); \n" |
Brian | 3af7876 | 2007-07-24 12:28:01 -0600 | [diff] [blame^] | 149 | " //gl_FragColor = gl_Color; \n" |
| 150 | " //gl_FragColor = vec4(1, 0, 0.5, 0); \n" |
Brian | 9110dbd | 2007-07-24 10:00:29 -0600 | [diff] [blame] | 151 | "}\n"; |
| 152 | #if 0 |
| 153 | static const char *vertShaderText = |
| 154 | "varying vec3 normal;\n" |
| 155 | "void main() {\n" |
| 156 | " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" |
| 157 | " normal = gl_NormalMatrix * gl_Normal;\n" |
| 158 | "}\n"; |
| 159 | #endif |
| 160 | const char *version; |
| 161 | |
| 162 | version = (const char *) glGetString(GL_VERSION); |
| 163 | if (version[0] != '2' || version[1] != '.') { |
| 164 | printf("This program requires OpenGL 2.x, found %s\n", version); |
| 165 | exit(1); |
| 166 | } |
| 167 | |
| 168 | GetExtensionFuncs(); |
| 169 | |
| 170 | fragShader = glCreateShader_func(GL_FRAGMENT_SHADER); |
| 171 | LoadAndCompileShader(fragShader, fragShaderText); |
| 172 | |
| 173 | #if 0 |
| 174 | vertShader = glCreateShader_func(GL_VERTEX_SHADER); |
| 175 | LoadAndCompileShader(vertShader, vertShaderText); |
| 176 | #endif |
| 177 | |
| 178 | program = glCreateProgram_func(); |
| 179 | glAttachShader_func(program, fragShader); |
| 180 | #if 0 |
| 181 | glAttachShader_func(program, vertShader); |
| 182 | #endif |
| 183 | glLinkProgram_func(program); |
| 184 | CheckLink(program); |
| 185 | glUseProgram_func(program); |
| 186 | |
| 187 | assert(glGetError() == 0); |
| 188 | |
| 189 | glClearColor(0.3f, 0.3f, 0.3f, 0.0f); |
| 190 | |
| 191 | printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | int |
| 196 | main(int argc, char *argv[]) |
| 197 | { |
| 198 | glutInit(&argc, argv); |
| 199 | glutInitWindowPosition( 0, 0); |
| 200 | glutInitWindowSize(200, 200); |
| 201 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); |
| 202 | win = glutCreateWindow(argv[0]); |
| 203 | glutReshapeFunc(Reshape); |
| 204 | glutKeyboardFunc(Key); |
| 205 | glutSpecialFunc(SpecialKey); |
| 206 | glutDisplayFunc(Redisplay); |
| 207 | Init(); |
| 208 | glutMainLoop(); |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | |