132001f49Smrg/**
232001f49Smrg * Test linking of multiple compilation units.
332001f49Smrg * Brian Paul
432001f49Smrg * 28 March 2009
532001f49Smrg */
632001f49Smrg
732001f49Smrg#include <assert.h>
832001f49Smrg#include <string.h>
932001f49Smrg#include <stdio.h>
1032001f49Smrg#include <stdlib.h>
1132001f49Smrg#include <math.h>
1232001f49Smrg#include <GL/glew.h>
1332001f49Smrg#include "glut_wrap.h"
1432001f49Smrg#include "shaderutil.h"
1532001f49Smrg
1632001f49Smrg
1732001f49Smrgstatic GLfloat diffuse[4] = { 0.5f, 1.0f, 0.5f, 1.0f };
1832001f49Smrgstatic GLfloat specular[4] = { 0.8f, 0.8f, 0.8f, 1.0f };
1932001f49Smrgstatic GLfloat lightPos[4] = { 0.0f, 10.0f, 20.0f, 0.0f };
2032001f49Smrgstatic GLfloat delta = 1.0f;
2132001f49Smrg
2232001f49Smrgstatic GLuint VertShader1;
2332001f49Smrgstatic GLuint VertShader2;
2432001f49Smrgstatic GLuint FragShader1;
2532001f49Smrgstatic GLuint FragShader2;
2632001f49Smrgstatic GLuint Program;
2732001f49Smrg
2832001f49Smrgstatic GLint uDiffuse;
2932001f49Smrgstatic GLint uSpecular;
3032001f49Smrgstatic GLint uTexture;
3132001f49Smrg
3232001f49Smrgstatic GLint Win = 0;
3332001f49Smrgstatic GLboolean anim = GL_TRUE;
3432001f49Smrg
3532001f49Smrg
3632001f49Smrg
3732001f49Smrgstatic const char *FragShaderSource1 =
3832001f49Smrg   "float compute_dotprod(const vec3 normal) \n"
3932001f49Smrg   "{ \n"
4032001f49Smrg   "   float dotProd = max(dot(gl_LightSource[0].position.xyz, \n"
4132001f49Smrg   "                           normalize(normal)), 0.0); \n"
4232001f49Smrg   "   return dotProd; \n"
4332001f49Smrg   "} \n";
4432001f49Smrg
4532001f49Smrgstatic const char *FragShaderSource2 =
4632001f49Smrg   "uniform vec4 diffuse;\n"
4732001f49Smrg   "uniform vec4 specular;\n"
4832001f49Smrg   "varying vec3 normal;\n"
4932001f49Smrg   "\n"
5032001f49Smrg   "// external function \n"
5132001f49Smrg   "float compute_dotprod(const vec3 normal); \n"
5232001f49Smrg   "\n"
5332001f49Smrg   "void main() \n"
5432001f49Smrg   "{ \n"
5532001f49Smrg   "   float dotProd = compute_dotprod(normal); \n"
5632001f49Smrg   "   gl_FragColor = diffuse * dotProd + specular * pow(dotProd, 20.0); \n"
5732001f49Smrg   "} \n";
5832001f49Smrg
5932001f49Smrg
6032001f49Smrgstatic const char *VertShaderSource1 =
6132001f49Smrg   "vec3 compute_normal() \n"
6232001f49Smrg   "{ \n"
6332001f49Smrg   "   return gl_NormalMatrix * gl_Normal; \n"
6432001f49Smrg   "} \n";
6532001f49Smrg
6632001f49Smrgstatic const char *VertShaderSource2 =
6732001f49Smrg   "varying vec3 normal;\n"
6832001f49Smrg   "\n"
6932001f49Smrg   "// external function \n"
7032001f49Smrg   "vec3 compute_normal(); \n"
7132001f49Smrg   "\n"
7232001f49Smrg   "void main() \n"
7332001f49Smrg   "{ \n"
7432001f49Smrg   "   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \n"
7532001f49Smrg   "   normal = compute_normal(); \n"
7632001f49Smrg   "} \n";
7732001f49Smrg
7832001f49Smrg
7932001f49Smrgstatic void
8032001f49Smrgnormalize(GLfloat *dst, const GLfloat *src)
8132001f49Smrg{
8232001f49Smrg   GLfloat len = sqrt(src[0] * src[0] + src[1] * src[1] + src[2] * src[2]);
8332001f49Smrg   dst[0] = src[0] / len;
8432001f49Smrg   dst[1] = src[1] / len;
8532001f49Smrg   dst[2] = src[2] / len;
8632001f49Smrg   dst[3] = src[3];
8732001f49Smrg}
8832001f49Smrg
8932001f49Smrg
9032001f49Smrgstatic void
9132001f49SmrgRedisplay(void)
9232001f49Smrg{
9332001f49Smrg   GLfloat vec[4];
9432001f49Smrg
9532001f49Smrg   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
9632001f49Smrg
9732001f49Smrg   /* update light position */
9832001f49Smrg   normalize(vec, lightPos);
9932001f49Smrg   glLightfv(GL_LIGHT0, GL_POSITION, vec);
10032001f49Smrg
10132001f49Smrg   glutSolidSphere(2.0, 10, 5);
10232001f49Smrg
10332001f49Smrg   glutSwapBuffers();
10432001f49Smrg}
10532001f49Smrg
10632001f49Smrg
10732001f49Smrgstatic void
10832001f49SmrgIdle(void)
10932001f49Smrg{
11032001f49Smrg   lightPos[0] += delta;
11132001f49Smrg   if (lightPos[0] > 25.0f || lightPos[0] < -25.0f)
11232001f49Smrg      delta = -delta;
11332001f49Smrg   glutPostRedisplay();
11432001f49Smrg}
11532001f49Smrg
11632001f49Smrg
11732001f49Smrgstatic void
11832001f49SmrgReshape(int width, int height)
11932001f49Smrg{
12032001f49Smrg   glViewport(0, 0, width, height);
12132001f49Smrg   glMatrixMode(GL_PROJECTION);
12232001f49Smrg   glLoadIdentity();
12332001f49Smrg   glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
12432001f49Smrg   glMatrixMode(GL_MODELVIEW);
12532001f49Smrg   glLoadIdentity();
12632001f49Smrg   glTranslatef(0.0f, 0.0f, -15.0f);
12732001f49Smrg}
12832001f49Smrg
12932001f49Smrg
13032001f49Smrgstatic void
13132001f49SmrgCleanUp(void)
13232001f49Smrg{
13332001f49Smrg   glDeleteShader(VertShader1);
13432001f49Smrg   glDeleteShader(VertShader2);
13532001f49Smrg   glDeleteShader(FragShader1);
13632001f49Smrg   glDeleteShader(FragShader2);
13732001f49Smrg   glDeleteProgram(Program);
13832001f49Smrg   glutDestroyWindow(Win);
13932001f49Smrg}
14032001f49Smrg
14132001f49Smrg
14232001f49Smrgstatic void
14332001f49SmrgKey(unsigned char key, int x, int y)
14432001f49Smrg{
14532001f49Smrg  (void) x;
14632001f49Smrg  (void) y;
14732001f49Smrg
14832001f49Smrg   switch(key) {
14932001f49Smrg   case ' ':
15032001f49Smrg   case 'a':
15132001f49Smrg      anim = !anim;
15232001f49Smrg      if (anim)
15332001f49Smrg         glutIdleFunc(Idle);
15432001f49Smrg      else
15532001f49Smrg         glutIdleFunc(NULL);
15632001f49Smrg      break;
15732001f49Smrg   case 'x':
15832001f49Smrg      lightPos[0] -= 1.0f;
15932001f49Smrg      break;
16032001f49Smrg   case 'X':
16132001f49Smrg      lightPos[0] += 1.0f;
16232001f49Smrg      break;
16332001f49Smrg   case 27:
16432001f49Smrg      CleanUp();
16532001f49Smrg      exit(0);
16632001f49Smrg      break;
16732001f49Smrg   }
16832001f49Smrg   glutPostRedisplay();
16932001f49Smrg}
17032001f49Smrg
17132001f49Smrg
17232001f49Smrgstatic void
17332001f49SmrgCheckLink(GLuint prog)
17432001f49Smrg{
17532001f49Smrg   GLint stat;
17632001f49Smrg   glGetProgramiv(prog, GL_LINK_STATUS, &stat);
17732001f49Smrg   if (!stat) {
17832001f49Smrg      GLchar log[1000];
17932001f49Smrg      GLsizei len;
18032001f49Smrg      glGetProgramInfoLog(prog, 1000, &len, log);
18132001f49Smrg      fprintf(stderr, "Linker error:\n%s\n", log);
18232001f49Smrg   }
18332001f49Smrg}
18432001f49Smrg
18532001f49Smrg
18632001f49Smrgstatic void
18732001f49SmrgInit(void)
18832001f49Smrg{
18932001f49Smrg   if (!ShadersSupported())
19032001f49Smrg      exit(1);
19132001f49Smrg
19232001f49Smrg   printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
19332001f49Smrg
19432001f49Smrg   VertShader1 = CompileShaderText(GL_VERTEX_SHADER, VertShaderSource1);
19532001f49Smrg   VertShader2 = CompileShaderText(GL_VERTEX_SHADER, VertShaderSource2);
19632001f49Smrg   FragShader1 = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderSource1);
19732001f49Smrg   FragShader2 = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderSource2);
19832001f49Smrg
19932001f49Smrg   Program = glCreateProgram();
20032001f49Smrg   glAttachShader(Program, VertShader1);
20132001f49Smrg   glAttachShader(Program, VertShader2);
20232001f49Smrg   glAttachShader(Program, FragShader1);
20332001f49Smrg   glAttachShader(Program, FragShader2);
20432001f49Smrg
20532001f49Smrg   glLinkProgram(Program);
20632001f49Smrg
20732001f49Smrg   CheckLink(Program);
20832001f49Smrg
20932001f49Smrg   glUseProgram(Program);
21032001f49Smrg
21132001f49Smrg   uDiffuse = glGetUniformLocation(Program, "diffuse");
21232001f49Smrg   uSpecular = glGetUniformLocation(Program, "specular");
21332001f49Smrg   uTexture = glGetUniformLocation(Program, "texture");
21432001f49Smrg   printf("DiffusePos %d  SpecularPos %d  TexturePos %d\n",
21532001f49Smrg          uDiffuse, uSpecular, uTexture);
21632001f49Smrg
21732001f49Smrg   glUniform4fv(uDiffuse, 1, diffuse);
21832001f49Smrg   glUniform4fv(uSpecular, 1, specular);
21932001f49Smrg
22032001f49Smrg   glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
22132001f49Smrg   glEnable(GL_DEPTH_TEST);
22232001f49Smrg
22332001f49Smrg   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
22432001f49Smrg   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
22532001f49Smrg   glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 10.0f);
22632001f49Smrg
22732001f49Smrg   assert(glIsProgram(Program));
22832001f49Smrg   assert(glIsShader(VertShader1));
22932001f49Smrg   assert(glIsShader(VertShader2));
23032001f49Smrg   assert(glIsShader(FragShader1));
23132001f49Smrg   assert(glIsShader(FragShader2));
23232001f49Smrg
23332001f49Smrg   glColor3f(1, 0, 0);
23432001f49Smrg}
23532001f49Smrg
23632001f49Smrg
23732001f49Smrgint
23832001f49Smrgmain(int argc, char *argv[])
23932001f49Smrg{
24032001f49Smrg   glutInit(&argc, argv);
24132001f49Smrg   glutInitWindowSize(300, 300);
24232001f49Smrg   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
24332001f49Smrg   Win = glutCreateWindow(argv[0]);
24432001f49Smrg   glewInit();
24532001f49Smrg   glutReshapeFunc(Reshape);
24632001f49Smrg   glutKeyboardFunc(Key);
24732001f49Smrg   glutDisplayFunc(Redisplay);
24832001f49Smrg   if (anim)
24932001f49Smrg      glutIdleFunc(Idle);
25032001f49Smrg   Init();
25132001f49Smrg   glutMainLoop();
25232001f49Smrg   return 0;
25332001f49Smrg}
25432001f49Smrg
25532001f49Smrg
256