質問編集履歴

1

文章を修正

2021/12/21 02:28

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -4,6 +4,10 @@
4
4
 
5
5
 
6
6
 
7
+ #### 現状追記
8
+
9
+ `Draw_Final()`関数が実行されることによってpingpongColorbuffers[1]がDraw_Final()のシェーダーによって書き換わることがわかりました。
10
+
7
11
  ##### 説明
8
12
 
9
13
  /////コメント部内部がbloom効果を発するコードです。
@@ -44,622 +48,602 @@
44
48
 
45
49
 
46
50
 
47
-
48
-
49
- glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
51
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
50
-
51
- glEnable(GL_DEPTH_TEST);
52
-
53
- glViewport(0,0, FrameWork::windowContext->getSize().x, FrameWork::windowContext->getSize().y);
54
52
 
55
53
  glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
56
54
 
55
+ ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
56
+
57
+ sprite->Draw(getViewProjection(), glm::vec3(0, 0, -200), 0, 0.0f, glm::vec2(1.0, 1.0), glm::vec2(0, 0), glm::vec2(100, 100)); //カラーバッファ
58
+
59
+ sprite->Draw_Luminance(colorBuffers[0], glm::vec3(0, 0, -200), 0, 0.0f, glm::vec2(1.0, 1.0), glm::vec2(0, 0), glm::vec2(100, 100)); //輝度バッファ
60
+
57
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
61
+ sprite->Draw_setBlur(quadVertices,quadVAO, quadVBO,colorBuffers[1],pingpongColorbuffers,pingpongFBO); //ブラー
58
-
59
-
60
62
 
61
63
 
62
64
 
63
- glBindFramebuffer(GL_FRAMEBUFFER, hdrFBO);
64
-
65
-
66
-
67
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
68
-
69
- glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
70
-
71
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
72
-
73
- sprite->Draw(getViewProjection(), glm::vec3(0, 0, -200), 0, 0.0f, glm::vec2(1.0, 1.0), glm::vec2(0, 0), glm::vec2(100, 100)); //カラーバッファ
74
-
75
- sprite->Draw_Luminance(colorBuffers[0], glm::vec3(0, 0, -200), 0, 0.0f, glm::vec2(1.0, 1.0), glm::vec2(0, 0), glm::vec2(100, 100)); //輝度バッファ
76
-
77
- sprite->Draw_setBlur(quadVertices,quadVAO, quadVBO,colorBuffers[1],pingpongColorbuffers,pingpongFBO); //ブラー
78
-
79
65
 
80
66
 
67
+
68
+
69
+ sprite->Draw_Final(colorBuffers[0], pingpongColorbuffers); //カラーバッファに書き込み
70
+
71
+ ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
72
+
73
+ glDisable(GL_DEPTH_TEST);
74
+
75
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
76
+
77
+
78
+
79
+ //フレームバッファ描画
80
+
81
+ glBindVertexArray(quadVAO);
82
+
83
+ glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
84
+
85
+ shader.setEnable();
86
+
87
+ shader.setUniform1i("hdr", 1);
88
+
89
+
90
+
91
+
92
+
93
+ //glBindTexture(GL_TEXTURE_2D, textureColorbuffer);
94
+
95
+ //glBindTexture(GL_TEXTURE_2D, colorBuffers[1]);
96
+
97
+ // glBindTexture(GL_TEXTURE_2D, colorBuffers[0]);
98
+
99
+
100
+
101
+ glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[0]);
102
+
103
+ // glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[1]);
104
+
105
+ glActiveTexture(GL_TEXTURE0);
106
+
81
107
 
82
108
 
