line overwrite

Hi. I am working on an MFC Dialog Based OpenGL Application. I use double buffering. My problem is: I have a gridline in grey and when I draw another line (which is yellow) on top of it, my new line does not completely cover the old one. I see some random grey points on yellow. My pixel format is as below. Does anyone know how I can fix this problem?

BOOL CMyDlg::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pfd;

pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);	// Size of this pfd
pfd.nVersion = 1;							// Version number : must be 1
pfd.dwFlags = PFD_DRAW_TO_WINDOW |			// Support window
			  PFD_SUPPORT_OPENGL |			// Support OpenGL
			  PFD_DOUBLEBUFFER |			// Double buffered
			  PFD_STEREO_DONTCARE;			// Support either monoscopic or stereoscopic
pfd.iPixelType = PFD_TYPE_RGBA;				// RGBA type
pfd.cColorBits = 0;						// Specifies the number of color bitplanes in each color buffer
pfd.cRedBits = 0;							// Specifies the number of red bitplanes in each RGBA color buffer
pfd.cRedShift = 0;							// Specifies the shift count for red bitplanes in each RGBA color buffer
pfd.cGreenBits = 0;							// Specifies the number of green bitplanes in each RGBA color buffer
pfd.cGreenShift = 0;						// Specifies the shift count for green bitplanes in each RGBA color buffer
pfd.cBlueBits = 0;							// Specifies the number of blue bitplanes in each RGBA color buffer
pfd.cBlueShift = 0;							// Specifies the shift count for blue bitplanes in each RGBA color buffer
pfd.cAlphaBits = 0;							// Specifies the number of alpha bitplanes in each RGBA color buffer. Alpha bitplanes are not supported
pfd.cAlphaShift = 0;						// Specifies the shift count for alpha bitplanes in each RGBA color buffer. Alpha bitplanes are not supported
pfd.cAccumBits = 0;						// Specifies the total number of bitplanes in the accumulation buffer
pfd.cAccumRedBits = 0;						// Specifies the number of red bitplanes in the accumulation buffer
pfd.cAccumGreenBits = 0;					// Specifies the number of green bitplanes in the accumulation buffer
pfd.cAccumBlueBits = 0;					// Specifies the number of blue bitplanes in the accumulation buffer
pfd.cAccumAlphaBits = 0;					// Specifies the number of alpha bitplanes in the accumulation buffer
pfd.cDepthBits = 0;						// Specifies the depth of the depth (z-axis) buffer
pfd.cStencilBits = 0;						// Specifies the depth of the stencil buffer
pfd.cAuxBuffers = 0;						// Specifies the number of auxiliary buffers. Auxiliary buffers are not supported
pfd.iLayerType = PFD_MAIN_PLANE;			// Ignored. Earlier implementations of OpenGL used this member, but it is no longer used
pfd.bReserved = 0;							// Specifies the number of overlay and underlay planes
pfd.dwLayerMask = 0;						// Ignored. Earlier implementations of OpenGL used this member, but it is no longer used
pfd.dwVisibleMask = 0;						// Specifies the transparent color or index of an underlay plane
pfd.dwDamageMask = 0;						// Ignored. Earlier implementations of OpenGL used this member, but it is no longer used

int m_GLPixelIndex = ChoosePixelFormat(hDC, &pfd);// Attempts to match an appropriate pixel format supported by a device context to a given pixel format specification
if(m_GLPixelIndex == 0)								// Choose default
{
	m_GLPixelIndex = 1;
	if(DescribePixelFormat(hDC, m_GLPixelIndex,	// Obtains information about the pixel format identified by iPixelFormat of the device associated with hdc
		sizeof(PIXELFORMATDESCRIPTOR), &pfd)==0)
		return FALSE;
}
if(!SetPixelFormat(hDC, m_GLPixelIndex, &pfd))	//Sets the pixel format of the specified device context to the format specified by the iPixelFormat index
	return FALSE;

return TRUE;

}

Note: I also tried many nonzero value combinations but I could not make it work.

OpenGL will only raster the exact same points twice if you do the exact same thing for both lines.
Means the vertex coordinates and matrices need to be the same, the order of vertices needs to be the same, the line need to have the same style and width, and if you have depth buffering enabled use one which has equal in it.

Is it z-fighting ? Two elements at almost the ‘same’ distance, with depth test on ? Even if you do not request on the PFD it you may get it. Try glDisable(GL_DEPTH_TEST) maybe.

Yes. It is z-fighting. I tried disabling depthtest but it made me lose the 3D look. By the way I use GL_LEQUAL. Any other solution suggestion?

Easy, search these forums (beginner&advanced) for “polygon offset” extension. It was designed to take care of this.

By the way, you do not request a zbuffer in your pfd… Your are lucky to get it by chance… Instead of 0, put 24…(or 16)


 pfd.cDepthBits = [b]0[/b]; // Specifies the depth of the depth (z-axis) buffer

Try depth func GL_LEQUAL to also draw the points which may be in front. If this still doesn’t help, what he ^^^ said.

I tried using glPolygonOffset but it did not work.


glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glEnable(GL_POLYGON_OFFSET_LINE);
glPolygonOffset(1.0,1.0);

glBegin(GL_LINES);
glVertex3d(…);
glVertex3d(…);
glEnd();
glDisable(GL_POLYGON_OFFSET_LINE);

what is wrong with my code?

Nothing, but you didn’t render polygonal primitives. PolygonOffset doesn’t’ work for point and line primitives, as the name says.
Was probably a bad advice for a line grid.
You could render the grid as set of outlined quads (e.g. one quad per two lines) in polygon mode line and move that to the zfar direction with polygon offset if it’s really important. But then it wouldn’t match with other GL_LINES primitives either.
As said, if this is some kind of editor you cannot expect the lines to perfectly match if you draw over the depth buffered grid with another line which hasn’t the exact same length, matrix, style etc.