回答編集履歴
2
書式修正
test
CHANGED
@@ -4,6 +4,8 @@
|
|
4
4
|
|
5
5
|
- 並行移動の行列を調整して奥に移動
|
6
6
|
|
7
|
+
|
8
|
+
|
7
9
|
とりあえず上記を修正したら表示はされました。
|
8
10
|
|
9
11
|
|
1
回答全面修正
test
CHANGED
@@ -1,14 +1,350 @@
|
|
1
|
+
- create_maxtri_mpの引数を調整。
|
2
|
+
|
3
|
+
- glUniformMatrix4fvの第2引数は個数なので1を指定
|
4
|
+
|
5
|
+
- 並行移動の行列を調整して奥に移動
|
6
|
+
|
1
|
-
|
7
|
+
とりあえず上記を修正したら表示はされました。
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
あとは、回転行列の部分は元が何をしようとしているかが不明なので、
|
12
|
+
|
13
|
+
とりあえず画面の更新で回転するようにしてみました。
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
```hpp
|
18
|
+
|
19
|
+
//平行移動
|
20
|
+
|
21
|
+
float mx = 0;
|
22
|
+
|
23
|
+
float my = 0;
|
24
|
+
|
25
|
+
float mz = -3; // 奥へ移動
|
26
|
+
|
27
|
+
const GLfloat move[4][4] =
|
28
|
+
|
29
|
+
{
|
30
|
+
|
31
|
+
1,0,0,mx,
|
32
|
+
|
33
|
+
0,1,0,my,
|
34
|
+
|
35
|
+
0,0,1,mz,
|
36
|
+
|
37
|
+
0,0,0,1,
|
38
|
+
|
39
|
+
};
|
40
|
+
|
41
|
+
```
|
2
42
|
|
3
43
|
|
4
44
|
|
5
45
|
```cpp
|
6
46
|
|
47
|
+
#include "Game.hpp"
|
48
|
+
|
49
|
+
#include <iostream>
|
50
|
+
|
51
|
+
//#include "stdafx.h"
|
52
|
+
|
53
|
+
#include <iostream>
|
54
|
+
|
55
|
+
#include <string>
|
56
|
+
|
57
|
+
#include <fstream>
|
58
|
+
|
59
|
+
#include <sstream>
|
60
|
+
|
61
|
+
#include "Vector.hpp"
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
//透視投影変換行列を作る
|
68
|
+
|
69
|
+
void Game::create_maxtri_mp(float top, float bottom, float left, float right,
|
70
|
+
|
71
|
+
float near, float far, float result[4][4])
|
72
|
+
|
73
|
+
{
|
74
|
+
|
75
|
+
result[0][0] = (2 * near) / (right - left);
|
76
|
+
|
77
|
+
result[0][1] = 0;
|
78
|
+
|
79
|
+
result[0][2] = (right + left) / (right - left);
|
80
|
+
|
81
|
+
result[0][3] = 0;
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
result[1][0] = 0;
|
86
|
+
|
87
|
+
result[1][1] = (2 * near) / (top - bottom);
|
88
|
+
|
89
|
+
result[1][2] = (top + bottom) / (top - bottom);
|
90
|
+
|
91
|
+
result[1][3] = 0;
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
result[2][0] = 0;
|
96
|
+
|
97
|
+
result[2][1] = 0;
|
98
|
+
|
99
|
+
result[2][2] = -(far + near) / (far - near);
|
100
|
+
|
101
|
+
result[2][3] = -(2 * far + near) / (far - near);
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
result[3][0] = 0;
|
106
|
+
|
107
|
+
result[3][1] = 0;
|
108
|
+
|
109
|
+
result[3][2] = -1;
|
110
|
+
|
111
|
+
result[3][3] = 0;
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
//コンストラクタ
|
120
|
+
|
121
|
+
Game::Game()
|
122
|
+
|
123
|
+
{
|
124
|
+
|
125
|
+
mIsRunLoop = true;
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
//メインループ
|
134
|
+
|
135
|
+
bool Game::RunLoop()
|
136
|
+
|
137
|
+
{
|
138
|
+
|
139
|
+
if (mIsRunLoop == true)
|
140
|
+
|
141
|
+
{
|
142
|
+
|
143
|
+
Update();
|
144
|
+
|
145
|
+
GenerateOutput();
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
else {
|
150
|
+
|
151
|
+
return false;
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
return true;
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
//シェーダー関係のログを取得
|
166
|
+
|
167
|
+
void Game::GetShader_Log(GLuint shader_type, GLuint Log_type)
|
168
|
+
|
169
|
+
{
|
170
|
+
|
171
|
+
GLint bufferSize = 0;
|
172
|
+
|
173
|
+
GLchar* infoLog = nullptr;
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
//成功したかどうか?
|
178
|
+
|
179
|
+
glGetShaderiv(shader_type, Log_type, &bufferSize);
|
180
|
+
|
181
|
+
if (bufferSize == GL_FALSE)
|
182
|
+
|
183
|
+
{
|
184
|
+
|
185
|
+
//printf("");
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
//ログの長さを取得する
|
190
|
+
|
191
|
+
glGetShaderiv(shader_type, GL_INFO_LOG_LENGTH, &bufferSize);
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
infoLog = (GLchar*)malloc(bufferSize);
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
if (infoLog != NULL)
|
200
|
+
|
201
|
+
{
|
202
|
+
|
203
|
+
GLsizei length;//ログの長さ
|
204
|
+
|
205
|
+
glGetShaderInfoLog(shader_type, bufferSize, &length, infoLog);
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
if (length > 1)//null文字を含むため一文字以上の場合ログ出力
|
210
|
+
|
211
|
+
{
|
212
|
+
|
213
|
+
fprintf(stderr, "InfoLog: %s\n\n", infoLog);
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
free(infoLog);//メモリ開放
|
220
|
+
|
221
|
+
infoLog = NULL;
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
//return true;
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
//シェーダを実装する
|
234
|
+
|
7
235
|
bool Game::Shader()
|
8
236
|
|
9
237
|
{
|
10
238
|
|
239
|
+
|
240
|
+
|
241
|
+
program = glCreateProgram();
|
242
|
+
|
243
|
+
GLuint out_vert = glCreateShader(GL_VERTEX_SHADER);
|
244
|
+
|
245
|
+
GLuint out_frag = glCreateShader(GL_FRAGMENT_SHADER);
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
//頂点シェーダーを読み込み
|
250
|
+
|
251
|
+
std::string fileName = "Basic.vert";
|
252
|
+
|
253
|
+
std::ifstream shaderfile(fileName);
|
254
|
+
|
255
|
+
if (shaderfile.is_open())
|
256
|
+
|
257
|
+
{
|
258
|
+
|
259
|
+
std::stringstream sstream;
|
260
|
+
|
261
|
+
sstream << shaderfile.rdbuf();
|
262
|
+
|
263
|
+
std::string str = sstream.str();
|
264
|
+
|
265
|
+
const char* cc = str.c_str();
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
out_vert = glCreateShader(GL_VERTEX_SHADER);
|
270
|
+
|
271
|
+
glShaderSource(out_vert, 1, &cc, nullptr);
|
272
|
+
|
273
|
+
glCompileShader(out_vert);
|
274
|
+
|
275
|
+
//printf("Basic.vert ファイル読み込み\n");
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
GetShader_Log(out_vert, GL_COMPILE_STATUS);
|
280
|
+
|
281
|
+
glAttachShader(program, out_vert);
|
282
|
+
|
283
|
+
shaderfile.close();
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
}
|
288
|
+
|
11
|
-
|
289
|
+
else {
|
290
|
+
|
291
|
+
shaderfile.close();
|
292
|
+
|
293
|
+
printf("ファイル: %s が読み込めませんでした。", fileName.c_str());
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
//バーティックスシェーダーを読み込み
|
302
|
+
|
303
|
+
fileName = "Basic.frag";
|
304
|
+
|
305
|
+
shaderfile.open(fileName);
|
306
|
+
|
307
|
+
if (shaderfile.is_open())
|
308
|
+
|
309
|
+
{
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
std::stringstream sstream;
|
314
|
+
|
315
|
+
sstream << shaderfile.rdbuf();
|
316
|
+
|
317
|
+
std::string str = sstream.str();
|
318
|
+
|
319
|
+
const char* cc = str.c_str();
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
out_frag = glCreateShader(GL_FRAGMENT_SHADER);
|
324
|
+
|
325
|
+
glShaderSource(out_frag, 1, &cc, nullptr);
|
326
|
+
|
327
|
+
glCompileShader(out_frag);
|
328
|
+
|
329
|
+
GetShader_Log(out_frag, GL_COMPILE_STATUS);
|
330
|
+
|
331
|
+
glAttachShader(program, out_frag);
|
332
|
+
|
333
|
+
shaderfile.close();
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
// printf("Basic.frag ファイル読み込み\n");
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
else {
|
342
|
+
|
343
|
+
shaderfile.close();
|
344
|
+
|
345
|
+
printf("ファイル: %s が読み込めませんでした。", fileName.c_str());
|
346
|
+
|
347
|
+
}
|
12
348
|
|
13
349
|
|
14
350
|
|
@@ -16,6 +352,8 @@
|
|
16
352
|
|
17
353
|
|
18
354
|
|
355
|
+
|
356
|
+
|
19
357
|
glLinkProgram(program);
|
20
358
|
|
21
359
|
//使わなくなったシェーダーオブジェクトを削除
|
@@ -24,12 +362,12 @@
|
|
24
362
|
|
25
363
|
glDeleteShader(out_vert);
|
26
364
|
|
27
|
-
|
28
|
-
|
29
365
|
glUseProgram(program);
|
30
366
|
|
31
367
|
|
32
368
|
|
369
|
+
|
370
|
+
|
33
371
|
//シェーダーに値を渡す
|
34
372
|
|
35
373
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
@@ -38,31 +376,47 @@
|
|
38
376
|
|
39
377
|
float mp[4][4];
|
40
378
|
|
41
|
-
create_maxtri_mp(
|
379
|
+
create_maxtri_mp(1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 10.0f, mp);//透視行列を作成
|
380
|
+
|
381
|
+
|
382
|
+
|
42
|
-
|
383
|
+
const GLfloat rot[4][4] = {
|
384
|
+
|
43
|
-
|
385
|
+
{1,0,0,0},
|
386
|
+
|
44
|
-
|
387
|
+
{0,1,0,0},
|
388
|
+
|
45
|
-
|
389
|
+
{0,0,1,0},
|
390
|
+
|
391
|
+
{0,0,0,1}
|
392
|
+
|
393
|
+
};
|
46
394
|
|
47
395
|
|
48
396
|
|
49
397
|
//頂点シェーダー
|
50
398
|
|
51
|
-
glUniformMatrix4fv(glGetUniformLocation(program,"scale"),
|
399
|
+
glUniformMatrix4fv(glGetUniformLocation(program, "scale"), 1, GL_TRUE, &scale[0][0]);
|
52
|
-
|
400
|
+
|
53
|
-
glUniformMatrix4fv(glGetUniformLocation(program, "rotate"), 1, GL_TRUE, &rot
|
401
|
+
glUniformMatrix4fv(glGetUniformLocation(program, "rotate"), 1, GL_TRUE, &rot[0][0]);
|
54
|
-
|
402
|
+
|
55
|
-
glUniformMatrix4fv(glGetUniformLocation(program, "move"),
|
403
|
+
glUniformMatrix4fv(glGetUniformLocation(program, "move"), 1, GL_TRUE, &move[0][0]);
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
|
408
|
+
|
61
|
-
glUniformMatrix4fv(glGetUniformLocation(program, "MP"),
|
409
|
+
glUniformMatrix4fv(glGetUniformLocation(program, "MP"), 1, GL_TRUE, &mp[0][0]);
|
62
|
-
|
410
|
+
|
63
|
-
//*****************************************************************************
|
411
|
+
//*****************************************************************************
|
64
|
-
|
412
|
+
|
65
|
-
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
413
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
414
|
+
|
415
|
+
glUseProgram(program);
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
|
66
420
|
|
67
421
|
|
68
422
|
|
@@ -70,4 +424,254 @@
|
|
70
424
|
|
71
425
|
}
|
72
426
|
|
427
|
+
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
//初期化
|
436
|
+
|
437
|
+
bool Game::Initialization()
|
438
|
+
|
439
|
+
{
|
440
|
+
|
441
|
+
//画面初期化 関係
|
442
|
+
|
443
|
+
///////////////////////////////////////////////////////////////////////////////////////////////////////////
|
444
|
+
|
445
|
+
//glfwの初期化に失敗
|
446
|
+
|
447
|
+
if (glfwInit() != GL_TRUE)
|
448
|
+
|
449
|
+
{
|
450
|
+
|
451
|
+
printf("glfwInit()失敗\n");
|
452
|
+
|
453
|
+
int _c = getchar();
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
return 0;
|
458
|
+
|
459
|
+
}
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
//コンテキストを作成
|
464
|
+
|
465
|
+
Window = glfwCreateWindow(640, 400, "Hello", NULL, NULL);
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
//OpenGLコンテキストの作成に失敗
|
470
|
+
|
471
|
+
if (Window == NULL)
|
472
|
+
|
473
|
+
{
|
474
|
+
|
475
|
+
printf("glfCreateWindow()失敗\n");
|
476
|
+
|
477
|
+
int c = getchar();
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
return 0;
|
482
|
+
|
483
|
+
}
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
//コンテキストをOpenGLの描画対象にする。
|
488
|
+
|
489
|
+
glfwMakeContextCurrent(Window);
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
//glewを初期化する。
|
494
|
+
|
495
|
+
glewExperimental = GL_TRUE;
|
496
|
+
|
497
|
+
if (glewInit() != GLEW_OK)
|
498
|
+
|
499
|
+
{
|
500
|
+
|
501
|
+
printf("glewInit() の初期化に失敗しました。");
|
502
|
+
|
503
|
+
return 0;
|
504
|
+
|
505
|
+
}
|
506
|
+
|
507
|
+
|
508
|
+
|
509
|
+
//OpenGLバージョン指定
|
510
|
+
|
511
|
+
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
512
|
+
|
513
|
+
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 0);
|
514
|
+
|
515
|
+
|
516
|
+
|
517
|
+
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
518
|
+
|
519
|
+
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
520
|
+
|
521
|
+
|
522
|
+
|
523
|
+
//垂直同期のタイミングを待つ
|
524
|
+
|
525
|
+
glfwSwapInterval(1);
|
526
|
+
|
527
|
+
///////////////////////////////////////////////////////////////////////////////////////////////////////////
|
528
|
+
|
529
|
+
|
530
|
+
|
531
|
+
|
532
|
+
|
533
|
+
|
534
|
+
|
535
|
+
if (Shader() == true)//シェーダーを読み込む
|
536
|
+
|
537
|
+
{
|
538
|
+
|
539
|
+
printf("シェーダーを読み込む\n");
|
540
|
+
|
541
|
+
}
|
542
|
+
|
543
|
+
|
544
|
+
|
545
|
+
|
546
|
+
|
547
|
+
glGenVertexArrays(1, &vbo);
|
548
|
+
|
549
|
+
glBindVertexArray(vbo);
|
550
|
+
|
551
|
+
glGenBuffers(1, &vbo);
|
552
|
+
|
553
|
+
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
554
|
+
|
555
|
+
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex), Vertex, GL_STATIC_DRAW);///////
|
556
|
+
|
557
|
+
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
558
|
+
|
559
|
+
glEnableVertexAttribArray(0);
|
560
|
+
|
561
|
+
//glEnableClientState(GL_VERTEX_ARRAY);//クライアントで頂点を有効にする。
|
562
|
+
|
563
|
+
|
564
|
+
|
565
|
+
|
566
|
+
|
567
|
+
|
568
|
+
|
569
|
+
|
570
|
+
|
571
|
+
|
572
|
+
|
573
|
+
mIsRunLoop = true;
|
574
|
+
|
575
|
+
return mIsRunLoop;
|
576
|
+
|
577
|
+
}
|
578
|
+
|
579
|
+
|
580
|
+
|
581
|
+
//アップデート
|
582
|
+
|
583
|
+
void Game::Update()
|
584
|
+
|
585
|
+
{
|
586
|
+
|
587
|
+
if (glfwGetKey(Window, GLFW_KEY_ESCAPE) == GLFW_PRESS || glfwWindowShouldClose(Window) != GL_FALSE)
|
588
|
+
|
589
|
+
{
|
590
|
+
|
591
|
+
mIsRunLoop = false;
|
592
|
+
|
593
|
+
}
|
594
|
+
|
595
|
+
|
596
|
+
|
597
|
+
|
598
|
+
|
599
|
+
}
|
600
|
+
|
601
|
+
|
602
|
+
|
603
|
+
static void rotateX(GLfloat radian, GLfloat m[])
|
604
|
+
|
605
|
+
{
|
606
|
+
|
607
|
+
float s = sin(radian);
|
608
|
+
|
609
|
+
float c = cos(radian);
|
610
|
+
|
611
|
+
m[0] = 1.0; m[1] = 0.0; m[2] = 0.0; m[3] = 0.0;
|
612
|
+
|
613
|
+
m[4] = 0.0; m[5] = c; m[6] = -s; m[7] = 0.0;
|
614
|
+
|
615
|
+
m[8] = 0.0; m[9] = s; m[10] = c; m[11] = 0.0;
|
616
|
+
|
617
|
+
m[12] = 0.0; m[13] = 0.0; m[14] = 0.0; m[15] = 1.0;
|
618
|
+
|
619
|
+
}
|
620
|
+
|
621
|
+
|
622
|
+
|
623
|
+
//描画アップデート
|
624
|
+
|
625
|
+
void Game::GenerateOutput()
|
626
|
+
|
627
|
+
{
|
628
|
+
|
629
|
+
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
630
|
+
|
631
|
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
632
|
+
|
633
|
+
glUseProgram(program);
|
634
|
+
|
635
|
+
|
636
|
+
|
637
|
+
static float rotX = 0;
|
638
|
+
|
639
|
+
float rot[16];
|
640
|
+
|
641
|
+
rotateX(rotX, rot);
|
642
|
+
|
643
|
+
rotX += 0.01f;
|
644
|
+
|
645
|
+
|
646
|
+
|
647
|
+
glUniformMatrix4fv(glGetUniformLocation(program, "rotate"), 1, GL_FALSE, rot);
|
648
|
+
|
649
|
+
|
650
|
+
|
651
|
+
|
652
|
+
|
653
|
+
glDrawArrays(GL_POLYGON, 0, 18);
|
654
|
+
|
655
|
+
|
656
|
+
|
657
|
+
|
658
|
+
|
659
|
+
|
660
|
+
|
661
|
+
|
662
|
+
|
663
|
+
|
664
|
+
|
665
|
+
|
666
|
+
|
667
|
+
glViewport(0, 0, WIDTH, HEIGHT);
|
668
|
+
|
669
|
+
glfwSwapBuffers(Window);
|
670
|
+
|
671
|
+
glfwPollEvents();
|
672
|
+
|
673
|
+
|
674
|
+
|
675
|
+
}
|
676
|
+
|
73
677
|
```
|