I am doing my first project. But for some unknown reason my program also draws what’s behind other objects allthough I do not use alpha or anything like that. Maybe somebody knows what I am doing wrong. (It’s written in CsGL, that’s OpenGL for C#)?
In the Gl_Draw function I am first doing these and then I start looping though my objects to draw them one by one. Each object has a Paint method as shown on the hyperlink above but I will post the code here too…
public void Paint()
{
bool painting = true;
switch(points.Count)
{
case 2:
GL.glBegin(GL.GL_LINES);
break;
case 3:
GL.glBegin(GL.GL_TRIANGLES);
break;
case 4:
GL.glBegin(GL.GL_QUADS);
break;
default:
painting = false;
break;
}
if (painting)
{
for(int i = 0; i < points.Count;i++)
{
PointMatrix p = (PointMatrix)points[i];
GL.glColor3fv(p.Color);
GL.glVertex3f(p.X,p.Y,p.Z);
}
GL.glEnd();
}
}
Oh and this my resize function…
Maybe something is wrong with this one?
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
Size s = Size;
double aspect_ratio = 1.0; //(double)s.Width /(double) s.Height;
GL.glMatrixMode(GL.GL_PROJECTION); // Select The Projection Matrix
GL.glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
GL.gluPerspective(45.0f, aspect_ratio, 0.1f, 100.0f);
GL.glMatrixMode(GL.GL_MODELVIEW); // Select The Modelview Matrix
GL.glLoadIdentity();// Reset The Modelview Matrix
}
Besides this I don’t have any OpenGL related code. (Except for the refreshing-thread).
Thank you for your efforts so far! I really hope to find what’s wrong. Strange that I have only this little code and still strange things happen.
Can you please post all your rendering code? I don’t even see that you’re clearing the color/depthbuffer.
And as stated already, you need to make sure your rendering context is created with a depth buffer (PFD_DOUBLEBUFFER flag and pfd.cDepthBits = 16 or 24), which I guess csgl takes care of for you or you need to explicitly request it as part of initialization.
I’d also recommend using Tao if you’re starting out, although with .NET 2.0 creating a wrapper isn’t very difficult, just incredibly tedious.
And a word of warning: while OpenGL function call overhead generally isn’t a big deal in C/C++, it is in C#/.NET (big time). Interop overhead is atrocious and can cut your framerate in half for nontrivial geometry (2000 plus vertices or so). Plan on using vertex arrays/buffers from the get-go or don’t be surprised if your framrate isn’t what you think it should be!