質問編集履歴
1
タイトルと文章を修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
opengl 正射投影で
|
1
|
+
opengl 正射投影で2Dスプライトを等倍のサイズで描画する方法が知りたい。
|
test
CHANGED
@@ -1,7 +1,11 @@
|
|
1
|
+
提示画像ですがこれは(32,32)のスプライト画像で画面サイズが(960,540)なのですが画像のキャラクタが異常に大きい原因がわかりません
|
1
|
-
|
2
|
+
おそらく倍の大きさで描画されていることが原因だと思うんですがこれは何が悪いのでしょうか?
|
3
|
+
|
4
|
+
Sprite.cppのsetSize()関数の値をいろいろ変更しましたが等倍にないません
|
5
|
+
|
2
6
|
|
3
7
|
参考サイト: https://johnnn.tech/q/opengl-move-2d-ortho-camera-with-mouse/
|
4
|
-
|
8
|
+
![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-02-04/8b0fc235-ed61-4f0f-90bd-6c09264eb9aa.png)
|
5
9
|
|
6
10
|
##### Camera.cpp
|
7
11
|
```cpp
|
@@ -32,22 +36,178 @@
|
|
32
36
|
view = glm::lookAt(position, position + vecLook,up);
|
33
37
|
}
|
34
38
|
|
35
|
-
// ##################################### 座標を取得 #####################################
|
36
|
-
glm::vec3 FrameWork::Camera::getPosition()
|
37
|
-
{
|
38
|
-
return position;
|
39
|
-
}
|
40
|
-
|
41
|
-
// ##################################### 視線を取得 #####################################
|
42
|
-
glm::vec3 FrameWork::Camera::getLook()
|
43
|
-
{
|
44
|
-
return vecLook;
|
45
|
-
}
|
46
|
-
|
47
39
|
// ##################################### 正射投影行列 #####################################
|
48
40
|
glm::mat4 FrameWork::Camera::getViewOrthographic()
|
49
41
|
{
|
50
|
-
return glm::ortho(-0.5f, 0.5f, -0.5f, 0.5f, 0.1f, 100.0f) * view;
|
42
|
+
//return glm::ortho(-0.5f, 0.5f, -0.5f, 0.5f, 0.1f, 100.0f) * view;
|
43
|
+
return glm::ortho(-(float)windowContext->getSize().x / 2.0f, (float)windowContext->getSize().y / 2.0f,
|
44
|
+
-(float)windowContext->getSize().x /2.0f , (float)windowContext->getSize().y / 2.0f,0.1f, 100.0f) * view;
|
51
|
-
}
|
45
|
+
}
|
46
|
+
|
52
47
|
|
53
48
|
```
|
49
|
+
|
50
|
+
##### Srpite.cpp
|
51
|
+
```cpp
|
52
|
+
#include "../header/Sprite.hpp"
|
53
|
+
|
54
|
+
#include <array>
|
55
|
+
#include <memory>
|
56
|
+
|
57
|
+
#include <GL/glew.h>
|
58
|
+
#include <glm/glm.hpp>
|
59
|
+
#include <glm/gtc/type_ptr.hpp>
|
60
|
+
#include <glm/gtc/quaternion.hpp>
|
61
|
+
#include <glm/gtc/type_ptr.hpp>
|
62
|
+
#include <glm/gtx/transform.hpp>
|
63
|
+
#include <glm/gtc/matrix_transform.hpp>
|
64
|
+
|
65
|
+
#include "../header/Resource.hpp"
|
66
|
+
#include "../header/Shader.hpp"
|
67
|
+
#include "../header/Vertex.hpp"
|
68
|
+
#include "../header/Sprite.hpp"
|
69
|
+
#include "../header/Shader.hpp"
|
70
|
+
#include "../header/Window.hpp"
|
71
|
+
#include "../header/Init.hpp"
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
FrameWork::Sprite::Sprite() : Render()
|
76
|
+
{
|
77
|
+
vao = 0;
|
78
|
+
vbo = 0;
|
79
|
+
|
80
|
+
glGenVertexArrays(1, &vao);
|
81
|
+
glGenBuffers(1, &vbo);
|
82
|
+
|
83
|
+
glBindVertexArray(vao);
|
84
|
+
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
85
|
+
|
86
|
+
shader = std::make_shared<Shader>();
|
87
|
+
shader->Load("Asset/shader/2D/BasicTexture_2D.vert","Asset/shader/2D/BasicTexture_2D.frag");
|
88
|
+
|
89
|
+
shader->setEnable();
|
90
|
+
|
91
|
+
glBufferData(GL_ARRAY_BUFFER, vertex.size() * sizeof(VertexAttributeSprite), (float*)vertex.data(), GL_DYNAMIC_DRAW);
|
92
|
+
|
93
|
+
GLuint attrib = shader->getAttribLocation("vertexPosition");
|
94
|
+
glEnableVertexAttribArray(attrib);
|
95
|
+
glVertexAttribPointer(attrib, 3, GL_FLOAT, GL_FALSE, (sizeof(VertexAttributeSprite) / sizeof(float)) * sizeof(float), (GLvoid*)(sizeof(GLfloat) * 0));
|
96
|
+
shader->setBindAttribLocation("vertexPosition");
|
97
|
+
|
98
|
+
attrib = shader->getAttribLocation("vertexUV");
|
99
|
+
glEnableVertexAttribArray(attrib);
|
100
|
+
glVertexAttribPointer(attrib, 2, GL_FLOAT, GL_FALSE, (sizeof(VertexAttributeSprite) / sizeof(float)) * sizeof(float), (GLvoid*)(sizeof(GLfloat) * 3));
|
101
|
+
shader->setBindAttribLocation("vertexUV");
|
102
|
+
|
103
|
+
shader->setDisable();
|
104
|
+
glBindVertexArray(0);
|
105
|
+
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
// ##################################### 描画 #####################################
|
110
|
+
void FrameWork::Sprite::Renderer(const glm::mat4 view,const FrameWork::Texture texture,const glm::vec3 position,const glm::ivec2 startSize,const glm::ivec2 endSize)
|
111
|
+
{
|
112
|
+
shader->setEnable();
|
113
|
+
glm::vec2 screenSize = glm::vec2(1.0f / windowContext->getSize().x,1.0f / windowContext->getSize().y);
|
114
|
+
glBindVertexArray(vao);
|
115
|
+
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
116
|
+
|
117
|
+
setSize(startSize,endSize);
|
118
|
+
setUV(startSize, endSize,texture.size);
|
119
|
+
|
120
|
+
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(VertexAttributeSprite) * vertex.size(), vertex.data());
|
121
|
+
|
122
|
+
// Transform::setPosition(glm::vec3(screenSize.x * position.x,screenSize.y * position.y,0));
|
123
|
+
Transform::setPosition(glm::vec3(position.x,position.y,0));
|
124
|
+
Transform::setRotate(glm::vec3(0, 0, 1), 0);
|
125
|
+
Transform::setScale(glm::vec3(endSize - startSize,1));
|
126
|
+
|
127
|
+
shader->setUniformSampler2D("uImage",0,texture.ID);
|
128
|
+
shader->setUniformMatrix4fv("uTranslate", Transform::getMatTranslation());
|
129
|
+
shader->setUniformMatrix4fv("uRotate", Transform::getMatRotate());
|
130
|
+
shader->setUniformMatrix4fv("uScale", Transform::getMatScale());
|
131
|
+
shader->setUniformMatrix4fv("uViewProjection", view);
|
132
|
+
|
133
|
+
glDrawArrays(GL_TRIANGLES,0,vertex.size());
|
134
|
+
|
135
|
+
glBindVertexArray(0);
|
136
|
+
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
137
|
+
shader->setDisable();
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
// ##################################### 頂点 #####################################
|
142
|
+
void FrameWork::Sprite::setSize(const glm::vec2 startSize,const glm::vec2 endSize)
|
143
|
+
{
|
144
|
+
|
145
|
+
glm::vec2 scr = glm::ivec2(1,1);
|
146
|
+
scr.x =1;
|
147
|
+
scr.y = 1;
|
148
|
+
|
149
|
+
// 頂点座標
|
150
|
+
vertex[0].position.x = -scr.x;
|
151
|
+
vertex[0].position.y = scr.y;
|
152
|
+
vertex[0].position.z = 0;
|
153
|
+
|
154
|
+
vertex[1].position.x = -scr.x;
|
155
|
+
vertex[1].position.y = -scr.y;
|
156
|
+
vertex[1].position.z = 0;
|
157
|
+
|
158
|
+
vertex[2].position.x = scr.x;
|
159
|
+
vertex[2].position.y = scr.y;
|
160
|
+
vertex[2].position.z = 0;
|
161
|
+
|
162
|
+
vertex[3].position.x = scr.x;
|
163
|
+
vertex[3].position.y = scr.y;
|
164
|
+
vertex[3].position.z = 0;
|
165
|
+
|
166
|
+
vertex[4].position.x = -scr.x;
|
167
|
+
vertex[4].position.y = -scr.y;
|
168
|
+
vertex[4].position.z = 0;
|
169
|
+
|
170
|
+
vertex[5].position.x = scr.x;
|
171
|
+
vertex[5].position.y = -scr.y;
|
172
|
+
vertex[5].position.z = 0;
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
|
178
|
+
// ##################################### 頂点 #####################################
|
179
|
+
void FrameWork::Sprite::setUV(const glm::vec2 startSize, const glm::vec2 endSize,const glm::ivec2 textureSize)
|
180
|
+
{
|
181
|
+
glm::vec2 size = endSize - startSize;
|
182
|
+
|
183
|
+
|
184
|
+
//UV座標
|
185
|
+
float sizeX = 1.0f / (float)textureSize.x;
|
186
|
+
float sizeY = 1.0f / (float)textureSize.y;
|
187
|
+
|
188
|
+
FrameWork::Sprite::vertex[0].uv.x = sizeX * startSize.x;
|
189
|
+
FrameWork::Sprite::vertex[0].uv.y = sizeY * startSize.y;
|
190
|
+
|
191
|
+
FrameWork::Sprite::vertex[1].uv.x = sizeX * startSize.x;
|
192
|
+
FrameWork::Sprite::vertex[1].uv.y = sizeY * endSize.y;
|
193
|
+
|
194
|
+
FrameWork::Sprite::vertex[2].uv.x = sizeX * endSize.x;
|
195
|
+
FrameWork::Sprite::vertex[2].uv.y = sizeY * startSize.y;
|
196
|
+
|
197
|
+
FrameWork::Sprite::vertex[3].uv.x = sizeX * endSize.x;
|
198
|
+
FrameWork::Sprite::vertex[3].uv.y = sizeY * startSize.y;
|
199
|
+
|
200
|
+
FrameWork::Sprite::vertex[4].uv.x = sizeX * startSize.x;
|
201
|
+
FrameWork::Sprite::vertex[4].uv.y = sizeY * endSize.y;
|
202
|
+
|
203
|
+
FrameWork::Sprite::vertex[5].uv.x = sizeX * endSize.x;
|
204
|
+
FrameWork::Sprite::vertex[5].uv.y = sizeY * endSize.y;
|
205
|
+
}
|
206
|
+
|
207
|
+
FrameWork::Sprite::~Sprite()
|
208
|
+
{
|
209
|
+
|
210
|
+
}
|
211
|
+
```
|
212
|
+
|
213
|
+
|