83
-
84
-
85
- sprite->Draw_Final(colorBuffers[0], pingpongColorbuffers); //カラーバッファに書き込み
86
-
87
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
88
-
89
- glDisable(GL_DEPTH_TEST);
109
+ glDrawArrays(GL_TRIANGLES, 0, 6);
110
+
111
+ shader.setDisable();
112
+
113
+
114
+
115
+ glBindVertexArray(0);
116
+
117
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
118
+
119
+ actor.clear();
120
+
121
+ shadowActor.clear();
122
+
123
+ }
124
+
125
+
126
+
127
+ ```
128
+
129
+
130
+
131
+ ##### Bloom実装部
132
+
133
+ ```CPP
134
+
135
+
136
+
137
+
138
+
139
+ // ##################################### テクスチャ描画 #####################################
140
+
141
+ void FrameWork::D2::Sprite::Draw(glm::mat4 projection,glm::vec3 position, int texNum, float r, glm::vec2 s, glm::vec2 start, glm::vec2 end)
142
+
143
+ {
144
+
145
+
146
+
147
+ shaderTexture.setEnable();
148
+
149
+ glBindVertexArray(vao);
150
+
151
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
152
+
153
+
154
+
155
+ startSize = start; //テクスチャ始点
156
+
157
+ endSize = end; //テクスチャ終点
158
+
159
+
160
+
161
+ vertex->resize(6);
162
+
163
+ setAttribute(); //頂点属性 設定
164
+
165
+
166
+
167
+ setNormal(); //法線 設定
168
+
169
+ glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(VertexAttribute) * vertex->size(), vertex->data());
170
+
171
+
172
+
173
+ //Transform
174
+
175
+ glm::vec3 pos = position;
176
+
177
+ pos.y = (float)FrameWork::windowContext->getSize().y - pos.y - (glm::vec2(end - start).y / 2);
178
+
179
+ pos.x = pos.x + (glm::vec2(end - start).x / 2);
180
+
181
+ //setPosition(pos); //座標
182
+
183
+
184
+
185
+
186
+
187
+ setPosition(position);
188
+
189
+
190
+
191
+ //setScale(s + glm::vec2(end - start)); //スケール
192
+
193
+ setScale(glm::vec2(100,100)); //スケール
194
+
195
+ setRotate(PI / 4); //回転
196
+
197
+
198
+
199
+ //描画
200
+
201
+
202
+
203
+ shaderTexture.setUniform3f("uLightPosition", glm::vec3(0, 0, -199));
204
+
205
+ //shaderTexture.setUniform3f("uLightPosition", glm::vec3(0, 0, 10));
206
+
207
+
208
+
209
+ shaderTexture.setUniform3f("uLightColor", glm::vec3(1, 1, 1));
210
+
211
+ shaderTexture.setUniform3f("uObjectColor", glm::vec3(1, 0, 0));
212
+
213
+ shaderTexture.setUniform3f("uViewPosition", glm::vec3(0, 0, 10));
214
+
215
+ shaderTexture.setUniform1f("uAmbientStrength",1.0f);
216
+
217
+ shaderTexture.setUniform1f("uSpecularStrength",0.5);
218
+
219
+ shaderTexture.setUniform1f("uShininessStrength",256.0);
220
+
221
+
222
+
223
+ shaderTexture.setUniformMatrix4fv("uTranslate",getMatTranslation());
224
+
225
+ shaderTexture.setUniformMatrix4fv("uRotate", getMatRotate());
226
+
227
+ shaderTexture.setUniformMatrix4fv("uScale", getMatScale());
228
+
229
+ shaderTexture.setUniformMatrix4fv("uViewProjection", projection);
230
+
231
+ //shaderTexture.setUniformMatrix4fv("uViewProjection", glm::ortho(0.0f, (float)FrameWork::windowContext->getSize().x, 0.0f, (float)FrameWork::windowContext->getSize().y));
232
+
233
+ //printf("%d\n",textureID.at(texNum));
234
+
235
+
236
+
237
+ //glActiveTexture(GL_TEXTURE0); //テクスチャ有効
238
+
239
+ //glBindTexture(GL_TEXTURE_2D, textureID.at(texNum)); //テクスチャバインド
240
+
241
+
242
+
243
+
244
+
245
+
246
+
247
+ glDrawArrays(GL_TRIANGLES, 0, vertex->size()); //描画
248
+
249
+
250
+
251
+ //バインド解除
252
+
253
+ glBindVertexArray(0);
254
+
255
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
256
+
257
+ //glBindTexture(GL_TEXTURE_2D, 0);
258
+
259
+
260
+
261
+ shaderTexture.setDisable();
262
+
263
+ }
264
+
265
+
266
+
267
+ // ##################################### 輝度描画 #####################################
268
+
269
+ void FrameWork::D2::Sprite::Draw_Luminance(GLuint texture, glm::vec3 position, int texNum, float r, glm::vec2 s, glm::vec2 start, glm::vec2 end)
270
+
271
+ {
272
+
273
+
274
+
275
+ shaderLuminance.setEnable();
276
+
277
+ glBindVertexArray(vao);
278
+
279
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
280
+
281
+
282
+
283
+ startSize = start; //テクスチャ始点
284
+
285
+ endSize = end; //テクスチャ終点
286
+
287
+
288
+
289
+ vertex->resize(6);
290
+
291
+ setAttribute(); //頂点属性 設定
292
+
293
+ setNormal(); //法線 設定
294
+
295
+
296
+
297
+
298
+
299
+ glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(VertexAttribute) * vertex->size(), vertex->data());
300
+
301
+
302
+
303
+ //Transform
304
+
305
+ glm::vec3 pos = position;
306
+
307
+ pos.y = (float)FrameWork::windowContext->getSize().y - pos.y - (glm::vec2(end - start).y / 2);
308
+
309
+ pos.x = pos.x + (glm::vec2(end - start).x / 2);
310
+
311
+ //setPosition(pos); //座標
312
+
313
+
314
+
315
+
316
+
317
+ setPosition(position);
318
+
319
+
320
+
321
+ setScale(s + glm::vec2(end - start)); //スケール
322
+
323
+ //setScale(glm::vec2(100,100)); //スケール
324
+
325
+ setRotate(r); //回転
326
+
327
+
328
+
329
+ glActiveTexture(GL_TEXTURE0); //テクスチャ有効
330
+
331
+ glBindTexture(GL_TEXTURE_2D, texture); //テクスチャバインド
332
+
333
+
334
+
335
+
336
+
337
+
338
+
339
+ glDrawArrays(GL_TRIANGLES, 0, vertex->size()); //描画
340
+
341
+
342
+
343
+ //バインド解除
344
+
345
+ glBindVertexArray(0);
346
+
347
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
348
+
349
+ glBindTexture(GL_TEXTURE_2D, 0);
350
+
351
+
352
+
353
+ shaderLuminance.setDisable();
354
+
355
+ }
356
+
357
+
358
+
359
+ // ##################################### bloom ブラー 描画 #####################################
360
+
361
+ void FrameWork::D2::Sprite::Draw_setBlur(float vert[24], const GLuint quadVAO, const GLuint quadVBO, const GLint frameBuffer_Luminance, GLuint pingpongColorbuffers[2], GLuint pingpongFBO[2])
362
+
363
+ {
364
+
365
+ horizontal = 0;
366
+
367
+ bool first_iteration = true;
368
+
369
+ unsigned int amount = 10;
370
+
371
+ setAttribute(); //頂点属性 設定
372
+
373
+
374
+
375
+
376
+
377
+ glBindVertexArray(vao2);
378
+
379
+ glBindBuffer(GL_ARRAY_BUFFER, vbo2);
380
+
381
+
382
+
383
+
384
+
385
+ horizontal = 0;
386
+
387
+ glBindFramebuffer(GL_FRAMEBUFFER, pingpongFBO[0]);
388
+
389
+ shaderBloomBlur.setEnable();
390
+
391
+ glBindTexture(GL_TEXTURE_2D, frameBuffer_Luminance);
392
+
393
+ glActiveTexture(GL_TEXTURE0);
394
+
395
+ shaderBloomBlur.setUniform1i("horizontal", 0);
396
+
397
+ glDrawArrays(GL_TRIANGLES, 0, 6); //描画
398
+
399
+ glBindTexture(GL_TEXTURE_2D, 0);
400
+
401
+ shaderBloomBlur.setDisable();
402
+
403
+
404
+
405
+ horizontal = 1;
406
+
407
+ for (int i = 0; i < amount; i++)
408
+
409
+ {
410
+
411
+ //printf("%d\n",horizontal);
412
+
413
+ shaderBloomBlur.setEnable();
414
+
415
+ glBindFramebuffer(GL_FRAMEBUFFER, pingpongFBO[horizontal]);
416
+
417
+
418
+
419
+ if (horizontal == 1)
420
+
421
+ {
422
+
423
+ glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[0]);
424
+
425
+ }
426
+
427
+ else
428
+
429
+ {
430
+
431
+ glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[1]);
432
+
433
+
434
+
435
+ }
436
+
437
+
438
+
439
+ glActiveTexture(GL_TEXTURE0);
440
+
441
+ shaderBloomBlur.setUniform1i("horizontal", horizontal);
442
+
443
+ glDrawArrays(GL_TRIANGLES, 0, 6); //描画
444
+
445
+ shaderBloomBlur.setDisable();
446
+
447
+ glBindTexture(GL_TEXTURE_2D, 0);
448
+
449
+
450
+
451
+ if (horizontal == 1)
452
+
453
+ {
454
+
455
+ horizontal = 0;
456
+
457
+ }
458
+
459
+ else
460
+
461
+ {
462
+
463
+ horizontal = 1;
464
+
465
+
466
+
467
+
468
+
469
+ }
470
+
471
+ }
472
+
473
+
474
+
475
+ //バインド解除
476
+
477
+ glBindVertexArray(0);
478
+
479
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
480
+
481
+ glBindTexture(GL_TEXTURE_2D, 0);
90
482
 
