Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: GLSL & Matrix Math

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

    GLSL & Matrix Math

    I'm new to 3-D math so I'm not sure if I'm doing this correctly.

    I currently have a GLfloat[ 36 ] pyramid set to the buffer, and that data is sent to an vec4 Vertex in the shader.

    Code :
        #version 400 core
     
        layout( location = 0 ) in vec4 Vertex;
        uniform mat4 Model;
     
        void main()
        {
            gl_Position = Model * Vertex;
        }

    I have a 4x4 matrix to rotate around the y-axis, and send that as a uniform to the "Model" variable in the shader.

    Code :
        glm::mat4 ModelR(
                cos( rStepAngle ), 0.f, -sin( rStepAngle ), 0.f,
                0.f, 1.f, 0.f, 0.f,
                sin( rStepAngle ), 0.f, cos( rStepAngle ), 0.f,
                0.f, 0.f, 0.f, 0.f
                );
     
        GLint ModelLoc = glGetUniformLocation( programs.basicShader, "Model" );
        glUniformMatrix4fv( ModelLoc, 1, GL_FALSE, glm::value_ptr( ModelR ) );

    I'm getting nothing on my screen except the clear color. I think I may be misunderstanding something here. Vertex is going to be 3 components (technically 4, with 1 at the end), that gets multiplied by the Model rotation. How many times is the main() of the shader being run? My understanding of it was that main() is run until all 12 vertices of the pyramid are multiplied by the Model matrix. Is this correct?

    There's probably something wrong with my math...
    Last edited by Qoheleth; 12-30-2012 at 12:27 AM.

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    You have no projection matrix nor view-to-camera matrix - this code will only move the vertex in world space.

    Vertex is going to be 3 components (technically 4, with 0 at the end),
    Vertices have 1 at the end so translations can happen; vectors have a 0 so only scaling and rotation can happen.

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    9
    thanks tonyo. I just wanted to do a rotation in an orthographic projection. I thought by default opengl does that automatically if a perspective projection is not specified. For example with just gl_Position = vertex, I am able to get a triangle on the screen with orthographic projection.

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    To see your object your gl_Position xyz values must stay within -1 - 1 and the w values is 1. There are no defaults when using shaders.

  5. #5
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,264
    You need a projection matrix. For ortho, try
    [1, 0, 0, 0]
    [0, 1, 0, 0]
    [0, 0, -1, 0]
    [0, 0, 0, 1]
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •