Sign in to follow this  
ChugginWindex

Annoying problem with perspective projection

Recommended Posts

I've been trying to figure this out forever!! I've got a small cross-like shape that I am drawing to the screen perfectly fine using a VBO and index buffer. When I don't use any projection matrix at all the cross shows up perfectly fine. When I use a perspective matrix the cross disappears entirely. Even stranger, when I use a simple orthogonal projection the cross becomes a tiny spec, and only returns when I upscale the model matrix by about 200x. I've tried various tutorials and examples and nothing I try to fit into what I've got so far works in MY demo.

Relevant code:

VBO / Index data
[code]
struct Vertex {
GLfloat XYZW[4];
GLfloat RGBA[4];
};


//create some data for the vbo
Vertex Vertices[] =
{
{ { 0.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f } },
// Top
{ { -0.2f, 0.8f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } },
{ { 0.2f, 0.8f, 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } },
{ { 0.0f, 0.8f, 0.0f, 1.0f }, { 0.0f, 1.0f, 1.0f, 1.0f } },
{ { 0.0f, 1.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f } },
// Bottom
{ { -0.2f, -0.8f, 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } },
{ { 0.2f, -0.8f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } },
{ { 0.0f, -0.8f, 0.0f, 1.0f }, { 0.0f, 1.0f, 1.0f, 1.0f } },
{ { 0.0f, -1.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f } },
// Left
{ { -0.8f, -0.2f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } },
{ { -0.8f, 0.2f, 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } },
{ { -0.8f, 0.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 1.0f, 1.0f } },
{ { -1.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f } },
// Right
{ { 0.8f, -0.2f, 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } },
{ { 0.8f, 0.2f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } },
{ { 0.8f, 0.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 1.0f, 1.0f } },
{ { 1.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f } }
};

//create some indices
unsigned int Indices[] = {
// Top
0, 1, 3,
0, 3, 2,
3, 1, 4,
3, 4, 2,
// Bottom
0, 5, 7,
0, 7, 6,
7, 5, 8,
7, 8, 6,
// Left
0, 9, 11,
0, 11, 10,
11, 9, 12,
11, 12, 10,
// Right
0, 13, 15,
0, 15, 14,
15, 13, 16,
15, 16, 14
};
[/code]


How I create my projection matrix
[code]

void Renderer::enableProjectionMode(ProjectionMode _mode) {
switch(_mode) {
case Perspective:
myProjectionMatrix = glm::perspective(
myViewportFOV,
(float)myViewportDimensions.x / (float)myViewportDimensions.y,
-1.0f, 100.0f);
break;
case Orthogonal:
myProjectionMatrix = glm::ortho(
-myViewportDimensions.x / 2, myViewportDimensions.x / 2,
myViewportDimensions.y / 2, -myViewportDimensions.y / 2,
-1.0f, 1.0f);
break;
}

myProjectionMode = _mode;
}[/code]


How I update the matrices in the shaders
[code]myProgram->bind();
myProgram->setUniformMatrix4fv("ProjectionMatrix", 1, false, &myProjectionMatrix[0][0]); //these are all just simple wrappers, they do the same thing as the underlying GL calls
myProgram->setUniformMatrix4fv("ModelMatrix", 1, false, &myModelMatrix[0][0]);
myProgram->setUniformMatrix4fv("ViewMatrix", 1, false, &myViewMatrix[0][0]);[/code]


I can post the shaders if it really is necessary, but they're unbelievably simple. You could probably guess the contents [img]http://public.gamedev.net/public/style_emoticons/default/biggrin.gif[/img]

Am I missing something in the above that's causing the projection matrix to not be created correctly? I've been looking for hours and can't find a single thing.

Share this post


Link to post
Share on other sites
The coordinates you specified are in pixels, when using a standard orthogonal matrix.
That means the object is barely a pixel wide. Of course it is going to be a tiny spec. Fix the coordinates or add a scaling matrix.

Using a perspective matrix can cause the vertex winding to change. Disable back-face culling to be sure.
It also causes the camera to look down -Z. Most likely your cross is behind the camera.


L. Spiro

Share this post


Link to post
Share on other sites
You can't just use fundamentally different projection types and expect similar behavior. You have fundamentally different projection matrices, and you get fundamentally different results; that is exactly what is expected to happen. You need to understand what each projection matrix really represent, and how it interacts with your scene. Let's go over your three cases:

I am, for simplicity, going to assume the model and view matrices are not set, unless otherwise stated in my points below.
[list=1][*]No projection matrix. This is equivalent to having a visible coordinate system which ranges from -1 to 1 along all three axes. So, covering your viewport is a coordinate system ranging from -1 to 1 on all axes, and your cross is on this scale as well. That's fine, the cross covers roughly the whole visible coordinate system, and consequently it will also cover roughly the whole viewport when drawn. What you get is a big cross.[*]Orthographic projection. You set the visible coordinate system to range from -viewport/2 to viewport/2, probably meaning that each unit now exactly corresponds to a pixel. Your cross is on the order of 2 units, and since each unit covers one pixel, your cross will be on the order of 2 pixels when drawn.[*]Perspective matrix. Let's assume, for simplicity of calculation, that your field of view is 90 degrees, and that the aspect ratio is 1; your reality is most likely not going to affect this reasoning to any greater extent if the values are not correct. First of all, having a near plane value of -1 may be accepted as a value itself in the particular formulas, but it doesn't make any sense mathematically. It has to be greater than zero. But let's assume you're setting the near plane to 1 instead and that there are no other transformations in the model and the view matrices. The object is then centered at Z=0, but the visible depth range is from -1 to -100, so no object is not visible at all.
You therefore need to translate the object along the negative Z-axis. Let's say you translate it by -1 so it ends up on the near plane. It will the, with a FOV of 90 degrees, cover most of the viewport. Why? At 90 degrees, and a near plane to 1, means that at Z=-1, the visible X- and Y-axes range from -1 to 1. Your cross is on that scale, just like in the first point above, and is drawn large. Translate it to Z=-2 instead, and the visible X- and Y-axes on that depth is from -2 to 2; your cross is still the same size, but the visible coordinate range is twice as large, so your cross appears with half its size as it did at X=-1. Just like perspective is meant to do, the object becomes smaller the further away it is.[/list]Three entirely different projections, three entirely different results. You cannot just swap matrices that are fundamentally different, nor matrices of the same type but with vastly different parameters, and expect comparable behavior. You did change the matrices, and the results were fundamentally different every time; nothing strange at all with that.

You need to read up on what the different matrices offer, and how they work. See the forum FAQ and look for a link to the Red Book for example. It has an interesting chapter on viewing.

Share this post


Link to post
Share on other sites
Oh man you guys are the best! I probably should have waited till the morning as it was late when I originally asked the question and didn't think it through properly. I've taken classes on this stuff at school and BELIEVE that I understand how projection matrices can differ, but I didn't recognize the error in having my perspective near-plane set to a negative. Changing that -1 to a 1 solved all of my problems.

Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this