简体   繁体   English

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

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

today I was trying to follow the tutorial at: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/ and i feel that I followed the tutorial almost exactly, but I can't seem to get the same results. 今天,我试图按照以下教程进行操作: http : //www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/ ,我觉得我几乎完全遵循了该教程,但是我似乎无法得到相同的结果。 I did use some classes defined by me to help handle the basic operations, but those are not at fault because I have used them before. 我确实使用了一些我定义的类来帮助处理基本操作,但是这些并没有错,因为我之前使用过它们。

The current result is nothing -- I can't see the triangle anywhere. 当前结果什么都没有-我在任何地方都看不到三角形。 If I get rid of the MVP * part of the vert shader then it works 如果我摆脱了vert shader的MVP *部分,那就可以了

A lot of the code is copied and pasted from the tutorial. 本教程复制并粘贴了很多代码。

Problems i've ruled out: 1. bad setup (i downloaded and compiled the source code for the tutorial.) 2. Bad init code (if out take the MVP * part out of the vertex shader it works fine) 我排除的问题:1.错误的设置(我下载并编译了本教程的源代码。)2.错误的初始化代码(如果将MVP *部分从顶点着色器中取出,则效果很好)

Thanks for any and all help I can get. 感谢您能获得的所有帮助。

Vertex Shader (Basic.vertexshader): 顶点着色器(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;
    }

Here is the init code (called only once) 这是初始化代码(仅调用一次)

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;

And finally the draw code (every frame): 最后是绘制代码(每帧):

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);

If there is some important code I missed, then the entire source code can be downloaded here: 如果我错过了一些重要的代码,则可以在此处下载整个源代码:

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

Edit: if it matters, I am using the Visual Studio 2013 compiler the .lib files I'm linking to are glfw3.lib, opengl32.lib, glew32.lib 编辑:如果有关系,我正在使用Visual Studio 2013编译器链接到的.lib文件是glfw3.lib,opengl32.lib,glew32.lib

You're looking at the triangle edge on. 您正在看三角形的边缘。 The triangle is in the x/y plane, since all z-coordinates are zero: 该三角形在x / y平面中,因为所有z坐标均为零:

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

The camera is also positioned in the x/y plane, and pointed at the origin: 摄像机也位于x / y平面中,并指向原点:

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

With both the triangle and the camera located in the same plane, the triangle is degenerate after transformation, and no pixels will be rendered. 由于三角形和相机位于同一平面上,因此三角形在变换后会退化,并且不会渲染任何像素。

The easiest fix is to move the camera position outside the x/y plane by giving it a non-zero z coordinate. 最简单的解决方法是通过将相机位置设置为非零的z坐标,将相机位置移动到x / y平面之外。 Eg: 例如:

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