PDA

View Full Version : MVP Matrix issue



BinaryFissionGames
10-17-2014, 06:46 AM
Hey, I'm having a problem getting my program to properly display an image. I think the problem in my code is either in my matrix code, my uploading of the code, or the matrices themselves.

My current output is just the clear color, and when I remove the model matrix, I can get a white color from the object on my screen, possibly from clipping with the object. I can rotate my camera around there, and I see parts of the object. It simply seems that my zfar is like half a unit past znear, which doesn't make any sense.

First off, here's my vertex shader.


#version 330 core
layout(location = 0) in vec4 posIn;
layout(location = 1) in vec4 colorIn;
layout(location = 2) in vec4 normalIn;

uniform mat4 modelMat;
uniform mat4 projectionMat;
uniform mat4 viewMat;

out vec4 colorOut;

void main(){
gl_Position = projectionMat*viewMat*modelMat*posIn;
colorOut = colorIn;
}


The code to upload is dispersed through the program, but I'm doing all of them similar to this line.

glUniformMatrix4fv(locationView, 1, GL_FALSE, cam->getBuffer());

And the biggest problem that I think I have is my matrix code. I can't figure out how to represent the matrix as a float buffer.

Do I represent a simple translation matrix as

{ // first row
1,0,0,transX,
//second row
0,1,0,transY,
//third row
0,0,1,transZ,
//fourth row
0,0,0,1
}
Or do I represent it as strips of columns? (I have trouble understanding "Row-major" and "Column-major", Is what I did above row-major?)

And the final thing that I think could really be wrong are my stored data in my matrices. From what I can gather, however, they're fine.



Projection:
[1.357995][0.000000][0.000000][0.000000]
[0.000000][2.414213][0.000000][0.000000]
[0.000000][0.000000][-1.002002][-0.200200
[0.000000][0.000000][-1.000000][0.000000]

View:
[1.000000][0.000000][0.000000][0.000000]
[0.000000][1.000000][0.000000][0.000000]
[0.000000][0.000000][1.000000][0.000000]
[0.000000][0.000000][0.000000][1.000000]

Model:
[1.000000][0.000000][0.000000][0.000000]
[0.000000][1.000000][0.000000][0.000000]
[0.000000][0.000000][1.000000][-5.000000]
[0.000000][0.000000][0.000000][1.000000]


Now that I think about it, it COULD be my projection code, so here it is .


Matrix* getProjection(float fov, float aspect, float znear, float zfar){

Matrix *m = new Matrix(4);

float top, bottom, right, left;

top = znear*tan((PI/180.0)*fov*0.5);
bottom = -(top);
right = (top)*aspect;
left = -(right);
// row1
// Set value set's a value in the matrix, viewing the top left as 0,0 , top right as 4,0 , bottom left as 0,4, and bottom right as 4,4, which I realize may be different than how matrices are usually represented
m->setValue(0,0,(2*znear)/((right)-(left)));
m->setValue(2,0, ((right)+(left))/((right)-(left)));
// row 2
m->setValue(1,1, (2*znear)/((top)-(bottom)));
m->setValue(2, 1, ((top)+(bottom))/((top)-(bottom)) );
//row 3

m->setValue(2,2, -1.0*(zfar+znear)/(zfar-znear));
m->setValue(3,2, -1.0*(2.0*zfar*znear)/(zfar-znear));
//row 4
m->setValue(2,3, -1.0);
return m;


Seriously, thanks to anyone who's willing to help me.

GClements
10-17-2014, 08:27 AM
Do I represent a simple translation matrix as

{ // first row
1,0,0,transX,
//second row
0,1,0,transY,
//third row
0,0,1,transZ,
//fourth row
0,0,0,1
}
Or do I represent it as strips of columns? (I have trouble understanding "Row-major" and "Column-major", Is what I did above row-major?)
The above is row-major. By default, OpenGL assumes column-major (i.e. the last four values should be transX,transY,transZ,1). But you can set the transpose parameter to glUniformMatrix* to GL_TRUE if you want to pass a row-major matrix.





Projection:
[1.357995][0.000000][0.000000][0.000000]
[0.000000][2.414213][0.000000][0.000000]
[0.000000][0.000000][-1.002002][-0.200200
[0.000000][0.000000][-1.000000][0.000000]

That's a perfectly sane perspective matrix:corresponding to e.g. gluPerspective(45,16./9,0.1,100). Although, 45 degrees is a bit narrow (the 45 degree angle is from the top of the view to the bottom, so the range is +/- 22.5 degrees either side of the horizon).

View is an identity matrix.

Model is a translation of (0,0,-5), i.e. 5 units in front of the viewer.

So I suspect that the problem is that your matrices are transposed.

BinaryFissionGames
10-17-2014, 08:49 AM
So I suspect that the problem is that your matrices are transposed.

Thank you so much, this was the problem.

Ruggero Visitnin
10-17-2014, 02:41 PM
EDIT: sorry, was the wrong thread;