91
483
  glBindFramebuffer(GL_FRAMEBUFFER, 0);
92
484
 
93
-
485
+ }
486
+
487
+
488
+
489
+
490
+
94
-
491
+ // ##################################### ブラーをカラーバッファに書き込み #####################################
492
+
493
+ void FrameWork::D2::Sprite::Draw_Final(GLuint frameBuffer, GLuint pingpongColorbuffers[2])
494
+
495
+ {
496
+
95
- //フレームバッファ描画
497
+ shaderBloomFinal.setEnable();
96
-
498
+
97
- glBindVertexArray(quadVAO);
499
+ glBindVertexArray(vao2);
98
-
500
+
99
- glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
501
+ glBindBuffer(GL_ARRAY_BUFFER, vbo2);
100
-
101
- shader.setEnable();
502
+
102
-
103
- shader.setUniform1i("hdr", 1);
503
+
104
-
105
-
106
-
107
-
108
-
109
- //glBindTexture(GL_TEXTURE_2D, textureColorbuffer);
110
-
111
- //glBindTexture(GL_TEXTURE_2D, colorBuffers[1]);
112
-
113
- // glBindTexture(GL_TEXTURE_2D, colorBuffers[0]);
114
-
115
-
116
-
117
- glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[0]);
118
-
119
- // glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[1]);
120
504
 
