Smooth Shading

Hi all!

My first post here and I hope you can help me :sorrow: .

I draw a smooth shaded quad with let’s say blue and red colors at the vertices. I expect that in the middle of the quad I get purple. But because of the triangulation all I get there is blue. Is there a way to force OpenGL to draw my quad with the “correct” colors?
Here is the little code snippet:

glBegin (GL_QUADS);
  glColor3f (1.0, 0.0, 0.0); glVertex3f (-1.0, -1.0, 0.0);
  glColor3f (0.0, 0.0, 1.0); glVertex3f (1.0, -1.0, 0.0);
  glColor3f (1.0, 0.0, 0.0); glVertex3f (1.0, 1.0, 0.0);
  glColor3f (0.0, 0.0, 1.0); glVertex3f (-1.0, 1.0, 0.0);
  glEnd (); 

there is, you draw 4 triangles pointing to the center which is then colored purple, or better yet, you use a texture with the correct colors, or maybe a finer grid of quads.

But at the current configuration no.

shading hapens on a per-pixel level. how the colors get interpolated depends on your vertex colors and shade mode, means you specify the vertex colors wrong.
othewise i would suggest to use a texture. it doesnt have to be a large texture, maybe 8x8 is fine with those colors you need.
then the quad you render gets textured and filetered in a linear way.

glBegin (GL_QUADS);
glTexCoord2f (1.0, 0.0); glVertex3f (-1.0, -1.0, 0.0);
glTexCoord2f (0.0, 0.0); glVertex3f (1.0, -1.0, 0.0);
glTexCoord2f (1.0, 1.0); glVertex3f (1.0, 1.0, 0.0);
glTexCoord2f (0.0, 1.0); glVertex3f (-1.0, 1.0, 0.0);
glEnd ();

Thanks guys for your fast responses!

Yeah I figured it out that it doesn’t work but there was just some hope ;).

Unfortunately I can’t use either triangles nor a texture. It’s an exercise for university. Here is it.
Sorry it’s in german but there is a demo of how it should look like. I hope you figure out what my problem is.

So basically we should draw a colored cube by using a Vertex-Array of QUADS. First you only have 8 vertices. After that you can add vertices by pressing ‘+’. So 27 in the next step. The colors of the new vertices should be INTERPOLATED. I was a bit confused because in the example the colors of the cube change a bit. I asked if that is a mistake and if the color of the cube should remain the same. I got a positive response. They told me that there seems to be an error in their calculation.

At that point I didn’t think of the triangulation. So I implemented a trilinear interpolation. For a new vertex I calculate 8 ‘old’ vertices which are considered for the interpolation. After that I use a similar approach as in this article.

To make a short story even shorter: What should I do? :confused: Or is the exercise just stupid?

Btw you can cycle through all vertices by pressing ‘a’ and ‘d’. With ‘s’ you can hide the spheres which represent the vertices. ‘c’ randomly changes the color of the selected vertex. With ‘+’ and ‘-’ you increase or decrease the slices of the cube. With the mouse you can change the view.

well the main problem here is that there is no such thing as pure quads, each quad is just 2 triangles, this makes the default interpolation break for quads as all the fertics are not accounted for.
Now this becomes less of a problem for tessellated meshes when the difference between the colors in each corner is small.
Infact the demo app works exactly like i expect it to be, it is just a tesselated mesh of quads
once you have the mesh it’s just a matter of bilineary interpolating the colors.

So what to do you ask

  1. you need a function that can take a quad and make 4 quads out of them, then take the intermediary colors between these and put them in the new quads.
    this function would then write it into a new array of polygons.
    it shouldn’t be complicated.

  2. you also need a similar function that takes the 4 outmost vertics of 4 quads, and writes them in a new array (for when you press “-”).

finally when you push either + or - you pass the polygon array to the corresponfing function, then you render, simple.

Oh well, now my program works just as the demo. I forgot to say that even if the vertices aren’t on one of the planes their colors should be interpolated anyway. So for a new vertex inside the cube I need a trilinear interpolation. Everything is fine, works, just the colors change a bit especially when there are few vertices. Telling me that there is some strange error in their calculation confused me a bit. So thanks for your responses :).