Results 1 to 2 of 2

Thread: OpenGL GLM C++ vertex and matrix transformations confusion

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    10

    OpenGL GLM C++ vertex and matrix transformations confusion

    I'm pretty confused now that I can no longer do a simply gl_Position = pos * MVP; in my vertex shader, and instead want to transform the vertices before I send them via glBufferSubData.

    This is my poor attempt at trying to get the vertex coordinates to be pixel based. Lets say I want a rect to be 100 pixels from 0,0. I was hoping this would work, and I've been fiddling it with for a while.

    What am I doing wrong here, or what is it that I actually want to do?



    Code :
            glm::mat4 view = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, 0.0f));
            glm::mat4 ortho = glm::ortho(0.0f, float(SCREEN_W), float(SCREEN_H), 0.0f, -1.0f, 1.0f);
     
            glm::vec4 rect = glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
     
            glm::vec4 transformed = rect * view * ortho;
     
            std::cout << "x: " << transformed.x << " y: " << transformed.y << " w: " << transformed.w << " t: " << transformed.t << " z : " << transformed.z << "\n";
     
            float x = transformed.x;
            float width = transformed.w;
     
            float y = transformed.y;
            float height = transformed.z;
     
            vertices[0][0] = x; // top left X
            vertices[0][1] = y; //top left Y
     
            vertices[1][0] = x; // bottom left X
            vertices[1][1] = height; // bottom left Y
     
            vertices[2][0] = width; // bottom right X
            vertices[2][1] = height; //bottom right Y
     
            vertices[3][0] = width; // top right X
            vertices[3][1] = y; // top right Y

  2. #2
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,677
    gl_Position = pos * MVP
    That pretty much explains everything; your transforms are backwards. The matrix goes on the left; the position goes on the right. And if that actually worked in your shader, it's probably because you transposed the matrix when you uploaded it with glUniformMatrix.

    That's why your CPU equivalent is failing; there's no transposition correction being applied, so your math is backwards.

Similar Threads

  1. Some confusion about matrix
    By Lee_Jennifer_82 in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 11-22-2015, 06:58 PM
  2. Matrix transformations for trackball rotation OpenGL (LWJGL)
    By jamie_wizard in forum OpenGL: General
    Replies: 1
    Last Post: 09-21-2014, 10:29 PM
  3. Confusion with values in modelview matrix.
    By Barbarosa in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 07-19-2013, 11:41 AM
  4. Matrix transformations
    By nk in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 05-25-2004, 02:06 AM
  5. vertex programs and extreme number of matrix transformations
    By bostrov in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 07-27-2003, 12:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean