PDA

View Full Version : perspective issue (transformation along Z axis)



Astroman
04-09-2008, 12:43 PM
Hi,

I read the Viewing chapter in the Red Book and searched the internet for examples, but am still having this issue... so I think somewhere along the lines I am missing the point, I'm hoping someone can show me where.

My issue is simple, I want to draw a square (a cube really, but I will settle for getting to work with a square first) and I want to be able to transform it along the Z-axis with perspective. Such that when I transform it in the negative Z it appears to get smaller and when I transform it in the positive Z it gets bigger, etc. Simple stuff. This does not seem to happen though, I can transform along the X and Y ok, the square moves accordingly, but transforming along the Z either has no visual effect or the square dissappears altogether.

Here is the code snippet:

void DrawQuad(float x1, float y1, float x2, float y2, float z1, float z2, float fWrapCount, GLuint uiTexture)
{
// Use the given texture
glBindTexture(GL_TEXTURE_2D, uiTexture);

// Pass the vertex data
VERTTYPE afVertices[] = {f2vt(x1),f2vt(y1),f2vt(z1), f2vt(x2),f2vt(y1),f2vt(z2), f2vt(x1),f2vt(y2),f2vt(z1), f2vt(x2),f2vt(y2),f2vt(z2)};
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,VERTTYPEENUM,0,afVertices);

// Pass the texture coordinates data
VERTTYPE afTexCoord[] = {f2vt(0),f2vt(0), f2vt(fWrapCount),f2vt(0), f2vt(0),f2vt(fWrapCount), f2vt(fWrapCount),f2vt(fWrapCount)};
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2,VERTTYPEENUM,0,afTexCoord);

// Draws an indexed triangle list
unsigned short aIndices[] = {0,1,2, 2,1,3};
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, aIndices);
}

bool RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

static float translate1 = 0.0f;
static float translate2 = 0.0f;
static float translate3 = 0.0f;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
myglFrustum(-1.0, 1.0, -1.0, 1.0, 0.0, 10.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
myglTranslate(translate1,translate2,translate3);

DrawQuad(-0.5, -0.5, +0.5, +0.5, -0.5, -0.5, 1, m_uiBackgroundTexture);

myglFlush();

return true;
}

int main(void)
{
//overly simplified illustration of what main does.
while(RenderScene());
}


Now when I run it as is.. I see my textured square in the middle of the screen about half the size. If I change the translate3 value to -1.0...nothing happens. If I change it to -2.0, the square is gone. I don't understand this given my projection matrix should go from 0 to -10, not to mention that the square doesn't change visual "size".

Any help is appreciated here.

-NiCo-
04-09-2008, 01:27 PM
Setting the near plane distance to 0 will produce undefined results because the left, right, bottom and top coordinates are located at the near plane. So in your case you will have a theoretically infinite opening angle of the camera.

Try using the gluPerspective call for setting up a perspective projection matrix more easily.

Astroman
04-09-2008, 02:22 PM
That appears to have done it. I was sure I tried that before. Thanks alot!