PDA

View Full Version : Is my ortho projection correct



AeroSujan
11-02-2011, 10:37 AM
Dear Friends
I am using orthographic projection to render my 3d model. As I rotate the geometry the model gets distorted near the edeges at the top. Can u tell me anyone why is it happening. please check my code snippet.



void CRevolutionProjView::orthogonalStart()
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(left_side*AR, right_side*AR, top_side, bottom_side, near_side, far_side);
glScalef(zoomFactor,zoomFactor,1);
glTranslatef(trans[0], trans[1], trans[2]);
glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
glRotatef(rot[2], 0.0f, 0.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
}

void CRevolutionProjView::orthogonalEnd()
{
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}

void CRevolutionProjView::DrawScene(CDC *pDC)
{
wglMakeCurrent(pDC->m_hDC, m_hrc);
//---------------------------------
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
orthogonalStart();
glCallList(axes);
glCallList(object);
glPrintX("X");
glPrintY("Y");
glPrintZ("Z");
glFlush();
orthogonalEnd();
SwapBuffers(pDC->m_hDC);
wglMakeCurrent(NULL, NULL);
}

void CRevolutionProjView::Reshape(CDC *pDC, int w, int h)
{
AR = (double)w/(double)h;
wd = w;
ht = h;
wglMakeCurrent(pDC->m_hDC, m_hrc);
//---------------------------------
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (45, (GLfloat)w / (GLfloat)h, 0.1, 1000.0);
glMatrixMode (GL_MODELVIEW);
Invalidate();
wglMakeCurrent(NULL, NULL);
}


Is my projection correct ??

thokra
11-02-2011, 10:53 AM
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(left_side*AR, right_side*AR, top_side, bottom_side, near_side, far_side);
glScalef(zoomFactor,zoomFactor,1);
glTranslatef(trans[0], trans[1], trans[2]);
glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
glRotatef(rot[2], 0.0f, 0.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);


You're realising that your altering the projection matrix computed by glOrtho() when issuing any other matrix commands without switching back to GL_MODELVIEW, right? So, no, you're projection is not correct since you're messing it up right before drawing anything.

AeroSujan
11-02-2011, 11:09 AM
so what would be the right projection. to render my 3D model. The below things I have to render where object is a 3D model. Can u help please.

glCallList(axes);
glCallList(object);
glPrintX("X");
glPrintY("Y");
glPrintZ("Z");

biller23
11-02-2011, 12:19 PM
He actually answered your question.

With the command glScalef and so on, you're changing the projection setted by glOrtho. Switch to Modelview matrix before these commands or your projection matrix will be invalid.

Also, make sure that "left_side", "right_side", "top_side" and "bottom_side" are the same, or the aspect ratio will be not mantained.

AeroSujan
11-03-2011, 04:17 AM
My projection is not proper...is this correct below??



void CRevolutionProjView::DrawScene(CDC *pDC)
{
wglMakeCurrent(pDC->m_hDC, m_hrc);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(left_side*AR, right_side*AR, top_side, bottom_side, near_side, far_side);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glScalef(zoomFactor,zoomFactor,1);
glTranslatef(trans[0], trans[1], trans[2]);
glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
glRotatef(rot[2], 0.0f, 0.0f, 1.0f);
glCallList(axes);
glCallList(object);
glPrintX("X");
glPrintY("Y");
glPrintZ("Z");
glFlush();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
SwapBuffers(pDC->m_hDC);
wglMakeCurrent(NULL, NULL);
}

thokra
11-03-2011, 04:30 AM
Would you care to explain what you're actually trying to do? Scaling an orthographic projection matrix CAN be right depending on what you want. The translation is probably meant to offset you're object, the rotation definitely is supposed to rotate your object.

How do you control the zoom factor? What's the acutal problem now.

With a vague sentence like "My projection is not proper" there's not much one can do.

BionicBytes
11-03-2011, 07:00 AM
If you just want to draw a 3D model with the usual scale, rotate and tranlations (like 99% of the whole world does), then your code ordering is wrong w.r.t the scale command.
It should be:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(trans[0], trans[1], trans[2]);
glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
glRotatef(rot[2], 0.0f, 0.0f, 1.0f);
glScalef(zoomFactor,zoomFactor,1);


...although why you are not also zooming on the z axis too is another question.

AeroSujan
11-03-2011, 12:59 PM
Thanks a lot Bionic now its somewhat working fine. But while I am rotating the model its breaking near the top edge, distorting the model. I dont know why its happening , any idea ?? Thanks for the reply.

AeroSujan
11-03-2011, 01:19 PM
Now my drawscene and reshape are looking like this......Everything is fine with Bionic's, thokra and biller's comments. But when I am rotating the model its breaking at the top edge distoring. If I zoom out little but then if I rotate its fine nothing is happening. but if i zoom in make it large then distorting while rotating. I am also showing the code for ratation pan and zoom.....Please look at it.



