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