【发布时间】:2021-09-27 22:27:34
【问题描述】:
为我的简单 2d 游戏设置(正交)投影矩阵后,屏幕上没有任何渲染。我正在使用 cglm(glm 但在 c 中)并将 cglm 的结果与渲染良好的正常 glm 正交投影实现进行比较,并且投影矩阵的结果匹配。这是我的渲染循环:
void RenderSprite(const struct Sprite *sprite) {
struct Shader *shader = GetSpriteShader(sprite);
UseShader(shader);
/* cglm starts here */
mat4 proj;
glm_ortho(0.0f, 800.0f, 600.0f, 0.0f, -1.0f, 1.0f, proj); /* screen width: 800, height: 600 */
mat4 model;
glm_mat4_identity(model); /* an identity model matrix - does nothing */
/* cglm ends here */
SetShaderUniformMat4(shader, "u_Projection", proj); /* set the relevant uniforms */
SetShaderUniformMat4(shader, "u_Model", model);
/* finally, bind the VAO and call the draw call (note that I am not using batch rendering - I am using just a simple plain rendering system) */
glBindVertexArray(ezGetSpriteVAO(sprite));
glDrawElements(GL_TRIANGLES, ezGetSpriteIndexCount(sprite), GL_UNSIGNED_INT, 0);
}
但是,这会导致一个空白屏幕 - 没有任何渲染。我相信我已经按顺序做了所有事情 - 但问题是没有渲染。 对于任何感兴趣的人,这是我的顶点着色器:
#version 330 core
layout (location = 0) in vec3 pos;
layout (location = 1) in vec2 uv;
uniform mat4 u_Model;
uniform mat4 u_Projection;
void main() {
gl_Position = u_Projection * u_Model * vec4(pos, 1.0f);
}
这是我的片段着色器:
#version 330 core
out vec4 color;
void main() {
color = vec4(1.0f);
}
据我所知,cglm 矩阵是按列排序的,这是 OpenGL 想要的。
任何帮助将不胜感激。 提前致谢。
编辑
精灵坐标是(在这种情况下它是顶点数据,我猜):
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
-0.5f, 0.5f, 0.0f
编辑 2 在@BDL 的评论之后,我将顶点数据调整如下:
float vertices[] = {
/* Position UV */
5.0f, 5.0f, 0.0f, 0.0f, 0.0f,// bottom left
10.0f, 5.0f, 0.0f, 1.0f, 0.0f, // bottom right
10.0f, 10.0f, 0.0f, 1.0f, 1.0f, // top right
5.0f, 10.0f, 0.0f, 0.0f, 1.0f // top left
};
但是,我在屏幕上看不到任何东西——此时没有渲染任何东西。
【问题讨论】:
-
请显示您尝试渲染的坐标。它们只是在可见区域之外吗?
-
坐标为:-0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.5f, 0.5f, 0.0f, -0.5f, 0.5f, 0.0 f
-
您的投影矩阵从 0,0 变为 800,600。四边形的大小为 1x1(又名 1 像素)。检查屏幕中间是否有彩色像素?
-
该死!我忘了编辑顶点坐标以适应 proj 矩阵!我现在将编辑它们并尽快发表评论。谢谢
-
好吧,我现在已经将坐标设置为:float vertices[] = { /* Position UV */ -10.0f, -10.0f, 0.0f, 0.0f, 0.0f,//左下 10.0f, -10.0f, 0.0f, 1.0f, 0.0f, // 右下 10.0f, 10.0f, 0.0f, 1.0f, 1.0f, // 右上 -10.0f, 10.0f, 0.0f , 0.0f, 1.0f // 左上角 };但是,仍然没有呈现任何内容:(
标签: c++ opengl shader projection glm-math