无法使用我的图像获得透视

Posted

技术标签:

【中文标题】无法使用我的图像获得透视【英文标题】:Can't get perspective to work with my image 【发布时间】:2018-11-10 02:37:58 【问题描述】:

所以我正在关注相机的 LearnOpenGL 教程。我正在尝试渲染一个立方体(用顶点向量定义)并围绕立方体旋转以确保它已正确渲染。我可以很好地渲染立方体的正面,但是一旦我将着色器切换为使用 MVP,什么都不会渲染

void GraphicsProgram::run_program() 

    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    state = RUNNING;

    // TODO: Move these into their respective classes
    // Hardcoded for some testing
    // ----------------------------------------------
    const char *vertexShaderPath = "vertex_shader.vs";
    const char *fragmentShaderPath = "fragment_shader.fs";
    Shader firstShader = Shader(vertexShaderPath, fragmentShaderPath);

    std::vector<Vertex> verts;
    Vertex v1, v2, v3, v4, v5, v6, v7, v8;
    v1.position =  -0.5, -0.5, +0.5 ;
    v2.position =  +0.5, -0.5, +0.5 ;
    v3.position =  +0.5, +0.5, +0.5 ;
    v4.position =  -0.5, +0.5, +0.5 ;
    v5.position =  +0.5, -0.5, -0.5 ;
    v6.position =  +0.5, +0.5, -0.5 ;
    v7.position =  -0.5, +0.5, -0.5 ;
    v8.position =  -0.5, -0.5, -0.5 ;

    verts.push_back(v1);
    verts.push_back(v2);
    verts.push_back(v3);
    verts.push_back(v4);
    verts.push_back(v5);
    verts.push_back(v6);
    verts.push_back(v7);
    verts.push_back(v8);

    std::vector<unsigned int> indicies =  
        0, 1, 2, 0, 2, 3, // front
        1, 4, 5, 1, 5, 2, // right
        4, 7, 6, 4, 6, 7, // back
        7, 0, 3, 7, 3, 6, // left
        3, 2, 5, 3, 5, 6, // top
        7, 4, 1, 7, 1, 0  // bottom
    ;


    unsigned int VBO, VAO, EBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);

    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, verts.size() * sizeof(Vertex), &verts[0], GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicies.size() * sizeof(unsigned int), &indicies[0], GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindVertexArray(0);


    glUseProgram(firstShader.shaderID);

    glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
    firstShader.set_mat4("projection", projection);

    while (state == RUNNING) 
        currentFrame = (float)SDL_GetTicks() / 1000;
        deltaTime = lastFrame - currentFrame;
        lastFrame = currentFrame;

        process_input();

        glUseProgram(firstShader.shaderID);

        glm::mat4 view;
        float radius = 10.0f;
        float camX = sin((float)SDL_GetTicks() / 1000) * radius;
        float camZ = cos((float)SDL_GetTicks() / 1000) * radius;
        view = glm::lookAt(glm::vec3(camX, 0.0f, camZ), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
        firstShader.set_mat4("view", view);

        glBindVertexArray(VAO);

        glm::mat4 model;
        model = glm::translate(model, glm::vec3(0.0, 0.0, 0.0));
        float angle = 0.0;
        model = glm::rotate(model, glm::radians(angle), glm::vec3(0,0,0));
        firstShader.set_mat4("model", model);

        glDrawElements(GL_TRIANGLES, 24, GL_UNSIGNED_INT, 0);
        SDL_GL_SwapWindow(window);
    

    shutdown_program();

顶点结构:

struct Vertex 
    glm::vec3 position;
    glm::vec3 normal;
    glm::vec2 texCoord;
;

顶点着色器代码:

#version 330 core
layout (location = 0) in vec3 aPos;

out vec4 vertexColor;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main() 
    gl_Position = projection * view * model * vec4(aPos, 1.0f);
    vertexColor = vec4(abs(aPos.x), abs(aPos.y), abs(aPos.z), 1.0);

片段着色器:

#version 330 core

in vec4 vertexColor;
out vec4 fragColor;

void main() 
    fragColor = vertexColor;

我最初使用的顶点着色器只是取了顶点位置。

【问题讨论】:

【参考方案1】:

你必须初始化矩阵变量glm::mat4 modelglm::mat4 view

glm API documentation 指的是The OpenGL Shading Language specification 4.20。

5.4.2 向量和矩阵构造函数

如果向量构造函数有一个标量参数,它用于将构造向量的所有组件初始化为该标量的值。 如果矩阵构造函数只有一个标量参数,则用于初始化矩阵对角线上的所有分量,其余分量初始化为 0.0。

这意味着,单位矩阵可以由单个参数 1.0 初始化:

glm::mat4 model(1.0f);

....

glm::mat4 view(1.0f);

您错过了在glm::rotate 中设置正确的旋转轴:

model = glm::rotate(model, glm::radians(angle), 
    glm::vec3(1.0,0,0)); // e.g. (1, 0, 0) instead of (0, 0, 0)

您必须在每一帧之前清除默认的farmebuffer (glClear):

glClearColor(0.0, 0.0, 0.0, 1.0);

while (state == RUNNING)

    glClear(GL_COLOR_BUFFER_BIT);

    .....

您应该启用Depth Test 并清除深度缓冲区:

glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 1.0);

while (state == RUNNING)

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    .....

【讨论】:

以上是关于无法使用我的图像获得透视的主要内容,如果未能解决你的问题,请参考以下文章

如何获得正交瓦片地图的透视图

Swift 中的 CIDetector 透视和裁剪

透视变换是如何工作的? [OpenCV 蟒蛇]

如屏幕截图中所述,我想在 SQL 中创建一个数据透视表 [重复]

Laravel 数据透视表获取值并传递给刀片模板

图像透视基于matlab图像逆透视映射含Matlab源码 2139期