teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

文章を編集しました。

2020/09/10 10:11

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,378 +1,337 @@
1
- 提示コードのメイン関数部です。画面サイズ(ピクセル)(640,400.1)のワー座標で頂点を設定してその頂点を画面である正規化座標(1.0 ~ -1.0)に変換画面の真ん中四角い正方形を描画たいですがどうすればいいのでしょうか?正しい計算をしいれば(-0.5,0.5,1)になるはずなのですがなりません画面サイズとはピクセルサイズです。また座標系は3Dですが今回は2Dの物を表示させたいです。
1
+ 質問1、提示コードのメイン関数部です。 画面サイズ(640,400)ピクセです。OpenGLなので左手座標す。こで自分は画面サイズ上での頂点座標作成しました。正規化座標である(+1 ~ -1) し画面に描画させたいです。提示コード座標はすべて0.5と-0.5になるはずです。り正方形を描画させいのですが上手くいきまんこれはどうしいのしょうか?
2
2
 
3
+ 質問2、またシェーダーで4 x (4x4)の行列で(3x3)と3までの値しか使っていないのですがこれは大丈夫なのでしょうか?
3
4
 
5
+ 質問3、この処理をコンソール出力(新規プロジェクト)で確認したあとシェーダーに乗っけたいのですがどうするのが適切なのでしょうか?
4
6
 
5
- 質問 **ワールド座標から正規化座標に変換する計算式を知りたいです。
6
- **
7
7
 
8
8
 
9
9
 
10
+ ![イメージ説明](64eaf395a821f39415bcf1b169cc2c59.png)
11
+
10
- ```ここに言語を入力
12
+ ```cpp
11
- #include "stdio.h"
12
- #include "math.h"
13
+ #include "Game.hpp"
13
14
  #include <iostream>
14
15
 
15
16
 
17
+ //コンストラクタ
18
+ Game::Game()
19
+ {
20
+ mIsRunLoop = true;
21
+
22
+ }
16
23
 
17
-
18
- class Position
24
+ bool Game::RunLoop()
19
25
  {
20
- public:
26
+ if (mIsRunLoop == true)
27
+ {
21
- float x;
28
+ Update();
22
- float y;
29
+ GenerateOutput();
23
- float z;
24
30
 
25
- public:
26
- Position(float xx = 0, float yy = 0, float zz = 0)
27
- {
28
- x = xx;
29
- y = yy;
30
- z = zz;
31
- }
31
+ }
32
+ else {
33
+ return false;
34
+ }
35
+ return true;
32
36
 
37
+ }
33
38
 
34
39
 
35
- //掛け算
40
+ //シェーダー関係のログを取得
36
- Position operator* (Position pos)
41
+ void Game::GetShader_Log(GLuint shader_type, GLuint Log_type)
37
- {
42
+ {
43
+ GLint bufferSize = 0;
38
- this->x = pos.x * this->x;
44
+ GLchar* infoLog = nullptr;
39
- this->y = pos.y * this->y;
40
- this->z = pos.z * this->z;
41
45
 
46
+ //成功したかどうか?
47
+ glGetShaderiv(shader_type, Log_type, &bufferSize);
48
+
42
49
 
50
+ //ログの長さを取得する
51
+ glGetShaderiv(shader_type, GL_INFO_LOG_LENGTH, &bufferSize);
52
+
53
+ infoLog = (GLchar*)malloc(bufferSize);
54
+
43
- return *this;
55
+ if (infoLog != NULL)
44
- }
56
+ {
57
+ GLsizei length;//ログの長さ
58
+ glGetShaderInfoLog(shader_type, bufferSize, &length, infoLog);
45
59
 
46
- //掛け算
47
- Position operator- (Position pos)
60
+ if (length > 1)//null文字を含むため一文字以上の場合ログ出力
48
- {
61
+ {
49
- this->x = this->x - pos.x;
62
+ fprintf(stderr, "InfoLog: %s\n\n", infoLog);
50
- this->y = this->y - pos.y;
51
- this->z = this->z - pos.z;
52
63
 
64
+ }
65
+ // printf("ああああ\n");
53
66
 
54
- return *this;
67
+ free(infoLog);
55
- }
68
+ infoLog = NULL;
69
+ // printf("いいいいいい\n");
56
70
 
57
- static Position Cross(Position a, Position b)
58
- {
59
- Position temp;
60
- temp.x = a.y * b.z - a.z * b.y;
61
- temp.y = a.z * b.x - a.x * b.z;
62
- temp.z = a.x * b.y - a.y * b.x;
63
- return temp;
64
- }
65
71
 
72
+ }
66
73
 
67
- static float Dot(Position a, Position b)
68
- {
69
- float t = (a.x * a.x) + (a.y * a.y);
70
- return t;
74
+ //return true;
71
- }
75
+ }
72
- };
73
76
 
