summaryrefslogtreecommitdiffstats
path: root/progs/glsl/linktest.c
blob: 601b24e893e02ee9fd478c356efb475cf841f411 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
 * Test linking of multiple compilation units.
 * Brian Paul
 * 28 March 2009
 */

#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glext.h>
#include "extfuncs.h"
#include "shaderutil.h"


static GLfloat diffuse[4] = { 0.5f, 1.0f, 0.5f, 1.0f };
static GLfloat specular[4] = { 0.8f, 0.8f, 0.8f, 1.0f };
static GLfloat lightPos[4] = { 0.0f, 10.0f, 20.0f, 0.0f };
static GLfloat delta = 1.0f;

static GLuint VertShader1;
static GLuint VertShader2;
static GLuint FragShader1;
static GLuint FragShader2;
static GLuint Program;

static GLint uDiffuse;
static GLint uSpecular;
static GLint uTexture;

static GLint Win = 0;
static GLboolean anim = GL_TRUE;



static const char *FragShaderSource1 =
   "float compute_dotprod(const vec3 normal) \n"
   "{ \n"
   "   float dotProd = max(dot(gl_LightSource[0].position.xyz, \n"
   "                           normalize(normal)), 0.0); \n"
   "   return dotProd; \n"
   "} \n";

static const char *FragShaderSource2 =
   "uniform vec4 diffuse;\n"
   "uniform vec4 specular;\n"
   "varying vec3 normal;\n"
   "\n"
   "// external function \n"
   "float compute_dotprod(const vec3 normal); \n"
   "\n"
   "void main() \n"
   "{ \n"
   "   float dotProd = compute_dotprod(normal); \n"
   "   gl_FragColor = diffuse * dotProd + specular * pow(dotProd, 20.0); \n"
   "} \n";


static const char *VertShaderSource1 =
   "vec3 compute_normal() \n"
   "{ \n"
   "   return gl_NormalMatrix * gl_Normal; \n"
   "} \n";

static const char *VertShaderSource2 =
   "varying vec3 normal;\n"
   "\n"
   "// external function \n"
   "vec3 compute_normal(); \n"
   "\n"
   "void main() \n"
   "{ \n"
   "   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \n"
   "   normal = compute_normal(); \n"
   "} \n";


static void
normalize(GLfloat *dst, const GLfloat *src)
{
   GLfloat len = sqrt(src[0] * src[0] + src[1] * src[1] + src[2] * src[2]);
   dst[0] = src[0] / len;
   dst[1] = src[1] / len;
   dst[2] = src[2] / len;
   dst[3] = src[3];
}


static void
Redisplay(void)
{
   GLfloat vec[4];

   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   /* update light position */
   normalize(vec, lightPos);
   glLightfv(GL_LIGHT0, GL_POSITION, vec);
   
   glutSolidSphere(2.0, 10, 5);

   glutSwapBuffers();
}


static void
Idle(void)
{
   lightPos[0] += delta;
   if (lightPos[0] > 25.0f || lightPos[0] < -25.0f)
      delta = -delta;
   glutPostRedisplay();
}


static void
Reshape(int width, int height)
{
   glViewport(0, 0, width, height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glTranslatef(0.0f, 0.0f, -15.0f);
}


static void
CleanUp(void)
{
   glDeleteShader_func(VertShader1);
   glDeleteShader_func(VertShader2);
   glDeleteShader_func(FragShader1);
   glDeleteShader_func(FragShader2);
   glDeleteProgram_func(Program);
   glutDestroyWindow(Win);
}


static void
Key(unsigned char key, int x, int y)
{
  (void) x;
  (void) y;

   switch(key) {
   case ' ':
   case 'a':
      anim = !anim;
      if (anim)
         glutIdleFunc(Idle);
      else
         glutIdleFunc(NULL);
      break;
   case 'x':
      lightPos[0] -= 1.0f;
      break;
   case 'X':
      lightPos[0] += 1.0f;
      break;
   case 27:
      CleanUp();
      exit(0);
      break;
   }
   glutPostRedisplay();
}


static void
CheckLink(GLuint prog)
{
   GLint stat;
   glGetProgramiv_func(prog, GL_LINK_STATUS, &stat);
   if (!stat) {
      GLchar log[1000];
      GLsizei len;
      glGetProgramInfoLog_func(prog, 1000, &len, log);
      fprintf(stderr, "Linker error:\n%s\n", log);
   }
}


static void
Init(void)
{
   if (!ShadersSupported())
      exit(1);

   GetExtensionFuncs();

   printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));

   VertShader1 = CompileShaderText(GL_VERTEX_SHADER, VertShaderSource1);
   VertShader2 = CompileShaderText(GL_VERTEX_SHADER, VertShaderSource2);
   FragShader1 = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderSource1);
   FragShader2 = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderSource2);

   Program = glCreateProgram_func();
   glAttachShader_func(Program, VertShader1);
   glAttachShader_func(Program, VertShader2);
   glAttachShader_func(Program, FragShader1);
   glAttachShader_func(Program, FragShader2);

   glLinkProgram_func(Program);

   CheckLink(Program);

   glUseProgram_func(Program);

   uDiffuse = glGetUniformLocation_func(Program, "diffuse");
   uSpecular = glGetUniformLocation_func(Program, "specular");
   uTexture = glGetUniformLocation_func(Program, "texture");
   printf("DiffusePos %d  SpecularPos %d  TexturePos %d\n",
          uDiffuse, uSpecular, uTexture);

   glUniform4fv_func(uDiffuse, 1, diffuse);
   glUniform4fv_func(uSpecular, 1, specular);

   glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
   glEnable(GL_DEPTH_TEST);

   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
   glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 10.0f);

   assert(glIsProgram_func(Program));
   assert(glIsShader_func(VertShader1));
   assert(glIsShader_func(VertShader2));
   assert(glIsShader_func(FragShader1));
   assert(glIsShader_func(FragShader2));

   glColor3f(1, 0, 0);
}


int
main(int argc, char *argv[])
{
   glutInit(&argc, argv);
   glutInitWindowPosition( 0, 0);
   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
   Win = glutCreateWindow(argv[0]);
   glutReshapeFunc(Reshape);
   glutKeyboardFunc(Key);
   glutDisplayFunc(Redisplay);
   if (anim)
      glutIdleFunc(Idle);
   Init();
   glutMainLoop();
   return 0;
}