-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
rendering_plugins.cpp
670 lines (569 loc) · 21.6 KB
/
rendering_plugins.cpp
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
#include <cmath>
#include "rendering_plugins.h"
#include "color.h"
extern color (*colormap)(double);
void Core::setup()
{
{
const unsigned long n_triangles = ntheta;
const unsigned long n_vertices = n_triangles+1;
vertices = new GLfloat[ n_vertices * coordinates_per_vertex ];
vertex_colors = new GLfloat[ n_vertices * colors_per_vertex ];
triangle_vertex_indices = new GLuint[ n_triangles * vertices_per_triangle ];
const float dtheta = 2.*M_PI/n_triangles;
const float r = r_inner;
color core_color = colormap(1.0);
//one vertex at the origin
vertices[0] = 0.0f;
vertices[1] = 0.0f;
vertex_colors[0] = core_color.R;
vertex_colors[1] = core_color.G;
vertex_colors[2] = core_color.B;
unsigned long v = 2, c=3, i=0; //start at the next vertex index
for (unsigned long n = 0; n < n_triangles; ++n)
{
vertex_colors[c + 0] = core_color.R;
vertex_colors[c + 1] = core_color.G;
vertex_colors[c + 2] = core_color.B;
triangle_vertex_indices[i + 0] = 0;
triangle_vertex_indices[i + 1] = n+1;
triangle_vertex_indices[i + 2] = (n == n_triangles-1 ? 1 : n+2);
v += coordinates_per_vertex;
c += colors_per_vertex;
i += vertices_per_triangle;
}
glGenBuffers(1, &plugin_triangle_vertex_indices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, plugin_triangle_vertex_indices);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*n_triangles*vertices_per_triangle, triangle_vertex_indices, GL_STATIC_DRAW);
glGenBuffers(1, &plugin_vertices);
glGenBuffers(1, &plugin_vertex_colors);
glBindBuffer(GL_ARRAY_BUFFER, plugin_vertex_colors);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*n_vertices*colors_per_vertex, vertex_colors, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
GLint compile_ok = GL_FALSE, link_ok = GL_FALSE;
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
const char *vs_source =
#ifdef GL_ES_VERSION_2_0
"#version 100\n" // OpenGL ES 2.0
"precision mediump float;"
#else
"#version 120\n" // OpenGL 2.1
#endif
"attribute vec2 plugin_coord2d;"
"attribute vec3 plugin_v_color;"
"varying vec3 f_color;"
"void main(void) {"
" f_color = plugin_v_color;"
" gl_Position = vec4(plugin_coord2d, 0.0, 1.0);"
"}";
glShaderSource(vs, 1, &vs_source, NULL);
glCompileShader(vs);
glGetShaderiv(vs, GL_COMPILE_STATUS, &compile_ok);
if (!compile_ok) {
fprintf(stderr, "Error in vertex shader\n");
return;
}
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
const char *fs_source =
#ifdef GL_ES_VERSION_2_0
"#version 100\n" // OpenGL ES 2.0
"precision mediump float;"
#else
"#version 120\n" // OpenGL 2.1
#endif
"varying vec3 f_color;"
"void main(void) {"
" gl_FragColor = vec4(f_color, 1.0);"
"}";
glShaderSource(fs, 1, &fs_source, NULL);
glCompileShader(fs);
glGetShaderiv(fs, GL_COMPILE_STATUS, &compile_ok);
if (!compile_ok) {
fprintf(stderr, "Error in fragment shader\n");
return;
}
plugin_program = glCreateProgram();
glAttachShader(plugin_program, vs);
glAttachShader(plugin_program, fs);
glLinkProgram(plugin_program);
glGetProgramiv(plugin_program, GL_LINK_STATUS, &link_ok);
if (!link_ok) {
fprintf(stderr, "glLinkProgram:");
return;
}
const char* attribute_name = "plugin_coord2d";
plugin_attribute_coord2d = glGetAttribLocation(plugin_program, attribute_name);
if (plugin_attribute_coord2d == -1) {
fprintf(stderr, "Could not bind attribute %s\n", attribute_name);
return;
}
attribute_name = "plugin_v_color";
plugin_attribute_v_color = glGetAttribLocation(plugin_program, attribute_name);
if (plugin_attribute_v_color == -1) {
fprintf(stderr, "Could not bind attribute %s\n", attribute_name);
return;
}
return;
}
void Core::draw()
{
const unsigned long n_triangles = ntheta;
const unsigned long n_vertices = n_triangles+1;
const float dtheta = 2.*M_PI/n_triangles;
const float angle = sim.spin_angle();
const float a = 1.0f;
const float b = 1.0f-flattening;
unsigned long v = 2; //start at the next vertex index after the one in the center
for (unsigned long n = 0; n < n_triangles; ++n)
{
const float theta = dtheta*n;
//Vertices of the inner ellipse
vertices[v + 0] = r_inner*(a+b)/2. * std::cos(theta) +
r_inner*(a-b)/2. * std::cos(-theta+ 2.*(angle+M_PI/2.));
vertices[v + 1] = r_inner*(a+b)/2. * std::sin(theta) +
r_inner*(a-b)/2. * std::sin(-theta+ 2.*(angle+M_PI/2.));
v += coordinates_per_vertex;
}
glUseProgram(plugin_program);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, plugin_triangle_vertex_indices);
glEnableVertexAttribArray(plugin_attribute_coord2d);
glBindBuffer(GL_ARRAY_BUFFER, plugin_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*n_vertices*coordinates_per_vertex, vertices, GL_DYNAMIC_DRAW);
glVertexAttribPointer(
plugin_attribute_coord2d, // attribute
2, // number of elements per vertex (x,y)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
0 // offset of first element
);
glEnableVertexAttribArray(plugin_attribute_v_color);
glBindBuffer(GL_ARRAY_BUFFER, plugin_vertex_colors);
glVertexAttribPointer(
plugin_attribute_v_color, // attribute
3, // number of elements per verte (r,g,b)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
0);
glDrawElements(GL_TRIANGLES, n_triangles*vertices_per_triangle, GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(plugin_attribute_coord2d);
glDisableVertexAttribArray(plugin_attribute_v_color);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Core::cleanup()
{
delete[] vertices;
delete[] vertex_colors;
delete[] triangle_vertex_indices;
glDeleteProgram(plugin_program);
glDeleteBuffers(1, &plugin_vertices);
glDeleteBuffers(1, &plugin_vertex_colors);
glDeleteBuffers(1, &plugin_triangle_vertex_indices);
}
void Axis::setup()
{
{
const unsigned long n_triangles = 2 /*axis*/ + 2 /*arrowheads*/;
const unsigned long n_vertices = 4 /*axis*/ + 6 /*arrowheads*/;
vertices = new GLfloat[ n_vertices * coordinates_per_vertex ];
vertex_colors = new GLfloat[ n_vertices * colors_per_vertex ];
triangle_vertex_indices = new GLuint[ n_triangles * vertices_per_triangle ];
color arrow_color;
arrow_color.R = 0.15; arrow_color.G=0.15; arrow_color.B=0.15;
unsigned short c = 0;
for (unsigned short i=0; i<n_vertices; ++i)
{
vertex_colors[c + 0] = arrow_color.R;
vertex_colors[c + 1] = arrow_color.G;
vertex_colors[c + 2] = arrow_color.B;
c += colors_per_vertex;
}
//Axis
triangle_vertex_indices[0] = 0;
triangle_vertex_indices[1] = 1;
triangle_vertex_indices[2] = 2;
triangle_vertex_indices[3] = 0;
triangle_vertex_indices[4] = 2;
triangle_vertex_indices[5] = 3;
//Arrowheads
triangle_vertex_indices[6] = 4;
triangle_vertex_indices[7] = 5;
triangle_vertex_indices[8] = 6;
triangle_vertex_indices[9] = 7;
triangle_vertex_indices[10] = 8;
triangle_vertex_indices[11] = 9;
glGenBuffers(1, &plugin_triangle_vertex_indices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, plugin_triangle_vertex_indices);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*n_triangles*vertices_per_triangle, triangle_vertex_indices, GL_STATIC_DRAW);
glGenBuffers(1, &plugin_vertices);
glBindBuffer(GL_ARRAY_BUFFER, plugin_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*n_vertices*coordinates_per_vertex, vertices, GL_DYNAMIC_DRAW);
glGenBuffers(1, &plugin_vertex_colors);
glBindBuffer(GL_ARRAY_BUFFER, plugin_vertex_colors);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*n_vertices*colors_per_vertex, vertex_colors, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
GLint compile_ok = GL_FALSE, link_ok = GL_FALSE;
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
const char *vs_source =
#ifdef GL_ES_VERSION_2_0
"#version 100\n" // OpenGL ES 2.0
"precision mediump float;"
#else
"#version 120\n" // OpenGL 2.1
#endif
"attribute vec2 plugin_coord2d;"
"attribute vec3 plugin_v_color;"
"varying vec3 f_color;"
"void main(void) {"
" f_color = plugin_v_color;"
" gl_Position = vec4(plugin_coord2d, 0.0, 1.0);"
"}";
glShaderSource(vs, 1, &vs_source, NULL);
glCompileShader(vs);
glGetShaderiv(vs, GL_COMPILE_STATUS, &compile_ok);
if (!compile_ok) {
fprintf(stderr, "Error in vertex shader\n");
return;
}
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
const char *fs_source =
#ifdef GL_ES_VERSION_2_0
"#version 100\n" // OpenGL ES 2.0
"precision mediump float;"
#else
"#version 120\n" // OpenGL 2.1
#endif
"varying vec3 f_color;"
"void main(void) {"
" gl_FragColor = vec4(f_color, 1.0);"
"}";
glShaderSource(fs, 1, &fs_source, NULL);
glCompileShader(fs);
glGetShaderiv(fs, GL_COMPILE_STATUS, &compile_ok);
if (!compile_ok) {
fprintf(stderr, "Error in fragment shader\n");
return;
}
plugin_program = glCreateProgram();
glAttachShader(plugin_program, vs);
glAttachShader(plugin_program, fs);
glLinkProgram(plugin_program);
glGetProgramiv(plugin_program, GL_LINK_STATUS, &link_ok);
if (!link_ok) {
fprintf(stderr, "glLinkProgram:");
return;
}
const char* attribute_name = "plugin_coord2d";
plugin_attribute_coord2d = glGetAttribLocation(plugin_program, attribute_name);
if (plugin_attribute_coord2d == -1) {
fprintf(stderr, "Could not bind attribute %s\n", attribute_name);
return;
}
attribute_name = "plugin_v_color";
plugin_attribute_v_color = glGetAttribLocation(plugin_program, attribute_name);
if (plugin_attribute_v_color == -1) {
fprintf(stderr, "Could not bind attribute %s\n", attribute_name);
return;
}
return;
}
void Axis::draw()
{
const unsigned long n_triangles = 2 /*axis*/ + 2 /*arrowheads*/;
const unsigned long n_vertices = 4 /*axis*/ + 6 /*arrowheads*/;
const float d2r = M_PI/180.0;
const float theta = float( sim.spin_angle() );
const float r = 0.8 * r_inner * (1.0f-flattening);
const float arrowhead = 0.1*r_inner;
const float dtheta = 0.5/r_inner * d2r;
//Draw the axis
vertices[0] = r * std::cos(theta - dtheta);
vertices[1] = r * std::sin(theta - dtheta);
vertices[2] = -r * std::cos(theta + dtheta);
vertices[3] = -r * std::sin(theta + dtheta);
vertices[4] = -r * std::cos(theta - dtheta);
vertices[5] = -r * std::sin(theta - dtheta);
vertices[6] = r * std::cos(theta + dtheta);
vertices[7] = r * std::sin(theta + dtheta);
//Draw the arrowheads
//first arrowhead
vertices[8] = (r + arrowhead) * std::cos(theta);
vertices[9] = (r + arrowhead) * std::sin(theta);
vertices[10] = (r - arrowhead) * std::cos(theta + 4.*dtheta);
vertices[11] = (r - arrowhead) * std::sin(theta + 4.*dtheta);
vertices[12] = (r - arrowhead) * std::cos(theta - 4.*dtheta);
vertices[13] = (r - arrowhead) * std::sin(theta - 4.*dtheta);
//second arrowhead
vertices[14] = -(r + arrowhead) * std::cos(theta);
vertices[15] = -(r + arrowhead) * std::sin(theta);
vertices[16] = -(r - arrowhead) * std::cos(theta + 4.*dtheta);
vertices[17] = -(r - arrowhead) * std::sin(theta + 4.*dtheta);
vertices[18] = -(r - arrowhead) * std::cos(theta - 4.*dtheta);
vertices[19] = -(r - arrowhead) * std::sin(theta - 4.*dtheta);
glEnable(GL_BLEND);
#ifndef __EMSCRIPTEN__
glEnable(GL_POLYGON_SMOOTH);
#endif
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(plugin_program);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, plugin_triangle_vertex_indices);
glEnableVertexAttribArray(plugin_attribute_coord2d);
glBindBuffer(GL_ARRAY_BUFFER, plugin_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*n_vertices*coordinates_per_vertex, vertices, GL_DYNAMIC_DRAW);
glVertexAttribPointer(
plugin_attribute_coord2d, // attribute
2, // number of elements per vertex (x,y)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
0 // offset of first element
);
glEnableVertexAttribArray(plugin_attribute_v_color);
glBindBuffer(GL_ARRAY_BUFFER, plugin_vertex_colors);
glVertexAttribPointer(
plugin_attribute_v_color, // attribute
3, // number of elements per verte (r,g,b)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
0);
glDrawElements(GL_TRIANGLES, n_triangles*vertices_per_triangle, GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(plugin_attribute_coord2d);
glDisableVertexAttribArray(plugin_attribute_v_color);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisable(GL_BLEND);
#ifndef __EMSCRIPTEN__
glDisable(GL_POLYGON_SMOOTH);
#endif
}
void Axis::cleanup()
{
delete[] vertices;
delete[] vertex_colors;
delete[] triangle_vertex_indices;
glDeleteProgram(plugin_program);
glDeleteBuffers(1, &plugin_vertices);
glDeleteBuffers(1, &plugin_vertex_colors);
glDeleteBuffers(1, &plugin_triangle_vertex_indices);
}
void Seismograph::clear_record()
{
unsigned int v = 0;
for (unsigned int i=0; i<n_lines+1; ++i)
{
vertices[v + 1] = 0.f;
v += coordinates_per_vertex;
}
}
void Seismograph::setup()
{
{
vertices = new GLfloat[ n_vertices * coordinates_per_vertex ];
vertex_colors = new GLfloat[ n_vertices * (colors_per_vertex+1) ];
line_vertex_indices = new GLuint[ n_lines * vertices_per_line + 1 * vertices_per_triangle /*seismometer*/];
//Begin with vertices all recording zero
unsigned int v = 0;
for (unsigned int i=0; i<n_lines+1; ++i)
{
vertices[v + 0] = -0.9f*r_inner*(1.0f-flattening) + float(i)/float(n_vertices) * 1.8f*r_inner*(1.0f-flattening);
vertices[v + 1] = 0.f;
v += coordinates_per_vertex;
}
color line_color;
line_color.R = 0.0; line_color.G=0.0; line_color.B=0.0;
unsigned int c = 0;
for (unsigned int i=0; i<n_lines+1; ++i)
{
vertex_colors[c + 0] = line_color.R;
vertex_colors[c + 1] = line_color.G;
vertex_colors[c + 2] = line_color.B;
//Add transparency to the edges
if ( i < (n_lines+1)/6)
vertex_colors[c + 3] = 6*float(i)/float(n_vertices);
else if ( i > 4 * (n_lines+1)/6 )
vertex_colors[c + 3] = 6*float(n_lines+1 - i)/float(n_lines+1);
else
vertex_colors[c + 3] = 1.0;
c += colors_per_vertex+1;
}
for (unsigned int i =0; i<n_lines; ++i)
{
line_vertex_indices[vertices_per_line*i + 0] = i;
line_vertex_indices[vertices_per_line*i + 1] = i+1;
}
//Fill the vertex information about the seismometer
line_vertex_indices[ (n_lines*vertices_per_line) + 0 ] = n_vertices - 3;
line_vertex_indices[ (n_lines*vertices_per_line) + 1 ] = n_vertices - 2;
line_vertex_indices[ (n_lines*vertices_per_line) + 2 ] = n_vertices - 1;
for (int i=vertices_per_triangle; i>0; --i)
{
vertex_colors[(n_vertices-i)*(colors_per_vertex+1) + 0] = 0.0;//line_color.R;
vertex_colors[(n_vertices-i)*(colors_per_vertex+1)+ 1] = 0.3;//line_color.G;
vertex_colors[(n_vertices-i)*(colors_per_vertex+1) + 2] = 1.0;//line_color.B;
vertex_colors[(n_vertices-i)*(colors_per_vertex+1) + 3] = 0.8;
}
glGenBuffers(1, &plugin_line_vertex_indices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, plugin_line_vertex_indices);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*(n_lines*vertices_per_line + vertices_per_triangle), line_vertex_indices, GL_STATIC_DRAW);
glGenBuffers(1, &plugin_vertices);
glBindBuffer(GL_ARRAY_BUFFER, plugin_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*n_vertices*coordinates_per_vertex, vertices, GL_DYNAMIC_DRAW);
glGenBuffers(1, &plugin_vertex_colors);
glBindBuffer(GL_ARRAY_BUFFER, plugin_vertex_colors);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*n_vertices*(colors_per_vertex+1), vertex_colors, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
GLint compile_ok = GL_FALSE, link_ok = GL_FALSE;
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
const char *vs_source =
#ifdef GL_ES_VERSION_2_0
"#version 100\n" // OpenGL ES 2.0
"precision mediump float;"
#else
"#version 120\n" // OpenGL 2.1
#endif
"attribute vec2 plugin_coord2d;"
"attribute vec4 plugin_v_color;"
"varying vec4 f_color;"
"void main(void) {"
" f_color = plugin_v_color;"
" gl_Position = vec4(plugin_coord2d, 0.0, 1.0);"
"}";
glShaderSource(vs, 1, &vs_source, NULL);
glCompileShader(vs);
glGetShaderiv(vs, GL_COMPILE_STATUS, &compile_ok);
if (!compile_ok) {
fprintf(stderr, "Error in vertex shader\n");
return;
}
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
const char *fs_source =
#ifdef GL_ES_VERSION_2_0
"#version 100\n" // OpenGL ES 2.0
"precision mediump float;"
#else
"#version 120\n" // OpenGL 2.1
#endif
"varying vec4 f_color;"
"void main(void) {"
" gl_FragColor = f_color;"
"}";
glShaderSource(fs, 1, &fs_source, NULL);
glCompileShader(fs);
glGetShaderiv(fs, GL_COMPILE_STATUS, &compile_ok);
if (!compile_ok) {
fprintf(stderr, "Error in fragment shader\n");
return;
}
plugin_program = glCreateProgram();
glAttachShader(plugin_program, vs);
glAttachShader(plugin_program, fs);
glLinkProgram(plugin_program);
glGetProgramiv(plugin_program, GL_LINK_STATUS, &link_ok);
if (!link_ok) {
fprintf(stderr, "glLinkProgram:");
return;
}
const char* attribute_name = "plugin_coord2d";
plugin_attribute_coord2d = glGetAttribLocation(plugin_program, attribute_name);
if (plugin_attribute_coord2d == -1) {
fprintf(stderr, "Could not bind attribute %s\n", attribute_name);
return;
}
attribute_name = "plugin_v_color";
plugin_attribute_v_color = glGetAttribLocation(plugin_program, attribute_name);
if (plugin_attribute_v_color == -1) {
fprintf(stderr, "Could not bind attribute %s\n", attribute_name);
return;
}
return;
}
void Seismograph::draw()
{
//Begin with vertices all recording zero
unsigned int v = 0;
for (unsigned int i=0; i<n_lines+1; ++i)
{
vertices[v + 1] = vertices[v+3];
v += coordinates_per_vertex;
}
vertices[(n_lines+1)*coordinates_per_vertex - 1] = sim.seismometer_reading() * 0.7*r_inner;
//Fill the vertex information about the seismometer
double theta, r;
sim.seismometer_position(theta, r);
r += r_inner;
//Information about ellipticity
const float angle = sim.spin_angle();
const float a = 1.0f;
const float b = 1.0f-flattening;
//Position of the seismometer in (possibly) elliptical space
const float seis_x = r*(a+b)/2. * std::cos(theta) +
r*(a-b)/2. * std::cos(-theta+ 2.*(angle+M_PI/2.));
const float seis_y = r*(a+b)/2. * std::sin(theta) +
r*(a-b)/2. * std::sin(-theta+ 2.*(angle+M_PI/2.));
//Seismometer vertices
vertices[ (n_lines+1)*coordinates_per_vertex + 0] = seis_x-0.04;
vertices[ (n_lines+1)*coordinates_per_vertex + 1] = seis_y+0.04;
vertices[ (n_lines+2)*coordinates_per_vertex + 0] = seis_x+0.04;
vertices[ (n_lines+2)*coordinates_per_vertex + 1] = seis_y+0.04;
vertices[ (n_lines+3)*coordinates_per_vertex + 0] = seis_x;
vertices[ (n_lines+3)*coordinates_per_vertex + 1] = seis_y-0.04;
glEnable(GL_BLEND);
#ifndef __EMSCRIPTEN__
glEnable(GL_LINE_SMOOTH);
#endif
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLineWidth(2.0);
glUseProgram(plugin_program);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, plugin_line_vertex_indices);
glEnableVertexAttribArray(plugin_attribute_coord2d);
glBindBuffer(GL_ARRAY_BUFFER, plugin_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*n_vertices*coordinates_per_vertex, vertices, GL_DYNAMIC_DRAW);
glVertexAttribPointer(
plugin_attribute_coord2d, // attribute
2, // number of elements per vertex (x,y)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
0 // offset of first element
);
glEnableVertexAttribArray(plugin_attribute_v_color);
glBindBuffer(GL_ARRAY_BUFFER, plugin_vertex_colors);
glVertexAttribPointer(
plugin_attribute_v_color, // attribute
4, // number of elements per verte (r,g,b,a)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
0);
glDrawElements(GL_LINES, n_lines*vertices_per_line, GL_UNSIGNED_INT, 0);
//Draw seismometer
glDrawElements(GL_TRIANGLES, vertices_per_triangle, GL_UNSIGNED_INT, (void*)(((n_lines)*vertices_per_line)*sizeof(GLuint)));
glDisableVertexAttribArray(plugin_attribute_coord2d);
glDisableVertexAttribArray(plugin_attribute_v_color);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glLineWidth(1.0);
glDisable(GL_BLEND);
#ifndef __EMSCRIPTEN__
glDisable(GL_LINE_SMOOTH);
#endif
}
void Seismograph::cleanup()
{
delete[] vertices;
delete[] vertex_colors;
delete[] line_vertex_indices;
glDeleteProgram(plugin_program);
glDeleteBuffers(1, &plugin_vertices);
glDeleteBuffers(1, &plugin_vertex_colors);
glDeleteBuffers(1, &plugin_line_vertex_indices);
}