繁体   English   中英

在OpenGL中使用矩阵-MVP矩阵不起作用

[英]Using Matrices in OpenGL — MVP matrix not working

今天,我试图按照以下教程进行操作: http : //www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/ ,我觉得我几乎完全遵循了该教程,但是我似乎无法得到相同的结果。 我确实使用了一些我定义的类来帮助处理基本操作,但是这些并没有错,因为我之前使用过它们。

当前结果什么都没有-我在任何地方都看不到三角形。 如果我摆脱了vert shader的MVP *部分,那就可以了

本教程复制并粘贴了很多代码。

我排除的问题:1.错误的设置(我下载并编译了本教程的源代码。)2.错误的初始化代码(如果将MVP *部分从顶点着色器中取出,则效果很好)

感谢您能获得的所有帮助。

顶点着色器(Basic.vertexshader):

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

    uniform mat4 MVP;


    void main()
    {

        gl_Position =  MVP * vec4(location,1);
// gl_Position = vec4(location, 1); *** this works fine, without the transformations


        //coord = texCoord;
    }

这是初始化代码(仅调用一次)

glClearColor(0.2f, 0.2f, 0.2f, 1.f);

    glGenVertexArrays(1, &vaoID);
    glBindVertexArray(vaoID);



    // load program
    pro = LoadShaders("Basic.vertexshader", "Basic.fragmentshader");

    MVPID = glGetUniformLocation(pro, "MVP");

    // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
    Projection = glm::perspective(45.0f, 4.f / 3.f, 0.1f, 100.f);
    // Or, for an ortho camera :
    //glm::mat4 Projection = glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.0f,100.0f); // In world coordinates

    // Camera matrix
    View = glm::lookAt(
        glm::vec3(4, 3, 0), // Camera is at (4,3,3), in World Space
        glm::vec3(0, 0, 0), // and looks at the origin
        glm::vec3(0, 1, 0)  // Head is up (set to 0,-1,0 to look upside-down)
        );
    // Model matrix : an identity matrix (model will be at the origin)
    Model = glm::mat4(1.0f);
    // Our ModelViewProjection : multiplication of our 3 matrices

    // Our ModelViewProjection : multiplication of our 3 matrices
    MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around


    GLfloat verts[] =
    {// ---LOCATION----
        -1.f, -1.f, 0.f,
        1.f, -1.f, 0.f,
        0.f, 1.f, 0.f, 
    };

    // init vbo
    glGenBuffers(1, &vboID);
    glBindBuffer(GL_ARRAY_BUFFER, vboID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);

    return 0;

最后是绘制代码(每帧):

glClear(GL_COLOR_BUFFER_BIT);

    glUseProgram(pro);

    glUniformMatrix4fv(MVPID, 1, GL_FALSE, &MVP[0][0]);

    // bind location
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vboID);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, (void*)0);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);

如果我错过了一些重要的代码,则可以在此处下载整个源代码:

https://www.dropbox.com/sh/zb3r85rj7lgfzs2/AADh0nJSVw5dO2fUAJdrnrTua?dl=0

编辑:如果有关系,我正在使用Visual Studio 2013编译器链接到的.lib文件是glfw3.lib,opengl32.lib,glew32.lib

您正在看三角形的边缘。 该三角形在x / y平面中,因为所有z坐标均为零:

GLfloat verts[] =
{
    -1.f, -1.f, 0.f,
    1.f, -1.f, 0.f,
    0.f, 1.f, 0.f, 
};

摄像机也位于x / y平面中,并指向原点:

View = glm::lookAt(
    glm::vec3(4, 3, 0),
    glm::vec3(0, 0, 0),
    glm::vec3(0, 1, 0)
    );

由于三角形和相机位于同一平面上,因此三角形在变换后会退化,并且不会渲染任何像素。

最简单的解决方法是通过将相机位置设置为非零的z坐标,将相机位置移动到x / y平面之外。 例如:

View = glm::lookAt(
    glm::vec3(4.0f, 3.0f, 10.0f),
    glm::vec3(0.0f, 0.0f, 0.0f),
    glm::vec3(0.0f, 1.0f, 0.0f)
    );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM