View Full Version : GLSL & Matrix Math

12-29-2012, 10:13 PM
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.

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

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

12-29-2012, 10:49 PM
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.

12-29-2012, 11:04 PM
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.

12-30-2012, 01:07 AM
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.

12-30-2012, 07:28 AM
You need a projection matrix. For ortho, try
[1, 0, 0, 0]
[0, 1, 0, 0]
[0, 0, -1, 0]
[0, 0, 0, 1]