Harry Patrick
Harry Patrick

Reputation: 1

how should I go about drawing one side coloured triangles opengl

I worked out how to read in the element color from the COLLADA .dae files but I'm not sure how I go about rendering colored elements in opengl. To render the elements I use glDrawElements

Two colored triangles can perfectly overlap each with only one face colored. How do I go about implementing this? How do I choose which side of a triangle shows the color?

From what I know the xml "sid" attribute identifies the side of the triangle to be colored based on the order of the vertices and it would appear the software producing the files chooses to use two separate triangles one for each colored side I have the normal and position vertices data being plotted successfully now I need to be able to apply colors to the elements.

Upvotes: 1

Views: 1956

Answers (1)

Wyzard
Wyzard

Reputation: 34563

It sounds like your model is made of single-sided triangles, so a two-sided surface is represented by two single-sided surfaces facing in opposite directions. To draw it properly, you just need to use OpenGL's built-in backface culling to draw only the correct side of each triangle.

OpenGL makes a distinction between the "front" and "back" of a triangle, based on the order of the vertices. By default, the "front" is the side from which the vertices appear in counterclockwise order. You can change that to clockwise by calling glFrontFace(GL_CW), but for the sake of argument I'll assume that you're using counterclockwise since it's the default.

From what I know the xml "sid" attribute identifies the side of the triangle to be colored based on the order of the vertices

It's not clear, from the above, whether the two sides of a surface are stored in the file with the vertices in opposite order, or if both triangles are stored with their vertices in the same order and the "sid" attribute tells you which one is the front and which is the back. I'm going to assume that opposite sides are in opposite order; if that's not true, you'll need to use the "sid" to decide which ones to reverse when building the vertex array for OpenGL.

If the two sides of a surface are stored with the vertices in opposite order, then they both follow the same counterclockwise rule. Suppose you have a surface that's red on one side and blue on the other. Looking at it from the red side, the vertices of the red triangle appear in counterclockwise order, and those of the blue triangle are clockwise. If you turn it around to look at the blue side, the blue triangle's vertices are now counterclockwise and the red's are now clockwise. Based on this, OpenGL can determine that you're looking at the "front" of one triangle and the "back" of the other.

All you need to do, then, is tell OpenGL to draw only the front side of each triangle:

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

This makes OpenGL "cull" (not draw) a triangle if the side that would be seen is the "back" one. When the viewer is looking at the red side of the surface, the red triangle will be drawn (since its "front" is facing the camera), and the blue triangle will not (since its "back" is facing the camera). When the viewer is looking at the blue side of the surface, the blue triangle is drawn and the red one is not. The two triangles overlap, but only one or the other is drawn, never both.

With backface culling enabled, you shouldn't need to do any other special handling for the two-sided surfaces. Just draw each triangle with its own color, and let OpenGL cull out the ones that are facing the wrong way.

Upvotes: 1

Related Questions