我正在尝试使用OpenGL在2D中渲染纹理,并遇到正交投影矩阵的一些问题:
它几乎只是使用GL函数的旧代码的简单端口,因此我不知道我在这里做错了我从平面得到一个奇怪的拉伸输出
因此问题是:我怎样才能得到它"扁平化"到我能够再次绘制2D纹理的2D级别?
我已经尝试将矩阵更改为导致无法显示任何内容的视角
添加视图矩阵来改变角度也没有帮助
X39
glGenVertexArrays(1, &texture2D_vao);
glBindVertexArray(texture2D_vao);
GLuint vertBufferID, indexBufferID;
glGenBuffers(1, &vertBufferID);
glGenBuffers(1, &indexBufferID);
struct Vec2 { float x, y; };
struct Vec3 { float x, y, z; };
struct Vert { Vec3 pos; Vec2 tex; };
std::array<Vert, 4> verts = { {
{ { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f } },
{ { 1.0f, 1.0f, 0.0f }, { 1.0f, 0.0f } },
{ { 1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f } },
{ { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f } }
} };
std::array<unsigned int, 6> indexies = { { 0, 1, 2, 3} };
// Vertex buffer
glBindBuffer(GL_ARRAY_BUFFER, vertBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vert) * verts.size(), verts.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(0); // Matches layout (location = 0)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vert), 0);
glEnableVertexAttribArray(1); // Matches layout (location = 1)
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vert), (GLvoid*)sizeof(Vec3));
// Index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indexies.size(), indexies.data(), GL_STATIC_DRAW);
glBindVertexArray(0);
void GuiBase::drawTexture2D(::X39::Singletons::MATERIAL* mat, unsigned int textureIndex, double tPosX, double tPosY, double tWidth, double tHeight, double uiPosX, double uiPosY, double uiWidth, double uiHeight, Shader& shad)
{
::X39::Singletons::TEXTURE* texture = mat->textures[textureIndex];
::X39::Singletons::MaterialManager::getInstance().loadMaterial(mat, textureIndex);
::glm::mat4 scaleMatrix = ::glm::scale(::glm::mat4(1.0f), ::glm::vec3((float)uiWidth, (float)uiHeight, 1.0f));
std::array<float, 6> uvManipulation = { tPosX, tPosY, tWidth, tHeight, texture->width, texture->height };
glm::mat4 projectionMatrix = glm::ortho(
(float)0,
(float)::X39::GlobalObject::getInstance().render_width / (float)::X39::GlobalObject::getInstance().render_height,
(float)0,
(float)::X39::GlobalObject::getInstance().render_height / (float)::X39::GlobalObject::getInstance().render_width,
0.0f,
100.0f
);
glBindVertexArray(texture2D_vao);
shad.setUniformMatrix4fv("scaleMatrix", 1, GL_FALSE, &scaleMatrix[0][0], -1);
shad.setUniformMatrix4fv("projectionMatrix", 1, GL_FALSE, &projectionMatrix[0][0], -1);
shad.setUniform1fv("UV_Manipulators", 6, uvManipulation.data(), -1);
shad.setUniform3fv("worldPosition", 1, &glm::vec3(
uiPosX,
uiPosY,
0
)[0], -1);
shad.setUniform1i("textureSampler", texture->textureUnit, -1);
glDrawElements(GL_QUADS, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
#version 440
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec2 vertexUV_modelspace;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 scaleMatrix;
uniform vec3 worldPosition;
out vec2 UV;
uniform float[6] UV_Manipulators;
void main()
{
gl_Position = scaleMatrix * vec4((vertexPosition_modelspace + worldPosition), 1) * projectionMatrix;
vec2 newUV = vertexUV_modelspace;
if(newUV.x > 0)
{
newUV.x = (UV_Manipulators[0] + UV_Manipulators[2]) / UV_Manipulators[4];
}
else
{
newUV.x = (UV_Manipulators[0]) / UV_Manipulators[4];
}
if(newUV.y > 0)
{
newUV.y = (UV_Manipulators[1] + UV_Manipulators[3]) / UV_Manipulators[5];
}
else
{
newUV.y = (UV_Manipulators[1]) / UV_Manipulators[5];
}
UV = newUV;
}