74
-
75
-
76
- class Matrix4
77
+ void Game::Shader()
77
78
  {
78
- public:
79
- float x;
80
- float y;
81
- float z;
82
79
 
83
- float w;
84
80
 
85
-
86
- //コンストラクタ
87
- Matrix4()
88
- {
89
- x = 0.0;
90
- y = 0.0;
91
- z = 0.0;
92
81
 
93
- w = 0.0;
94
- }
95
82
 
83
+ program = glCreateProgram();
84
+ GLuint out_vert = glCreateShader(GL_VERTEX_SHADER);
85
+ GLuint out_frag = glCreateShader(GL_FRAGMENT_SHADER);
86
+
87
+
88
+ std::string fileName = "Basic.vert";
89
+ std::ifstream shaderfile(fileName);
90
+ if (shaderfile.is_open())
91
+ {
92
+ std::stringstream sstream;
93
+ sstream << shaderfile.rdbuf();
94
+ std::string str = sstream.str();
95
+ const char* cc = str.c_str();
96
96
 
97
+ out_vert = glCreateShader(GL_VERTEX_SHADER);
98
+ glShaderSource(out_vert, 1, &cc, nullptr);
97
- Matrix4(Position p)
99
+ glCompileShader(out_vert);
98
- {
99
- x = p.x;
100
+ printf("Basic.vert ファイル読み込み\n");
100
- y = p.y;
101
- z = p.z;
102
101
 
102
+ GetShader_Log(out_vert, GL_COMPILE_STATUS);
103
+ glAttachShader(program, out_vert);
103
- w = 1.0f;
104
+ shaderfile.close();
104
- }
105
105
 
106
+ }
107
+ else {
108
+ shaderfile.close();
106
- Matrix4(float xx, float yy, float zz, float ww)
109
+ printf("Basic.vert読み込みエラー");
107
- {
110
+ }
108
- x = xx;
109
- y = yy;
110
- z = zz;
111
111
 
112
- w = ww;
113
- }
114
112
 
115
- Matrix4(float xx, float yy, float zz)
116
- {
117
- x = xx;
118
- y = yy;
119
- z = zz;
120
113
 
114
+ fileName = "Basic.frag";
121
- w = 1.0f;
115
+ shaderfile.open(fileName);
122
- }
123
116
 
117
+ if (shaderfile.is_open())
118
+ {
124
119
 
125
- //一列と行列を掛け算
120
+ std::stringstream sstream;
121
+ sstream << shaderfile.rdbuf();
126
- Matrix4 operator * (const Matrix4 &M)const
122
+ std::string str = sstream.str();
127
- {
128
- Matrix4 t;
129
- t.x = (x * 1) + (y * 0) + (z * 0) + (w * M.x);
123
+ const char* cc = str.c_str();
130
124
 
131
- t.y = (x * 0) + (y * 1) + (z * 0) + (w * M.y);
125
+ out_frag = glCreateShader(GL_FRAGMENT_SHADER);
126
+ glShaderSource(out_frag, 1, &cc, nullptr);
127
+ glCompileShader(out_frag);
128
+ GetShader_Log(out_frag, GL_COMPILE_STATUS);
129
+ glAttachShader(program, out_frag);
130
+ shaderfile.close();
132
131
 
133
- t.z = (x * 0) + (y * 0) + (z * 1) + (w * M.z);
132
+ printf("Basic.frag ファイル読み込み\n");
133
+ }
134
+ else {
135
+ shaderfile.close();
136
+ printf("Basic.frag読み込みエラー");
137
+ }
138
+
134
139
 
135
- t.w = (x * 0) + (y * 0) + (z * 0) + (w * M.w);
136
140
 
137
- return t;
141
+ //シェーダーに値を設定
138
- }
139
- };
142
+ /////////////////////////////////////////////////////////////////////////
140
143
 
