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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
/**
* Test using a geometry shader to implement polygon outlining.
* Using GLSL 1.50 (or any later version)
*
* Based on the technique "Single-Pass Wireframe Rendering" by Andreas
* Bærentzen, Steen Lund Nielsen, Mikkel Gjael, Bent D. Larsen & Niels
* Jaergen Christensen, SIGGRAPH 2006
*
* Brian Paul
* May 2012
*/
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glew.h>
#include "glut_wrap.h"
#include "shaderutil.h"
#include "trackball.h"
static GLint WinWidth = 500, WinHeight = 500;
static GLint Win = 0;
static GLuint VertShader, GeomShader, FragShader, Program;
static GLuint vao, vbo;
static GLboolean Anim = GL_TRUE;
static int uViewportSize = -1, uModelViewProj = -1, uColor = -1;
static const GLfloat Orange[4] = {1.0, 0.6, 0.0, 1};
static float CurQuat[4] = { 0, 0, 0, 1 };
static GLboolean ButtonDown = GL_FALSE;
static GLint ButtonX, ButtonY;
static GLfloat Projection[16];
static void
CheckError(int line)
{
GLenum err = glGetError();
if (err) {
printf("GL Error %s (0x%x) at line %d\n",
gluErrorString(err), (int) err, line);
}
}
static void
mat_identity(GLfloat mat[16])
{
memset(mat, 0, 16*sizeof(GLfloat));
mat[0] = mat[5] = mat[10] = mat[15] = 1.0;
}
static void
mat_translate(GLfloat mat[16], float tx, float ty, float tz)
{
mat_identity(mat);
mat[12] = tx;
mat[13] = ty;
mat[14] = tx;
}
static void
mat_frustum(GLfloat mat[16],
GLfloat left, GLfloat right,
GLfloat bottom, GLfloat top,
GLfloat nearval, GLfloat farval)
{
GLfloat x, y, a, b, c, d;
x = (2.0F*nearval) / (right-left);
y = (2.0F*nearval) / (top-bottom);
a = (right+left) / (right-left);
b = (top+bottom) / (top-bottom);
c = -(farval+nearval) / ( farval-nearval);
d = -(2.0F*farval*nearval) / (farval-nearval); /* error? */
#define M(row,col) mat[col*4+row]
M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F;
M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F;
M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d;
M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F;
#undef M
}
static void
mat_multiply(GLfloat product[16], const GLfloat a[16], const GLfloat b[16])
{
#define A(row,col) a[(col<<2)+row]
#define B(row,col) b[(col<<2)+row]
#define P(row,col) product[(col<<2)+row]
GLint i;
for (i = 0; i < 4; i++) {
const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
}
#undef A
#undef B
#undef P
}
static void
Redisplay(void)
{
GLfloat rot[4][4];
GLfloat trans[16], mvp[16];
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
/* Build the modelview * projection matrix */
build_rotmatrix(rot, CurQuat);
mat_translate(trans, 0, 0, -10);
mat_multiply(mvp, trans, (GLfloat *) rot);
mat_multiply(mvp, Projection, mvp);
/* Set the MVP matrix */
glUniformMatrix4fv(uModelViewProj, 1, GL_FALSE, (float *) mvp);
/* Draw */
glDrawArrays(GL_TRIANGLES, 0, 3);
glutSwapBuffers();
}
static void
Idle(void)
{
static const float yAxis[3] = {0, 1, 0};
static double t0 = -1.;
float quat[4];
double dt, t = glutGet(GLUT_ELAPSED_TIME) / 2000.0;
if (t0 < 0.0)
t0 = t;
dt = t - t0;
t0 = t;
axis_to_quat(yAxis, 2.0 * dt, quat);
add_quats(quat, CurQuat, CurQuat);
glutPostRedisplay();
}
static void
Reshape(int width, int height)
{
float ar = (float) width / height;
WinWidth = width;
WinHeight = height;
glViewport(0, 0, width, height);
mat_frustum(Projection, -ar, ar, -1, 1, 3, 25);
/* pass viewport dims to the shader */
{
GLfloat viewport[4];
glGetFloatv(GL_VIEWPORT, viewport);
glUniform2f(uViewportSize, viewport[2], viewport[3]);
}
}
static void
MouseMotion(int x, int y)
{
if (ButtonDown) {
float x0 = (2.0 * ButtonX - WinWidth) / WinWidth;
float y0 = (WinHeight - 2.0 * ButtonY) / WinHeight;
float x1 = (2.0 * x - WinWidth) / WinWidth;
float y1 = (WinHeight - 2.0 * y) / WinHeight;
float q[4];
trackball(q, x0, y0, x1, y1);
ButtonX = x;
ButtonY = y;
add_quats(q, CurQuat, CurQuat);
glutPostRedisplay();
}
}
static void
MouseButton(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
ButtonDown = GL_TRUE;
ButtonX = x;
ButtonY = y;
}
else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
ButtonDown = GL_FALSE;
}
}
static void
CleanUp(void)
{
glDeleteShader(FragShader);
glDeleteShader(VertShader);
glDeleteShader(GeomShader);
glDeleteProgram(Program);
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo);
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 27:
CleanUp();
exit(0);
break;
}
glutPostRedisplay();
}
static void
Init(void)
{
const GLubyte *version;
static const char *vertShaderText =
"#version 150 \n"
"uniform mat4 ModelViewProjection; \n"
"in vec4 Vertex; \n"
"void main() \n"
"{ \n"
" gl_Position = ModelViewProjection * Vertex; \n"
"} \n";
static const char *geomShaderText =
"#version 150 \n"
"#extension GL_ARB_geometry_shader4: enable \n"
"uniform vec2 ViewportSize; \n"
"out vec2 Vert0, Vert1, Vert2; \n"
"\n"
"// Transform NDC coord to window coord \n"
"vec2 vpxform(vec4 p) \n"
"{ \n"
" return (p.xy / p.w + 1.0) * 0.5 * ViewportSize; \n"
"} \n"
"\n"
"void main() \n"
"{ \n"
" Vert0 = vpxform(gl_in[0].gl_Position); \n"
" Vert1 = vpxform(gl_in[1].gl_Position); \n"
" Vert2 = vpxform(gl_in[2].gl_Position); \n"
" gl_Position = gl_PositionIn[0]; \n"
" EmitVertex(); \n"
" gl_Position = gl_PositionIn[1]; \n"
" EmitVertex(); \n"
" gl_Position = gl_PositionIn[2]; \n"
" EmitVertex(); \n"
"} \n";
static const char *fragShaderText =
"#version 150 \n"
"#define LINE_WIDTH 2.5 \n"
"uniform vec4 Color; \n"
"in vec2 Vert0, Vert1, Vert2; \n"
"out vec4 FragColor; \n"
"// Compute distance from a point to a line \n"
"float point_line_dist(vec2 p, vec2 v1, vec2 v2) \n"
"{ \n"
" float s = (v2.x - v1.x) * (v1.y - p.y) - (v1.x - p.x) * (v2.y - v1.y); \n"
" float t = length(v2 - v1); \n"
" return abs(s) / t; \n"
"} \n"
"\n"
"void main() \n"
"{ \n"
" float d0 = point_line_dist(gl_FragCoord.xy, Vert0, Vert1); \n"
" float d1 = point_line_dist(gl_FragCoord.xy, Vert1, Vert2); \n"
" float d2 = point_line_dist(gl_FragCoord.xy, Vert2, Vert0); \n"
" float m = min(d0, min(d1, d2)); \n"
" FragColor = Color * smoothstep(0.0, LINE_WIDTH, m); \n"
"} \n";
static const GLfloat verts[3][2] = {
{ -1, -1 },
{ 1, -1 },
{ 0, 1 }
};
if (!ShadersSupported())
exit(1);
version = glGetString(GL_VERSION);
if (version[0] * 10 + version[2] < 32) {
fprintf(stderr, "Sorry, OpenGL 3.2 or later required.\n");
exit(1);
}
VertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
FragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
GeomShader = CompileShaderText(GL_GEOMETRY_SHADER_ARB, geomShaderText);
Program = LinkShaders3(VertShader, GeomShader, FragShader);
assert(Program);
CheckError(__LINE__);
glBindAttribLocation(Program, 0, "Vertex");
glBindFragDataLocation(Program, 0, "FragColor");
/*
* The geometry shader will receive and emit triangles.
*/
glProgramParameteriARB(Program, GL_GEOMETRY_INPUT_TYPE_ARB,
GL_TRIANGLES);
glProgramParameteriARB(Program, GL_GEOMETRY_OUTPUT_TYPE_ARB,
GL_TRIANGLE_STRIP);
glProgramParameteriARB(Program,GL_GEOMETRY_VERTICES_OUT_ARB, 3);
CheckError(__LINE__);
/* relink */
glLinkProgramARB(Program);
assert(glIsProgram(Program));
assert(glIsShader(FragShader));
assert(glIsShader(VertShader));
assert(glIsShader(GeomShader));
glUseProgram(Program);
uViewportSize = glGetUniformLocation(Program, "ViewportSize");
uModelViewProj = glGetUniformLocation(Program, "ModelViewProjection");
uColor = glGetUniformLocation(Program, "Color");
glUniform4fv(uColor, 1, Orange);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
glEnable(GL_DEPTH_TEST);
printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
}
int
main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(WinWidth, WinHeight);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
Win = glutCreateWindow(argv[0]);
glewInit();
glutReshapeFunc(Reshape);
glutKeyboardFunc(Key);
glutDisplayFunc(Redisplay);
glutMotionFunc(MouseMotion);
glutMouseFunc(MouseButton);
if (Anim)
glutIdleFunc(Idle);
Init();
glutMainLoop();
return 0;
}
|