gpt4 book ai didi

c++ - OpenGL - 翻译拉伸(stretch)和扭曲 Sprite

转载 作者:行者123 更新时间:2023-11-28 02:25:13 25 4
gpt4 key购买 nike

我正在松散地遵循 opengl-tutorial.org 上非常方便的教程.我已经能够创建一个网格,为它绘制一个 Sprite ,然后完美地旋转和缩放该网格。

但是,我在尝试平移网格时遇到了一些问题。 (下图)

这是 Sprite 的更新函数:

    Transform* transform = static_cast<Transform*>(owner->GetComponent(CID_TRANSFORM));

glUseProgram(shaderID_);

glm::mat4 projection = glm::perspective(45.0f , 4.0f / 3.0f, 0.1f, 100.0f);

glm::mat4 view = glm::lookAt(
glm::vec3(3, 3, 3),
glm::vec3(0, 0, 0),
glm::vec3(0, 1, 0)
);

glm::mat4 model = transform->GetModelMatrix();

glm::mat4 mvp = projection * view * model;

GLuint matrixID = glGetUniformLocation(shaderID_, "MVP");

glUniformMatrix4fv(matrixID, 1, GL_FALSE, &mvp[0][0]);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture_);
glUniform1i(samplerID_, 0);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer_);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, uvBuffer_);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);

glDrawArrays(GL_TRIANGLES, 0, 3 * 2);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);

这里是 getModelMatrix 函数:

glm::mat4 Transform::GetModelMatrix()
{
glm::mat4 trans = glm::mat4(
1.0f, 0.0f, 0.0f, translation.x,
0.0f, 1.0f, 0.0f, translation.y,
0.0f, 0.0f, 1.0f, translation.z,
0.0f, 0.0f, 0.0f, 1.0f);

float xCos = glm::cos(rotation.x);
float xSin = glm::sin(rotation.x);
float yCos = glm::cos(rotation.y);
float ySin = glm::sin(rotation.y);
float zCos = glm::cos(rotation.z);
float zSin = glm::sin(rotation.z);

glm::mat4 xRotation = glm::mat4(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, xCos, -xSin, 0.0f,
0.0f, xSin, xCos, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);

glm::mat4 yRotation = glm::mat4(
yCos, 0.0f, ySin, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
-ySin, 0.0f, yCos, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);

glm::mat4 zRotation = glm::mat4(
zCos, -zSin, 0.0f, 0.0f,
zSin, zCos, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);

glm::mat4 rot = xRotation * yRotation * zRotation;

glm::mat4 sca = glm::mat4(
scale.x, 0.0f, 0.0f, 0.0f,
0.0f, scale.y, 0.0f, 0.0f,
0.0f, 0.0f, scale.z, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);

return trans * rot * sca;
}

这是从 (3, 3, 3) 看原点的 Sprite 。 first image

这是从 (3, 3, 3) 观察到的转换为 (1, 0, 0) 的 Sprite 。 second image

最佳答案

与 OpenGL 相匹配,GLM 以列主要顺序存储矩阵。构造函数还期望以相同的顺序指定元素。

但是,您的翻译矩阵是按行主要顺序指定的:

glm::mat4 trans = glm::mat4(
1.0f, 0.0f, 0.0f, translation.x,
0.0f, 1.0f, 0.0f, translation.y,
0.0f, 0.0f, 1.0f, translation.z,
0.0f, 0.0f, 0.0f, 1.0f);

要以正确的列主顺序指定矩阵,这需要:

glm::mat4 trans = glm::mat4(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
translation.x, translation.y, translation.z, 1.0f);

关于c++ - OpenGL - 翻译拉伸(stretch)和扭曲 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30960009/

25 4 0
文章推荐: javascript - 从 HTML5 本地存储项创建 JSON 数组
文章推荐: javascript - 根据当前 URL 创建超链接
文章推荐: javascript - 强制从数据列表或等效项中选择(文本输入)
文章推荐: html - 有没有办法阻止 Outlook 破坏
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com