简体   繁体   中英

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. 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

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)

Thanks for any and all help I can get.

Vertex Shader (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

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

You're looking at the triangle edge on. The triangle is in the x/y plane, since all z-coordinates are zero:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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