blending twix

Started by
7 comments, last by mathfeel 22 years, 3 months ago
looking at the blending code of lesson8, I did a little twix to it. Instead of glColor4f(1.0f, 1.0f, 1.0f, 0.5);, I changed it to glColor4f(0.0f, 1.0f, 0.0f, 0.5); so I expect each pixel to be "greenified"...and it does...but when I turn the light on, the effects seems to be gone. What can I do so that when I turn on the light, all it adds is different brightness to each side? and every thing remain green?
Advertisement
The problem is that your light color is set to blue. So when it illuminates some polygon (for which glColorMaterial is enabled) it "assigns" its' own color to that polygon. Try changing the light color to that you need ( that is glColor4f(0.0f, 1.0f, 0.0f, 0.5); ).

P.S. It is in the top of the lesson8.cpp:

GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };

is to be changed by:

GLfloat LightAmbient[]= { 0.0f, 1.0f, 0.0f, 1.0f };
GLfloat LightDiffuse[]= { 0.0f, 1.0f, 0.0f, 1.0f };

P.S. I recommend you reading about glColorMaterial in MSDN

Alex


Edited by - Alex Bijamov on January 14, 2002 8:07:52 AM
Alex solution works (even though I''d recommend a green channel of 0.5f instead of 1.0f for ambient), but there exists another solution (pointed by Alex too) which lets you use your beloved glColor function.

Excuse me Alex, but I think your solution has a problem : you simply change the light properties, which set all the scene to green. To my mind, mathfeel would like to use the material properties, to set only a few objects to green, not the whole scene.

This second solution is glColorMaterial.

When the material properties are set with glMaterial, they are fixed until next call to glMaterial.
But with glColorMaterial, you can set one of the material properties using glColor.
First, set the material property destination using glColorMaterial, and then use glColor as usual.

The following code :
GLfloat mat_amb[] = { 0.0f, 1.0f, 0.0f, 1.0f };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_amb);

is equivalent to :
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT);
glColor3f(0.0f, 1.0f, 0.0f);


Please note that it''s a bit complex to use the alpha channel, and it may yield some unexpected results.
> Excuse me Alex, but I think your solution has a problem : you
> simply change the light properties, which set all the scene to
> green. To my mind, mathfeel would like to use the material
> properties, to set only a few objects to green, not the whole
> scene.

Well, why not to use different light styles (GL_LIGHT0-...-8) for different polygons, and just Enable or Disable them when needed. Of course, in more complex programs it may be difficult, and slow down all the process, to say nothing about that it is rather cumbersome to trace which lights are to be enabled or disabled for the current scene . In such cases, of course, vincoof''s GL_COLOR_MATERIAL is better (perfect) solution!

> Please note that it''s a bit complex to use the alpha channel,
> and it may yield some unexpected results.

That sounds interesting... what results do you mean?

Alex
Well, your solution works very well, and in some particular cases it can even be a better solutions.
Though, I admit that both solutions gives identical results on the sample scene that mathfeel probably use.


quote:That sounds interesting... what results do you mean?

Eh eh. I mean :
  glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT);glColor4f(0.0f, 1.0f, 0.0f, 0.25f);glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR);glColor4f(1.0f, 1.0f, 0.0f, 0.75f);  

What alpha channel will be used at the end of this sample code ?
> Well, your solution works very well, and in some particular
> cases it can even be a better solutions.

Moreover, IMHO these two solutions are qualitatively the same: the main task for OpenGL here is to calculate the color components for each pixel of our polygons. So the proccess is the same in both cases => the computer resources, that are used, are the same!
Or am i wrong somewhere?

> Though, I admit that both solutions gives identical results on
> the sample scene that mathfeel probably use.
Sure, it's up to programmer to choose which method to use.

It's curious, but i noticed no difference while changing the alpha channels in your sample code. Actually interesting is the following: Is the result of using Alpha channels (in this case) UNEXPECTED (random), or is it just undesirable? (i mean, it can be used for receiving different results, such as we receive different colors while mixing up R G B componetnts)
... or alpha channel is simply ignored when we deal with lighting?

Thanks in advance!


Edited by - Alex Bijamov on January 15, 2002 4:50:16 AM
> So the proccess is the same in both cases => the
> computer resources, that are used, are the same!

Doh!
I just realized that in this case the computer resources used are exactly the same !
Well, huh okay. For educational purposes they are the same. You won this time


> Actually interesting is the following: Is the result of using Alpha channels
> (in this case) UNEXPECTED (random), or is it just undesirable?

That's the answer I wanted to see : you don't know ! And to be honest, I didn't know until today when I tweaked some parameters in a sample application.
The thing I wanted to point out is that it's a bit complex to know what OpenGL really do, and then you have to be very careful on what alpha channels you use with material parameters.

> .. or alpha channel is simply ignored when we deal with lighting?

In fact, the alpha channel used is the one used in diffuse parameter, whatever the other parameters alpha channels are.
So, in this example the alpha component will NOT be modified, since we're not setting the diffuse color.
It's a bit weird. I don't really know how it should really work (I mean, according to OpenGL specifications).
If anyone has the answer, I'm greatly interested.


Also, something I forgot to mention is that the function call order is important with glColorMaterial.
The following code :
    glDisable(GL_COLOR_MATERIAL);glColor3f(1.0f, 0.0f, 0.0f); // redglColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR); // specular componentglEnable(GL_COLOR_MATERIAL);glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); // diffuse componentglColor3f(0.0f, 1.0f, 0.0f); // greenglColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT); // ambient component  

will set the specular color to red, and both abient and diffuse colors to green.

How ?
glEnable(GL_COLOR_MATERIAL) sets the last colour (red) to the last material property (specular).
glColor3f(0.0f, 1.0f, 0.0f) sets this color (green) to the last material property (diffuse).
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT) sets the last color (green) to this material property (ambient).

Looking at the code first time, I would not expect to get those results. So, again, be careful of the order of your calls.

Edited by - vincoof on January 15, 2002 7:11:42 AM
Well, Thank you for such an interesting discussion!
To say the truth, I thought that the diffuse component in your sample above will be set to red, until i compiled this code!You''re completely right, about that call order is very important, and not only in this case.
Thanks again
You''re welcome Alex

In fact, the diffuse component is set to red for a very short period (when glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE) is called) but it is immediately overriden by the call of glColor(0.0f, 1.0f, 0.0f) which sets it to green.

This topic is closed to new replies.

Advertisement