PDA

View Full Version : Rendering Axis in Ortho mode



soconne
07-13-2004, 07:59 PM
I'm trying to figure out how to render Axis in an editor I'm working on. I want to be able to display the axis for an object when the user clicks on the object, but for some reason I cannot get the axis to position correctly when the camera is moving around. Basically I set up my rendering like so...

SetUpPerspectiveProjection()
RenderModels()

if(ModelIsSelected) {
glOrtho(...)
glTranslatef(selectedmodel.x, selectedmodel.y, selectedmodel.z);
RenderAxis();
}

Just assume I called the appropriate glMatrixMode stuff already. What I'm confused about is what to set glOrtho parameters to. Right now I set them to (-10,10,-10,10,0.1,1000) and the Axis being rendered have a length of 1. But when I move the camera up or down or look to the side, the axis swerve way off away from the model.

How can I get the axis to always stay exactly where the model is?

Also, the reason I'm trying to do this with glOrtho is that I want the axis to stay the same size in the window no matter how far away the model is. I'm basically trying to reproduce the gizmo 3dsmax has while you're editing.

Kilam Malik
07-14-2004, 12:53 AM
Why don't you render the axis of the object in the same projection? Also, you had no rotation in your code in the ortho projection.
Could you post a picture of how it should look like? I have an idea of how to solve but I'm not sure we're talking of the same result...

Kilam.

soconne
07-14-2004, 03:05 AM
The reason I have to render it in glOrtho is because I need the size of the axis to not be dependent on the distance away from the camera. Here is the code:



camera.Look();

glPushMatrix();
glTranslatef(posX, posY, posZ);
glRotatef(rotY, 0, 1, 0);
glRotatef(rotX, 1, 0, 0);
glRotatef(rotZ, 0, 0, 1);
glScalef(scX, scY, scZ);
RenderModel();
glPopMatrix();


if(ModelSelected) {

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0, 10.0, -10.0, 10.0, 0.1, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

camera.Look();

glTranslatef(posX, posY, posZ);

glLineWidth(3);
glBegin(GL_LINES);
glColor3f(1,0,0);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glColor3f(0,1,0);
glVertex3f(0,0,0);
glVertex3f(0,0,1);
glColor3f(0,0,1);
glVertex3f(0,0,0);
glVertex3f(0,1,0);
glEnd();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70, aspect, 0.5, GeometryClipDepth);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
} Here are some screenshots of what it looks like:
pic1 (http://cs.selu.edu/~soconnell/pic1.JPG)
pic2 (http://cs.selu.edu/~soconnell/pic2.JPG)
pic3 (http://cs.selu.edu/~soconnell/pic3.JPG)
pic4 (http://cs.selu.edu/~soconnell/pic4.JPG)
pic5 (http://cs.selu.edu/~soconnell/pic5.JPG)

Kilam Malik
07-14-2004, 04:46 AM
In your screenshots I can see a rotation of the coordinate system, but I don't see it in your code. There is a rotation before the RenderModel(), but not before the coordinatesystem drawing.

Nevertheless, here is a different solution. The problem when you mix orthogonal and perspective is, that the axis will never look correct. So you should use
perspective view for them too. When I understood you correct, the only problem you have is the length of the coordinate system? I'm working on a CAD-modelling system and have to show some guiding tetrahedrons sometimes which I want to have always the same size in pixels. I calculate this by converting screen coordinates back to object coordinates. This is done with the gluUnproject function. Here is a short sample:

void OGLPane3D::UnProject(Point2D pIn, Point3D &pOut)
{
GLdouble x, y;
GLdouble ox = 0, oy = 0, oz = 0;

x = pIn.x();
y = drawSizeY - pIn.y();

GLdouble emat[] ={1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1};
GLdouble z = 1.0;
gluUnProject(x, y, z, emat, projMatrix, viewport, &ox, &oy, &oz);

pOut.x(ox);
pOut.y(oy);
pOut.z(oz);
}

double OGLPane3D::GetObjFromPixels(int pixels)
{
Point3D a1, a2;

UnProject(Point2D(0, 0), a1);
UnProject(Point2D(0, pixels), a2);

return (a2 - a1).Length();
}

GetObjFromPixels will return you the size of x pixels in your object space. Now just render the coordinate system with that size.

Kilam.

Kilam Malik
07-14-2004, 04:54 AM
Forgot something... you should pass the z value of your object in the gluunproject and not 1.0 like I do it. But this is not the z-value that you use, it is the zbuffer z-value. You get it with the gluproject function.

Kilam.

soconne
07-14-2004, 08:14 AM
You're the MAN Kilam !!!

It works perfectly now, thanks a lot :-)