Merge remote branch 'origin/master' into radeon-rewrite
[mesa.git] / progs / glsl / linktest.c
1 /**
2 * Test linking of multiple compilation units.
3 * Brian Paul
4 * 28 March 2009
5 */
6
7 #include <assert.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <math.h>
12 #include <GL/gl.h>
13 #include <GL/glut.h>
14 #include <GL/glext.h>
15 #include "extfuncs.h"
16 #include "shaderutil.h"
17
18
19 static GLfloat diffuse[4] = { 0.5f, 1.0f, 0.5f, 1.0f };
20 static GLfloat specular[4] = { 0.8f, 0.8f, 0.8f, 1.0f };
21 static GLfloat lightPos[4] = { 0.0f, 10.0f, 20.0f, 0.0f };
22 static GLfloat delta = 1.0f;
23
24 static GLuint VertShader1;
25 static GLuint VertShader2;
26 static GLuint FragShader1;
27 static GLuint FragShader2;
28 static GLuint Program;
29
30 static GLint uDiffuse;
31 static GLint uSpecular;
32 static GLint uTexture;
33
34 static GLint Win = 0;
35 static GLboolean anim = GL_TRUE;
36
37
38
39 static const char *FragShaderSource1 =
40 "float compute_dotprod(const vec3 normal) \n"
41 "{ \n"
42 " float dotProd = max(dot(gl_LightSource[0].position.xyz, \n"
43 " normalize(normal)), 0.0); \n"
44 " return dotProd; \n"
45 "} \n";
46
47 static const char *FragShaderSource2 =
48 "uniform vec4 diffuse;\n"
49 "uniform vec4 specular;\n"
50 "varying vec3 normal;\n"
51 "\n"
52 "// external function \n"
53 "float compute_dotprod(const vec3 normal); \n"
54 "\n"
55 "void main() \n"
56 "{ \n"
57 " float dotProd = compute_dotprod(normal); \n"
58 " gl_FragColor = diffuse * dotProd + specular * pow(dotProd, 20.0); \n"
59 "} \n";
60
61
62 static const char *VertShaderSource1 =
63 "vec3 compute_normal() \n"
64 "{ \n"
65 " return gl_NormalMatrix * gl_Normal; \n"
66 "} \n";
67
68 static const char *VertShaderSource2 =
69 "varying vec3 normal;\n"
70 "\n"
71 "// external function \n"
72 "vec3 compute_normal(); \n"
73 "\n"
74 "void main() \n"
75 "{ \n"
76 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \n"
77 " normal = compute_normal(); \n"
78 "} \n";
79
80
81 static void
82 normalize(GLfloat *dst, const GLfloat *src)
83 {
84 GLfloat len = sqrt(src[0] * src[0] + src[1] * src[1] + src[2] * src[2]);
85 dst[0] = src[0] / len;
86 dst[1] = src[1] / len;
87 dst[2] = src[2] / len;
88 dst[3] = src[3];
89 }
90
91
92 static void
93 Redisplay(void)
94 {
95 GLfloat vec[4];
96
97 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
98
99 /* update light position */
100 normalize(vec, lightPos);
101 glLightfv(GL_LIGHT0, GL_POSITION, vec);
102
103 glutSolidSphere(2.0, 10, 5);
104
105 glutSwapBuffers();
106 }
107
108
109 static void
110 Idle(void)
111 {
112 lightPos[0] += delta;
113 if (lightPos[0] > 25.0f || lightPos[0] < -25.0f)
114 delta = -delta;
115 glutPostRedisplay();
116 }
117
118
119 static void
120 Reshape(int width, int height)
121 {
122 glViewport(0, 0, width, height);
123 glMatrixMode(GL_PROJECTION);
124 glLoadIdentity();
125 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
126 glMatrixMode(GL_MODELVIEW);
127 glLoadIdentity();
128 glTranslatef(0.0f, 0.0f, -15.0f);
129 }
130
131
132 static void
133 CleanUp(void)
134 {
135 glDeleteShader_func(VertShader1);
136 glDeleteShader_func(VertShader2);
137 glDeleteShader_func(FragShader1);
138 glDeleteShader_func(FragShader2);
139 glDeleteProgram_func(Program);
140 glutDestroyWindow(Win);
141 }
142
143
144 static void
145 Key(unsigned char key, int x, int y)
146 {
147 (void) x;
148 (void) y;
149
150 switch(key) {
151 case ' ':
152 case 'a':
153 anim = !anim;
154 if (anim)
155 glutIdleFunc(Idle);
156 else
157 glutIdleFunc(NULL);
158 break;
159 case 'x':
160 lightPos[0] -= 1.0f;
161 break;
162 case 'X':
163 lightPos[0] += 1.0f;
164 break;
165 case 27:
166 CleanUp();
167 exit(0);
168 break;
169 }
170 glutPostRedisplay();
171 }
172
173
174 static void
175 CheckLink(GLuint prog)
176 {
177 GLint stat;
178 glGetProgramiv_func(prog, GL_LINK_STATUS, &stat);
179 if (!stat) {
180 GLchar log[1000];
181 GLsizei len;
182 glGetProgramInfoLog_func(prog, 1000, &len, log);
183 fprintf(stderr, "Linker error:\n%s\n", log);
184 }
185 }
186
187
188 static void
189 Init(void)
190 {
191 if (!ShadersSupported())
192 exit(1);
193
194 GetExtensionFuncs();
195
196 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
197
198 VertShader1 = CompileShaderText(GL_VERTEX_SHADER, VertShaderSource1);
199 VertShader2 = CompileShaderText(GL_VERTEX_SHADER, VertShaderSource2);
200 FragShader1 = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderSource1);
201 FragShader2 = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderSource2);
202
203 Program = glCreateProgram_func();
204 glAttachShader_func(Program, VertShader1);
205 glAttachShader_func(Program, VertShader2);
206 glAttachShader_func(Program, FragShader1);
207 glAttachShader_func(Program, FragShader2);
208
209 glLinkProgram_func(Program);
210
211 CheckLink(Program);
212
213 glUseProgram_func(Program);
214
215 uDiffuse = glGetUniformLocation_func(Program, "diffuse");
216 uSpecular = glGetUniformLocation_func(Program, "specular");
217 uTexture = glGetUniformLocation_func(Program, "texture");
218 printf("DiffusePos %d SpecularPos %d TexturePos %d\n",
219 uDiffuse, uSpecular, uTexture);
220
221 glUniform4fv_func(uDiffuse, 1, diffuse);
222 glUniform4fv_func(uSpecular, 1, specular);
223
224 glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
225 glEnable(GL_DEPTH_TEST);
226
227 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
228 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
229 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 10.0f);
230
231 assert(glIsProgram_func(Program));
232 assert(glIsShader_func(VertShader1));
233 assert(glIsShader_func(VertShader2));
234 assert(glIsShader_func(FragShader1));
235 assert(glIsShader_func(FragShader2));
236
237 glColor3f(1, 0, 0);
238 }
239
240
241 int
242 main(int argc, char *argv[])
243 {
244 glutInit(&argc, argv);
245 glutInitWindowPosition( 0, 0);
246 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
247 Win = glutCreateWindow(argv[0]);
248 glutReshapeFunc(Reshape);
249 glutKeyboardFunc(Key);
250 glutDisplayFunc(Redisplay);
251 if (anim)
252 glutIdleFunc(Idle);
253 Init();
254 glutMainLoop();
255 return 0;
256 }
257
258