glm::rotation OpenGL C++ 无法正常工作
glm::rotation doesn't work properly OpenGL C++
我正在学习 OpenGL,现在我正在从矩阵中学习更多,我想旋转我的纹理,但是当我这样做时,它旋转错误(idk,只有一个三角形?我不知道怎么解释)。您可以在下面看到我的代码和有问题的图片。谢谢你的帮助。我的循环代码:
/* My Vertex Array with Struct */
struct Vertex
{
glm::vec3 position;
glm::vec2 texCoord;
Vertex(glm::vec3 pos, glm::vec2 tcoord)
: position(pos), texCoord(tcoord) {}
};
Vertex vertices[] = {
Vertex(glm::vec3(100.f, 100.f, 0.f), glm::vec2(0.f, 1.f)),
Vertex(glm::vec3(100.f, 400.f, 0.f), glm::vec2(0.f, 0.f)),
Vertex(glm::vec3(400.f, 100.f, 0.f), glm::vec2(1.f, 1.f)),
Vertex(glm::vec3(400.f, 400.f, 0.f), glm::vec2(1.f, 0.f))
};
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glm::mat4 proj = glm::ortho(0.f, 800.f, 600.f, 0.f, 0.f, 1.f);
glm::mat4 trans(1.0f);
trans = glm::translate(trans, glm::vec3(0.5, -0.5, 0));
trans = glm::rotate(trans, glm::radians(1.f), glm::vec3(1.f));
proj *= trans;
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(proj));
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDrawElements(GL_TRIANGLE_STRIP, 6, GL_UNSIGNED_INT, nullptr);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glfwSwapBuffers(window);
glfwPollEvents();
}
我的加载纹理代码:
GLuint loadTexture(std::string texturePath, int slot)
{
stbi_set_flip_vertically_on_load(1);
int x, y, bpp;
unsigned char* file = stbi_load(texturePath.c_str(), &x, &y, &bpp, 4);
GLuint texture = 0;
glGenTextures(1, &texture);
bindTexture(texture, slot);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, file);
glGenerateMipmap(GL_TEXTURE_2D);
bindTexture(0, 0);
if (file)
stbi_image_free(file);
return texture;
}
Image with Problem (Don't look at picture, I just took a random picture from Memes folder lol)
网格被正射投影的近平面和远平面裁剪。增加到近平面和远平面的距离:
glm::mat4 proj = glm::ortho(0.f, 800.f, 600.f, 0.f, 0.f, 1.f);
glm::mat4 proj = glm::ortho(0.f, 800.f, 600.f, 0.f, -1000.f, 1000.f);
投影矩阵定义投影到视口上的观察者(观看者)的区域(体积)。在正交投影中,该区域(体积)由到观察者位置的 6 个距离(左、右、下、上、近和远)定义。体积中的所有对象在视口上都是“可见的”。所有超出(或部分超出)该体积的对象都在该体积的边界处被剪裁。
我正在学习 OpenGL,现在我正在从矩阵中学习更多,我想旋转我的纹理,但是当我这样做时,它旋转错误(idk,只有一个三角形?我不知道怎么解释)。您可以在下面看到我的代码和有问题的图片。谢谢你的帮助。我的循环代码:
/* My Vertex Array with Struct */
struct Vertex
{
glm::vec3 position;
glm::vec2 texCoord;
Vertex(glm::vec3 pos, glm::vec2 tcoord)
: position(pos), texCoord(tcoord) {}
};
Vertex vertices[] = {
Vertex(glm::vec3(100.f, 100.f, 0.f), glm::vec2(0.f, 1.f)),
Vertex(glm::vec3(100.f, 400.f, 0.f), glm::vec2(0.f, 0.f)),
Vertex(glm::vec3(400.f, 100.f, 0.f), glm::vec2(1.f, 1.f)),
Vertex(glm::vec3(400.f, 400.f, 0.f), glm::vec2(1.f, 0.f))
};
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glm::mat4 proj = glm::ortho(0.f, 800.f, 600.f, 0.f, 0.f, 1.f);
glm::mat4 trans(1.0f);
trans = glm::translate(trans, glm::vec3(0.5, -0.5, 0));
trans = glm::rotate(trans, glm::radians(1.f), glm::vec3(1.f));
proj *= trans;
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(proj));
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDrawElements(GL_TRIANGLE_STRIP, 6, GL_UNSIGNED_INT, nullptr);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glfwSwapBuffers(window);
glfwPollEvents();
}
我的加载纹理代码:
GLuint loadTexture(std::string texturePath, int slot)
{
stbi_set_flip_vertically_on_load(1);
int x, y, bpp;
unsigned char* file = stbi_load(texturePath.c_str(), &x, &y, &bpp, 4);
GLuint texture = 0;
glGenTextures(1, &texture);
bindTexture(texture, slot);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, file);
glGenerateMipmap(GL_TEXTURE_2D);
bindTexture(0, 0);
if (file)
stbi_image_free(file);
return texture;
}
Image with Problem (Don't look at picture, I just took a random picture from Memes folder lol)
网格被正射投影的近平面和远平面裁剪。增加到近平面和远平面的距离:
glm::mat4 proj = glm::ortho(0.f, 800.f, 600.f, 0.f, 0.f, 1.f);
glm::mat4 proj = glm::ortho(0.f, 800.f, 600.f, 0.f, -1000.f, 1000.f);
投影矩阵定义投影到视口上的观察者(观看者)的区域(体积)。在正交投影中,该区域(体积)由到观察者位置的 6 个距离(左、右、下、上、近和远)定义。体积中的所有对象在视口上都是“可见的”。所有超出(或部分超出)该体积的对象都在该体积的边界处被剪裁。