141
144
 
142
- class Matrix3
143
- {
144
- public:
145
145
 
146
+ //カラーインデックス
147
+ struct Color_idx {
146
- float x;
148
+ float R;
147
- float y;
149
+ float G;
148
- float z;
150
+ float B;
149
151
 
150
-
152
+ float A;//不透明度
153
+ };
151
154
 
155
+ float idx[4] = { 1.0,1.0,0.0 , 1.0};
152
156
 
153
- //コンストラクタ
154
- Matrix3()
155
- {
156
- x = 0.0;
157
- y = 0.0;
158
- z = 0.0;
159
- }
160
157
 
161
158
 
162
- Matrix3(Position p)
159
+ //フラグメントシェーダー
163
- {
164
- this->x = p.x;
160
+ glBindAttribLocation(program, 0, "idx");
165
- this->y = p.y;
166
- this->z = p.z;
167
- }
168
161
 
169
-
170
162
 
163
+ //頂点シェーダー
164
+ //glBindAttribLocation(program, 0, "Vertex");
165
+ //void glUniform1fv(GLint location, GLsizei count, const GLfloat* value);
166
+ glUniform1fv(0, 18, Vertex);
171
- Matrix3(float x = 0, float y = 0, float z = 0)
167
+ glUniformMatrix3fv(0, 18, GL_TRUE, view);
172
- {
173
- this->x = x;
174
- this->y = y;
175
- this->z = z;
176
- }
177
168
 
169
+ //glUniformMatrix3fv(1, 9 ,GL_TRUE,view);
178
170
 
179
-
180
- };
181
171
 
172
+ glLinkProgram(program);
182
173
 
174
+ //Link_log(program);
175
+ ///////////////////////////////////////////////////////////////////////////
176
+ glDeleteShader(out_frag);
177
+ glDeleteShader(out_vert);
178
+ }
183
179
 
184
180
 
185
- //ベクトルの大きさを出す
181
+ //////////////////////初期化
186
- float Vector_scalar(Position p);
187
- float Vector_scalar(Position p)
182
+ bool Game::Initialization()
188
183
  {
189
- float r = (p.x * p.x) + (p.y * p.y) + (p.z * p.z);
190
- return r;
184
+ Shader();
191
- }
192
185
 
193
- //スケール変換
194
- void Scale_change(float Sx, float Sy, float Sz, float pos_x, float pos_y, float pos_z);
195
- void Scale_change(float Sx, float Sy, float Sz, float pos_x, float pos_y, float pos_z) {
196
186
 
187
+ glGenVertexArrays(1, &vbo);
197
- //Scale行列
188
+ glBindVertexArray(vbo);
189
+ glGenBuffers(1,&vbo);
190
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
191
+ glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float) * 3, Vertex, GL_STATIC_DRAW);
192
+ glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
198
- float ScaleMatrix4[4][4] = {
193
+ glEnableVertexAttribArray(0);
194
+ glBindVertexArray(vbo);
195
+
199
196
 
200
- {0,0,0,0},
197
+ mIsRunLoop = true;
201
- {0,0,0,0},
198
+ return mIsRunLoop;
202
- {0,0,0,0},
203
- {0,0,0,1},
204
- };
199
+ }
205
200
 
