Merge branch '7.8'
[mesa.git] / progs / glsl / pointcoord.c
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 <GL/glut.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
28
29 static void
30 Redisplay(void)
31 {
32 glClear(GL_COLOR_BUFFER_BIT);
33
34 /* draw one point/sprite */
35 glPushMatrix();
36 glPointSize(60);
37 glBegin(GL_POINTS);
38 glVertex2f(WinWidth / 2.0f, WinHeight / 2.0f);
39 glEnd();
40 glPopMatrix();
41
42 glutSwapBuffers();
43 }
44
45
46 static void
47 Reshape(int width, int height)
48 {
49 glViewport(0, 0, width, height);
50 glMatrixMode(GL_PROJECTION);
51 glLoadIdentity();
52 glOrtho(0, width, 0, height, -1, 1);
53
54 glMatrixMode(GL_MODELVIEW);
55 glLoadIdentity();
56
57 WinWidth = width;
58 WinHeight = height;
59 }
60
61
62 static void
63 CleanUp(void)
64 {
65 glDeleteShader(fragShader);
66 glDeleteShader(vertShader);
67 glDeleteProgram(program);
68 glutDestroyWindow(win);
69 }
70
71
72 static void
73 Key(unsigned char key, int x, int y)
74 {
75 (void) x;
76 (void) y;
77
78 switch(key) {
79 case 27:
80 CleanUp();
81 exit(0);
82 break;
83 }
84 glutPostRedisplay();
85 }
86
87
88
89 static void
90 MakeTexture(void)
91 {
92 #define SZ 16
93 GLubyte image[SZ][SZ][4];
94 GLuint i, j;
95
96 for (i = 0; i < SZ; i++) {
97 for (j = 0; j < SZ; j++) {
98 if ((i + j) & 1) {
99 image[i][j][0] = 0;
100 image[i][j][1] = 0;
101 image[i][j][2] = 0;
102 image[i][j][3] = 255;
103 }
104 else {
105 image[i][j][0] = j * 255 / (SZ-1);
106 image[i][j][1] = i * 255 / (SZ-1);
107 image[i][j][2] = 0;
108 image[i][j][3] = 255;
109 }
110 }
111 }
112
113 glActiveTexture(GL_TEXTURE0); /* unit 0 */
114 glBindTexture(GL_TEXTURE_2D, 42);
115 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SZ, SZ, 0,
116 GL_RGBA, GL_UNSIGNED_BYTE, image);
117 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Filter);
118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, Filter);
119 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
120 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
121 #undef SZ
122 }
123
124
125 static void
126 Init(void)
127 {
128 static const char *fragShaderText =
129 "#version 120 \n"
130 "uniform sampler2D tex0; \n"
131 "void main() { \n"
132 " gl_FragColor = texture2D(tex0, gl_PointCoord.xy, 0.0); \n"
133 "}\n";
134 static const char *vertShaderText =
135 "void main() {\n"
136 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
137 "}\n";
138
139 if (!ShadersSupported())
140 exit(1);
141
142 vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
143 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
144 program = LinkShaders(vertShader, fragShader);
145
146 glUseProgram(program);
147
148 tex0 = glGetUniformLocation(program, "tex0");
149 printf("Uniforms: tex0: %d\n", tex0);
150
151 glUniform1i(tex0, 0); /* tex unit 0 */
152
153 /*assert(glGetError() == 0);*/
154
155 glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
156
157 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
158
159 assert(glIsProgram(program));
160 assert(glIsShader(fragShader));
161 assert(glIsShader(vertShader));
162
163 MakeTexture();
164
165 glEnable(GL_POINT_SPRITE);
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 glutInitWindowSize(WinWidth, WinHeight);
191 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
192 win = glutCreateWindow(argv[0]);
193 glewInit();
194 glutReshapeFunc(Reshape);
195 glutKeyboardFunc(Key);
196 glutDisplayFunc(Redisplay);
197 ParseOptions(argc, argv);
198 Init();
199 glutMainLoop();
200 return 0;
201 }