Skip to main content
GameDev.net gamedev.net
🔒 Locked

Projection matrix problems

Started by zix99 Dec 17, 2004 at 6:57 PM 7 replies 900+ views
Original Post
zix99
zix99
Hi all again, back to doing 3D manually... My original post here if you need to read that. I wrote a small program to draw a grid, (-25 to 25, -25 to 25, -2) with camera at (0,0,0), no rotation. I draw this grid, but it only outputs one little dot in the center of the screen, and i'm 99% sure it has something to do with my projection matrix. According to Drillan, from the previous post, projection matrix should look like
w       0       0               0
0       h       0               0
0       0       zf/(zf-zn)      1
0       0       -zn*zf/(zf-zn)  0
where:
h is the view space height. It is calculated from h = cot(fovY/2);
w is the view space width. It is calculated from w = h * aspect   (where aspect = screenWidth/screenHeight)
zf = farthest visible distance, zn = nearest visible distance

void TGBUFFER::wProjection(float fovy, float aspect, float zn, float zf){
	mProj=MatrixIdentity();
	mProj.m22=(1.0 / tan(fovy/2.0));
	mProj.m11=mProj.m22 * aspect;
	mProj.m33=zFar/(zFar-zNear);
	mProj.m43=-zNear*zFar/(zNear-zFar);
	mProj.m34=1;
	mProj.m44=0;
}


One thing that might be going wrong is i'm using radians instead of dregrees? (although when i looked in the DX SDK it seemed like it was using radians, but i'm not sure) Any help would be nice Thanks all again ~zix~
nts
nts
Looked at your matrix and compared to mine from my project, mine looks like

radians = fovY / (2 * PI_OVER_180);matrix[ 0] = (cos(radians) / sin(radians)) / fAspect;matrix[ 5] = (cos(radians) / sin(radians)) ;matrix[10] = -(fFar + fNear) / (fFar - fNear);matrix[11] = -1;matrix[14] = -2 * fNear * fFar / (fFar - fNear);matrix[15] = 0;


Hopefully didn't make a mistake with the above, replaced some variables/code to make it look more readable :p

Sorry couldn't be of more help, in a little hurry

HTH
zix99
zix99
Not much sucess

Here is all my source for drawing a 3D Vector (vertex point)
void TGBUFFER::dDrawVector(float x, float y, float z){	//convert to 2D screen coord and depth	Vector4 vec;	vec.x=x; vec.y=y; vec.z=z; vec.w=1;	int screenX, screenY;	float depth;	/*vec=MatrixVectorMultiply(mCam, vec);	vec=MatrixVectorMultiply(mProj, vec);*/	Matrix44 mat=MatrixIdentity();	mat=MatrixMultiply(mCam, MatrixFromVector(vec));	mat=MatrixMultiply(mProj, mat);	vec=VectorFromMatrix(mat);	if (vec.z==0)		return;	vec.x/=vec.w;	vec.y/=vec.w;	vec.z/=vec.w;	vec.w=1;		screenX=(vec.x+1.0)/2.0 * width;	screenY=(vec.y+1.0)/2.0 * height;	depth=vec.z;	//check bounds	if (screenX<0 || screenX>=width || screenY<0 || screenY>=height || depth<=0)		return;	//write pixel to buffer	cbits[4*(screenY*width+screenX)+0]=1.0;	cbits[4*(screenY*width+screenX)+1]=1.0;	cbits[4*(screenY*width+screenX)+2]=1.0;}
nts
nts
are you using Direct3D or OpenGL, my code came from an OpenGL project which i doubt will work "as is" since D3D uses a right handed coord system while OpenGL uses a left handed one. You can flip the system but not sure if that will do it, not too familiar with D3D or how it handles matricies. Sorry couln't be of more help.
silvermace
silvermace
Quote:
Original post by nts
are you using Direct3D or OpenGL, my code came from an OpenGL project which i doubt will work "as is" since D3D uses a right handed coord system while OpenGL uses a left handed one. You can flip the system but not sure if that will do it, not too familiar with D3D or how it handles matricies. Sorry couln't be of more help.
err.. that looks like a software rasterizer... :) but nts's point about the handedness of the matricies is valid, if you're a GL coder, node that OpenGL matricies are COLUMN major and they are LEFT handed. D3D is ROW major and RIGHT handed. make sure you dont mix the two.
zix99
zix99
I had originally programmed with a D3D matrix, because that is what i was given in the previous thread. Sense i know that is an OpenGL matrix now, i think i will use that because i'm more use to OpenGL to begin with, so that will help me in the future.

I'm beganning to think it's my math someplace, what i need to do is sit down and do all the equations manually and see what the program has.. and i will as soon as i have time. I must have 4 school projects and tests abundent right now.. but christmas break is soon.. so that's plenty of time.

Thanks again, I'm sure you'll see my thread again soon [wink]
~zix~

PS: I'm making my own software rendering engine, i want to test out a theory i have about rendering photo-realistic scenes, but i need to get the basic of drawing 3D down first w/o using a library because i need total control of everything.
zix99
zix99
Well.. i figured out where i'm going wrong to some extent. It seems that my vector has to be in the range of [-1 to 1, -1 to 1, -1 to 1, 1]... but let's say i want to draw a point [25,25,0,1], how would i do this?

I see two possibilities. First is to scale the vertex point to get it to those bounds, or two edit the perspective matrix.
Simply editing the projection matrix would be better, one less matrix the point has to be multiplied by... but how would i know how to transform it?

I found the matrix, with right, left, top and bottom.. but how would i change these variables?

Thanks
~zix~

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.