OpenGL Perspective Projection

Поделиться
HTML-код
  • Опубликовано: 12 ноя 2024
  • Sets up a perspective projection matrix and passes it as a uniform parameter to the vertex shader. The vertex shader transforms each vertex to screen space.

Комментарии • 26

  • @chenxin4741
    @chenxin4741 5 лет назад +8

    For those who follow the step but cannot get the cube shown: the reason might be that mat4() will initialize a full-zero matrix after some version (maybe 0.9.9). This eliminates everything. So use mat4(1.0f) as the first argument in glm::translate() instead. See stackoverflow.com/questions/52285271/what-values-should-a-glmmat4-made-via-a-zero-argument-constructor-contain.

    • @shn--zj9dl
      @shn--zj9dl 4 года назад +1

      ty so much. i wouldn't find solution without you probably

    • @YugaldeepSingh
      @YugaldeepSingh 4 года назад

      thanks buddy you saved me

    • @kasufert
      @kasufert 8 месяцев назад

      The real MVP is in the comments

  • @lonelytraveler8
    @lonelytraveler8 7 лет назад +10

    It may have been mentioned, but since this video was released, it seems GLM has switched the glm::perspective() function to accept the field of view in radians instead of degrees. If the square is displayed much smaller than expected, multiply the 60.0f by glm::pi()/180, which requires the glm/gtc/constants.hpp header. Or, for a simpler solution, use glm::radians(60.0f), which is already included in the glm core.

  • @charvakpatel962
    @charvakpatel962 8 лет назад +9

    Its funny how the playlist goes forward and views gets decreased.
    Someone said it right "Grit is the big blessing."
    On the side note, how can one stop watching this, this stuff is fascinating.

  • @samuelec
    @samuelec 5 лет назад +9

    //Year 2019, couple of fixes are needed for GLM 0.9.9 , C++ compiler and high DPI displays:
    // includes wants forward slashes and instead of :
    #include // glm::translate, glm::rotate, glm::scale
    #include // glm::perspective
    // use device pixel ratio otherwise your view is not centered
    glViewport(0, 0, width() * devicePixelRatio(), height() * devicePixelRatio());
    // mat4(1.0f) as identity matrix, radians instead degree and static_cast makes compiler happy
    mat4 modelTransformMatrix = glm::translate(mat4(1.0f), vec3(0.0f, 0.0f, -3.0f));
    mat4 projectionMatrix = glm::perspective(glm::radians(60.0f), static_cast(width()) / height(), 0.1f, 10.0f);
    happy coding :)

  • @Matthew-mr2pv
    @Matthew-mr2pv 6 лет назад +1

    Just spent half an hour to discover that if you don't pass 1.0f to mat4 constructor then the projection matrix component w will default to 0, so change
    glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, -3.0f));
    to
    glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -3.0f));
    (I am using glm 0.9.9.0)
    also change glm::perspective's FOV to radians instead of degrees like the others have mentioned

  • @xmaly2856
    @xmaly2856 9 лет назад +3

    Hey, i'm having a slight problem with my cube. I'm using FreeGlut instead of Qt for "reasons" so there are slight alterations with some function names.
    This is what i am getting i.imgur.com/fDeWAIo.png
    sendData - i.imgur.com/Z2wjsDw.png
    paintGL - i.imgur.com/AIY3kFo.png
    vertexShader - i.imgur.com/nDUrRcY.png
    shapeGenerator pt1 - i.imgur.com/wWdIBmi.png
    shapeGenerator pt2 - i.imgur.com/Eb561My.png
    Any ideas? If you need more code i'll prntscr some more. I can't copy/paste code apparently.
    Update:
    Edited the shapeGenerator pt1, there was a - instead of a + for 1 vertice information, which has gotten the result below.
    i.imgur.com/czntlbo.png
    but going over the rest of the information, I can't see anything else that is a mistake. I know there has to be, but its finding it.
    Update 2:
    by changing the stackIndices's layout I can get other sides to draw, this however does not complete the shape, there is still four triangles that are not being drawn.
    I feel as if some of it isn't being noticed with the stack indicies, like the indicies are being left out, but I can't get them all to be drawn.
    Update: Solved.
    So 2 MSc undergrads + PhD Lecturer later, I messed up my ShapeData, accidently returning the numVertices instead of the numIndices. I'm leaving all of this up though for others who might have a similar mistake. It really is the little things.

    • @kasufert
      @kasufert 8 месяцев назад

      This is a certified OpenGL moment

  • @CatchTheBus
    @CatchTheBus 9 лет назад +2

    You need to add this
    glClearDepth(1.0f);
    glDepthFunc(GL_LESS);
    glEnable(GL_DEPTH_TEST);
    And this glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    And also this glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    To get this working correctly.

    • @JamieKingCS
      @JamieKingCS  9 лет назад +1

      +MoulinRougexD Hehe, some of this depends. :) Nowhere in this list will you see me use the GLUT libraries, but if you're using GLUT then cool. The default for clear depth is already 1, so that's not necessary. As far as enabling the depth test with glEnable(), I may have done that in a previous video in this playlist. Not sure if it's turned off or on in this video. If it is turned on, then the glClear() is necessary. Keep 'em coming!

    • @CatchTheBus
      @CatchTheBus 9 лет назад +1

      Jamie King Oh yeah, sure, you would need to replace glut functions with the library that you are using, i just had the same problem after watching this video, so i posted my solution :)

    • @JamieKingCS
      @JamieKingCS  9 лет назад +1

      Thanks tons!

  • @aruji-sama
    @aruji-sama 4 года назад

    can i do this without using glew.....coz our clg professor wants us to do this in Glut and I dont know why or how...I just have to do it.

  • @miroslavkovac2159
    @miroslavkovac2159 9 лет назад +3

    hello sorry to bother you but i have this problem that is not showing up correctly.
    it s way smaller then yours.. like it s too far and also its upside down.. i could change translate number from -3.0f to -1.17f and also had to change perspective first number from 60.0f to -60.0f but then it still looks like there is one platform of cube drawing over it.. here is my code
    void MeGlWindow::paintGL()
    {
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); //applaing depth
    glViewport(0,0,width(),height());
    mat4 translationMatrix = glm::translate(mat4(), vec3(0.0f,0.0f, -1.17f));
    mat4 projectionMatrix = glm::perspective(60.0f, ((float) width()) / height(), 0.1f, 10.0f);
    GLint modelTransformMatrixUniformLocation = glGetUniformLocation(programID,"modelTransformMatrix");
    GLint projectionMatrixUniformLocation = glGetUniformLocation(programID,"projectionMatrix");
    glUniformMatrix4fv(modelTransformMatrixUniformLocation,1,GL_FALSE, &translationMatrix[0][0]);
    glUniformMatrix4fv(projectionMatrixUniformLocation,1,GL_FALSE, &projectionMatrix[0][0]);
    glDrawElements(GL_TRIANGLES,numIndices,GL_UNSIGNED_SHORT,0);
    }
    vertex shader
    #version 430
    in layout(location=0) vec3 position;
    in layout(location=1) vec3 vertexColor;
    uniform mat4 modelTransformMatrix;
    uniform mat4 projectionMatrix;
    out vec3 theColor;
    void main()
    {
    vec4 p = vec4(position, 1.0);
    vec4 newPosition = modelTransformMatrix *p ;
    vec4 projectedPosition = projectionMatrix * newPosition;
    gl_Position = projectedPosition;
    theColor = vertexColor;
    }
    ShapeData ShapeGenerator::makeCube(){
    ShapeData ret;
    Vertex stackVerts[] = {
    vec3(-1.0f, +1.0f, +1.0f), // 0
    vec3(+1.0f, +0.0f, +0.0f), // Colour
    vec3(+1.0f, +1.0f, +1.0f), // 1
    vec3(+0.0f, +1.0f, +0.0f), // Colour
    vec3(+1.0f, +1.0f, -1.0f), // 2
    vec3(+0.0f, +0.0f, +1.0f), // Colour
    vec3(-1.0f, +1.0f, -1.0f), // 3
    vec3(+1.0f, +1.0f, +1.0f), // Colour
    vec3(-1.0f, +1.0f, -1.0f), // 4
    vec3(+1.0f, +0.0f, +1.0f), // Colour
    vec3(+1.0f, +1.0f, -1.0f), // 5
    vec3(+0.0f, +0.5f, +0.2f), // Colour
    vec3(+1.0f, -1.0f, -1.0f), // 6
    vec3(+0.8f, +0.6f, +0.4f), // Colour
    vec3(-1.0f, -1.0f, -1.0f), // 7
    vec3(+0.3f, +1.0f, +0.5f), // Colour
    vec3(+1.0f, +1.0f, -1.0f), // 8
    vec3(+0.2f, +0.5f, +0.2f), // Colour
    vec3(+1.0f, +1.0f, +1.0f), // 9
    vec3(+0.9f, +0.3f, +0.7f), // Colour
    vec3(+1.0f, -1.0f, +1.0f), // 10
    vec3(+0.3f, +0.7f, +0.5f), // Colour
    vec3(+1.0f, -1.0f, -1.0f), // 11
    vec3(+0.5f, +0.7f, +0.5f), // Colour
    vec3(-1.0f, +1.0f, +1.0f), // 12
    vec3(+0.7f, +0.8f, +0.2f), // Colour
    vec3(-1.0f, +1.0f, -1.0f), // 13
    vec3(+0.5f, +0.7f, +0.3f), // Colour
    vec3(-1.0f, -1.0f, -1.0f), // 14
    vec3(+0.4f, +0.7f, +0.7f), // Colour
    vec3(-1.0f, -1.0f, +1.0f), // 15
    vec3(+0.2f, +0.5f, +1.0f), // Colour
    vec3(+1.0f, +1.0f, +1.0f), // 16
    vec3(+0.6f, +1.0f, +0.7f), // Colour
    vec3(-1.0f, +1.0f, +1.0f), // 17
    vec3(+0.6f, +0.4f, +0.8f), // Colour
    vec3(-1.0f, -1.0f, +1.0f), // 18
    vec3(+0.2f, +0.8f, +0.7f), // Colour
    vec3(+1.0f, -1.0f, +1.0f), // 19
    vec3(+0.2f, +0.7f, +1.0f), // Colour
    vec3(+1.0f, -1.0f, -1.0f), // 20
    vec3(+0.8f, +0.3f, +0.7f), // Colour
    vec3(-1.0f, -1.0f, -1.0f), // 21
    vec3(+0.8f, +0.9f, +0.5f), // Colour
    vec3(-1.0f, -1.0f, +1.0f), // 22
    vec3(+0.5f, +0.8f, +0.5f), // Colour
    vec3(+1.0f, -1.0f, +1.0f), // 23
    vec3(+0.9f, +1.0f, +0.2f), // Colour
    };
    ret.numVertices = NUM_ARRAY_ELEMENTS(stackVerts);
    ret.Vertices = new Vertex[ret.numVertices];
    memcpy(ret.Vertices, stackVerts, sizeof(stackVerts));
    unsigned short stackIndices[] = {
    0, 1, 2, 0, 2, 3, // Top
    4, 5, 6, 4, 6, 7, // Front
    8, 9, 10, 8, 10, 11, // Right
    12, 13, 14, 12, 14, 15, // Left
    16, 17, 18, 16, 18, 19, // Back
    20, 22, 21, 20, 23, 22, // Bottom
    };
    ret.numIndices = NUM_ARRAY_ELEMENTS(stackIndices);
    ret.Inicies = new GLushort[ret.numIndices];
    memcpy(ret.Inicies, stackIndices, sizeof(stackIndices));
    return ret;
    }
    i thought that there would be some vertex changed but i checked it twice and it seemes to be the same as yours in previous video...

    • @LeMowalle
      @LeMowalle 9 лет назад +9

      Miroslav Kováč Hey there, I don't know if you still need it, but in case anyone runs into the same problem: I think you can solve it by not passing the 60.0f directly as an angle, but instead passing glm::radians(60.0f), so the line should be:
      glm::mat4 projectionMatrix = glm::perspective(glm::radians(60.0f), (float)width() / height(), 0.1f, 10.0f);
      I think that the glm-functions like ::perspective and ::rotate expect the angles in radian format, that's why. It's working now for me at least.

    • @Horhew
      @Horhew 9 лет назад

      +Mowalle Dude, thank you for taking the time to post the solution. It works!

  • @tazdevil9718
    @tazdevil9718 8 лет назад +1

    Hi, your tutorials are amazing and easy to follow .... but now I'm stuck ... I can't get the cube to show, I tried to troubleshooting passing the projectedPosition to the color (for seeing if the uniforms were wrong) and everything is fine but when I do gl_Position = projectedPosition instead of gl_Position = v the cube doesn't show up at all .... I'm really confused now (P.S. I'm using glew and glfw instead of QT but since now everything went fine ... with only a few modification on the initial setup)

    • @tazdevil9718
      @tazdevil9718 8 лет назад +1

      sorry, my problem was quite simple i was translating the cube +3.0f instead of -3.0 so it was behind the camera ... I'm feeling so stupid ... but maybe this will help somebody else ;)

  • @TheGruselmops
    @TheGruselmops 5 лет назад

    I have a problem, where nothing shows up. I checked the code several times can't find any differences. I then copied the code from github and it compiled, but still nothing is showing up.
    I tried the solutions others posted for their problems. Nothing worked, I must have a different issue. Pure black screen with the exact same code.
    using qmake, GNUmake, g++
    Everything else worked so far,
    only other issue: I don't get a shader linking error, when I remove the glAttachShader () calls. I have no idea atm.
    The code from the next video, the 'fullTransformMatrix' works . Also had to adjust to glm::perspective(60.0f * glm::() / 180.0f, ...) for the projectionMatrix.

  • @areriff
    @areriff 7 лет назад

    Can you add the full source?
    The rectangle projected on my screen is very small compared to yours. I'm using SFML. I might've tripped some lines but can't see it from you video to compare it to.

    • @areriff
      @areriff 7 лет назад

      By the way... good job. This playlist rocks.

    • @JamieKingCS
      @JamieKingCS  7 лет назад +1

      +Are Riff my code is on guthub. You have annotations turned off?

    • @areriff
      @areriff 7 лет назад

      Yeah kinda but no. First few videos i found it very useful so i go ahead and download the whole videos in the playlist offline into my laptop before the battery died. Sorry...
      I noticed you haven't had new videos recently.