PDA

View Full Version : Flickering Sphere when using Normals



TSorcerer
11-20-2008, 02:38 AM
Hi,

I'm currently working at implementing lighting into my OpenGL program. Currently I draw a Sphere, loaded from a 3ds file, to my scene. To enable lightning I calculate Normals for each of the sphere's faces after loading it from the file. I then added to my draw method the glNormal3fv command that defines each faces normal using the calculated valeus from when I loaded the sphere.

When running the program I see my sphere lit up by light. However, the lighting looks odd, so I switched it off again and watched my sphere without lighting, but still issueing the glNormal command while drawing the sphere. And then following happened:

Screenshots (http://img252.imageshack.us/img252/7610/flickergb1.jpg)

The sphere is flickering. It looks like if the "fractured" part gets bigger and bigger until filling all of the sphere at which it suddenly disappears and will begin to grow again.
When I remove the glNormal part the sphere will draw fine again.
To figure out what's going on, I added a small routine, that should draw me each of the face's normals. Maybe they're pointing in the wrong direction or something?
However, when I draw the normals they show fine pointing out of the sphere.

However, when I load another mesh (just some random polygons scattered around) and draw them instead or together with the sphere, it does not flicker.

Only the sphere is mocking. I compared the number of vertices and faces loaded from the file for the sphere and it equals to the number Blender shows me.

I don't have any idea why the sphere is flickering, maybe someone here has an idea?

Greets,
Daniel

_NK47
11-20-2008, 03:36 AM
sphere looks weird. a code snippet would help (normal calculation and drawing part). is your light moving around?

TSorcerer
11-20-2008, 03:54 AM
The flickering only occurs when lighting is turned off.

Building Normals


CVector v1, v2;
register int n=m_nPolygons;
register int j;
while(n--)
{
SPolygon& poly = m_pPolygons[n];

v1 = this->m_pVertices[poly.pIndices[1].nVertex] - this->m_pVertices[poly.pIndices[0].nVertex];
v2 = this->m_pVertices[poly.pIndices[1].nVertex] - this->m_pVertices[poly.pIndices[2].nVertex];
poly.normal = (v1 | v2).normalize();
}


Drawing:


register int n=m_nPolygons;
register int j;

while(n--)
{
SPolygon& poly = m_pPolygons[n];
CVector polygonCenter;// draw normals stuff

glBegin(GL_POLYGON);
glNormal3fv(poly.normal);
j=poly.nCount;
while(j--)
{
glTexCoord2fv((GLfloat*)&poly.pIndices[j].textureMapping);
glColor3fv((GLfloat*)&poly.pIndices[j].color);
glVertex3fv(m_pVertices[ poly.pIndices[j].nVertex ]);
polygonCenter += m_pVertices[ poly.pIndices[j].nVertex ]; // draw normals stuff
}
glEnd();


The drawing part is stored in a display list and the list is used to draw the mesh. However, I just recognized that when I disabled the display list the other mesh will start to flicker aswell, but very fast. The flickering on the sphere will remain, but get slower. I assume that the other mesh flickers aswell, when display lists are on, but so fast, that I cannot see it.

sharp_pixel
11-20-2008, 06:34 AM
Just to be sure, what is the type of SPolygon::normal ?
If you store them as double then weird things will occur loading them with glNormal3fv.
If the type is right, then you can try debugging using glNormal3f(nx, ny, nz) instead of glNormal3fv, and see if the values of nx, ny and nz are correct.

TSorcerer
11-20-2008, 06:44 AM
I believe I've figured out what the problem was. I had ShadeModel set to GL_SMOOTH which would expect a normal for every vertex, instead of every polygon. After I calculated a normal for each vertex and changed my drawing algorithm to fit it the flickering disappeared. My bad.

PS: poly.normal is of CVector class, which has operator float*() overloaded.