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