206
- //行列に大きさを設定
201
+ //アップデート
207
- ScaleMatrix4[0][0] = Sx;
202
+ void Game::Update()
208
- ScaleMatrix4[1][1] = Sy;
203
+ {
204
+
209
- ScaleMatrix4[2][2] = Sz;
205
+ }
210
206
 
211
- //座標を行列に設定
207
+ //描画アップデート
208
+ void Game::GenerateOutput()
209
+ {
210
+
212
- float position[4];
211
+ glUseProgram(program);
213
- position[0] = pos_x;
214
- position[1] = pos_y;
215
- position[2] = pos_z;
216
- position[3] = 1;
217
212
 
213
+ glDrawArrays(GL_POLYGON, 0, 6);
218
214
 
215
+ glViewport(0,0,640,300);
219
216
 
220
- float t = 0;
217
+ }
221
218
 
222
- t = (position[0] * ScaleMatrix4[0][0]) +
223
- (position[1] * ScaleMatrix4[0][1]) +
224
- (position[2] * ScaleMatrix4[0][2]) +
225
- (position[3] * ScaleMatrix4[0][3]);
226
- printf("%f\n", t);
219
+ ```
227
220
 
228
- t = (position[0] * ScaleMatrix4[1][0]) +
229
- (position[1] * ScaleMatrix4[1][1]) +
230
- (position[2] * ScaleMatrix4[1][2]) +
231
- (position[3] * ScaleMatrix4[1][3]);
232
- printf("%f\n", t);
233
221
 
234
- t = (position[0] * ScaleMatrix4[2][0]) +
222
+ ```hpp
235
- (position[1] * ScaleMatrix4[2][1]) +
236
- (position[2] * ScaleMatrix4[2][2]) +
237
- (position[3] * ScaleMatrix4[2][3]);
238
- printf("%f\n", t);
223
+ #ifndef ___GMAE_H_
224
+ #define ___GMAE_H_
239
225
 
240
- t = (position[0] * ScaleMatrix4[3][0]) +
241
- (position[1] * ScaleMatrix4[3][1]) +
242
- (position[2] * ScaleMatrix4[3][2]) +
243
- (position[3] * ScaleMatrix4[3][3]);
244
- printf("%f\n", t);
226
+ #define WIDTH 640
245
- };
227
+ #define HEIGHT 400
246
228
 
229
+ #include "stdio.h"
230
+ #include <fstream>
231
+ #include <sstream>
232
+ #include <iostream>
247
233
 