121
505
  glActiveTexture(GL_TEXTURE0);
122
506
 
123
-
507
+ glBindTexture(GL_TEXTURE_2D, frameBuffer);
508
+
509
+
510
+
511
+ glActiveTexture(GL_TEXTURE1);
512
+
513
+ glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[horizontal]);
514
+
515
+
516
+
517
+ shaderBloomFinal.setUniform1i("bloom", 1);
518
+
519
+ shaderBloomFinal.setUniform1f("exposure", 2.0f);
520
+
521
+
124
522
 
125
523
  glDrawArrays(GL_TRIANGLES, 0, 6);
126
524
 
127
- shader.setDisable();
128
-
129
525
 
130
526
 
131
527
  glBindVertexArray(0);
132
528
 
133
529
  glBindBuffer(GL_ARRAY_BUFFER, 0);
134
530
 
135
- actor.clear();
531
+
136
-
532
+
137
- shadowActor.clear();
533
+ shaderBloomFinal.setDisable();
534
+
535
+
138
536
 
139
537
  }
140
538
 
141
539
 
142
540
 
541
+
542
+
143
543
  ```
144
544
 
145
545
 
146
546
 
147
- ##### Bloom実装
547
+ ##### Draw_Final()シェーダー
148
-
548
+
149
- ```CPP
549
+ ```glsl
150
-
151
-
152
-
153
-
154
-
550
+
155
- // ##################################### テクスチャ描画 #####################################
551
+ /*#########################################################################
552
+
156
-
553
+ # バーティックスシェーダー
554
+
555
+ ###########################################################################*/
556
+
557
+
558
+
157
- void FrameWork::D2::Sprite::Draw(glm::mat4 projection,glm::vec3 position, int texNum, float r, glm::vec2 s, glm::vec2 start, glm::vec2 end)
559
+ layout (location = 0) in vec2 vertexPosition;
560
+
561
+ layout (location = 1) in vec2 vertexUV;
562
+
563
+
564
+
565
+ out vec2 TexCoords;
566
+
567
+
568
+
569
+ void main()
158
570
 
159
571
  {
160
572
 
161
-
162
-
163
- shaderTexture.setEnable();
164
-
165
- glBindVertexArray(vao);
166
-
167
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
168
-
169
-
170
-
171
- startSize = start; //テクスチャ始点
172
-
173
- endSize = end; //テクスチャ終点
174
-
175
-
176
-
177
- vertex->resize(6);
573
+ TexCoords = vertexUV;
178
-
179
- setAttribute(); //頂点属性 設定
574
+
180
-
181
-
182
-
183
- setNormal(); //法線 設定
184
-
185
- glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(VertexAttribute) * vertex->size(), vertex->data());
186
-
187
-
188
-
189
- //Transform
190
-
191
- glm::vec3 pos = position;
192
-
193
- pos.y = (float)FrameWork::windowContext->getSize().y - pos.y - (glm::vec2(end - start).y / 2);
194
-
195
- pos.x = pos.x + (glm::vec2(end - start).x / 2);
196
-
197
- //setPosition(pos); //座標
198
-
199
-
200
-
201
-
202
-
203
- setPosition(position);
575
+ gl_Position = vec4(vertexPosition,-1.0, 1.0);
204
-
205
-
206
-
207
- //setScale(s + glm::vec2(end - start)); //スケール
208
-
209
- setScale(glm::vec2(100,100)); //スケール
210
-
211
- setRotate(PI / 4); //回転
212
-
213
-
214
-
215
- //描画
216
-
217
-
218
-
219
- shaderTexture.setUniform3f("uLightPosition", glm::vec3(0, 0, -199));
220
-
221
- //shaderTexture.setUniform3f("uLightPosition", glm::vec3(0, 0, 10));
222
-
223
-
224
-
225
- shaderTexture.setUniform3f("uLightColor", glm::vec3(1, 1, 1));
226
-
227
- shaderTexture.setUniform3f("uObjectColor", glm::vec3(1, 0, 0));
228
-
229
- shaderTexture.setUniform3f("uViewPosition", glm::vec3(0, 0, 10));
230
-
231
- shaderTexture.setUniform1f("uAmbientStrength",1.0f);
232
-
233
- shaderTexture.setUniform1f("uSpecularStrength",0.5);
234
-
235
- shaderTexture.setUniform1f("uShininessStrength",256.0);
236
-
237
-
238
-
239
- shaderTexture.setUniformMatrix4fv("uTranslate",getMatTranslation());
240
-
241
- shaderTexture.setUniformMatrix4fv("uRotate", getMatRotate());
242
-
243
- shaderTexture.setUniformMatrix4fv("uScale", getMatScale());
244
-
245
- shaderTexture.setUniformMatrix4fv("uViewProjection", projection);
246
-
247
- //shaderTexture.setUniformMatrix4fv("uViewProjection", glm::ortho(0.0f, (float)FrameWork::windowContext->getSize().x, 0.0f, (float)FrameWork::windowContext->getSize().y));
248
-
249
- //printf("%d\n",textureID.at(texNum));
250
-
251
-
252
-
253
- //glActiveTexture(GL_TEXTURE0); //テクスチャ有効
254
-
255
- //glBindTexture(GL_TEXTURE_2D, textureID.at(texNum)); //テクスチャバインド
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
- glDrawArrays(GL_TRIANGLES, 0, vertex->size()); //描画
264
-
265
-
266
-
267
- //バインド解除
268
-
269
- glBindVertexArray(0);
270
-
271
- glBindBuffer(GL_ARRAY_BUFFER, 0);
272
-
273
- //glBindTexture(GL_TEXTURE_2D, 0);
274
-
275
-
276
-
277
- shaderTexture.setDisable();
278
576
 
279
577
  }
280
578
 
281
-
282
-
283
- // ##################################### 輝度描画 #####################################
284
-
285
- void FrameWork::D2::Sprite::Draw_Luminance(GLuint texture, glm::vec3 position, int texNum, float r, glm::vec2 s, glm::vec2 start, glm::vec2 end)
286
-
287
- {
288
-
289
-
290
-
291
- shaderLuminance.setEnable();
292
-
293
- glBindVertexArray(vao);
294
-
295
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
296
-
297
-
298
-
299
- startSize = start; //テクスチャ始点
300
-
301
- endSize = end; //テクスチャ終点
302
-
303
-
304
-
305
- vertex->resize(6);
306
-
307
- setAttribute(); //頂点属性 設定
308
-
309
- setNormal(); //法線 設定
310
-
311
-
312
-
313
-
314
-
315
- glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(VertexAttribute) * vertex->size(), vertex->data());
316
-
317
-
318
-
319
- //Transform
320
-
321
- glm::vec3 pos = position;
322
-
323
- pos.y = (float)FrameWork::windowContext->getSize().y - pos.y - (glm::vec2(end - start).y / 2);
324
-
325
- pos.x = pos.x + (glm::vec2(end - start).x / 2);
326
-
327
- //setPosition(pos); //座標
328
-
329
-
330
-
331
-
332
-
333
- setPosition(position);
334
-
335
-
336
-
337
- setScale(s + glm::vec2(end - start)); //スケール
338
-
339
- //setScale(glm::vec2(100,100)); //スケール
340
-
341
- setRotate(r); //回転
342
-
343
-
344
-
345
- glActiveTexture(GL_TEXTURE0); //テクスチャ有効
346
-
347
- glBindTexture(GL_TEXTURE_2D, texture); //テクスチャバインド
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
- glDrawArrays(GL_TRIANGLES, 0, vertex->size()); //描画
356
-
357
-
358
-
359
- //バインド解除
360
-
361
- glBindVertexArray(0);
362
-
363
- glBindBuffer(GL_ARRAY_BUFFER, 0);
364
-
365
- glBindTexture(GL_TEXTURE_2D, 0);
366
-
367
-
368
-
369
- shaderLuminance.setDisable();
579
+ ```
580
+
581
+ ```glsl
582
+
583
+ /*#########################################################################
584
+
585
+ # フラグメントシェーダー
586
+
587
+ ###########################################################################*/
588
+
589
+
590
+
591
+ out vec4 FragColor;
592
+
593
+ //layout(location = 0) out vec4 FragColor;
594
+
595
+
596
+
597
+ in vec2 TexCoords;
598
+
599
+
600
+
601
+ uniform sampler2D scene;
602
+
603
+ uniform sampler2D bloomBlur;
604
+
605
+ uniform int bloom;
606
+
607
+ uniform float exposure;
608
+
609
+
610
+
611
+ void main()
612
+
613
+ {
614
+
615
+ const float gamma = 2.2;
616
+
617
+ vec3 hdrColor = texture(scene, TexCoords).rgb;
618
+
619
+ vec3 bloomColor = texture(bloomBlur, TexCoords).rgb;
620
+
621
+ if(bloom == 1)
622
+
623
+ {
624
+
625
+ hdrColor += bloomColor; // additive blending
626
+
627
+ }
628
+
629
+
630
+
631
+ // tone mapping
632
+
633
+ vec3 result = vec3(1.0) - exp(-hdrColor * exposure);
634
+
635
+ // also gamma correct while we're at it
636
+
637
+
638
+
639
+ result = pow(result, vec3(1.0 / gamma));
640
+
641
+ FragColor = vec4(result, 1.0);
642
+
643
+ // FragColor = vec4(hdrColor, 1.0);
644
+
645
+
370
646
 
371
647
  }
372
648
 
373
-
374
-
375
- // ##################################### bloom ブラー 描画 #####################################
376
-
377
- void FrameWork::D2::Sprite::Draw_setBlur(float vert[24], const GLuint quadVAO, const GLuint quadVBO, const GLint frameBuffer_Luminance, GLuint pingpongColorbuffers[2], GLuint pingpongFBO[2])
378
-
379
- {
380
-
381
- horizontal = 0;
382
-
383
- bool first_iteration = true;
384
-
385
- unsigned int amount = 10;
386
-
387
- setAttribute(); //頂点属性 設定
388
-
389
-
390
-
391
-
392
-
393
- glBindVertexArray(vao2);
394
-
395
- glBindBuffer(GL_ARRAY_BUFFER, vbo2);
396
-
397
-
398
-
399
-
400
-
401
- horizontal = 0;
402
-
403
- glBindFramebuffer(GL_FRAMEBUFFER, pingpongFBO[0]);
404
-
405
- shaderBloomBlur.setEnable();
406
-
407
- glBindTexture(GL_TEXTURE_2D, frameBuffer_Luminance);
408
-
409
- glActiveTexture(GL_TEXTURE0);
410
-
411
- shaderBloomBlur.setUniform1i("horizontal", 0);
412
-
413
- glDrawArrays(GL_TRIANGLES, 0, 6); //描画
414
-
415
- glBindTexture(GL_TEXTURE_2D, 0);
416
-
417
- shaderBloomBlur.setDisable();
418
-
419
-
420
-
421
- horizontal = 1;
422
-
423
- for (int i = 0; i < amount; i++)
424
-
425
- {
426
-
427
- //printf("%d\n",horizontal);
428
-
429
- shaderBloomBlur.setEnable();
430
-
431
- glBindFramebuffer(GL_FRAMEBUFFER, pingpongFBO[horizontal]);
432
-
433
-
434
-
435
- if (horizontal == 1)
436
-
437
- {
438
-
439
- glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[0]);
440
-
441
- }
442
-
443
- else
444
-
445
- {
446
-
447
- glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[1]);
448
-
449
-
450
-
451
- }
452
-
453
-
454
-
455
- glActiveTexture(GL_TEXTURE0);
456
-
457
- shaderBloomBlur.setUniform1i("horizontal", horizontal);
458
-
459
- glDrawArrays(GL_TRIANGLES, 0, 6); //描画
460
-
461
- shaderBloomBlur.setDisable();
462
-
463
- glBindTexture(GL_TEXTURE_2D, 0);
464
-
465
-
466
-
467
- if (horizontal == 1)
468
-
469
- {
470
-
471
- horizontal = 0;
472
-
473
- }
474
-
475
- else
476
-
477
- {
478
-
479
- horizontal = 1;
480
-
481
-
482
-
483
-
484
-
485
- }
486
-
487
- }
488
-
489
-
490
-
491
- //バインド解除
492
-
493
- glBindVertexArray(0);
494
-
495
- glBindBuffer(GL_ARRAY_BUFFER, 0);
496
-
497
- glBindTexture(GL_TEXTURE_2D, 0);
498
-
499
- glBindFramebuffer(GL_FRAMEBUFFER, 0);
500
-
501
- }
502
-
503
-
504
-
505
-
506
-
507
- // ##################################### ブラーをカラーバッファに書き込み #####################################
508
-
509
- void FrameWork::D2::Sprite::Draw_Final(GLuint frameBuffer, GLuint pingpongColorbuffers[2])
510
-
511
- {
512
-
513
- shaderBloomFinal.setEnable();
514
-
515
- glBindVertexArray(vao2);
516
-
517
- glBindBuffer(GL_ARRAY_BUFFER, vbo2);
518
-
519
-
520
-
521
- glActiveTexture(GL_TEXTURE0);
522
-
523
- glBindTexture(GL_TEXTURE_2D, frameBuffer);
524
-
525
-
526
-
527
- glActiveTexture(GL_TEXTURE1);
528
-
529
- glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[horizontal]);
530
-
531
-
532
-
533
- shaderBloomFinal.setUniform1i("bloom", 1);
534
-
535
- shaderBloomFinal.setUniform1f("exposure", 2.0f);
536
-
537
-
538
-
539
- glDrawArrays(GL_TRIANGLES, 0, 6);
540
-
541
-
542
-
543
- glBindVertexArray(0);
544
-
545
- glBindBuffer(GL_ARRAY_BUFFER, 0);
546
-
547
-
548
-
549
- shaderBloomFinal.setDisable();
550
-
551
-
552
-
553
- }
554
-
555
-
556
-
557
-
558
-
559
649
  ```