void CRevolutionProjView::DrawScene(CDC *pDC)
{
wglMakeCurrent(pDC->m_hDC, m_hrc);
//---------------------------------
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (wd <= ht)
glOrtho(-range, range, -range*ht/wd, range*ht/wd, -range, range);
else
glOrtho(-range*wd/ht, range*wd/ht, -range, range, -range, range);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(trans[0], trans[1], trans[2]);
glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
glRotatef(rot[2], 0.0f, 0.0f, 1.0f);
glScalef(zoomFactor,zoomFactor,zoomFactor);
glCallList(object);
glCallList(axes);
glPrintX("X");
glPrintY("Y");
glPrintZ("Z");
glFlush();
glPopMatrix();
SwapBuffers(pDC->m_hDC);
wglMakeCurrent(NULL, NULL);
}

void CRevolutionProjView::Reshape(CDC *pDC, int w, int h)
{
AR = (double)w/(double)h;
wd = w;
ht = h;
wglMakeCurrent(pDC->m_hDC, m_hrc);
//---------------------------------
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glMatrixMode (GL_MODELVIEW);
wglMakeCurrent(NULL, NULL);
}

///////////////////////////////////////////////////////////////
void CRevolutionProjView::OnMouseMove(UINT nFlags, CPoint point)
{
CDC* pDC = GetDC();
wglMakeCurrent(pDC->m_hDC, m_hrc);
newP = point;
// TODO: Add your message handler code here and/or call default
int dx = oldP.x - newP.x;
int dy = newP.y - oldP.y;
switch(STATE)
{
case PAN:
{
trans[0] -= dx;
trans[1] -= dy;
Invalidate();
}
break;
case ZOOM:
{
if(newP.y < oldP.y)
{
zoomFactor /= (0.75);
}
else
{
zoomFactor *= (0.75);
}
Invalidate();
}
break;
case ROTATE:
{
rot[0] += (dy * 180.0f) / 500.0f;
rot[1] -= (dx * 180.0f) / 500.0f;
#define clamp(x) x = x > 360.0f ? x-360.0f : x < -360.0f ? x+=360.0f : x
clamp(rot[0]);
clamp(rot[1]);
Invalidate();

}
break;
}

oldP = newP;
CView::OnMouseMove(nFlags, point);
wglMakeCurrent(NULL,NULL);
}

AeroSujan
11-04-2011, 01:16 PM
Dear Friends Please give me suggestions why is it coming breaking the model. isn't my code correct ??
My drawscene and reshape functions are like this



void CRevolutionProjView::DrawScene(CDC *pDC)
{
wglMakeCurrent(pDC->m_hDC, m_hrc);
//---------------------------------
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (wd <= ht)
glOrtho(-range, range, -range*ht/wd, range*ht/wd, -range, range);
else
glOrtho(-range*wd/ht, range*wd/ht, -range, range, -range, range);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(trans[0], trans[1], trans[2]);
glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
glRotatef(rot[2], 0.0f, 0.0f, 1.0f);
glScalef(zoomFactor,zoomFactor,zoomFactor);
glCallList(object);
glCallList(axes);
glPrintX("X");
glPrintY("Y");
glPrintZ("Z");
glPopMatrix();
glFlush();
SwapBuffers(pDC->m_hDC);
wglMakeCurrent(NULL, NULL);
}

void CRevolutionProjView::Reshape(CDC *pDC, int w, int h)
{
AR = (double)w/(double)h;
wd = w;
ht = h;
wglMakeCurrent(pDC->m_hDC, m_hrc);
//---------------------------------
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode (GL_MODELVIEW);
wglMakeCurrent(NULL, NULL);
}



Please give some comment where it's till going wrong. ?? Thanks a lot for reply. Sujan

Dark Photon
11-05-2011, 02:09 PM
Dear Friends Please give me suggestions why is it coming breaking the model. isn't my code correct ??
What specifically is "breaking" and what have you done to try and diagnose the problem?

AeroSujan
11-06-2011, 11:24 AM
while I am zoomig to make it large and then rotating then its breaking at the top edge. I think its because of domain problem. Give me your suggestions. Thanks Sujan

Dark Photon
11-06-2011, 01:16 PM
Still not clear what "breaking" means. Post a picture. Main thing that jumps out to me is I don't see a VIEWING transform on the top of the MODELVIEW stack. Which is fine as long as you always want WORLD-SPACE to be EYE-SPACE.

Try:
glDisable ( GL_CULL_FACE );
glPolygonMode ( GL_FRONT_AND_BACK, GL_LINE );

to see your vertices and edges more clearly. Also consider:
glDisable( GL_LIGHTING );
glColor4f( 1,0,0,1);

AeroSujan
11-08-2011, 09:46 AM
check this file pic 1 is no distorsion and pic2 is distorted.....Please help me resolving this why is it happening......When I zoom in make the model large then its breaking at the top edge.

AeroSujan
11-08-2011, 10:40 AM
Thanks friends for your helps....my problem is solved. I needed to decraese the4 znear and increase zfar..That solved my problem. PLease help me in findijng bottom view and north aest and southeast view of my isometric view on my other threads. Thanks sujan