Need quick help with OpenGL glRotate!

Started by
4 comments, last by mshafrir 20 years, 9 months ago
I am writing an app in C# (VS.NET). I am using CsGL for some basic OpenGL stuff. I want to draw an x,y, and z axes in the control, and then rotate the view so that it looks 3D - if you draw the axes and don't rotate them, you wouldn't be able to see the Z axis. Here's my code, which I based mainly on (http://www.componentsnotebook.com/notebooks/csharp/csgl.aspx). You will see that I called GL.glRotatef(1.0f, 1.0f, 1.0f, 0.0f); at one point. I picked 1.0 for the angle arbitrarily; however, no matter what angle I choose, it only seems to make the axes smaller. Please help, and if there are any questions ask away!

protected override void InitGLContext() 
{		
   GL.glClearColor( 1.0f, 1.0f, 1.0f, 0.0f );
   GL.glColor3f( 0.0f, 0.0f, 0.0f );
   GL.glPointSize( 4.0f );
}

protected override void OnSizeChanged(EventArgs e)
{
   base.OnSizeChanged(e);
   GL.glMatrixMode(GL.GL_PROJECTION);
   GL.glLoadIdentity();
   GL.glOrtho( 0.0, Size.Width, 0.0, Size.Height, 1.0, -1.0 );
}

public override void glDraw() 
{
   GL.glRotatef(1.0f, 1.0f, 1.0f, 0.0f);
   drawAxis();				
}

private void drawAxis() 
{
   double left = 0.05 * Size.Width;
   double right = 0.95 * Size.Width;
   double bottom = 0.05 * Size.Height;
   double top = 0.95 * Size.Height;
   double origin_x = Size.Width / 2;
   double origin_y = Size.Height / 2;

   // draw X axis

   GL.glBegin( GL.GL_LINES );
   GL.glVertex3d( left, origin_x, 0 );
   GL.glVertex3d( right, origin_x, 0 );
   GL.glEnd();

   // draw Y axis

   GL.glBegin( GL.GL_LINES );
   GL.glVertex3d( origin_y, bottom, 0 );
   GL.glVertex3d( origin_y, top, 0 );
   GL.glEnd();	

   // draw Z axis

   GL.glBegin( GL.GL_LINES );
   GL.glVertex3d( origin_x, origin_y, 0.0 );
   GL.glVertex3d( origin_x, origin_y, 1.0 );
   GL.glEnd();	

   GL.glFlush();
}
Advertisement
Are you setting the matrix mode back to whatever it was before you changed it, which was likely GL_MODELVIEW?
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
quote:Original post by smart_idiot
Are you setting the matrix mode back to whatever it was before you changed it, which was likely GL_MODELVIEW?


Well I''m new to OpenGL, so I''m really not sure what you''re asking or what I have to do. The code I''m showing is everything that deals with OpenGL, so what you see is what you get. Could you explain what I should be doing? Thanks!
Okay. . .

In OpenGL whenever you change something, it stays that way until you change it again. That includes colours, the matricies, and pretty much everything else.

If you don''t change back to the modelview matrix, then you will end up rotating the projection matrix, which isn''t what you wanted.

So basically what I''m saying is to change the function OnSizeChanged to this:

protected override void OnSizeChanged(EventArgs e){   base.OnSizeChanged(e);   GL.glMatrixMode(GL.GL_PROJECTION);   GL.glLoadIdentity();   GL.glOrtho( 0.0, Size.Width, 0.0, Size.Height, 1.0, -1.0 );   GL.glMatrixMode(GL.GL_MODELVIEW);}
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Perhaps these images will help explain my problem.

public override void glDraw() {	   //GL.glRotatef(10f, 0.71f, 0.71f, 0);   drawAxis();				}


The following is the picture of the unrotated axes:
http://www.terptrader.com/normalaxis.png

........

When I include the glRotate function:

   public override void glDraw() {	   GL.glRotatef(10f, 0.71f, 0.71f, 0);   drawAxis();				}


I get the following image:
http://www.terptrader.com/rotatedaxis.png

Can anyone explain to me what is going on? My goal is to have the axes look 3D, like in this picture: http://wonkosite.ausbone.net/vrml/axis.gif

[edited by - mshafrir on July 7, 2003 3:03:52 PM]
^

This topic is closed to new replies.

Advertisement