2 * Test GLSL gl_FragCoord fragment program attribute.
3 * Color the quad's fragments according to their window position.
17 #include "shaderutil.h"
20 static GLint WinWidth
= 200, WinHeight
= 200;
21 static char *FragProgFile
= NULL
;
22 static char *VertProgFile
= NULL
;
23 static GLuint fragShader
;
24 static GLuint vertShader
;
25 static GLuint program
;
27 static GLboolean Anim
= GL_TRUE
;
28 static GLfloat PosX
= 0.0, PosY
= 0.0;
34 float r
= (WinWidth
< WinHeight
) ? WinWidth
: WinHeight
;
35 float a
= glutGet(GLUT_ELAPSED_TIME
) * 0.001;
37 PosX
= WinWidth
/ 2 + r
* cos(a
);
38 PosY
= WinHeight
/ 2 + r
* sin(a
);
47 glClear(GL_COLOR_BUFFER_BIT
);
50 glTranslatef(PosX
, PosY
, 0.0);
59 glutSolidSphere(50, 20, 10);
68 Reshape(int width
, int height
)
70 glViewport(0, 0, width
, height
);
71 glMatrixMode(GL_PROJECTION
);
73 glOrtho(0, width
, 0, height
, -55, 55);
75 glMatrixMode(GL_MODELVIEW
);
86 glDeleteShader(fragShader
);
87 glDeleteShader(vertShader
);
88 glDeleteProgram(program
);
89 glutDestroyWindow(win
);
94 Key(unsigned char key
, int x
, int y
)
103 glutIdleFunc(Anim
? Idle
: NULL
);
117 static const char *fragShaderText
=
119 " vec4 scale = vec4(.005, 0.005, 0.5, 1.0);\n"
120 " gl_FragColor = gl_FragCoord * scale; \n"
122 static const char *vertShaderText
=
124 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
127 if (!ShadersSupported())
130 vertShader
= CompileShaderText(GL_VERTEX_SHADER
, vertShaderText
);
131 fragShader
= CompileShaderText(GL_FRAGMENT_SHADER
, fragShaderText
);
132 program
= LinkShaders(vertShader
, fragShader
);
134 glUseProgram(program
);
136 /*assert(glGetError() == 0);*/
138 glClearColor(0.3f
, 0.3f
, 0.3f
, 0.0f
);
140 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
142 assert(glIsProgram(program
));
143 assert(glIsShader(fragShader
));
144 assert(glIsShader(vertShader
));
151 ParseOptions(int argc
, char *argv
[])
154 for (i
= 1; i
< argc
; i
++) {
155 if (strcmp(argv
[i
], "-fs") == 0) {
156 FragProgFile
= argv
[i
+1];
158 else if (strcmp(argv
[i
], "-vs") == 0) {
159 VertProgFile
= argv
[i
+1];
166 main(int argc
, char *argv
[])
168 glutInit(&argc
, argv
);
169 glutInitWindowSize(WinWidth
, WinHeight
);
170 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
);
171 win
= glutCreateWindow(argv
[0]);
173 glutReshapeFunc(Reshape
);
174 glutKeyboardFunc(Key
);
175 glutDisplayFunc(Redisplay
);
176 ParseOptions(argc
, argv
);
178 glutIdleFunc(Anim
? Idle
: NULL
);