質問編集履歴

11

文章を修正

2021/10/31 03:37

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
 
4
4
 
5
+ ##### 現状
6
+
5
7
  GLSLコードはPhongシェーディングです。
6
8
 
7
9
  Mode.cppはobjローダーです。
@@ -10,10 +12,6 @@
10
12
 
11
13
  頂点と法線は正しく読み込まれていることを確認しました。頂点インデックスは使っていません。
12
14
 
13
-
14
-
15
- ##### 追記
16
-
17
15
  立方体の面を1つの三角形に減らして実装したのでが一枚目の画像ですがライティングの陰影が出てきるのでシェーダーファイルが原因でないことを突き止めました。
18
16
 
19
17
 
@@ -22,7 +20,7 @@
22
20
 
23
21
  ##### 知りたいこと
24
22
 
25
- objローダーの原因なのか知りたい。
23
+ objローダーの法線の設定方法が知りたい。
26
24
 
27
25
 
28
26
 

10

文章を修正

2021/10/31 03:37

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -28,11 +28,13 @@
28
28
 
29
29
 
30
30
 
31
-
31
+ 参考サイト: [https://learnopengl.com/Lighting/Basic-Lighting](https://learnopengl.com/Lighting/Basic-Lighting)
32
+
33
+ Github: [https://github.com/Shigurechan/GL/tree/14b9ed0e2d87eafaae0b3b11fd42660b7d3f07b7](https://github.com/Shigurechan/GL/tree/14b9ed0e2d87eafaae0b3b11fd42660b7d3f07b7)
32
34
 
33
35
  ![イメージ説明](48347fd2650e65632749659c4abd6286.png)
34
36
 
35
- 参考サイト: [https://learnopengl.com/Lighting/Basic-Lighting](https://learnopengl.com/Lighting/Basic-Lighting)
37
+
36
38
 
37
39
 
38
40
 

9

文章を修正

2021/10/31 03:31

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -300,8 +300,6 @@
300
300
 
301
301
  vertex.push_back(vert);
302
302
 
303
- // obj.vertex.push_back(vert);
304
-
305
303
  }
306
304
 
307
305
  else if (strcmp(line, "vt") == 0)
@@ -312,10 +310,6 @@
312
310
 
313
311
  fscanf(file, "%f %fn", &u.x, &u.y);
314
312
 
315
- //u.y = 1.0f - u.y;
316
-
317
- // obj.uv.push_back(u);
318
-
319
313
  uv.push_back(u);
320
314
 
321
315
  }
@@ -324,14 +318,10 @@
324
318
 
325
319
  {
326
320
 
327
-
328
-
329
321
  glm::vec3 norm;
330
322
 
331
323
  fscanf(file, "%f %f %fn", &norm.x, &norm.y, &norm.z);
332
324
 
333
- // obj.normal.push_back(norm);
334
-
335
325
  normal.push_back(norm);
336
326
 
337
327
  }
@@ -340,28 +330,10 @@
340
330
 
341
331
  {
342
332
 
343
-
344
-
345
- std::string vertex1, vertex2, vertex3;
346
-
347
333
  unsigned int v[3], u[3], n[3];
348
334
 
349
335
  int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%dn", &v[0], &u[0], &n[0], &v[1], &u[1], &n[1], &v[2], &u[2], &n[2]);
350
336
 
351
- if (matches != 9)
352
-
353
- {
354
-
355
- printf("File can't be read by our simple parser : ( Try exporting with other optionsn");
356
-
357
- assert(0);
358
-
359
- }
360
-
361
-
362
-
363
-
364
-
365
337
 
366
338
 
367
339
  vertexIndex.push_back(v[0]);
@@ -370,7 +342,7 @@
370
342
 
371
343
  vertexIndex.push_back(v[2]);
372
344
 
373
-
345
+
374
346
 
375
347
  uvIndex.push_back(u[0]);
376
348
 
@@ -378,15 +350,13 @@
378
350
 
379
351
  uvIndex.push_back(u[2]);
380
352
 
381
-
353
+
382
354
 
383
355
  normalIndex.push_back(n[0]);
384
356
 
385
357
  normalIndex.push_back(n[1]);
386
358
 
387
- normalIndex.push_back(n[2]);
359
+ normalIndex.push_back(n[2]);
388
-
389
-
390
360
 
391
361
  }
392
362
 
@@ -394,44 +364,62 @@
394
364
 
395
365
 
396
366
 
397
- for( unsigned int i = 0; i < vertexIndex.size(); i++ )
367
+ for (unsigned int i = 0; i < vertexIndex.size(); i++)
398
368
 
399
369
  {
400
370
 
401
-
402
-
403
371
  unsigned int vi = vertexIndex[i];
404
372
 
405
373
  unsigned int ui = uvIndex[i];
406
374
 
407
375
  unsigned int ni = normalIndex[i];
408
376
 
377
+
378
+
379
+ glm::vec3 v = vertex[vi - 1];
380
+
381
+ glm::vec2 u = uv[ui - 1];
382
+
383
+ glm::vec3 n = normal[ni - 1];
384
+
385
+
386
+
387
+
388
+
389
+ VertexAttribute attrib;
390
+
391
+
392
+
393
+ attrib.position[0] = v.x;
394
+
395
+ attrib.position[1] = v.y;
396
+
397
+ attrib.position[2] = v.z;
398
+
399
+
400
+
401
+ attrib.uv[0] = u.x;
402
+
403
+ attrib.uv[1] = u.y;
404
+
405
+
406
+
407
+ attrib.normal[0] = n.x;
408
+
409
+ attrib.normal[1] = n.y;
410
+
411
+ attrib.normal[2] = n.z;
412
+
409
413
 
410
414
 
411
- glm::vec3 v = vertex[ vi - 1];
412
-
413
- glm::vec2 u = uv[ ui - 1];
414
-
415
- glm::vec3 n = normal[ ni - 1];
416
-
417
-
418
-
419
- obj.vertex.push_back(v);
415
+ obj.attribute.push_back(attrib);
420
-
421
- obj.uv.push_back(u);
422
-
423
- obj.normal.push_back(n);
424
416
 
425
417
  }
426
418
 
427
-
428
-
429
- }
419
+ }
430
420
 
