PDA

View Full Version : problems with GL_POLYGON/GL_TRIANGLES



Vurdilla
12-07-2011, 01:46 AM
Hi all!
I have searched for an answer for a few days and have not found. May be i am lost in terms and search with wrong keywords, i don't know. Please help :)

The problem is:
I have a little program to look at my scientific results. It draws two types of objects:
1) set of cylinders
2) surface, as a set of triangles

I draw set of cylinders using gluCylinder() function, and set up lights and so on. Thus I have a nice picture of my vector field distribution. Nice. There are 4 light to light up the scene, and there are nice shining, shades on cylinders etc

Then I add surface to the scene. The list of triangles is defined, and i use simple cycle across the list:

for(int q=0;q<s.size();q++)
{
glBegin(GL_POLYGON);
glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
glVertex(s[q].A.x, s[q].A.y, s[q].A.z, 1.0f);
glVertex(s[q].B.x, s[q].B.y, s[q].C.z, 1.0f);
glVertex(s[q].C.x, s[q].C.y, s[q].B.z, 1.0f);
glEnd();
}

The result is good if i use
glEnable(GL_POLYGON_OFFSET_LINES);
before. It is just carcass of the surface, OK.
But when i replace it with
glEnable(GL_POLYGON_OFFSET_FILL);
to look on the surface as a solid body, rendering behaves very strange!
In more details: there is one color across all triangles, that changes from blue to black during rotation of the scene. Color of each triangle is the same, independent from it's position in space (while i was waiting for some changing in color depending on it's angle of rotation, like in cyliners i have drawn before).

Using GL_TRIANGLES instead of GL_POLYGONS doesn't matter.

The result doesn't depend on glShadeModel(GL_FLAT) or glFlatModel(GL_SMOOTH), doesn't depend on positions of lights and many other parameters i have tried to change.

Please help me to understand where i am wrong or give me a path to look at. I am frustrated.

Thanks in advance.

tksuoran
12-07-2011, 03:58 AM
You should have normals for the triangle surface, if you want to it to be lit properly.

Vurdilla
12-07-2011, 04:28 AM
You should have normals for the triangle surface, if you want to it to be lit properly.

Could you describe shortly how to do it please (a little example or at least a keyword to search in web)?

I mean i clearly understand what is normal and how to calculate it, however, there are two questions:

1) what function to use?
2) should i use normal vectors with direction strongly outside the surface, or could use normal vector without care of it's direction (inside/outside)?

tksuoran
12-07-2011, 06:41 AM
You should call glNormal before glVertex. The direction matters, it should point away from the surface point.

Vurdilla
12-07-2011, 07:02 AM
You should call glNormal before glVertex.
Got it, thanks!


The direction matters, it should point away from the surface point.
D'oh! Will use ray-hull intersection algorithm.
If normal vectors are appointed randomly, the coloring is a kind of frustrating.

Thanks a lot for the answers!

tksuoran
12-07-2011, 07:14 AM
I don't quite follow, why should you use ray-hull intersection? You can easily compute normal for a triangle, assuming all of them are consistently oriented, and then compute vertex normals from triangle normals.