在 OpenGL 中更改视图透视时出错

Error changing view perspective in OpenGL

我试图在 OpenGL 中改变我的正方形的视角,但是当我将一些向量与我的位置向量相乘时,图像消失了,我不知道我是否改变了视角足以使它从屏幕或者我认为这是一个渲染问题更有可能

顶点着色器:

#version 330 core

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTex;

out vec3 color;

out vec2 texCoord;

//uniform float scale;

uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;

void main()
{
    gl_Position = proj * view * model * vec4(position, 1.0);
    color = aColor;
    texCoord = aTex;
}

片段着色器:

#version 330 core

out vec4 FragColor;

in vec3 color;

in vec2 texCoord;

uniform sampler2D tex0;

void main()
{
 FragColor = texture(tex0, texCoord);
}

部分程序实现:

while (true)
  {
    if (SDL_PollEvent(&windowEvent))
    {
      if (SDL_QUIT == windowEvent.type)
      {
        break;
      }
    }
    //Cor do background
    glClearColor(0.05f, 0.57f, 0.38f, 1.0f);
    //Limpa o buffer e o atribui uma nova cor
    glClear(GL_COLOR_BUFFER_BIT);
    //Ativa o Programa Shader designado
    shaderProgram.Activate(shaderProgram);

    mat4 model = {1.f};
    mat4 view = {1.f};
    mat4 proj = {1.f};

    glm_translate(view, (vec3){0.f, -0.5f, -2.f});
    glm_perspective(glm_rad(45.f), (float)(SCREEN_WIDTH / SCREEN_HEIGHT), 0.1f, 100.f, proj);
    int modelLoc = glGetUniformLocation(shaderProgram.id, "model");
    glUniformMatrix4fv(modelLoc, 1, GL_FALSE, (float *)model);
    int viewLoc = glGetUniformLocation(shaderProgram.id, "view");
    glUniformMatrix4fv(viewLoc, 1, GL_FALSE, (float *)view);
    int projLoc = glGetUniformLocation(shaderProgram.id, "proj");
    glUniformMatrix4fv(projLoc, 1, GL_FALSE, (float *)proj);

    //Atribui um valor uniforme podendo aumentar ou reduzir o objeto renderizado
    glUniform1f(uniID, 0.5);
    //Liga a textura para aparecer na renderização
    imagem.Bind(imagem);
    //Vincula o VAO ao OpenGL para ser usado
    vao1.Bind(vao1);
    //Desenha o elemento
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    glEnd();
    //update the window
    SDL_GL_SwapWindow(window);
  }

如果您使用的是 C++ 并且 glm,则 glm::mat4 model(1.0f); 构造单位矩阵。

如果您使用 cglm,则 mat4 model = {1.f}; 不会初始化单位矩阵。 mat4 是结构,但不是带有构造函数的 class。

你必须使用 glm_mat4_identity:

mat4 model = {1.f}; mat4 view = {1.f}; mat4 proj = {1.f};

mat4 model, view, proj;
glm_mat4_identity(model); 
glm_mat4_identity(view);
glm_mat4_identity(proj);