560
-
561
-
562
-
563
- ##### Draw_Final()シェーダー部
564
-
565
- ```glsl
566
-
567
- /*#########################################################################
568
-
569
- # バーティックスシェーダー
570
-
571
- ###########################################################################*/
572
-
573
-
574
-
575
- layout (location = 0) in vec2 vertexPosition;
576
-
577
- layout (location = 1) in vec2 vertexUV;
578
-
579
-
580
-
581
- out vec2 TexCoords;
582
-
583
-
584
-
585
- void main()
586
-
587
- {
588
-
589
- TexCoords = vertexUV;
590
-
591
- gl_Position = vec4(vertexPosition,-1.0, 1.0);
592
-
593
- }
594
-
595
- ```
596
-
597
- ```glsl
598
-
599
- /*#########################################################################
600
-
601
- # フラグメントシェーダー
602
-
603
- ###########################################################################*/
604
-
605
-
606
-
607
- out vec4 FragColor;
608
-
609
- //layout(location = 0) out vec4 FragColor;
610
-
611
-
612
-
613
- in vec2 TexCoords;
614
-
615
-
616
-
617
- uniform sampler2D scene;
618
-
619
- uniform sampler2D bloomBlur;
620
-
621
- uniform int bloom;
622
-
623
- uniform float exposure;
624
-
625
-
626
-
627
- void main()
628
-
629
- {
630
-
631
- const float gamma = 2.2;
632
-
633
- vec3 hdrColor = texture(scene, TexCoords).rgb;
634
-
635
- vec3 bloomColor = texture(bloomBlur, TexCoords).rgb;
636
-
637
- if(bloom == 1)
638
-
639
- {
640
-
641
- hdrColor += bloomColor; // additive blending
642
-
643
- }
644
-
645
-
646
-
647
- // tone mapping
648
-
649
- vec3 result = vec3(1.0) - exp(-hdrColor * exposure);
650
-
651
- // also gamma correct while we're at it
652
-
653
-
654
-
655
- result = pow(result, vec3(1.0 / gamma));
656
-
657
- FragColor = vec4(result, 1.0);
658
-
659
- // FragColor = vec4(hdrColor, 1.0);
660
-
661
-
662
-
663
- }
664
-
665
- ```