View Full Version : Drawing Polygons and Texture Mapping

02-12-2009, 04:46 AM
Question : Are all polygons drawn as a combination of the triangles?

I am drawing a surface where I am given the points on a plane and their colormap values. I have a predefined 1D texture using which and the colormap values of the points I draw the surface.
I am drawing the surface as a combinations of triangles.
Here is the problem:
In a surface with all the points colormap values same(say 0) except one point's colormap value(lets say its values is 1), the surface drawn has a square or a rhombus of the different color(for the different colormap value point) depending on the position of the point. The rhombus is half the size as the square.
Now this is happening because when a surface is drawn as a combination of triangles, each point is either a part of 8 triangles or 4 triangles depending on its position. Due to this, the texture mapping effects either 4 triangles or 8 triangles around the point giving me a square or a rhombus.

So I thought that instead of drawing the surface as combination of triangles, I should draw the surface as a combination of squares(QUADS) and this should solve the problem. I tried this, but the same behavior is seen in this also.
This led me to conclude that these squares are being drawn as two triangles internally or the texturing is occurring for three points at once. Am I right about this? And how can this be fixed?

02-12-2009, 04:58 AM
"This led me to conclude that these squares are being drawn as two triangles internally or the texturing is occurring for three points at once. Am I right about this? And how can this be fixed? "
correct. GPU is made and optimized for triangles only.
to solve your problem and to have a static color among the quad use GL_TRIANGLES and 6 vertices resulting in two triangles making up a quad instead of 4 vertices with GL_TRIANGLE_STRIP or FAN.
everything is drawn in triangles even GL_LINES are drawn in triangles on older hardware and maybe newer.

02-12-2009, 07:19 AM
@_NK47 - Thanks for the reply.
The problem I described would exist if the surface is drawn as triangles. Even when the surface is drawn as you have suggested, the same problem exists.
I am sure that its mathematically impossible to draw a surface as triangles and not have the above described problem unless there is some option in the OpenGL to vary the texturing.
Let me reduce the problem and explain again.
Consider a square which is textured based on a 1D texture which has two colors. Now set three vertices of the square to 1 color and the remaining vertex to another color. When the topleft vertex of the quad is the vertex with different color, the drawing will have a square(of second color) inside another square(of first color). When the bottomleft vertex is the vertex with different color, then you will see a triangle(of second color) inside a square(of first color). Whether it will happen with the topleft or bottomleft vertex will depend on the orientation of drawing the quad. In all the orientations, one of the vertices will give you a square and the other will give you a triangle.
If you expand the above example to a combination of quads, then you will see a square and rhombus instead of square and triangle.

Is there some way to get the same texture no matter which vertex has the different color?

02-12-2009, 10:03 AM
I see 2 possible ways.
Maybe the easier, if you can change the geometry, add a vertex at the center of each quad.
As preprocessing, set its 1D texcoord as average of 4 neighbors.

before :

after :

This way the interpolation will at least be isotropic, even if not really bilinear.

A better solution is to store the 1D texcoords in a 2D texture. Use simple planar texture coordinates, so that each vertex maps to each texel.
With a simple GLSL shader you can then sample the 2D texture (with free bilinear interpolation) to get the 1D texcoord, and seek the 1D texture value with it.