smooth shading a quad

Started by
15 comments, last by Xero-X2 18 years, 7 months ago
I have a quad in OpenGL, and I want it shaded with white on the top that gradually blends into blue on the bottom. I'm using smooth shading, and it looks fine when I view it head on. However, when the quad is rotated, there is a visible seam between the two triangles that comprise it (probably due to the way Gouraud shading works). Is there any way to eliminate this seam? Thanks. mike http://www.coolgroups.com/
Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/
Advertisement
When you say you have a quad, is it an actual quad? Or two triangles put together to make a quad? If it's the later (2 triangles), you need to do a couple things to get them to align just right. Let's say you have something like:
1---2|  /|| / ||/  |3---4


The verticies at points 2 and 3 must be exactly the same. If there's any floating point fuzziness, you'll probably see a seam. Also, if triangle A goes 1, 2, 3, triangle B must go in the direction of 4, 3, 2. In other words, if triangle A goes from vertex 2 to vertex 3, triangle B must go from vertex 3 to vertex 2 if you want a seemless connection. I believe this is a requirement in the OpenGL spec.

Someone please correct me if I'm wrong.

If you're actually using a quad, and getting a seam within the quad itself... post a picture of it.
I'm using GL_QUADS, but OpenGL splits this into two triangles.

mike
http://www.coolgroups.com/
Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/
There's a pic at http://www.coolgroups.com/badmaze.jpg

My guess is that OpenGL is using the 2d coordinates of the triangles for the Gouraud interpolation.

Anyone know how to fix this?

Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/
Mike, are you sure that ALL of the top vertices are white, and all of the bottom vertices are blue? From the screenshot it seems that only the top left vertex is white and the bottom right vertex is blue.

EDIT: Opengl does not do Gouraud shading on it's own as far as I know. If you are using glShadeModel(GL_SMOOTH), that just means that vertex colours are interpolated across the polygon.
Yes, I'm sure the top vertices are white, and the bottom are blue.

What's the difference between Gouraud shading and having the colors interpolated
across the polygon?

mike
http://www.coolgroups.com/
Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/
Colors interpolated means (I'm assuming) what you think is Gouraud shading. Say you have a poly with 4 vertices having 4 different colors. With interpolation on, the color of the polygon surface will have a smooth transition to each of the 4 vertices. Like you see in your own program. Gouraud shading is: well the shading of a 3d surface. It uses color interpolation, yes, but there is more to it. I suggest you read this: wiki article.

About your problem, could you please post your setup and drawing code?
Check your normals. Make sure they're normalized and pointing in the correct direction.
I'm not using lighting.

mike
http://www.coolgroups.com/

Quote:Original post by Mantear
Check your normals. Make sure they're normalized and pointing in the correct direction.


Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/
setup:

/* enable OpenGL for the window */
EnableOpenGL (hWnd, &hDC, &hRC);

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

/*******************
* Enable OpenGL
*
*******************/

void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;

/* get the device context (DC) */
*hDC = GetDC (hWnd);

/* set the pixel format for the DC */
ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);

/* create and enable the render context (RC) */
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );

}



drawing code:

glBegin(GL_QUADS);
while (i2 != thepoints.end())
{
glColor3f(0, 0, 1);
Point &p = *i2;
glVertex3f(p.seg.x0, 0, p.seg.y0);
glVertex3f(p.seg.x1, 0, p.seg.y1);

glColor3f(1, 1, 1);
glVertex3f(p.seg.x1, 1, p.seg.y1);
glVertex3f(p.seg.x0, 1, p.seg.y0);
++i2;
}
glEnd();

mike
http://www.coolgroups.com/
Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/

This topic is closed to new replies.

Advertisement