248
- //三次元回転変換
249
- class Quaternion
250
- {
251
- public:
252
- float x;
253
- float y;
254
- float z;
255
234
 
256
- float w;
257
235
 
258
- public:
236
+ #include "GLEW/include/GL/glew.h"
259
237
 
238
+ #include "gl/GL.h"
260
- Quaternion(float xx = 0, float yy = 0, float zz = 0, float ww = 0)
239
+ #include "GLFW/include/GLFW/glfw3.h"
261
- {
262
- x = xx;
240
+ #include "Vector.hpp"
263
- y = yy;
241
+ //#include "Vector.hpp"
264
- z = zz;
265
242
 
266
- w = ww;
267
- }
268
243
 
269
- //今の方向 , 向きたい方向
270
- void process(Position s, Position p)
271
- {
272
- // Position s(1, 1, 0);//今の方向
273
- // Position p(-1, 1, 0);//新しい方向ベクトル
274
244
 
245
+ class Game
246
+ {
247
+ public:
275
- Position newFacing;//新しい座標に向かうベクトルを計算して正規化
248
+ Vector3 cv;
276
249
 
277
- Position t = p - s;
250
+ Game();
278
- float len = (float)sqrt(Vector_scalar(t));
279
251
 
280
- newFacing.x = t.x / len;
252
+ bool Initialization();
281
- newFacing.y = t.y / len;
282
- newFacing.z = t.z / len;
283
253
 
254
+ bool RunLoop();
284
255
 
256
+ void Update();
285
- // printf("方向ベクトル x: %f\n", newFacing.x);
257
+ void GenerateOutput();
286
- // printf("方向ベクトル y: %f\n", newFacing.y);
287
- // printf("方向ベクトル z: %f\n\n", newFacing.z);
288
258
 
289
259
 
290
- Position pa = Position::Cross(Position(0, 0, 1), newFacing);
260
+ private:
291
- float sp = (float)sqrt(Vector_scalar(pa));
261
+ bool mIsRunLoop;//メインループ
262
+
263
+
292
264
 
293
- Position a;//回転軸
294
- a.x = pa.x / sp;
295
- a.y = pa.y / sp;
296
- a.z = pa.z / sp;
297
265
 
266
+ //ビュー行列
267
+ const GLfloat view[9] = {
268
+ (2.0f / 640.0f),0.0f,0.0f,
269
+ 0.0f,(2.0f / 400.0f),0.0f,
270
+ 0.0f,0.0f,1.0f
271
+ };
298
272
 
299
- float r = (float)acos(Position::Dot(Position(0, 0, 1), newFacing));
300
273
 
301
- // printf("回転軸 x: %f\n", a.x);
274
+ //頂点バッファー
302
- // printf("回転軸 y: %f\n", a.y);
275
+ const GLfloat Vertex[18] =
303
- // printf("回転軸 z: %f\n\n", a.z);
276
+ {
304
277
 
305
- // printf("回転角 θ: %f\n", r);
278
+ -160, +100, 1.0,
279
+ -160, -100, 1.0,
280
+ +160, -100, 1.0,
306
281
 
282
+ -160, +100, 1.0,
283
+ +160, +100, 1.0,
307
- Quaternion q;
284
+ +160, -100, 1.0
308
- q.w = (float)cos(r / 2);
285
+ };
286
+
309
287
 
310
- q.x = (float)newFacing.x * (float)cos(r / 2);
288
+ GLuint vbo = 0;//頂点配列バッファオブジェクト名
311
- q.y = (float)newFacing.y * (float)sin(r / 2);
289
+ GLuint vao = 0;//頂点バッファオブジェクト名
312
- q.z = (float)newFacing.z * (float)sin(r / 2);
290
+
313
291
 
314
- //////////////////////////////////////////////////////////
315
- printf("q w: %f\n", q.w);
292
+ GLuint program = 0;
316
- printf("q x: %f\n", q.x);
293
+ void Shader();//シェーダ読み込み
294
+ void Compile_log(GLuint Shader);//シェーダーコンパイルログ
317
- printf("q y: %f\n", q.y);
295
+ void Link_log(GLuint Shader);//シェーダーリンクログ
318
- printf("q z: %f\n", q.z);
319
- /////////////////////////////////////////////////////////
320
296
 
321
- }
297
+ void GetShader_Log(GLuint shader_type,GLuint Log_type);
322
298
 
323
299
  };
324
300
 
325
301
 
326
302
 
303
+ #endif
327
304
 
328
- ///////////////////////////////////////////////////////////////////////////////////////
329
- int main()
330
- {
305
+ ```
331
306
 
307
+ ```GLSL
308
+ #version 400
332
- //ワールド座標
309
+ //頂点
310
+ layout(Location = 0) in vec4 position;//頂点座標
333
- Matrix3 now[6] =
311
+ layout(Location = 1) in mat4 view;//頂点座標
334
- {
335
312
 
313
+ void main()
314
+ {
336
- Matrix3(-160, +100, 1),
315
+ gl_Position = position * view;
316
+ }
337
317
 
338
- Matrix3(-160, -100, 1),
339
318
 
340
- Matrix3(+160, -100, 1),
319
+ ```
341
320
 
342
- ///////////////////////////////////
343
321
 
322
+ ```GLSL
323
+ #version 400
344
- Matrix3(-160, +100, 1),
324
+ //フラグメント
325
+ layout(location = 0) in vec4 fragment;
326
+ out vec4 f;
345
327
 
346
- Matrix3(+160, +100, 1),
328
+ void main()
329
+ {
330
+
347
331
 
348
- Matrix3(+160, -100, 1)
332
+ //f = fragment;
349
- };
350
333
 
351
- //ビュー行列
352
- Matrix3 s[3] = {
353
- Matrix3((640 / 2),0,0),
334
+ f = vec4(0.0,0.0,1.0,1.0);
354
- Matrix3(0,(400 / 2),0),
355
- Matrix3(0,0,1),
356
- };
357
-
358
-
359
- Position p(0,0,0);
360
- p.x = (now[0].x * s[0].x) + ( now[0].y * s[0].y) + ( now[0].z * s[0].z);
361
- p.y = (now[0].x * s[1].x) + ( now[0].y * s[1].y) + ( now[0].z * s[1].z);
362
- p.z = (now[0].x * s[2].x) + ( now[0].y * s[2].y) + ( now[0].z * s[2].z);
363
-
364
- printf("%f\n\n\n\n", 640.0 / (-160.0 * 320.0 ));///
365
-
366
- // printf("r %f\n\n\n", p.x / 640);
335
+ //fragment = vec4(1.0,0.0,1.0,1.0);
367
-
368
- printf("p.x: %.2f\n", p.x);
369
- printf("p.y: %.2f\n", p.y);
370
- printf("p.z: %.2f\n", p.z);
371
-
372
-
373
-
374
- int _C = getchar();
375
- return 0;
376
336
  }
377
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
378
337
  ```

2

文章を修正

2020/09/10 10:11

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- 提示コードのメイン関数部です。画面サイズ(640,400.1)のワールド座標で頂点を設定してその頂点を画面である正規化座標(1.0 ~ -1.0)に変換して画面の真ん中に四角い正方形を描画したいのですがどうすればいいのでしょうか?正しい計算をしていれば(-0.5,0.5,1)になるはずなのですがなりません。
1
+ 提示コードのメイン関数部です。画面サイズ(ピクセル)(640,400.1)のワールド座標で頂点を設定してその頂点を画面である正規化座標(1.0 ~ -1.0)に変換して画面の真ん中に四角い正方形を描画したいのですがどうすればいいのでしょうか?正しい計算をしていれば(-0.5,0.5,1)になるはずなのですがなりません。画面サイズとはピクセルサイズです。また座標系は3Dですが今回は2Dの物を表示させたいです。
2
2
 
3
3
 
4
4
 

1

文章を編集

2020/09/10 02:53

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,7 @@
1
- 提示コードのメイン関数部です。画面サイズ(640,400.1)のワールド座標で頂点を設定してその頂点を画面である正規化座標に変換して画面の真ん中に四角い正方形を描画したいのですがどうすればいいのでしょうか?正しい計算をしていれば(-0.5,0.5,1)になるはずなのですがなりません。
1
+ 提示コードのメイン関数部です。画面サイズ(640,400.1)のワールド座標で頂点を設定してその頂点を画面である正規化座標(1.0 ~ -1.0)に変換して画面の真ん中に四角い正方形を描画したいのですがどうすればいいのでしょうか?正しい計算をしていれば(-0.5,0.5,1)になるはずなのですがなりません。
2
2
 
3
+
4
+
3
5
  質問 **ワールド座標から正規化座標に変換する計算式を知りたいです。
4
6
  **
5
7