• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need help with OpenGL + C#

JonTheBaller

Golden Member
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(10.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!

Btw, this is how the axes look before I rotate: http://www.terptrader.com/normalaxis.png
And after I rotate: http://www.terptrader.com/rotatedaxis.png
My goal is to have the axes look 3D, like in this picture: http://wonkosite.ausbone.net/vrml/axis.gif

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(10.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();
}

 
It's been a while since I've played with OpenGL, but shouldn't you be clearing the color and depth buffers and then loading the identity matrix? If you want to be able to see the Z axis, you should only have to do rotate it like so:

glRotate(30, 0.0, 1.0, 0.0)

which should rotate everything 30 degrees along the Y axis.
 
Originally posted by: darktubbly
It's been a while since I've played with OpenGL, but shouldn't you be clearing the color and depth buffers and then loading the identity matrix? If you want to be able to see the Z axis, you should only have to do rotate it like so:

glRotate(30, 0.0, 1.0, 0.0)

which should rotate everything 30 degrees along the Y axis.

When I use glRotate(30, 0.0, 1.0, 0.0), nothing comes up. I'm stumped 🙁.
 
I added the

GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

line to my code but all it does is clear the background color to white... the rotating problem is still there 🙁.
 

Assuming all of your window code is correct this is what you have to do.

this is all off the top of my head so there might be a few mistakes, pm me or something if you need more help

glViewport(0,0,window_width,window_height); // 800x600 or whatever
glMatrixMode(GL_PROJECTION); //load the projection matrix stack
glLoadIdentity(); //clear the top matrix
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 200.0f); //set it up
glMatrixMode(GL_MODELVIEW); //load the modelview matrix stack
glLoadIdentity(); //clear the top matrix

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glEnable(GL_DEPTH_TEST);
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);

gluLookAt( 0.0f, 0.0f, 50.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f );

//make this a loop or whatever if you want
{
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glColor4f(1.0f,1.0f,1.0f,0.0f);
glPushMatrix(); //pushes the viewing matrix down the stack and copies a new one to the top

glRotatef(30.0f,1.0f,1.0f,1.0f); //put the rotation on the top of the modelview matrix stack

glBegin(GL_LINES); { //start immediate mode rendering

// Do all your glVertex*() calls here.

} glEnd(); //stop it.

glPopMatrix(); //pop the rotation off the stack and go back to the viewing matrix
}

 
Back
Top