PDA

View Full Version : GLSL moving an object with projection,view and transformation matrix



ED_RockStarGuy
06-17-2016, 03:36 AM
I have been trying to get a simple triangle to render on the screen but an unable to using the Projection, View and Transformation Matrix's but an unable to get anything to the screen.

Main loop


...
shader->start();

Matrix4f transformation = Math::createTransformationMatrix(Vec3<float>(0,0,-25),0,0,0, 1);
shader->loadTransformationMatrix(transformation);

Matrix4f camera = Math::createViewMatrix(Vec3<float>(0, 0, 0), 1, 0, 0);
shader->loadViewMatrix(camera);

Matrix4f projection = Math::getProjectionMatrix(1080,720);
shader->loadProjectionMatrix(projection);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);

shader->stop();
...
Math.h


static Matrix4f createTransformationMatrix(Vec3<float> translation, float rx, float ry, float rz, float scale) {
Matrix4f matrix;
Matrix4f::translate(translation, matrix,matrix);
Matrix4f::rotate(toRadians(rx), Vec3<float>(1, 0, 0), matrix, matrix);
Matrix4f::rotate(toRadians(ry), Vec3<float>(0, 1, 0), matrix, matrix);
Matrix4f::rotate(toRadians(rz), Vec3<float>(0, 0, 1), matrix, matrix);
Matrix4f::scale(Vec3<float>(scale, scale, scale), matrix, matrix);
return matrix;
}
static Matrix4f createViewMatrix(Vec3<float> pos, float pitch, float yaw, float roll) {
Matrix4f matrix;
Matrix4f::rotate(toRadians(pitch), Vec3<float>(1, 0, 0), matrix, matrix);
Matrix4f::rotate(toRadians(yaw), Vec3<float>(0, 1, 0), matrix, matrix);
Matrix4f::rotate(toRadians(roll), Vec3<float>(0, 0, 1), matrix, matrix);

Vec3<float> negativeCam(-pos.x, -pos.y, -pos.z);
Matrix4f::translate(negativeCam, matrix, matrix);

return matrix;
}
static Matrix4f getProjectionMatrix(int width, int height) {
float aspectRatio = (float)width / (float)height;
float y_scale = (float)((1 / tan(toRadians(FOV / 2)))*aspectRatio);
float x_scale = y_scale / aspectRatio;
float frustum_length = FAR_PLANE - NEAR_PLANE;

Matrix4f projectionMatrix;
projectionMatrix[0] = x_scale;
projectionMatrix[5] = y_scale;
projectionMatrix[10] = -((FAR_PLANE + NEAR_PLANE) / frustum_length);
projectionMatrix[11] = -1;
projectionMatrix[14] = -((2 * NEAR_PLANE * FAR_PLANE) / frustum_length);
projectionMatrix[15] = 0;
return projectionMatrix;
}
Vertex Shader


#version 330 core

layout(location = 0) in vec3 position;

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;

void main() {
//Works but renders without any camera or projection applied
//gl_Position.xyz = position;
//gl_Position.w = 1.0;

vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
vec4 positionRelitiveToCam = viewMatrix * worldPosition;
gl_Position = projectionMatrix * positionRelitiveToCam;
}

john_connor
06-17-2016, 10:04 AM
step 0: rendering without uniform matrices works

step 1: apply matrices, give the a default value, and test it:


uniform mat4 transformationMatrix = mat4(1);
uniform mat4 projectionMatrix = mat4(1);
uniform mat4 viewMatrix = mat4(1);


if that works, then try uploading just a simple transformation, e.g. a movement to x = 0.5 (to the right)

if that workt, then try uploading just a simple camera position, e.g. a movement upwards y = 0.5

if that works, then test your projection matrix

ED_RockStarGuy
06-17-2016, 12:43 PM
step 0: rendering without uniform matrices works

step 1: apply matrices, give the a default value, and test it:


uniform mat4 transformationMatrix = mat4(1);
uniform mat4 projectionMatrix = mat4(1);
uniform mat4 viewMatrix = mat4(1);


if that works, then try uploading just a simple transformation, e.g. a movement to x = 0.5 (to the right)

if that workt, then try uploading just a simple camera position, e.g. a movement upwards y = 0.5

if that works, then test your projection matrix


When I use your shader code it works but when I enable the old transformation matrix it stops working once again

Could it be to do with how I load the Matrix into the shader?



void ShaderProgram::loadMatrix(GLint location, Matrix4f matrix) {
glUniformMatrix4fv(location, 1, false,matrix.get());
}

Matrix4f get()


inline const float* Matrix4f::get() const{
return identitys;
}

GClements
06-17-2016, 02:02 PM
Could it be to do with how I load the Matrix into the shader?

Possibly. OpenGL wants matrices in column-major order by default, so you need to set transpose to true (or GL_TRUE) if you're storing them in row-major order.

If that isn't the issue, then I suggest printing out the actual matrix data being passed to OpenGL to check that it's correct.

ED_RockStarGuy
06-20-2016, 08:10 AM
Possibly. OpenGL wants matrices in column-major order by default, so you need to set transpose to true (or GL_TRUE) if you're storing them in row-major order.

If that isn't the issue, then I suggest printing out the actual matrix data being passed to OpenGL to check that it's correct.


Neither orientation seems to work. After some further review this seems to be more of a bug with my GPU or its software as it works as expected on my laptop.

I shall debug this issue further and see what I find.