draws what's behind other things

Hi,

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#)?

I added some programmingcode and a screenshot at this link: http://www.vbforums.com/showthread.php?t=410183

As you can see I first posted my question on a C# forum (with little result so far). My next topics I will place here immediately.

Thank you in advance for all help !

Is depth buffering enabled? Are you using the right comparison function for the depth buffer?

I am not sure. I am still a bit new to it. I think it is enabled:

is it this?:

 protected override void InitGLContext() 
		{
			GL.glShadeModel(GL.GL_SMOOTH);         // Set Smooth Shading                 
			GL.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);     // BackGround Color         
			GL.glClearDepth(1.0f);                 // Depth buffer setup             
			GL.glEnable(GL.GL_DEPTH_TEST);         // Enables Depth Testing             
			GL.glDepthFunc(GL.GL_LEQUAL);             // The Type Of Depth Test To Do     
			GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);     /* Really Nice Perspective Calculations */     
		} 

Maybe when you create your rendering context you use pixel format without depth buffer?

			GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
			GL.glLoadIdentity();
			GL.glTranslatef(paintposx,paintposy, paintposz);
			GL.glRotatef(angleY, 1.0f, 0.0f, 0.0f);
			GL.glRotatef(angleZ, 0.0f, 1.0f, 0.0f);

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.

Hmm, I did some additional tests.

And it turns out he always draws closer objects behind objects which are farther away.

Also I have the impression that objects even get a little bit smaller as they come closer to the camera.

Does anybody have any idea why this is happening?

Please don’t use csgl (not supported anymore) but TAO which is also compatible with mono…

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!

Thank you all, the problem has been fixed. It turns out I was just confusing some things. :slight_smile: Sorry that I might have wasted your time.

I don’t know about Tao though. Sounds like I will have to restart again then. :slight_smile: A bit painful.