Jump to content
  • Advertisement
Sign in to follow this  
Journey

Polygons drawn out of order

This topic is 4447 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I draw the polygons in the following screenshot in the correct order? I made sure to call glEnable(GL_DEPTH_TEST). I even try it precisely before the drawing code, but it's making no difference. What could be wrong? I'm using orthographic projection btw. http://i14.photobucket.com/albums/a350/luisjoseromero/out_of_order.jpg here's the code that draws the surface


	S3DPoint Point1S, Point2S, Point3S, Point4S;

	glEnable(GL_DEPTH_TEST);

	for(int nCurrLayer = 0; nCurrLayer < (*this->m_pVecLayers2C).size(); nCurrLayer++)
	{

		fV = 0.0;

		for(int nCurrVTri = 0; nCurrVTri < nRes; nCurrVTri++)
		{

			glBegin(GL_TRIANGLE_STRIP);

				float fU = 0;
				
				for(int nCurrUTri = 0; nCurrUTri < nRes; nCurrUTri++)
				{
				
					Point1S = (*this->m_pVecLayers2C)[nCurrLayer]->GetEvalPoint(fU, fV);
					Point2S = (*this->m_pVecLayers2C)[nCurrLayer]->GetEvalPoint(fU, fV+fVInc);
					Point3S = (*this->m_pVecLayers2C)[nCurrLayer]->GetEvalPoint(fU+fUInc, fV);
					Point4S = (*this->m_pVecLayers2C)[nCurrLayer]->GetEvalPoint(fU+fUInc, fV+fVInc);
					
					fU += fUInc;

					C3DVector Vec21C(Point2S.m_dX-Point1S.m_dX, Point2S.m_dY-Point1S.m_dY, Point2S.m_dZ-Point1S.m_dZ);
					C3DVector Vec34C(Point3S.m_dX-Point4S.m_dX, Point3S.m_dY-Point4S.m_dY, Point3S.m_dZ-Point4S.m_dZ);
					C3DVector Vec31C(Point3S.m_dX-Point1S.m_dX, Point3S.m_dY-Point1S.m_dY, Point3S.m_dZ-Point1S.m_dZ);
					C3DVector Vec24C(Point2S.m_dX-Point4S.m_dX, Point2S.m_dY-Point4S.m_dY, Point2S.m_dZ-Point4S.m_dZ);
					// first normal
					C3DVector Normal1C(Vec21C^Vec31C);
					Normal1C.Normalize();
					Normal1C.Negate();
					// last normal
					C3DVector Normal4C(Vec34C^Vec24C);
					Normal4C.Normalize();
					Normal4C.Negate();

					glNormal3d(Normal1C.m_dX, Normal1C.m_dY, Normal1C.m_dZ);
					glVertex3f(Point1S.m_dX, Point1S.m_dY, Point1S.m_dZ);
					glVertex3f(Point2S.m_dX, Point2S.m_dY, Point2S.m_dZ);
					glVertex3f(Point3S.m_dX, Point3S.m_dY, Point3S.m_dZ);
					glNormal3d(Normal4C.m_dX, Normal4C.m_dY, Normal4C.m_dZ);
					glVertex3f(Point4S.m_dX, Point4S.m_dY, Point4S.m_dZ);

				}// end for

			glEnd();

			fV += fVInc;

		}// end for

	}// end for





Share this post


Link to post
Share on other sites
Advertisement
I'm going to claim that depth testing isn't performed, even though you said you enabled it. Just enabling the test is not enough, you must have a depth buffer aswell. Check the pixel format; no depth buffer, no depth test.

Share this post


Link to post
Share on other sites
Here's the code for the pixel format descriptor
The depth buffer is set to 16 bits



// set the pixel format we want
PIXELFORMATDESCRIPTOR PFDS = {
sizeof(PIXELFORMATDESCRIPTOR), // size of structure
1, // default version
PFD_DRAW_TO_WINDOW | // window drawing support
PFD_SUPPORT_OPENGL | // OpenGL support
PFD_DOUBLEBUFFER, // double buffering support
PFD_TYPE_RGBA, // RGBA color mode
32, // 32 bit color mode
0, 0, 0, 0, 0, 0, // ignore color bits, non-palettized mode
0, // no alpha buffer
0, // ignore shift bit
0, // no accumulation buffer
0, 0, 0, 0, // ignore accumulation bits
16, // 16 bit z-buffer size
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main drawing plane
0, // reserved
0, 0, 0 }; // layer masks ignored



Share this post


Link to post
Share on other sites
Oh by the way this is the code for the orthographic projection



// set the perspective with the appropriate aspect ratio
if(pDimS_->nX <= pDimS_->nY)
{

glOrtho(-this->m_dAspRatioScale, this->m_dAspRatioScale, -this->m_dAspRatioScale*pDimS_->nY/pDimS_->nX, this->m_dAspRatioScale*pDimS_->nY/pDimS_->nX, 0.5, 1000.0);

}else
{

glOrtho(-this->m_dAspRatioScale*pDimS_->nX/pDimS_->nY, this->m_dAspRatioScale*pDimS_->nX/pDimS_->nY, -this->m_dAspRatioScale, this->m_dAspRatioScale, 0.5, 1000.0);

}// end else



Share this post


Link to post
Share on other sites
well if you're trying to draw the polygons in 3D you're going to want to be using a perspective projection, not an orthographic one. The orthographic projection essentially flattens your scene, thereby eliminating any depth you could hope to be seeing in the image.

change to perspective mode and see if that gives you the desired results.

Share this post


Link to post
Share on other sites
Quote:
Original post by Morpheus011
well if you're trying to draw the polygons in 3D you're going to want to be using a perspective projection, not an orthographic one. The orthographic projection essentially flattens your scene, thereby eliminating any depth you could hope to be seeing in the image.

change to perspective mode and see if that gives you the desired results.

Uh, no.

Ortho projection still has depth, it just doesn't do the perspective divide (ie. objects don't get smaller as they move further away). Depth values and zbuffering works just fine.

Journey: Your framebuffer setup is slightly odd, generally you should request a depth buffer the same size as your colour buffer (so if you're requesting a 32bit colour buffer, you should request a 32bit depth buffer, not a 16bit one). Mismatched formats tend to either fail to create or are unoptimal.

Unfortunately I can't remember the pixelformat params off the top of my head, but I'd guess you've got the '16' in the wrong place. After you've created your display use glGet(GL_DEPTH_BITS) to double check you're actually getting a zbuffer.

Also, check you've got an appropriate depth func set, typically you want GL_LEQUAL.

Share this post


Link to post
Share on other sites
I set the depth buffer to 32 bits and it returned 24 with glGetIntegerv()
I set the color depth to the same value. I set the function to GL_LEQUAL, and the problem is still there. Changing to perspective works though, but it would be much nicer to make it work in orthographic projection.

Share this post


Link to post
Share on other sites
Orangy, what I was implying was that the perception of depth is lost in an orthographic projection, because, as you said there is no perspective divide. Without the perspective divide our perception of depth becomes skewed, hence why the image the OP was getting may not have matched the image he was aiming for.

IMO Orthographic projections, in general, are not meant for drawing 3D scenes. They are best saved for engineering schematics such as in a CAD application, which I believe was what they were used for long before computer graphics became relavent, or for HUD elements (that's what I generally use them for). Sorry if I was misleading in my wording.

I would like to know though, why do you want to do this in orthographic if you have it working in perspective now?

Share this post


Link to post
Share on other sites
I just thought it looked "neater". So is there a way to make the depth buffer work with orthographic projection?

Share this post


Link to post
Share on other sites
Make sure your far-plane is a large enough value as well... I made that error once, substituting the far-plane value with a "viewdistance" - but I forgot to set it to any value bigger than 0!

It almost drove me mad, but luckily I found the answer before going nuts. ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!