質問編集履歴
3
文章とタイトルを修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
[OpenGL] カメラの視点回転
|
1
|
+
[OpenGL] カメラの視点回転が正しいのか知りたい。
|
body
CHANGED
@@ -1,165 +1,8 @@
|
|
1
|
-
提示コードですが
|
1
|
+
提示コードですがCamera.cppのコメント部内部のコードなのですがsetLook()関数をMain.cppで使っているのですがcos.sinの使い方と視線の動かし方は正しいのでしょうか?
|
2
2
|
|
3
3
|
|
4
|
-
参考サイト:[ https://blog.natade.net/2017/05/27/directx-opengl-d3dxmatrixrotation-glrotate/
|
5
|
-
](https://blog.natade.net/2017/05/27/directx-opengl-d3dxmatrixrotation-glrotate/)
|
6
|
-
|
7
4
|
参考サイト: [https://learnopengl.com/Getting-started/Camera](https://learnopengl.com/Getting-started/Camera)
|
8
5
|
|
9
|
-

|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
##### Main.cpp
|
14
|
-
```
|
15
|
-
include "../header/FrameWork.hpp"
|
16
|
-
#include "../header/Resource.hpp"
|
17
|
-
|
18
|
-
#include <iostream>
|
19
|
-
#include "../header/Camera.hpp"
|
20
|
-
|
21
|
-
|
22
|
-
int main()
|
23
|
-
{
|
24
|
-
FrameWork::Init(glm::ivec2(800, 600), glm::ivec2(4,2), "FrameWork"); // 初期化
|
25
|
-
FrameWork::Camera::Init(); //カメラ初期化
|
26
|
-
|
27
|
-
|
28
|
-
FrameWork::ObjFile cubeFile;
|
29
|
-
FrameWork::D3::LoadObj("Model/Cube.obj",cubeFile);
|
30
|
-
FrameWork::D3::Object cube(cubeFile);
|
31
|
-
|
32
|
-
FrameWork::ObjFile groundFile;
|
33
|
-
FrameWork::D3::LoadObj("Model/ground.obj",groundFile);
|
34
|
-
FrameWork::D3::Object ground(groundFile);
|
35
|
-
|
36
|
-
|
37
|
-
float y = 0;
|
38
|
-
float x = 0;
|
39
|
-
|
40
|
-
float angleY = 0;
|
41
|
-
float angleX = 0;
|
42
|
-
|
43
|
-
float vecSpeed = PI / 100;
|
44
|
-
while (*FrameWork::windowContext)
|
45
|
-
{
|
46
|
-
FrameWork::windowContext->FrameUpdate(glm::vec4(0,0,0,255));
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
if(FrameWork::windowContext->getKeyInput(GLFW_KEY_A) > (short)0)
|
52
|
-
{
|
53
|
-
angleY = 0.001;
|
54
|
-
|
55
|
-
cube.setRotate(glm::vec3(0,1,0),angleY);
|
56
|
-
}
|
57
|
-
else if(FrameWork::windowContext->getKeyInput(GLFW_KEY_D) > (short)0)
|
58
|
-
{
|
59
|
-
angleY = -0.001;
|
60
|
-
cube.setRotate(glm::vec3(0,1,0),angleY);
|
61
|
-
|
62
|
-
}
|
63
|
-
else if(FrameWork::windowContext->getKeyInput(GLFW_KEY_W) > (short)0)
|
64
|
-
{
|
65
|
-
angleY = 0.001;
|
66
|
-
cube.setRotate(glm::vec3(1,0,0),angleY);
|
67
|
-
|
68
|
-
}
|
69
|
-
else if(FrameWork::windowContext->getKeyInput(GLFW_KEY_S) > (short)0)
|
70
|
-
{
|
71
|
-
angleY = -0.001;
|
72
|
-
cube.setRotate(glm::vec3(1,0,0),angleY);
|
73
|
-
}
|
74
|
-
|
75
|
-
|
76
|
-
if(FrameWork::windowContext->getKeyInput(GLFW_KEY_LEFT) > (short)0)
|
77
|
-
{
|
78
|
-
x += -vecSpeed;
|
79
|
-
|
80
|
-
// printf("left\n");
|
81
|
-
// printf("%f , %f \n",x,y);
|
82
|
-
|
83
|
-
}
|
84
|
-
else if(FrameWork::windowContext->getKeyInput(GLFW_KEY_RIGHT) > (short)0)
|
85
|
-
{
|
86
|
-
x += vecSpeed;
|
87
|
-
// printf("right\n");
|
88
|
-
// printf("%f , %f \n",x,y);
|
89
|
-
|
90
|
-
}
|
91
|
-
|
92
|
-
if(FrameWork::windowContext->getKeyInput(GLFW_KEY_UP) > (short)0)
|
93
|
-
{
|
94
|
-
y += vecSpeed;
|
95
|
-
// printf("up\n");
|
96
|
-
// printf("%f , %f \n",x,y);
|
97
|
-
|
98
|
-
|
99
|
-
}
|
100
|
-
else if(FrameWork::windowContext->getKeyInput(GLFW_KEY_DOWN) > (short)0)
|
101
|
-
{
|
102
|
-
y += -vecSpeed;
|
103
|
-
// printf("down\n");
|
104
|
-
// printf("%f , %f \n",x,y);
|
105
|
-
|
106
|
-
}
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
-
FrameWork::Camera::setLook(glm::vec3(cos(x) + 0,sin(y) + 100,sin(x) + 300));
|
112
|
-
FrameWork::Camera::setPosition(glm::vec3(0,100,300));
|
113
|
-
//////////////////////////////////////////////////////////////////////////////////////////////////////////
|
114
|
-
|
115
|
-
//Cube
|
116
|
-
cube.shader->setEnable();
|
117
|
-
cube.setPosition(glm::vec3(0,70,30));
|
118
|
-
cube.setScale(glm::vec3(30,30,30));
|
119
|
-
// cube.shader->setUniform4f("uFragment",FrameWork::GetGlColor(glm::vec4(0,0,100,255)));
|
120
|
-
cube.shader->setUniform3f("objectColor", glm::vec3(1.0f, 0.5f, 0.31f));
|
121
|
-
cube.shader->setUniform3f("lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
|
122
|
-
cube.shader->setUniform3f("lightPos", glm::vec3(0,0,0.0f));
|
123
|
-
cube.shader->setUniform3f("viewPos",FrameWork::Camera::getPosition());
|
124
|
-
cube.Renderer();
|
125
|
-
cube.shader->setDisable();
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
//Ground
|
132
|
-
ground.shader->setEnable();
|
133
|
-
ground.setPosition(glm::vec3(0,0,0));
|
134
|
-
ground.setScale(glm::vec3(1,1,1));
|
135
|
-
ground.shader->setUniform3f("objectColor", glm::vec3(1.0f, 0.5f, 0.31f));
|
136
|
-
ground.shader->setUniform3f("lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
|
137
|
-
ground.shader->setUniform3f("lightPos", glm::vec3(0,0,0));
|
138
|
-
ground.shader->setUniform3f("viewPos", FrameWork::Camera::getPosition());
|
139
|
-
//ground.shader->setUniform4f("uFragment",FrameWork::GetGlColor(glm::vec4(0,100,0,255)));
|
140
|
-
ground.Renderer();
|
141
|
-
ground.shader->setDisable();
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
FrameWork::windowContext->Wait();
|
149
|
-
FrameWork::windowContext->SwapBuffers();
|
150
|
-
}
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
return 0;
|
157
|
-
}
|
158
|
-
|
159
|
-
```
|
160
|
-
|
161
|
-
|
162
|
-
|
163
6
|
##### Camera.cpp
|
164
7
|
```
|
165
8
|
#include "../header/Camera.hpp"
|
@@ -200,20 +43,20 @@
|
|
200
43
|
{
|
201
44
|
position = p; //座標
|
202
45
|
}
|
203
|
-
|
46
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
204
47
|
// ##################################### 視線を設定 #####################################
|
205
48
|
void FrameWork::Camera::setLook(glm::vec3 l)
|
206
49
|
{
|
207
50
|
vecLook = l; //向き
|
51
|
+
|
208
52
|
glm::vec3 up = glm::vec3(0, 1, 0);
|
209
|
-
|
210
53
|
glm::vec3 cameraDirection = glm::normalize(position - vecLook);
|
211
54
|
glm::vec3 cameraRight = glm::normalize(glm::cross(up, cameraDirection));
|
212
55
|
glm::vec3 cameraUp = glm::cross(vecLook, cameraRight);
|
213
56
|
|
214
|
-
view = glm::lookAt(glm::vec3(position.x, position.y, position.z), position + vecLook,
|
57
|
+
view = glm::lookAt(glm::vec3(position.x, position.y, position.z), position + vecLook,up);
|
215
58
|
}
|
216
|
-
|
59
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
217
60
|
// ##################################### 座標を取得 #####################################
|
218
61
|
glm::vec3 FrameWork::Camera::getPosition()
|
219
62
|
{
|
@@ -243,4 +86,41 @@
|
|
243
86
|
{
|
244
87
|
|
245
88
|
}
|
89
|
+
```
|
90
|
+
|
91
|
+
|
92
|
+
##### Main.cpp
|
93
|
+
```cpp
|
94
|
+
|
95
|
+
//関数の外に宣言 初期値↓
|
96
|
+
float cameraLookSpeed = PI / 100; //視点移動速度
|
97
|
+
glm::vec3 cameraLook = glm::vec3(PI /2,PI,0);
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
if (FrameWork::windowContext->getKeyInput(GLFW_KEY_LEFT_CONTROL) > 0)
|
103
|
+
{
|
104
|
+
if (FrameWork::windowContext->getKeyInput(GLFW_KEY_LEFT) > (short)0)
|
105
|
+
{
|
106
|
+
cameraLook.x += -cameraLookSpeed;
|
107
|
+
}
|
108
|
+
else if (FrameWork::windowContext->getKeyInput(GLFW_KEY_RIGHT) > (short)0)
|
109
|
+
{
|
110
|
+
cameraLook.x += cameraLookSpeed;
|
111
|
+
}
|
112
|
+
|
113
|
+
if (FrameWork::windowContext->getKeyInput(GLFW_KEY_UP) > (short)0)
|
114
|
+
{
|
115
|
+
cameraLook.y += cameraLookSpeed;
|
116
|
+
}
|
117
|
+
else if (FrameWork::windowContext->getKeyInput(GLFW_KEY_DOWN) > (short)0)
|
118
|
+
{
|
119
|
+
cameraLook.y += -cameraLookSpeed;
|
120
|
+
}
|
121
|
+
}
|
122
|
+
|
123
|
+
FrameWork::Camera::setLook(glm::vec3(cos(cameraLook.x) * cos(cameraLook.y), sin(cameraLook.y), sin(cameraLook.x) * cos(cameraLook.y)));
|
124
|
+
FrameWork::Camera::setPosition(cameraPos);
|
125
|
+
|
246
126
|
```
|
2
文章を修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,6 +4,8 @@
|
|
4
4
|
参考サイト:[ https://blog.natade.net/2017/05/27/directx-opengl-d3dxmatrixrotation-glrotate/
|
5
5
|
](https://blog.natade.net/2017/05/27/directx-opengl-d3dxmatrixrotation-glrotate/)
|
6
6
|
|
7
|
+
参考サイト: [https://learnopengl.com/Getting-started/Camera](https://learnopengl.com/Getting-started/Camera)
|
8
|
+
|
7
9
|

|
8
10
|
|
9
11
|
|
@@ -203,7 +205,13 @@
|
|
203
205
|
void FrameWork::Camera::setLook(glm::vec3 l)
|
204
206
|
{
|
205
207
|
vecLook = l; //向き
|
208
|
+
glm::vec3 up = glm::vec3(0, 1, 0);
|
209
|
+
|
210
|
+
glm::vec3 cameraDirection = glm::normalize(position - vecLook);
|
211
|
+
glm::vec3 cameraRight = glm::normalize(glm::cross(up, cameraDirection));
|
212
|
+
glm::vec3 cameraUp = glm::cross(vecLook, cameraRight);
|
213
|
+
|
206
|
-
view = glm::lookAt(glm::vec3(position.x, position.y, position.z), vecLook,
|
214
|
+
view = glm::lookAt(glm::vec3(position.x, position.y, position.z), position + vecLook,glm::vec3(0, 1, 0));
|
207
215
|
}
|
208
216
|
|
209
217
|
// ##################################### 座標を取得 #####################################
|
1
文章を修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
提示コードですがMain.cppのようにカメラを上下左右に回転させたいのですがそれをするには回転姿勢を扱う必要があるのですがそれはどういった行列の計算が必要なのでしょうか?
|
1
|
+
提示コードですがMain.cppのコメント部のようにカメラを上下左右に回転させたいのですがそれをするには回転姿勢を扱う必要があるのですがそれはどういった行列の計算が必要なのでしょうか?
|
2
2
|
|
3
3
|
|
4
4
|
参考サイト:[ https://blog.natade.net/2017/05/27/directx-opengl-d3dxmatrixrotation-glrotate/
|
@@ -105,11 +105,11 @@
|
|
105
105
|
|
106
106
|
|
107
107
|
|
108
|
-
|
108
|
+
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
109
109
|
FrameWork::Camera::setLook(glm::vec3(cos(x) + 0,sin(y) + 100,sin(x) + 300));
|
110
110
|
FrameWork::Camera::setPosition(glm::vec3(0,100,300));
|
111
|
+
//////////////////////////////////////////////////////////////////////////////////////////////////////////
|
111
112
|
|
112
|
-
|
113
113
|
//Cube
|
114
114
|
cube.shader->setEnable();
|
115
115
|
cube.setPosition(glm::vec3(0,70,30));
|