Skip to content

Instantly share code, notes, and snippets.

@dpogue
Created July 10, 2014 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpogue/638a58e7f0e344dca6f0 to your computer and use it in GitHub Desktop.
Save dpogue/638a58e7f0e344dca6f0 to your computer and use it in GitHub Desktop.
OpenGL Matrix issues

Screen Projection Matrix

    // pipe->Width() == 800
    // pipe->Height() == 600
    float yon = 500.0f;

    pipe->SetFOV(60.f, int32_t(60.f * pipe->Height() / pipe->Width()));
    pipe->SetDepth(0.3f, yon);

Resulting matrix is (as represented in memory):

Proj: hsMatrix44[[1.7321, 0.0000, 0.0000, 0.0000]; [0.0000, 2.4142, 0.0000, 0.0000]; [0.0000, 0.0000, 1.0006, -0.3002]; [0.0000, 0.0000, 1.0000, 0.0000]]

World-To-Camera Matrix

    hsVector3 up;
    hsPoint3 from, at;
    from.Set(0, 5.f, 1.f);
    at.Set(0, 20.f, 1.f);
    up.Set(0,0.f,1.f);
    hsMatrix44 cam;
    cam.MakeCamera(&from,&at,&up);

    hsMatrix44 id;
    id.Reset();

    pipe->SetWorldToCamera(cam, id); /* w2c, c2w */

Resulting matrix is (as represented in memory):

W2C: hsMatrix44[[1.0000, 0.0000, 0.0000, 0.0000]; [0.0000, 0.0000, 1.0000, -1.0000]; [0.0000, 1.0000, 0.0000, -5.0000]; [0.0000, 0.0000, 0.0000, 1.0000]]

GL Shader Code

#version 130

attribute vec3 position;
attribute vec4 color;

uniform mat4 matrix_l2w;    /* This is always identity */
uniform mat4 matrix_w2c;    /* See World-to-Camera above */
uniform mat4 matrix_proj;   /* See Screen Projection above */

varying vec4 v_color;

void main() {
    gl_Position = vec4(position, 1.0) * matrix_l2w * matrix_w2c * matrix_proj;
    v_color = color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment