1 /**
2 * Test GLSL 1.20 gl_PointCoord fragment program attribute.
3 * Brian Paul
4 * 11 Aug 2007
5 */
6
7
8 #include <assert.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <math.h>
13 #include <GL/glew.h>
14 #include "glut_wrap.h"
15 #include "shaderutil.h"
16
17
18 static GLint WinWidth = 300, WinHeight = 300;
19 static char *FragProgFile = NULL;
20 static char *VertProgFile = NULL;
21 static GLuint fragShader;
22 static GLuint vertShader;
23 static GLuint program;
24 static GLint win = 0;
25 static GLint tex0;
26 static GLenum Filter = GL_NEAREST;
27 static GLenum Origin = GL_UPPER_LEFT;
28
29
30 static void
Redisplay(void)31 Redisplay(void)
32 {
33 printf("GL_POINT_SPRITE_COORD_ORIGIN = %s\n",
34 (Origin == GL_UPPER_LEFT ? "GL_UPPER_LEFT" : "GL_LOWER_LEFT"));
35
36 glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, Origin);
37
38 glClear(GL_COLOR_BUFFER_BIT);
39
40 /* draw one point/sprite */
41 glPushMatrix();
42 glPointSize(60);
43 glBegin(GL_POINTS);
44 glVertex2f(WinWidth / 2.0f, WinHeight / 2.0f);
45 glEnd();
46 glPopMatrix();
47
48 glutSwapBuffers();
49 }
50
51
52 static void
Reshape(int width,int height)53 Reshape(int width, int height)
54 {
55 glViewport(0, 0, width, height);
56 glMatrixMode(GL_PROJECTION);
57 glLoadIdentity();
58 glOrtho(0, width, 0, height, -1, 1);
59
60 glMatrixMode(GL_MODELVIEW);
61 glLoadIdentity();
62
63 WinWidth = width;
64 WinHeight = height;
65 }
66
67
68 static void
CleanUp(void)69 CleanUp(void)
70 {
71 glDeleteShader(fragShader);
72 glDeleteShader(vertShader);
73 glDeleteProgram(program);
74 glutDestroyWindow(win);
75 }
76
77
78 static void
Key(unsigned char key,int x,int y)79 Key(unsigned char key, int x, int y)
80 {
81 (void) x;
82 (void) y;
83
84 switch(key) {
85 case 'o':
86 case 'O':
87 if (Origin == GL_UPPER_LEFT)
88 Origin = GL_LOWER_LEFT;
89 else
90 Origin = GL_UPPER_LEFT;
91 break;
92 case 27:
93 CleanUp();
94 exit(0);
95 break;
96 }
97 glutPostRedisplay();
98 }
99
100
101
102 static void
MakeTexture(void)103 MakeTexture(void)
104 {
105 #define SZ 16
106 GLubyte image[SZ][SZ][4];
107 GLuint i, j;
108
109 for (i = 0; i < SZ; i++) {
110 for (j = 0; j < SZ; j++) {
111 if ((i + j) & 1) {
112 image[i][j][0] = 0;
113 image[i][j][1] = 0;
114 image[i][j][2] = 0;
115 image[i][j][3] = 255;
116 }
117 else {
118 image[i][j][0] = j * 255 / (SZ-1);
119 image[i][j][1] = i * 255 / (SZ-1);
120 image[i][j][2] = 0;
121 image[i][j][3] = 255;
122 }
123 }
124 }
125
126 glActiveTexture(GL_TEXTURE0); /* unit 0 */
127 glBindTexture(GL_TEXTURE_2D, 42);
128 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SZ, SZ, 0,
129 GL_RGBA, GL_UNSIGNED_BYTE, image);
130 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Filter);
131 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, Filter);
132 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
133 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
134 #undef SZ
135 }
136
137
138 static void
Init(void)139 Init(void)
140 {
141 static const char *fragShaderText =
142 "#version 120 \n"
143 "uniform sampler2D tex0; \n"
144 "void main() { \n"
145 " gl_FragColor = texture2D(tex0, gl_PointCoord.xy, 0.0); \n"
146 "}\n";
147 static const char *vertShaderText =
148 "void main() {\n"
149 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
150 "}\n";
151
152 if (!ShadersSupported())
153 exit(1);
154
155 vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
156 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
157 program = LinkShaders(vertShader, fragShader);
158
159 glUseProgram(program);
160
161 tex0 = glGetUniformLocation(program, "tex0");
162 printf("Uniforms: tex0: %d\n", tex0);
163
164 glUniform1i(tex0, 0); /* tex unit 0 */
165
166 /*assert(glGetError() == 0);*/
167
168 glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
169
170 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
171
172 assert(glIsProgram(program));
173 assert(glIsShader(fragShader));
174 assert(glIsShader(vertShader));
175
176 MakeTexture();
177
178 glEnable(GL_POINT_SPRITE);
179
180 glColor3f(1, 0, 0);
181 }
182
183
184 static void
ParseOptions(int argc,char * argv[])185 ParseOptions(int argc, char *argv[])
186 {
187 int i;
188 for (i = 1; i < argc; i++) {
189 if (strcmp(argv[i], "-fs") == 0) {
190 FragProgFile = argv[i+1];
191 }
192 else if (strcmp(argv[i], "-vs") == 0) {
193 VertProgFile = argv[i+1];
194 }
195 }
196 }
197
198
199 int
main(int argc,char * argv[])200 main(int argc, char *argv[])
201 {
202 glutInit(&argc, argv);
203 glutInitWindowSize(WinWidth, WinHeight);
204 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
205 win = glutCreateWindow(argv[0]);
206 glewInit();
207 glutReshapeFunc(Reshape);
208 glutKeyboardFunc(Key);
209 glutDisplayFunc(Redisplay);
210 ParseOptions(argc, argv);
211 Init();
212 glutMainLoop();
213 return 0;
214 }
215