2 * Test linking of multiple compilation units.
16 #include "shaderutil.h"
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
;
24 static GLuint VertShader1
;
25 static GLuint VertShader2
;
26 static GLuint FragShader1
;
27 static GLuint FragShader2
;
28 static GLuint Program
;
30 static GLint uDiffuse
;
31 static GLint uSpecular
;
32 static GLint uTexture
;
35 static GLboolean anim
= GL_TRUE
;
39 static const char *FragShaderSource1
=
40 "float compute_dotprod(const vec3 normal) \n"
42 " float dotProd = max(dot(gl_LightSource[0].position.xyz, \n"
43 " normalize(normal)), 0.0); \n"
47 static const char *FragShaderSource2
=
48 "uniform vec4 diffuse;\n"
49 "uniform vec4 specular;\n"
50 "varying vec3 normal;\n"
52 "// external function \n"
53 "float compute_dotprod(const vec3 normal); \n"
57 " float dotProd = compute_dotprod(normal); \n"
58 " gl_FragColor = diffuse * dotProd + specular * pow(dotProd, 20.0); \n"
62 static const char *VertShaderSource1
=
63 "vec3 compute_normal() \n"
65 " return gl_NormalMatrix * gl_Normal; \n"
68 static const char *VertShaderSource2
=
69 "varying vec3 normal;\n"
71 "// external function \n"
72 "vec3 compute_normal(); \n"
76 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \n"
77 " normal = compute_normal(); \n"
82 normalize(GLfloat
*dst
, const GLfloat
*src
)
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
;
97 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
99 /* update light position */
100 normalize(vec
, lightPos
);
101 glLightfv(GL_LIGHT0
, GL_POSITION
, vec
);
103 glutSolidSphere(2.0, 10, 5);
112 lightPos
[0] += delta
;
113 if (lightPos
[0] > 25.0f
|| lightPos
[0] < -25.0f
)
120 Reshape(int width
, int height
)
122 glViewport(0, 0, width
, height
);
123 glMatrixMode(GL_PROJECTION
);
125 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
126 glMatrixMode(GL_MODELVIEW
);
128 glTranslatef(0.0f
, 0.0f
, -15.0f
);
135 glDeleteShader_func(VertShader1
);
136 glDeleteShader_func(VertShader2
);
137 glDeleteShader_func(FragShader1
);
138 glDeleteShader_func(FragShader2
);
139 glDeleteProgram_func(Program
);
140 glutDestroyWindow(Win
);
145 Key(unsigned char key
, int x
, int y
)
175 CheckLink(GLuint prog
)
178 glGetProgramiv_func(prog
, GL_LINK_STATUS
, &stat
);
182 glGetProgramInfoLog_func(prog
, 1000, &len
, log
);
183 fprintf(stderr
, "Linker error:\n%s\n", log
);
191 if (!ShadersSupported())
196 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
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
);
203 Program
= glCreateProgram_func();
204 glAttachShader_func(Program
, VertShader1
);
205 glAttachShader_func(Program
, VertShader2
);
206 glAttachShader_func(Program
, FragShader1
);
207 glAttachShader_func(Program
, FragShader2
);
209 glLinkProgram_func(Program
);
213 glUseProgram_func(Program
);
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
);
221 glUniform4fv_func(uDiffuse
, 1, diffuse
);
222 glUniform4fv_func(uSpecular
, 1, specular
);
224 glClearColor(0.3f
, 0.3f
, 0.3f
, 0.0f
);
225 glEnable(GL_DEPTH_TEST
);
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
);
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
));
242 main(int argc
, char *argv
[])
244 glutInit(&argc
, argv
);
245 glutInitWindowSize(300, 300);
246 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
247 Win
= glutCreateWindow(argv
[0]);
248 glutReshapeFunc(Reshape
);
249 glutKeyboardFunc(Key
);
250 glutDisplayFunc(Redisplay
);