【问题标题】:Having trouble when using 2d orthographic matrix with glm使用带有 glm 的 2d 正交矩阵时遇到问题
【发布时间】: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


【解决方案1】:

正如@BDL 和其他人所建议的,这是我的最终渲染循环,它现在就像一个魅力: (作为参考发布)

void RenderSprite(const struct Sprite *sprite) {
    ASSERT(sprite, "[ERROR]: Can't render a sprite which is NULL\n");

    struct Shader *shader = GetSpriteShader(sprite);
    UseShader(shader);

    mat4 proj;
    glm_ortho(0.0f, 800.0f, 600.0f, 0.0f, -1.0f, 1.0f, proj);
    mat4 model, view;
    glm_mat4_identity(model);
    glm_mat4_identity(view);

    SetShaderUniformMat4(shader, "u_Projection", proj);
    SetShaderUniformMat4(shader, "u_Model", model);
    SetShaderUniformMat4(shader, "u_View", view);

    glBindVertexArray(GetSpriteVAO(sprite));
    glDrawElements(GL_TRIANGLES, GetSpriteIndexCount(sprite), GL_UNSIGNED_INT, 0);
}

这是我的坐标:

float vertices[] = {
           /*   Position       UV */
       350.0f, 350.0f, 0.0f, 0.0f, 0.0f,// bottom left
       450.0f, 350.0f, 0.0f, 1.0f, 0.0f, // bottom right
       450.0f, 250.0f, 0.0f, 1.0f, 1.0f,  // top right
       350.0f, 250.0f, 0.0f, 0.0f, 1.0f  // top left
};

还有我的着色器:

const char *default_vs = "#version 330 core\n"
                         "layout (location = 0) in vec3 pos;\n"
                         "layout (location = 1) in vec2 uv;\n"
                         "uniform mat4 u_Model;\n"
                         "uniform mat4 u_Projection;\n"
                         "uniform mat4 u_View;\n"
                         "void main() {\n"
                         "    gl_Position = u_Projection * u_View * u_Model * vec4(pos, 1.0f);\n"
                         "}";

const char *default_fs = "#version 330 core\n"
                         "out vec4 color;\n"
                         "void main() {\n"
                         "    color = vec4(1.0f);\n"
                         "}";

希望这对遇到同样问题的人有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 2019-09-13
    相关资源
    最近更新 更多