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