Problem with translate in z direction

Hi,
I have a simple draw line code …
I am drawing a line in VC++ MFC application.
void CSim_4_26_02View::draw3dobject()
{
glBegin ( GL_LINES);
glColor3f ( 0.0f, 1.0f, 1.0f ) ;
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(12.5f,0.0f,0.0f);
glEnd();
}
Now I am able to see the line.But the line is going out of the screen.
So what I want to do is to scale the screen so that I can see the whole line falling within the screen.

So what I thought that by translating in Z direction I will be able to see the whole line…
void CSim_4_26_02View::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

CView::OnLButtonDblClk(nFlags, point);

glTranslatef(-0.0f,0.0f,+0.9f);
draw3dobject();
}
Now whenever I click on the screen the line diappears…
It works when i translate only in x direction or y…
I have also tried changing the Z value of the translate function from + to - but to no avail…
So when i translate my line in z direction regardless of direction it disappears…
Can any body help me with it…what am i doing wrong…

Thanks in advance
aus

What are you setting your near clipping plane to? For example, if your near clipping plane is 1.0f, you won’t see the line being drawn at 0.9f.

I think part of your problem is structure of your openGL program; I am not a big on object-oriented programming but will try to give you an example of structure for drawing and you my have to translate a little more to OOP.

Also you must understand that you have to redraw all the objects on the screen every time you move an object. You should not be translating your object on a mouse button press, but changing a variable. Then during the Drawing of the screen, the variable change will then will make the line position updated.

Let’s create a few basic program objects:

  1. opengl initializes screen: Cview::Init_Opengl()
  2. Draw objects: Cview: :Draw_Screen()
  3. Draw a object: Cview: :Draw_Object()
  4. opengl object control: Cview::Object_Control()

Let’s create a basic object storage variable:

struct My_Object
{
float x,y,z;
float r,g,b
float *data;
}

Draw_Screen()
{
// Do all screen processing here.
for(x=0; x < object_total; x++) // process all the object on our screen.
{
Draw_Object( My_Object );
}
SwapBuffers();
}

Draw_Object( *My_Object )
{
glPushMatrix();
glColor3f(My_Object.r, My_Object.g, My_Object.b );
glTranslate(My_Object.x, My_Object.y, My_Object.z );

// Process Object data here.
My_Object.data

glPopMatrix();
}

Object_Control()
{

if ( On_left_button ) Update_Object( My_Object ); Change objects variables here.

}

Originally posted by aus79er:
[b]Hi,
I have a simple draw line code …
I am drawing a line in VC++ MFC application.
void CSim_4_26_02View::draw3dobject()
{
glBegin ( GL_LINES);
glColor3f ( 0.0f, 1.0f, 1.0f ) ;
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(12.5f,0.0f,0.0f);
glEnd();
}
Now I am able to see the line.But the line is going out of the screen.
So what I want to do is to scale the screen so that I can see the whole line falling within the screen.

So what I thought that by translating in Z direction I will be able to see the whole line…
void CSim_4_26_02View::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

CView::OnLButtonDblClk(nFlags, point);
glTranslatef(-0.0f,0.0f,+0.9f);
draw3dobject();
}
Now whenever I click on the screen the line diappears…
It works when i translate only in x direction or y…
I have also tried changing the Z value of the translate function from + to - but to no avail…
So when i translate my line in z direction regardless of direction it disappears…
Can any body help me with it…what am i doing wrong…

Thanks in advance
aus

[/b]

[This message has been edited by nexusone (edited 05-15-2002).]

Also, remember that opengl has the z axis running -ve into the screen. So, to translate the line away from you, you need:
glTranslatef(-0.0f,0.0f,-0.9f);
not
glTranslatef(-0.0f,0.0f,+0.9f);