Jump to content

Projection Camera Matrix


theDream
 Share

Recommended Posts

Same as GLUT does it:

//Make orthogonal projection matrix
	void Mat4::Ortho(const float left, const float right, const float top, const float bottom, const float znear, const float zfar)
	{
		i.x = 2.0f/(right-left);
		i.y = 0.0f;
		i.z = 0.0f;
		i.w = 0.0f;
		j.x = 0.0f;
		j.y = 2.0f/(top-bottom);
		j.z = 0.0f;
		j.w = 0.0f;
		k.x = 0.0f;
		k.y = 0.0f;
		k.z = 2.0f/(zfar-znear);
		k.w = 0.0f;
		t.x = -(right+left)/(right-left);
		t.y = -(top+bottom)/(top-bottom);
		t.z = -(zfar+znear)/(zfar-znear);
		t.w = 1.0f;
	}

void Mat4::Perspective(const float left, const float right, const float bottom, const float top, const float znear, const float zfar)
	{
		float A = (right+left)/(right-left);
		float B = (top+bottom)/(top-bottom);
		float C = -(zfar+znear)/(zfar-znear);
		float D = -(2.0*zfar*znear)/(zfar-znear);

		i.x = (2.0*znear)/(right-left);
		i.y = 0;
		i.z = 0;
		i.w = 0;
		j.x = 0;
		j.y = (2.0*znear)/(top-bottom);
		j.z = 0;
		j.w = 0;
		k.x = A;
		k.y = B;
		k.z = C;
		k.w = -1;
		t.x = 0;
		t.y = 0;
		t.z = D;
		t.w = 0;
	}

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Thanks for the reply. I am having some issues calculating left, right, top, bottom. What I have researched says that  (bottom = -top).

But in your B calculation and the ones I have seen, top and bottom are added together. To me (top + bottom) should equal 0. But that is obviously not the case since there would be no need to calculate B since it would always be 0.

And thank you again for the help, I appreciate it.

Link to comment
Share on other sites

I don't really understand the math, I just copied the code from the explanation in the OpenGL documentation.

In GL, the bottom/top might be the reverse of what you are expecting..

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...