-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial14.cpp
1083 lines (852 loc) · 39.3 KB
/
tutorial14.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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Include standard headers
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
// Include standard headers
#include <stdio.h>
#include <stdlib.h>
#include <vector>
// Include GLEW
#include <GL/glew.h>
// Include GLFW
#include <glfw3.h>
GLFWwindow* window;
// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace glm;
#include <common/shader.hpp>
#include <common/texture.hpp>
#include <common/controls.hpp>
#include <common/objloader.hpp>
#include <common/vboindexer.hpp>
glm::vec3 calculateHermit(float u, glm::vec3 p1, glm::vec3 p2, glm::vec3 tangentP1, glm::vec3 tangentP2)
{
return (((2 * pow(u, 3)) - (3 * pow(u, 2)) + 1) * p1) +
(((-2 * pow(u, 3)) + (3 * pow(u, 2))) * p2) +
((pow(u, 3) - (2 * pow(u, 2)) + u) * tangentP1) +
((pow(u, 3) - pow(u, 2)) * tangentP2);
}
// CPU representation of a particle
struct Particle {
glm::vec3 pos, speed;
unsigned char r, g, b, a; // Color
float size, angle, weight;
float life; // Remaining life of the particle. if <0 : dead and unused.
float cameradistance; // *Squared* distance to the camera. if dead : -1.0f
bool isBlue = true;
bool isCentered = false;
//Next attributes are used to throw particule in a hermit curve
float u = 0; //Normalized u used for curvature interpolation
glm::vec3 beginningPos;
glm::vec3 tangent1;
glm::vec3 tangent2;
bool operator<(const Particle& that) const {
// Sort in reverse order : far particles drawn first.
return this->cameradistance > that.cameradistance;
}
};
const int MaxParticles = 10000;
glm::vec3 flammeStart, endFlamme;
Particle ParticlesContainer[MaxParticles];
int LastUsedParticle = 0;
// Finds a Particle in ParticlesContainer which isn't used yet.
// (i.e. life < 0);
int FindUnusedParticle() {
for (int i = LastUsedParticle; i<MaxParticles; i++) {
if (ParticlesContainer[i].life < 0) {
LastUsedParticle = i;
return i;
}
}
for (int i = 0; i<LastUsedParticle; i++) {
if (ParticlesContainer[i].life < 0) {
LastUsedParticle = i;
return i;
}
}
return 0; // All particles are taken, override the first one
}
void SortParticles() {
std::sort(&ParticlesContainer[0], &ParticlesContainer[MaxParticles]);
}
struct Wind
{
glm::vec3 generateWind()
{
if (windStillBlow())
{
return currentWind;
}
else
{
const float windIntensity = 0.1;
float x = windIntensity *((float)rand()) / (float)RAND_MAX;
if ((rand() % 2) == 0) x *= -1;
float y = 0;
float z = windIntensity * ((rand() % 2) * -1) * ((float)rand()) / (float)RAND_MAX;
if ((rand() % 2) == 0) z *= -1;
beginningWind = glfwGetTime();
currentWind = glm::vec3{ x, y, z };
return currentWind;
}
}
private:
float beginningWind = -1;
glm::vec3 currentWind{ 0,0,0 };
bool windStillBlow()
{
if (beginningWind == -1)
{
return false;
}
else
{
auto now = glfwGetTime();
bool isWindStillBlowing = (now - beginningWind) < 0.1f;
return isWindStillBlowing;
}
}
}wind;
int main( void )
{
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
getchar();
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create its OpenGL context
window = glfwCreateWindow( 1024, 768, "Tutorial 14 - Render To Texture", NULL, NULL);
if( window == NULL ){
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
getchar();
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// We would expect width and height to be 1024 and 768
int windowWidth = 1024;
int windowHeight = 768;
// But on MacOS X with a retina screen it'll be 1024*2 and 768*2, so we get the actual framebuffer size:
glfwGetFramebufferSize(window, &windowWidth, &windowHeight);
// Initialize GLEW
glewExperimental = true; // Needed for core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
getchar();
glfwTerminate();
return -1;
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
// Hide the mouse and enable unlimited mouvement
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// Set the mouse at the center of the screen
glfwPollEvents();
glfwSetCursorPos(window, 1024/2, 768/2);
// Dark blue background
glClearColor(0.05f, 0.2f, 0.05f, 0);
// Enable depth test
glEnable(GL_DEPTH_TEST);
// Accept fragment if it closer to the camera than the former one
glDepthFunc(GL_LESS);
// Cull triangles which normal is not towards the camera
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
/////////////////////////////////////
// Create and compile our GLSL program from the shaders
GLuint particleProgramID = LoadShaders("Particle.vertexshader", "Particle.fragmentshader");
// Vertex shader
GLuint CameraRight_worldspace_ID = glGetUniformLocation(particleProgramID, "CameraRight_worldspace");
GLuint CameraUp_worldspace_ID = glGetUniformLocation(particleProgramID, "CameraUp_worldspace");
GLuint ViewProjMatrixID = glGetUniformLocation(particleProgramID, "VP");
// fragment shader
GLuint particleTextureID = glGetUniformLocation(particleProgramID, "myTextureSampler");
static GLfloat* g_particule_position_size_data = new GLfloat[MaxParticles * 4];
static GLubyte* g_particule_color_data = new GLubyte[MaxParticles * 4];
for (int i = 0; i<MaxParticles; i++) {
ParticlesContainer[i].life = -1.0f;
ParticlesContainer[i].cameradistance = -1.0f;
}
GLuint particuleTexture = loadDDS("particle.DDS");
// The VBO containing the 4 vertices of the particles.
// Thanks to instancing, they will be shared by all particles.
static const GLfloat g_vertex_buffer_data[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
};
GLuint billboard_vertex_buffer;
glGenBuffers(1, &billboard_vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, billboard_vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
// The VBO containing the positions and sizes of the particles
GLuint particles_position_buffer;
glGenBuffers(1, &particles_position_buffer);
glBindBuffer(GL_ARRAY_BUFFER, particles_position_buffer);
// Initialize with empty (NULL) buffer : it will be updated later, each frame.
glBufferData(GL_ARRAY_BUFFER, MaxParticles * 4 * sizeof(GLfloat), NULL, GL_STREAM_DRAW);
// The VBO containing the colors of the particles
GLuint particles_color_buffer;
glGenBuffers(1, &particles_color_buffer);
glBindBuffer(GL_ARRAY_BUFFER, particles_color_buffer);
// Initialize with empty (NULL) buffer : it will be updated later, each frame.
glBufferData(GL_ARRAY_BUFFER, MaxParticles * 4 * sizeof(GLubyte), NULL, GL_STREAM_DRAW);
double lastTime = glfwGetTime();
/////////////////////////////////////
// Create and compile our GLSL program from the shaders
GLuint depth_programID = LoadShaders("depth.vertexshader", "depth.fragmentshader");
// Get a handle for our "MVP" uniform
GLuint MatrixDepthID = glGetUniformLocation(depth_programID, "MVP");
/////////////////////////////////////
// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders( "StandardShadingRTT.vertexshader", "StandardShadingRTT.fragmentshader" );
// Get a handle for our "MVP" uniform
GLuint MatrixID = glGetUniformLocation(programID, "MVP");
GLuint ViewMatrixID = glGetUniformLocation(programID, "V");
GLuint ModelMatrixID = glGetUniformLocation(programID, "M");
GLuint DepthBiasID = glGetUniformLocation(programID, "DepthBiasMVP");
// Get a handle for our "myTextureSampler" uniform
GLuint TextureID = glGetUniformLocation(programID, "myTextureSampler");
// Get a handle for our "LightPosition" uniform
GLuint LightID = glGetUniformLocation(programID, "LightPosition_worldspace");
// Load the texture
GLuint Texture = loadBMP_custom("candle.bmp");
// Read our .obj file
std::vector<glm::vec3> vertices;
std::vector<glm::vec2> uvs;
std::vector<glm::vec3> normals;
bool res = loadOBJ("candle.obj", vertices, uvs, normals);
std::vector<unsigned short> indices;
std::vector<glm::vec3> indexed_vertices;
std::vector<glm::vec2> indexed_uvs;
std::vector<glm::vec3> indexed_normals;
indexVBO(vertices, uvs, normals, indices, indexed_vertices, indexed_uvs, indexed_normals);
// Load it into a VBO
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, indexed_vertices.size() * sizeof(glm::vec3), &indexed_vertices[0], GL_STATIC_DRAW);
GLuint uvbuffer;
glGenBuffers(1, &uvbuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glBufferData(GL_ARRAY_BUFFER, indexed_uvs.size() * sizeof(glm::vec2), &indexed_uvs[0], GL_STATIC_DRAW);
GLuint normalbuffer;
glGenBuffers(1, &normalbuffer);
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
glBufferData(GL_ARRAY_BUFFER, indexed_normals.size() * sizeof(glm::vec3), &indexed_normals[0], GL_STATIC_DRAW);
// Generate a buffer for the indices as well
GLuint elementbuffer;
glGenBuffers(1, &elementbuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0], GL_STATIC_DRAW);
// ---------------------------------------------
// Render to Texture - specific code begins here
// ---------------------------------------------
// The framebuffer, which regroups 0, 1, or more textures, and 0 or 1 depth buffer.
GLuint FramebufferName;
glGenFramebuffers(1, &FramebufferName);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
// The texture we're going to render to
GLuint renderedTexture_texID;
glGenTextures(1, &renderedTexture_texID);
// "Bind" the newly created texture : all future texture functions will modify this texture
glBindTexture(GL_TEXTURE_2D, renderedTexture_texID);
// Give an empty image to OpenGL ( the last "0" means "empty" )
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, windowWidth, windowHeight, 0,GL_RGB, GL_UNSIGNED_BYTE, 0);
// Poor filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Set "renderedTexture" as our colour attachement #0
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderedTexture_texID, 0);
//////////////////////////////////////////
//// Depth texture. Slower than a depth buffer, but you can sample it later in your shader
//GLuint depthTexture;
//glGenTextures(1, &depthTexture);
//glBindTexture(GL_TEXTURE_2D, depthTexture);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, 1024, 1024, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
//glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthTexture, 0);
// The framebuffer for candleFrontDepth
GLuint candleFrontDepthBuffer;
glGenFramebuffers(1, &candleFrontDepthBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, candleFrontDepthBuffer);
// Texture that will contain candle front depth
GLuint candleFrontDepth_texID;
glGenTextures(1, &candleFrontDepth_texID);
glBindTexture(GL_TEXTURE_2D, candleFrontDepth_texID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, windowWidth, windowHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Set "candleFrontDepth" as our colour attachement #1
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, candleFrontDepth_texID, 0);
// The framebuffer for candleFrontDepth
GLuint candleBackDepthBuffer;
glGenFramebuffers(1, &candleBackDepthBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, candleBackDepthBuffer);
// Texture that will contain candle thickness
GLuint candleBackDepth_texID;
glGenTextures(1, &candleBackDepth_texID);
glBindTexture(GL_TEXTURE_2D, candleBackDepth_texID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, windowWidth, windowHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Set "candleFrontDepth" as our colour attachement #2
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, candleBackDepth_texID, 0);
// The framebuffer for candleFrontDepth
GLuint candleFrontDepthFromLightBuffer;
glGenFramebuffers(1, &candleFrontDepthFromLightBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, candleFrontDepthFromLightBuffer);
// Texture that will contain candle front depth from light view
GLuint candleFrontDepthFromLight_texID;
glGenTextures(1, &candleFrontDepthFromLight_texID);
glBindTexture(GL_TEXTURE_2D, candleFrontDepthFromLight_texID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, windowWidth, windowHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Set "candleFrontDepth" as our colour attachement #3
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, candleFrontDepthFromLight_texID, 0);
// The framebuffer for candleFrontDepth
GLuint candleBackFromLightBuffer;
glGenFramebuffers(1, &candleBackFromLightBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, candleBackFromLightBuffer);
// Texture that will contain candle thickness front depth from light view
GLuint candleBackDepthFromLight_texID;
glGenTextures(1, &candleBackDepthFromLight_texID);
glBindTexture(GL_TEXTURE_2D, candleBackDepthFromLight_texID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, windowWidth, windowHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Set "candleFrontDepth" as our colour attachement #4
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, candleBackDepthFromLight_texID, 0);
//////////////////////////////////////////
// The depth buffer
GLuint depthrenderbuffer;
glGenRenderbuffers(1, &depthrenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, windowWidth, windowHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer);
// Alternative : Depth texture. Slower, but you can sample it later in your shader
GLuint depthTexture_texID;
glGenTextures(1, &depthTexture_texID);
glBindTexture(GL_TEXTURE_2D, depthTexture_texID);
glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT24, windowWidth, windowHeight, 0,GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Depth texture alternative :
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthTexture_texID, 0);
// Set the list of draw buffers.
GLenum DrawBuffers[1] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers
// Always check that our framebuffer is ok
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
return false;
// The fullscreen quad's FBO
static const GLfloat g_quad_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
};
float cameraDepth = 100.0f;
GLuint quad_vertexbuffer;
glGenBuffers(1, &quad_vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_quad_vertex_buffer_data), g_quad_vertex_buffer_data, GL_STATIC_DRAW);
GLuint fronttex_loc = glGetUniformLocation(programID, "fronttexture");
GLuint thicktex_loc = glGetUniformLocation(programID, "thicknesstexture");
GLuint frontFromLighttex_loc = glGetUniformLocation(programID, "fronttexturefromlight");
GLuint thickFromLighttex_loc = glGetUniformLocation(programID, "thicknesstexturefromlight");
GLuint cameraDepth_loc = glGetUniformLocation(programID, "cameraDepth");
GLuint depth_loc = glGetUniformLocation(programID, "depthTexture");
// Create and compile our GLSL program from the shaders
GLuint quad_programID = LoadShaders( "Passthrough.vertexshader", "WobblyTexture.fragmentshader" );
GLuint tex_loc = glGetUniformLocation(quad_programID, "renderedTexture");
GLuint frontquadtex_loc = glGetUniformLocation(quad_programID, "fronttexture");
GLuint depthtex_loc = glGetUniformLocation(quad_programID, "depthtexture");
GLuint timeID = glGetUniformLocation(quad_programID, "time");
flammeStart = glm::vec3(0, 0, 0);
endFlamme = glm::vec3(0.f, 3.f, 0.f);
do{
// Compute the MVP matrix from keyboard and mouse input
computeMatricesFromInputs();
glm::mat4 ProjectionMatrix = getProjectionMatrix();
glm::mat4 ViewMatrix = getViewMatrix();
glm::mat4 ModelMatrix = glm::mat4(1.0);
glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
glDisable(GL_BLEND);
// ICI, ON VA STOCKER LA PROFONDEUR DE LA SURFACE "FRONT" DE NOTRE OBJET
// Use our shader
glUseProgram(depth_programID);
glCullFace(GL_BACK);
// Render to our framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, candleFrontDepthBuffer);
glViewport(0, 0, windowWidth, windowHeight); // Render on the whole framebuffer, complete from the lower left corner to the upper right
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Send our transformation to the currently bound shader, in the "MVP" uniform
glUniformMatrix4fv(MatrixDepthID, 1, GL_FALSE, &MVP[0][0]);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
// Index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);
// Draw the triangles !
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, (void*)0);
glDisableVertexAttribArray(0);
// ICI, ON VA STOCKER LA PROFONDEUR DE LA SURFACE "back" DE NOTRE OBJET
// Use our shader
glUseProgram(depth_programID);
glCullFace(GL_FRONT);
// Render to our framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, candleBackDepthBuffer);
glViewport(0, 0, windowWidth, windowHeight); // Render on the whole framebuffer, complete from the lower left corner to the upper right
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Send our transformation to the currently bound shader, in the "MVP" uniform
glUniformMatrix4fv(MatrixDepthID, 1, GL_FALSE, &MVP[0][0]);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
// Index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);
// Draw the triangles !
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, (void*)0);
glDisableVertexAttribArray(0);
// ICI, ON VA STOCKER LA PROFONDEUR DE LA SURFACE "back" DE NOTRE OBJET
// Use our shader
glUseProgram(depth_programID);
glCullFace(GL_BACK);
// Render to our framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, depthrenderbuffer);
glViewport(0, 0, windowWidth, windowHeight); // Render on the whole framebuffer, complete from the lower left corner to the upper right
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Compute the MVP matrix from keyboard and mouse input
computeMatricesFromInputs();
ProjectionMatrix = getProjectionMatrix();
ViewMatrix = getViewMatrix();
ViewMatrix[0][2] += 100;
ModelMatrix = glm::mat4(1.0);
MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
// Send our transformation to the currently bound shader, in the "MVP" uniform
glUniformMatrix4fv(MatrixDepthID, 1, GL_FALSE, &MVP[0][0]);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
// Index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);
// Draw the triangles !
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, (void*)0);
glDisableVertexAttribArray(0);
//ProjectionMatrix = glm::perspective(glm::radians(FoV), 4.0f / 3.0f, 0.1f, 100.0f);
//// Camera matrix
//ViewMatrix = glm::lookAt(
// position, // Camera is here
// position + direction, // and looks here : at the same position, plus "direction"
// up // Head is up (set to 0,-1,0 to look upside-down)
//);
glm::vec3 lightPos = endFlamme + wind.generateWind()*4.0f;
//glm::mat4 depthProjectionMatrix = glm::perspective<float>(glm::radians(45.0), 1.0, 0.1f, 20.0);
//glm::mat4 depthViewMatrix = glm::lookAt(lightPos, glm::vec3(0, -1, 0), glm::vec3(0, 0, -1));
//// ProjectionMatrix = glm::perspective(
//glm::mat4 depthModelMatrix = glm::mat4(1.0);
//glm::mat4 depthMVP = depthProjectionMatrix * depthViewMatrix * depthModelMatrix;
// ICI, ON VA STOCKER LA PROFONDEUR DE LA SURFACE "back" DE NOTRE OBJET
// Use our shader
glUseProgram(depth_programID);
glCullFace(GL_BACK);
// Render to our framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, candleFrontDepthFromLightBuffer);
glViewport(0, 0, windowWidth, windowHeight); // Render on the whole framebuffer, complete from the lower left corner to the upper right
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Compute the MVP matrix from keyboard and mouse input
computeMatricesFromInputs();
ProjectionMatrix = glm::perspective<float>(glm::radians(45.0), 1.0, 0.1f, 15);
ViewMatrix = glm::lookAt(lightPos, glm::vec3(0, -1, 0), glm::vec3(0, 0, -1));
ModelMatrix = glm::mat4(1.0);
glm::mat4 depthMVP = MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
// Send our transformation to the currently bound shader, in the "MVP" uniform
glUniformMatrix4fv(MatrixDepthID, 1, GL_FALSE, &MVP[0][0]);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
// Index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);
// Draw the triangles !
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, (void*)0);
glDisableVertexAttribArray(0);
// ICI, ON VA STOCKER LA PROFONDEUR DE LA SURFACE "back" DE NOTRE OBJET
// Use our shader
glUseProgram(depth_programID);
glCullFace(GL_FRONT);
// Render to our framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, candleBackFromLightBuffer);
glViewport(0, 0, windowWidth, windowHeight); // Render on the whole framebuffer, complete from the lower left corner to the upper right
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Compute the MVP matrix from keyboard and mouse input
computeMatricesFromInputs();
//ProjectionMatrix = getProjectionMatrix();
//ViewMatrix = getViewMatrix();
//ModelMatrix = glm::mat4(1.0);
//MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
// Send our transformation to the currently bound shader, in the "MVP" uniform
glUniformMatrix4fv(MatrixDepthID, 1, GL_FALSE, &MVP[0][0]);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
// Index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);
// Draw the triangles !
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, (void*)0);
glDisableVertexAttribArray(0);
// Remettre la camera à sa position initiale
// Enable blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Render to our framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, windowWidth, windowHeight); // Render on the whole framebuffer, complete from the lower left corner to the upper right
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glCullFace(GL_BACK);
// Use our shader
glUseProgram(programID);
// Set our "renderedTexture" sampler to use Texture Unit 0
glUniform1i(tex_loc, 0);
// Bind our texture in Texture Unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, renderedTexture_texID);
glUniform1i(fronttex_loc, 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, candleFrontDepth_texID);
glUniform1i(thicktex_loc, 2);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, candleBackDepth_texID);
glUniform1i(frontFromLighttex_loc, 3);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, candleFrontDepthFromLight_texID);
glUniform1i(thickFromLighttex_loc, 4);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, candleBackDepthFromLight_texID);
glUniform1i(depth_loc, 5);
glActiveTexture(GL_TEXTURE5);
glBindTexture(GL_TEXTURE_2D, depthTexture_texID);
glUniform1f(cameraDepth_loc, 100.0f);
glUniform1f(timeID, (float)(glfwGetTime()*10.0f));
// Compute the MVP matrix from keyboard and mouse input
computeMatricesFromInputs();
ProjectionMatrix = getProjectionMatrix();
ViewMatrix = getViewMatrix();
ModelMatrix = glm::mat4(1.0);
MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
glm::mat4 biasMatrix(
0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0
);
glm::mat4 depthBiasMVP = biasMatrix*depthMVP;
// Send our transformation to the currently bound shader,
// in the "MVP" uniform
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
glUniformMatrix4fv(ModelMatrixID, 1, GL_FALSE, &ModelMatrix[0][0]);
glUniformMatrix4fv(ViewMatrixID, 1, GL_FALSE, &ViewMatrix[0][0]);
glUniformMatrix4fv(DepthBiasID, 1, GL_FALSE, &depthBiasMVP[0][0]);
glUniform3f(LightID, lightPos.x, lightPos.y, lightPos.z);
// Bind our texture in Texture Unit 6
glActiveTexture(GL_TEXTURE6);
glBindTexture(GL_TEXTURE_2D, Texture);
// Set our "myTextureSampler" sampler to use Texture Unit 6
glUniform1i(TextureID, 6);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
// 2nd attribute buffer : UVs
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
// 3rd attribute buffer : normals
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glVertexAttribDivisor(0, 0);
glVertexAttribDivisor(1, 0);
glVertexAttribDivisor(2, 0);
// Index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);
// Draw the triangles !
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, (void*)0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
//////////////////////////////////////
// Particle part
// Clear the screen
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
double currentTime = glfwGetTime();
double delta = currentTime - lastTime;
lastTime = currentTime;
computeMatricesFromInputs();
ProjectionMatrix = getProjectionMatrix();
ViewMatrix = getViewMatrix();
// We will need the camera's position in order to sort the particles
// w.r.t the camera's distance.
// There should be a getCameraPosition() function in common/controls.cpp,
// but this works too.
glm::vec3 CameraPosition(glm::inverse(ViewMatrix)[3]);
glm::mat4 ViewProjectionMatrix = ProjectionMatrix * ViewMatrix;
// Generate 10 new particule each millisecond,
// but limit this to 16 ms (60 fps), or if you have 1 long frame (1sec),
// newparticles will be huge and the next frame even longer.
int newparticles = (int)(delta*10000.0);
if (newparticles > (int)(0.016f*10000.0))
newparticles = (int)(0.016f*10000.0);
const float particuleLifeTime = 1.f;
const auto endFlamme = glm::vec3(0.f, 2.6f, 0.f);
for (int i = 0; i<newparticles; i++) {
int particleIndex = FindUnusedParticle();
ParticlesContainer[particleIndex].life = particuleLifeTime; // This particle will live 1 seconds.
//To imit a flamme the beginning point is a circle at the top of the candle where the flamme begins
glm::vec3 centerCircle(0, 0, 0);
const static float cercleRadius = 0.1f;
int randomAngle = rand() % 360;
const float randomReel1 = ((float)rand()) / (float)RAND_MAX;
const float randomReel2 = ((float)rand()) / (float)RAND_MAX;
float randomXDelta, randomZDelta;
//50% of the time, the particule will be of the edge of the circle for beautif effect
if (rand() % 2)
{
//Somewhere inside the circle
randomXDelta = randomReel1 * (cos(randomAngle) * cercleRadius);
randomZDelta = randomReel2 * (sin(randomAngle) * cercleRadius);
}
else
{
//On the edge of the circle
randomXDelta = cos(randomAngle) * cercleRadius;
randomZDelta = sin(randomAngle) * cercleRadius;
}
//is the flamme particule near the center of the flamme
ParticlesContainer[particleIndex].pos = glm::vec3(centerCircle.x + randomXDelta, centerCircle.y + 0, centerCircle.z + randomZDelta);
ParticlesContainer[particleIndex].beginningPos = ParticlesContainer[particleIndex].pos;
ParticlesContainer[particleIndex].isCentered = max(abs(randomXDelta), abs(randomZDelta)) < (cercleRadius / 2);
//calculate tangent to indicated the curves of particule
const float curvature = 20;
glm::vec3 flammeDirection = endFlamme - ParticlesContainer[particleIndex].pos;
ParticlesContainer[particleIndex].speed = flammeDirection;
ParticlesContainer[particleIndex].tangent1 = glm::vec3(curvature*randomXDelta, 5.f, 0 + curvature*randomZDelta)
- ParticlesContainer[particleIndex].pos;
ParticlesContainer[particleIndex].tangent2 = glm::vec3(-curvature*endFlamme.x, endFlamme.y, endFlamme.z - curvature*randomZDelta)
- endFlamme;
//Flamme begins blue at the beginning
float randomGreen = 20 + (rand() % 30);
float darkenCenter = max(abs(randomXDelta), abs(randomZDelta)) / cercleRadius;
ParticlesContainer[particleIndex].r = darkenCenter * (randomGreen / 3);
ParticlesContainer[particleIndex].g = darkenCenter * (randomGreen);
ParticlesContainer[particleIndex].b = darkenCenter * (100 - (rand() % 20));
ParticlesContainer[particleIndex].a = (rand() % 256) / 2;
ParticlesContainer[particleIndex].isBlue = true;
const float randomReelSize = ((float)rand()) / (float)RAND_MAX;
ParticlesContainer[particleIndex].size = 0.2f + (randomReelSize*0.05f);
}
// Simulate all particles
int ParticlesCount = 0;
for (int i = 0; i<MaxParticles; i++) {
Particle& p = ParticlesContainer[i];
//The flamme begins blue but it must change to light yellow or red later
if (p.life < 0.95f && p.isBlue)
{
bool isNonCenterHotFlame = (rand() % 10 == 0);
if (p.isCentered || isNonCenterHotFlame)
{
int randomRed = 255 - (rand() % 10);
p.r = randomRed;
p.g = randomRed / 2;
p.b = 0;
p.a = (rand() % 256) / 2;
}
else
{
int randomYellow = 255 - (rand() % 10);
p.r = randomYellow;
p.g = randomYellow;
p.b = 100 + (rand() % 155);
p.a = (rand() % 256) / 2;
}
p.isBlue = false;
}
if (p.life > 0.0f) {
p.life -= delta;
p.u = 1 - ((max(p.life, 0.f)) / particuleLifeTime); //Normalized u used for curvature interpolation
//Diminish particules size at the end of the flamme, so it looks good
if (p.u > 0.7f)
{
p.size = 0.5 * ((1.f - p.u) / 0.7);
}
if (p.life > 0.0f) {
// Simulate simple physics : gravity only, no collisions
p.speed += glm::vec3(0.0f, -9.81f, 0.0f) * (float)delta * 0.5f;
p.pos = calculateHermit(p.u, p.beginningPos, endFlamme, p.tangent1, p.tangent2);
p.pos += 3 * (p.u) * wind.generateWind();
p.cameradistance = glm::length(p.pos - CameraPosition);
// Fill the GPU buffer
g_particule_position_size_data[4 * ParticlesCount + 0] = p.pos.x;
g_particule_position_size_data[4 * ParticlesCount + 1] = p.pos.y;
g_particule_position_size_data[4 * ParticlesCount + 2] = p.pos.z;
g_particule_position_size_data[4 * ParticlesCount + 3] = p.size;
g_particule_color_data[4 * ParticlesCount + 0] = p.r;
g_particule_color_data[4 * ParticlesCount + 1] = p.g;
g_particule_color_data[4 * ParticlesCount + 2] = p.b;
g_particule_color_data[4 * ParticlesCount + 3] = p.a;
}
else {
//Particles that just died will be put at the end of the buffer in SortParticles();
p.cameradistance = -1.0f;
}
ParticlesCount++;
}
}
SortParticles();
//printf("%d ",ParticlesCount);
// Update the buffers that OpenGL uses for rendering.
// There are much more sophisticated means to stream data from the CPU to the GPU,
// but this is outside the scope of this tutorial.
// http://www.opengl.org/wiki/Buffer_Object_Streaming
glBindBuffer(GL_ARRAY_BUFFER, particles_position_buffer);
glBufferData(GL_ARRAY_BUFFER, MaxParticles * 4 * sizeof(GLfloat), NULL, GL_STREAM_DRAW); // Buffer orphaning, a common way to improve streaming perf. See above link for details.
glBufferSubData(GL_ARRAY_BUFFER, 0, ParticlesCount * sizeof(GLfloat) * 4, g_particule_position_size_data);
glBindBuffer(GL_ARRAY_BUFFER, particles_color_buffer);
glBufferData(GL_ARRAY_BUFFER, MaxParticles * 4 * sizeof(GLubyte), NULL, GL_STREAM_DRAW); // Buffer orphaning, a common way to improve streaming perf. See above link for details.
glBufferSubData(GL_ARRAY_BUFFER, 0, ParticlesCount * sizeof(GLubyte) * 4, g_particule_color_data);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Use our shader
glUseProgram(particleProgramID);
// Bind our texture in Texture Unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, particuleTexture);
// Set our "myTextureSampler" sampler to use Texture Unit 0
glUniform1i(particleTextureID, 0);
// Same as the billboards tutorial
glUniform3f(CameraRight_worldspace_ID, ViewMatrix[0][0], ViewMatrix[1][0], ViewMatrix[2][0]);
glUniform3f(CameraUp_worldspace_ID, ViewMatrix[0][1], ViewMatrix[1][1], ViewMatrix[2][1]);
glUniformMatrix4fv(ViewProjMatrixID, 1, GL_FALSE, &ViewProjectionMatrix[0][0]);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, billboard_vertex_buffer);
glVertexAttribPointer(
0, // attribute. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// 2nd attribute buffer : positions of particles' centers
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, particles_position_buffer);
glVertexAttribPointer(
1, // attribute. No particular reason for 1, but must match the layout in the shader.
4, // size : x + y + z + size => 4
GL_FLOAT, // type