431
421
  attribute = obj;
432
422
 
433
423
  }
434
424
 
435
-
436
-
437
- ```
425
+ ```

8

文章を修正

2021/10/29 09:11

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -30,7 +30,7 @@
30
30
 
31
31
 
32
32
 
33
- ![イメージ説明](63dffc05c043500582c63b39d065733c.png)
33
+ ![イメージ説明](48347fd2650e65632749659c4abd6286.png)
34
34
 
35
35
  参考サイト: [https://learnopengl.com/Lighting/Basic-Lighting](https://learnopengl.com/Lighting/Basic-Lighting)
36
36
 

7

文章を修正

2021/10/29 09:01

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -30,13 +30,7 @@
30
30
 
31
31
 
32
32
 
33
-
34
-
35
- ![イメージ説明](3f8bb75bf1f8e5bf824355b956e6ced0.png)
33
+ ![イメージ説明](63dffc05c043500582c63b39d065733c.png)
36
-
37
- ![イメージ説明](67a99888ded0b478d2394f0468c8a307.png)
38
-
39
-
40
34
 
41
35
  参考サイト: [https://learnopengl.com/Lighting/Basic-Lighting](https://learnopengl.com/Lighting/Basic-Lighting)
42
36
 

6

文章を修正

2021/10/29 07:19

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- Phongのシェディングを実装したい。陰影おかしい原因とは
1
+ OBJロダーで法線く設定できない原因が知りたい。
test CHANGED
@@ -12,21 +12,27 @@
12
12
 
13
13
 
14
14
 
15
+ ##### 追記
16
+
17
+ 立方体の面を1つの三角形に減らして実装したのでが一枚目の画像ですがライティングの陰影が出てきるのでシェーダーファイルが原因でないことを突き止めました。
18
+
19
+
20
+
15
21
 
16
22
 
17
23
  ##### 知りたいこと
18
24
 
19
- シェーダーが原因なのか?それともobjローダーが原因なのか知りたい。
25
+ objローダーの何が原因なのか知りたい。
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
20
-
35
+ ![イメージ説明](3f8bb75bf1f8e5bf824355b956e6ced0.png)
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
36
 
31
37
  ![イメージ説明](67a99888ded0b478d2394f0468c8a307.png)
32
38
 

5

文章を修正

2021/10/29 04:55

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -6,8 +6,6 @@
6
6
 
7
7
  Mode.cppはobjローダーです。
8
8
 
9
-
10
-
11
9
  現状、Model.cppのLoadobj()関数でblenderで出力したobjファイルをロードしています。GLSLは提示サイトを参考に記述しました。
12
10
 
13
11
  頂点と法線は正しく読み込まれていることを確認しました。頂点インデックスは使っていません。
@@ -16,6 +14,12 @@
16
14
 
17
15
 
18
16
 
17
+ ##### 知りたいこと
18
+
19
+ シェーダーが原因なのか?それともobjローダーが原因なのか知りたい。
20
+
21
+
22
+
19
23
 
20
24
 
21
25
 

4

シェーダーコードを修正

2021/10/29 04:34

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -136,6 +136,28 @@
136
136
 
137
137
 
138
138
 
139
+ ```
140
+
141
+
142
+
143
+ ##### Vertex Shader
144
+
145
+ ```
146
+
147
+ /*#########################################################################
148
+
149
+ #
150
+
151
+ ###########################################################################*/
152
+
153
+ #version 420
154
+
155
+ //#extension GL_ARB_explicit_uniform_location : require
156
+
157
+
158
+
159
+
160
+
139
161
  layout(location = 0) in vec3 vertexPosition;
140
162
 
141
163
  layout(location = 1) in vec3 vertexNormal;
@@ -186,318 +208,226 @@
186
208
 
187
209
  }
188
210
 
211
+
212
+
189
- ```
213
+ ```
190
-
191
-
192
-
214
+
215
+
216
+
193
- ##### Vertex Shader
217
+ ##### Model.cpp
194
-
218
+
195
- ```
219
+ ```
196
-
220
+
221
+
222
+
197
- /*#########################################################################
223
+ // ##################################### .objファイル読み込み #####################################
198
-
199
- #
224
+
200
-
201
- ###########################################################################*/
202
-
203
- #version 420
204
-
205
- //#extension GL_ARB_explicit_uniform_location : require
225
+ void FrameWork::D3::LoadObj(const char *fileName, ObjFile &attribute)
206
-
207
-
208
-
209
- layout(location = 2) in vec3 fPosition;
210
-
211
- layout(location = 3) in vec3 fNormal;
212
-
213
-
214
-
215
-
216
-
217
- uniform vec3 lightPos;
218
-
219
- uniform vec3 lightColor;
220
-
221
- uniform vec3 objectColor;
222
-
223
- uniform vec3 viewPos;
224
-
225
-
226
-
227
- out vec4 fragment;
228
-
229
-
230
-
231
- void main()
232
226
 
233
227
  {
234
228
 
235
-
236
-
237
- // ambient
238
-
239
- float ambientStrength = 0.1;
240
-
241
- vec3 ambient = ambientStrength * lightColor;
242
-
243
-
244
-
245
- // diffuse
246
-
247
- vec3 norm = normalize(fNormal);
248
-
249
- vec3 lightDir = normalize(lightPos - fPosition);
250
-
251
- float diff = max(dot(norm, lightDir), 0.0);
252
-
253
- vec3 diffuse = diff * lightColor;
254
-
255
-
256
-
257
- // specular
258
-
259
- float specularStrength = 0.5;
260
-
261
- vec3 viewDir = normalize(viewPos - fPosition);
262
-
263
- vec3 reflectDir = reflect(-lightDir, norm);
264
-
265
- float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
266
-
267
- vec3 specular = specularStrength * spec * lightColor;
268
-
269
-
270
-
271
-
272
-
273
- vec3 result = (ambient + diffuse + specular) * objectColor;
274
-
275
- fragment = vec4(result, 1.0);
229
+ ObjFile obj;
230
+
231
+
232
+
233
+ std::vector<int> vertexIndex;
234
+
235
+ std::vector<int> uvIndex;
236
+
237
+ std::vector<int> normalIndex;
238
+
239
+
240
+
241
+ std::vector<glm::vec3> vertex;
242
+
243
+ std::vector<glm::vec2> uv;
244
+
245
+ std::vector<glm::vec3> normal;
246
+
247
+
248
+
249
+ FILE *file = fopen(fileName, "r");
250
+
251
+
252
+
253
+ if (file == NULL)
254
+
255
+ {
256
+
257
+ std::cerr << ".OBJファイルが開けません: " << fileName << std::endl;
258
+
259
+ assert(0);
260
+
261
+ }
262
+
263
+ else
264
+
265
+ {
266
+
267
+ while (true)
268
+
269
+ {
270
+
271
+ char line[500];
272
+
273
+
274
+
275
+ int res = fscanf(file, "%s", line);
276
+
277
+
278
+
279
+ if (res == EOF)
280
+
281
+ {
282
+
283
+ break;
284
+
285
+ }
286
+
287
+
288
+
289
+ if (strcmp(line, "v") == 0)
290
+
291
+ {
292
+
293
+ glm::vec3 vert;
294
+
295
+ fscanf(file, "%f %f %fn", &vert.x, &vert.y, &vert.z);
296
+
297
+ vertex.push_back(vert);
298
+
299
+ // obj.vertex.push_back(vert);
300
+
301
+ }
302
+
303
+ else if (strcmp(line, "vt") == 0)
304
+
305
+ {
306
+
307
+ glm::vec2 u;
308
+
309
+ fscanf(file, "%f %fn", &u.x, &u.y);
310
+
311
+ //u.y = 1.0f - u.y;
312
+
313
+ // obj.uv.push_back(u);
314
+
315
+ uv.push_back(u);
316
+
317
+ }
318
+
319
+ else if (strcmp(line, "vn") == 0)
320
+
321
+ {
322
+
323
+
324
+
325
+ glm::vec3 norm;
326
+
327
+ fscanf(file, "%f %f %fn", &norm.x, &norm.y, &norm.z);
328
+
329
+ // obj.normal.push_back(norm);
330
+
331
+ normal.push_back(norm);
332
+
333
+ }
334
+
335
+ else if (strcmp(line, "f") == 0)
336
+
337
+ {
338
+
339
+
340
+
341
+ std::string vertex1, vertex2, vertex3;
342
+
343
+ unsigned int v[3], u[3], n[3];
344
+
345
+ int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%dn", &v[0], &u[0], &n[0], &v[1], &u[1], &n[1], &v[2], &u[2], &n[2]);
346
+
347
+ if (matches != 9)
348
+
349
+ {
350
+
351
+ printf("File can't be read by our simple parser : ( Try exporting with other optionsn");
352
+
353
+ assert(0);
354
+
355
+ }
356
+
357
+
358
+
359
+
360
+
361
+
362
+
363
+ vertexIndex.push_back(v[0]);
364
+
365
+ vertexIndex.push_back(v[1]);
366
+
367
+ vertexIndex.push_back(v[2]);
368
+
369
+
370
+
371
+ uvIndex.push_back(u[0]);
372
+
373
+ uvIndex.push_back(u[1]);
374
+
375
+ uvIndex.push_back(u[2]);
376
+
377
+
378
+
379
+ normalIndex.push_back(n[0]);
380
+
381
+ normalIndex.push_back(n[1]);
382
+
383
+ normalIndex.push_back(n[2]);
384
+
385
+
386
+
387
+ }
388
+
389
+ }
390
+
391
+
392
+
393
+ for( unsigned int i = 0; i < vertexIndex.size(); i++ )
394
+
395
+ {
396
+
397
+
398
+
399
+ unsigned int vi = vertexIndex[i];
400
+
401
+ unsigned int ui = uvIndex[i];
402
+
403
+ unsigned int ni = normalIndex[i];
404
+
405
+
406
+
407
+ glm::vec3 v = vertex[ vi - 1];
408
+
409
+ glm::vec2 u = uv[ ui - 1];
410
+
411
+ glm::vec3 n = normal[ ni - 1];
412
+
413
+
414
+
415
+ obj.vertex.push_back(v);
416
+
417
+ obj.uv.push_back(u);
418
+
419
+ obj.normal.push_back(n);
420
+
421
+ }
422
+
423
+
424
+
425
+ }
426
+
427
+ attribute = obj;
276
428
 
277
429
  }
278
430
 
279
431
 
280
432
 
281
-
282
-
283
- ```
433
+ ```
284
-
285
-
286
-
287
- ##### Model.cpp
288
-
289
- ```
290
-
291
-
292
-
293
- // ##################################### .objファイル読み込み #####################################
294
-
295
- void FrameWork::D3::LoadObj(const char *fileName, ObjFile &attribute)
296
-
297
- {
298
-
299
- ObjFile obj;
300
-
301
-
302
-
303
- std::vector<int> vertexIndex;
304
-
305
- std::vector<int> uvIndex;
306
-
307
- std::vector<int> normalIndex;
308
-
309
-
310
-
311
- std::vector<glm::vec3> vertex;
312
-
313
- std::vector<glm::vec2> uv;
314
-
315
- std::vector<glm::vec3> normal;
316
-
317
-
318
-
319
- FILE *file = fopen(fileName, "r");
320
-
321
-
322
-
323
- if (file == NULL)
324
-
325
- {
326
-
327
- std::cerr << ".OBJファイルが開けません: " << fileName << std::endl;
328
-
329
- assert(0);
330
-
331
- }
332
-
333
- else
334
-
335
- {
336
-
337
- while (true)
338
-
339
- {
340
-
341
- char line[500];
342
-
343
-
344
-
345
- int res = fscanf(file, "%s", line);
346
-
347
-
348
-
349
- if (res == EOF)
350
-
351
- {
352
-
353
- break;
354
-
355
- }
356
-
357
-
358
-
359
- if (strcmp(line, "v") == 0)
360
-
361
- {
362
-
363
- glm::vec3 vert;
364
-
365
- fscanf(file, "%f %f %fn", &vert.x, &vert.y, &vert.z);
366
-
367
- vertex.push_back(vert);
368
-
369
- // obj.vertex.push_back(vert);
370
-
371
- }
372
-
373
- else if (strcmp(line, "vt") == 0)
374
-
375
- {
376
-
377
- glm::vec2 u;
378
-
379
- fscanf(file, "%f %fn", &u.x, &u.y);
380
-
381
- //u.y = 1.0f - u.y;
382
-
383
- // obj.uv.push_back(u);
384
-
385
- uv.push_back(u);
386
-
387
- }
388
-
389
- else if (strcmp(line, "vn") == 0)
390
-
391
- {
392
-
393
-
394
-
395
- glm::vec3 norm;
396
-
397
- fscanf(file, "%f %f %fn", &norm.x, &norm.y, &norm.z);
398
-
399
- // obj.normal.push_back(norm);
400
-
401
- normal.push_back(norm);
402
-
403
- }
404
-
405
- else if (strcmp(line, "f") == 0)
406
-
407
- {
408
-
409
-
410
-
411
- std::string vertex1, vertex2, vertex3;
412
-
413
- unsigned int v[3], u[3], n[3];
414
-
415
- int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%dn", &v[0], &u[0], &n[0], &v[1], &u[1], &n[1], &v[2], &u[2], &n[2]);
416
-
417
- if (matches != 9)
418
-
419
- {
420
-
421
- printf("File can't be read by our simple parser : ( Try exporting with other optionsn");
422
-
423
- assert(0);
424
-
425
- }
426
-
427
-
428
-
429
-
430
-
431
-
432
-
433
- vertexIndex.push_back(v[0]);
434
-
435
- vertexIndex.push_back(v[1]);
436
-
437
- vertexIndex.push_back(v[2]);
438
-
439
-
440
-
441
- uvIndex.push_back(u[0]);
442
-
443
- uvIndex.push_back(u[1]);
444
-
445
- uvIndex.push_back(u[2]);
446
-
447
-
448
-
449
- normalIndex.push_back(n[0]);
450
-
451
- normalIndex.push_back(n[1]);
452
-
453
- normalIndex.push_back(n[2]);
454
-
455
-
456
-
457
- }
458
-
459
- }
460
-
461
-
462
-
463
- for( unsigned int i = 0; i < vertexIndex.size(); i++ )
464
-
465
- {
466
-
467
-
468
-
469
- unsigned int vi = vertexIndex[i];
470
-
471
- unsigned int ui = uvIndex[i];
472
-
473
- unsigned int ni = normalIndex[i];
474
-
475
-
476
-
477
- glm::vec3 v = vertex[ vi - 1];
478
-
479
- glm::vec2 u = uv[ ui - 1];
480
-
481
- glm::vec3 n = normal[ ni - 1];
482
-
483
-
484
-
485
- obj.vertex.push_back(v);
486
-
487
- obj.uv.push_back(u);
488
-
489
- obj.normal.push_back(n);
490
-
491
- }
492
-
493
-
494
-
495
- }
496
-
497
- attribute = obj;
498
-
499
- }
500
-
501
-
502
-
503
- ```

3

文書を再度修正

2021/10/29 04:25

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
 
4
4
 
5
+ GLSLコードはPhongシェーディングです。
6
+
7
+ Mode.cppはobjローダーです。
8
+
9
+
10
+
5
11
  現状、Model.cppのLoadobj()関数でblenderで出力したobjファイルをロードしています。GLSLは提示サイトを参考に記述しました。
6
12
 
7
13
  頂点と法線は正しく読み込まれていることを確認しました。頂点インデックスは使っていません。

2

文章を修正

2021/10/29 04:19

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
 
4
4
 
5
+ 現状、Model.cppのLoadobj()関数でblenderで出力したobjファイルをロードしています。GLSLは提示サイトを参考に記述しました。
6
+
7
+ 頂点と法線は正しく読み込まれていることを確認しました。頂点インデックスは使っていません。
8
+
9
+
10
+
11
+
12
+
13
+
14
+
5
15
 
6
16
 
7
17
 
@@ -268,7 +278,7 @@
268
278
 
269
279
 
270
280
 
271
- ##### Model
281
+ ##### Model.cpp
272
282
 
273
283
  ```
274
284
 

1

文章を修正

2021/10/29 04:18

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -18,6 +18,162 @@
18
18
 
19
19
 
20
20
 
21
+ ##### Fragment Shader
22
+
23
+ ```
24
+
25
+ /*#########################################################################
26
+
27
+ #
28
+
29
+ ###########################################################################*/
30
+
31
+ #version 420
32
+
33
+ //#extension GL_ARB_explicit_uniform_location : require
34
+
35
+
36
+
37
+ /*#########################################################################
38
+
39
+ #
40
+
41
+ ###########################################################################*/
42
+
43
+ #version 420
44
+
45
+ //#extension GL_ARB_explicit_uniform_location : require
46
+
47
+
48
+
49
+ layout(location = 2) in vec3 fPosition;
50
+
51
+ layout(location = 3) in vec3 fNormal;
52
+
53
+
54
+
55
+
56
+
57
+ uniform vec3 lightPos;
58
+
59
+ uniform vec3 lightColor;
60
+
61
+ uniform vec3 objectColor;
62
+
63
+ uniform vec3 viewPos;
64
+
65
+
66
+
67
+ out vec4 fragment;
68
+
69
+
70
+
71
+ void main()
72
+
73
+ {
74
+
75
+
76
+
77
+ // ambient
78
+
79
+ float ambientStrength = 0.1;
80
+
81
+ vec3 ambient = ambientStrength * lightColor;
82
+
83
+
84
+
85
+ // diffuse
86
+
87
+ vec3 norm = normalize(fNormal);
88
+
89
+ vec3 lightDir = normalize(lightPos - fPosition);
90
+
91
+ float diff = max(dot(norm, lightDir), 0.0);
92
+
93
+ vec3 diffuse = diff * lightColor;
94
+
95
+
96
+
97
+ // specular
98
+
99
+ float specularStrength = 0.5;
100
+
101
+ vec3 viewDir = normalize(viewPos - fPosition);
102
+
103
+ vec3 reflectDir = reflect(-lightDir, norm);
104
+
105
+ float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
106
+
107
+ vec3 specular = specularStrength * spec * lightColor;
108
+
109
+
110
+
111
+
112
+
113
+ vec3 result = (ambient + diffuse + specular) * objectColor;
114
+
115
+ fragment = vec4(result, 1.0);
116
+
117
+ }
118
+
119
+
120
+
121
+
122
+
123
+ layout(location = 0) in vec3 vertexPosition;
124
+
125
+ layout(location = 1) in vec3 vertexNormal;
126
+
127
+
128
+
129
+ uniform mat4 uTranslate;
130
+
131
+ uniform mat4 uRotate;
132
+
133
+ uniform mat4 uScale;
134
+
135
+ uniform mat4 uViewProjection;
136
+
137
+
138
+
139
+
140
+
141
+ layout(location = 2) out vec3 vPosition;
142
+
143
+ layout(location = 3) out vec3 vNormal;
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+ void main()
152
+
153
+ {
154
+
155
+ vec4 vertex = vec4(vertexPosition,1.0);
156
+
157
+ mat4 model = uTranslate * uRotate * uScale;
158
+
159
+ gl_Position = (uViewProjection * model) * vertex;
160
+
161
+
162
+
163
+ vPosition = vec3(model * vec4(vertexPosition,1.0));
164
+
165
+ vNormal = mat3(transpose(inverse(model))) * vertexNormal;
166
+
167
+
168
+
169
+
170
+
171
+ }
172
+
173
+ ```
174
+
175
+
176
+
21
177
  ##### Vertex Shader
22
178
 
23
179
  ```
@@ -34,18 +190,6 @@
34
190
 
35
191
 
36
192
 
37
- /*#########################################################################
38
-
39
- #
40
-
41
- ###########################################################################*/
42
-
43
- #version 420
44
-
45
- //#extension GL_ARB_explicit_uniform_location : require
46
-
47
-
48
-
49
193
  layout(location = 2) in vec3 fPosition;
50
194
 
51
195
  layout(location = 3) in vec3 fNormal;
@@ -120,368 +264,224 @@
120
264
 
121
265
 
122
266
 
123
- layout(location = 0) in vec3 vertexPosition;
124
-
125
- layout(location = 1) in vec3 vertexNormal;
126
-
127
-
128
-
129
- uniform mat4 uTranslate;
130
-
131
- uniform mat4 uRotate;
132
-
133
- uniform mat4 uScale;
267
+ ```
134
-
135
- uniform mat4 uViewProjection;
268
+
136
-
137
-
138
-
139
-
140
-
141
- layout(location = 2) out vec3 vPosition;
269
+
142
-
143
- layout(location = 3) out vec3 vNormal;
270
+
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
- void main()
271
+ ##### Model
272
+
273
+ ```
274
+
275
+
276
+
277
+ // ##################################### .objファイル読み込み #####################################
278
+
279
+ void FrameWork::D3::LoadObj(const char *fileName, ObjFile &attribute)
152
280
 
153
281
  {
154
282
 
155
- vec4 vertex = vec4(vertexPosition,1.0);
156
-
157
- mat4 model = uTranslate * uRotate * uScale;
158
-
159
- gl_Position = (uViewProjection * model) * vertex;
160
-
161
-
162
-
163
- vPosition = vec3(model * vec4(vertexPosition,1.0));
164
-
165
- vNormal = mat3(transpose(inverse(model))) * vertexNormal;
166
-
167
-
168
-
169
-
283
+ ObjFile obj;
284
+
285
+
286
+
287
+ std::vector<int> vertexIndex;
288
+
289
+ std::vector<int> uvIndex;
290
+
291
+ std::vector<int> normalIndex;
292
+
293
+
294
+
295
+ std::vector<glm::vec3> vertex;
296
+
297
+ std::vector<glm::vec2> uv;
298
+
299
+ std::vector<glm::vec3> normal;
300
+
301
+
302
+
303
+ FILE *file = fopen(fileName, "r");
304
+
305
+
306
+
307
+ if (file == NULL)
308
+
309
+ {
310
+
311
+ std::cerr << ".OBJファイルが開けません: " << fileName << std::endl;
312
+
313
+ assert(0);
314
+
315
+ }
316
+
317
+ else
318
+
319
+ {
320
+
321
+ while (true)
322
+
323
+ {
324
+
325
+ char line[500];
326
+
327
+
328
+
329
+ int res = fscanf(file, "%s", line);
330
+
331
+
332
+
333
+ if (res == EOF)
334
+
335
+ {
336
+
337
+ break;
338
+
339
+ }
340
+
341
+
342
+
343
+ if (strcmp(line, "v") == 0)
344
+
345
+ {
346
+
347
+ glm::vec3 vert;
348
+
349
+ fscanf(file, "%f %f %fn", &vert.x, &vert.y, &vert.z);
350
+
351
+ vertex.push_back(vert);
352
+
353
+ // obj.vertex.push_back(vert);
354
+
355
+ }
356
+
357
+ else if (strcmp(line, "vt") == 0)
358
+
359
+ {
360
+
361
+ glm::vec2 u;
362
+
363
+ fscanf(file, "%f %fn", &u.x, &u.y);
364
+
365
+ //u.y = 1.0f - u.y;
366
+
367
+ // obj.uv.push_back(u);
368
+
369
+ uv.push_back(u);
370
+
371
+ }
372
+
373
+ else if (strcmp(line, "vn") == 0)
374
+
375
+ {
376
+
377
+
378
+
379
+ glm::vec3 norm;
380
+
381
+ fscanf(file, "%f %f %fn", &norm.x, &norm.y, &norm.z);
382
+
383
+ // obj.normal.push_back(norm);
384
+
385
+ normal.push_back(norm);
386
+
387
+ }
388
+
389
+ else if (strcmp(line, "f") == 0)
390
+
391
+ {
392
+
393
+
394
+
395
+ std::string vertex1, vertex2, vertex3;
396
+
397
+ unsigned int v[3], u[3], n[3];
398
+
399
+ int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%dn", &v[0], &u[0], &n[0], &v[1], &u[1], &n[1], &v[2], &u[2], &n[2]);
400
+
401
+ if (matches != 9)
402
+
403
+ {
404
+
405
+ printf("File can't be read by our simple parser : ( Try exporting with other optionsn");
406
+
407
+ assert(0);
408
+
409
+ }
410
+
411
+
412
+
413
+
414
+
415
+
416
+
417
+ vertexIndex.push_back(v[0]);
418
+
419
+ vertexIndex.push_back(v[1]);
420
+
421
+ vertexIndex.push_back(v[2]);
422
+
423
+
424
+
425
+ uvIndex.push_back(u[0]);
426
+
427
+ uvIndex.push_back(u[1]);
428
+
429
+ uvIndex.push_back(u[2]);
430
+
431
+
432
+
433
+ normalIndex.push_back(n[0]);
434
+
435
+ normalIndex.push_back(n[1]);
436
+
437
+ normalIndex.push_back(n[2]);
438
+
439
+
440
+
441
+ }
442
+
443
+ }
444
+
445
+
446
+
447
+ for( unsigned int i = 0; i < vertexIndex.size(); i++ )
448
+
449
+ {
450
+
451
+
452
+
453
+ unsigned int vi = vertexIndex[i];
454
+
455
+ unsigned int ui = uvIndex[i];
456
+
457
+ unsigned int ni = normalIndex[i];
458
+
459
+
460
+
461
+ glm::vec3 v = vertex[ vi - 1];
462
+
463
+ glm::vec2 u = uv[ ui - 1];
464
+
465
+ glm::vec3 n = normal[ ni - 1];
466
+
467
+
468
+
469
+ obj.vertex.push_back(v);
470
+
471
+ obj.uv.push_back(u);
472
+
473
+ obj.normal.push_back(n);
474
+
475
+ }
476
+
477
+
478
+
479
+ }
480
+
481
+ attribute = obj;
170
482
 
171
483
  }
172
484
 
485
+
486
+
173
- ```
487
+ ```
174
-
175
-
176
-
177
- ##### Fragment Shader
178
-
179
- ```
180
-
181
- /*#########################################################################
182
-
183
- #
184
-
185
- ###########################################################################*/
186
-
187
- #version 420
188
-
189
- //#extension GL_ARB_explicit_uniform_location : require
190
-
191
-
192
-
193
- layout(location = 2) in vec3 fPosition;
194
-
195
- layout(location = 3) in vec3 fNormal;
196
-
197
-
198
-
199
-
200
-
201
- uniform vec3 lightPos;
202
-
203
- uniform vec3 lightColor;
204
-
205
- uniform vec3 objectColor;
206
-
207
- uniform vec3 viewPos;
208
-
209
-
210
-
211
- out vec4 fragment;
212
-
213
-
214
-
215
- void main()
216
-
217
- {
218
-
219
-
220
-
221
- // ambient
222
-
223
- float ambientStrength = 0.1;
224
-
225
- vec3 ambient = ambientStrength * lightColor;
226
-
227
-
228
-
229
- // diffuse
230
-
231
- vec3 norm = normalize(fNormal);
232
-
233
- vec3 lightDir = normalize(lightPos - fPosition);
234
-
235
- float diff = max(dot(norm, lightDir), 0.0);
236
-
237
- vec3 diffuse = diff * lightColor;
238
-
239
-
240
-
241
- // specular
242
-
243
- float specularStrength = 0.5;
244
-
245
- vec3 viewDir = normalize(viewPos - fPosition);
246
-
247
- vec3 reflectDir = reflect(-lightDir, norm);
248
-
249
- float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
250
-
251
- vec3 specular = specularStrength * spec * lightColor;
252
-
253
-
254
-
255
-
256
-
257
- vec3 result = (ambient + diffuse + specular) * objectColor;
258
-
259
- fragment = vec4(result, 1.0);
260
-
261
- }
262
-
263
-
264
-
265
-
266
-
267
- ```
268
-
269
-
270
-
271
- ##### Model
272
-
273
- ```
274
-
275
-
276
-
277
- // ##################################### .objファイル読み込み #####################################
278
-
279
- void FrameWork::D3::LoadObj(const char *fileName, ObjFile &attribute)
280
-
281
- {
282
-
283
- ObjFile obj;
284
-
285
-
286
-
287
- std::vector<int> vertexIndex;
288
-
289
- std::vector<int> uvIndex;
290
-
291
- std::vector<int> normalIndex;
292
-
293
-
294
-
295
- std::vector<glm::vec3> vertex;
296
-
297
- std::vector<glm::vec2> uv;
298
-
299
- std::vector<glm::vec3> normal;
300
-
301
-
302
-
303
- FILE *file = fopen(fileName, "r");
304
-
305
-
306
-
307
- if (file == NULL)
308
-
309
- {
310
-
311
- std::cerr << ".OBJファイルが開けません: " << fileName << std::endl;
312
-
313
- assert(0);
314
-
315
- }
316
-
317
- else
318
-
319
- {
320
-
321
- while (true)
322
-
323
- {
324
-
325
- char line[500];
326
-
327
-
328
-
329
- int res = fscanf(file, "%s", line);
330
-
331
-
332
-
333
- if (res == EOF)
334
-
335
- {
336
-
337
- break;
338
-
339
- }
340
-
341
-
342
-
343
- if (strcmp(line, "v") == 0)
344
-
345
- {
346
-
347
- glm::vec3 vert;
348
-
349
- fscanf(file, "%f %f %fn", &vert.x, &vert.y, &vert.z);
350
-
351
- vertex.push_back(vert);
352
-
353
- // obj.vertex.push_back(vert);
354
-
355
- }
356
-
357
- else if (strcmp(line, "vt") == 0)
358
-
359
- {
360
-
361
- glm::vec2 u;
362
-
363
- fscanf(file, "%f %fn", &u.x, &u.y);
364
-
365
- //u.y = 1.0f - u.y;
366
-
367
- // obj.uv.push_back(u);
368
-
369
- uv.push_back(u);
370
-
371
- }
372
-
373
- else if (strcmp(line, "vn") == 0)
374
-
375
- {
376
-
377
-
378
-
379
- glm::vec3 norm;
380
-
381
- fscanf(file, "%f %f %fn", &norm.x, &norm.y, &norm.z);
382
-
383
- // obj.normal.push_back(norm);
384
-
385
- normal.push_back(norm);
386
-
387
- }
388
-
389
- else if (strcmp(line, "f") == 0)
390
-
391
- {
392
-
393
-
394
-
395
- std::string vertex1, vertex2, vertex3;
396
-
397
- unsigned int v[3], u[3], n[3];
398
-
399
- int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%dn", &v[0], &u[0], &n[0], &v[1], &u[1], &n[1], &v[2], &u[2], &n[2]);
400
-
401
- if (matches != 9)
402
-
403
- {
404
-
405
- printf("File can't be read by our simple parser : ( Try exporting with other optionsn");
406
-
407
- assert(0);
408
-
409
- }
410
-
411
-
412
-
413
-
414
-
415
-
416
-
417
- vertexIndex.push_back(v[0]);
418
-
419
- vertexIndex.push_back(v[1]);
420
-
421
- vertexIndex.push_back(v[2]);
422
-
423
-
424
-
425
- uvIndex.push_back(u[0]);
426
-
427
- uvIndex.push_back(u[1]);
428
-
429
- uvIndex.push_back(u[2]);
430
-
431
-
432
-
433
- normalIndex.push_back(n[0]);
434
-
435
- normalIndex.push_back(n[1]);
436
-
437
- normalIndex.push_back(n[2]);
438
-
439
-
440
-
441
- }
442
-
443
- }
444
-
445
-
446
-
447
- for( unsigned int i = 0; i < vertexIndex.size(); i++ )
448
-
449
- {
450
-
451
-
452
-
453
- unsigned int vi = vertexIndex[i];
454
-
455
- unsigned int ui = uvIndex[i];
456
-
457
- unsigned int ni = normalIndex[i];
458
-
459
-
460
-
461
- glm::vec3 v = vertex[ vi - 1];
462
-
463
- glm::vec2 u = uv[ ui - 1];
464
-
465
- glm::vec3 n = normal[ ni - 1];
466
-
467
-
468
-
469
- obj.vertex.push_back(v);
470
-
471
- obj.uv.push_back(u);
472
-
473
- obj.normal.push_back(n);
474
-
475
- }
476
-
477
-
478
-
479
- }
480
-
481
- attribute = obj;
482
-
483
- }
484
-
485
-
486
-
487
- ```