Specular effects

How can you achieve good specular effects in OpenGL? I’ve seen some people use spheremapping and multitexturing but that seems a bit slow.

Either increase the resolution of your objects and let OpenGL’s own lighting engine do the work, or use spheremapping/multitexturing, as you mentioned. Multitexturing is not that expensive, nor is spheremapping. Not even on a few years old hardware.

Both ways will have some performance hits, but what else can you expect when you want a good result? Always when you want to improve something, you have to offer something else. Most of the time you have to offer speed. Nothing is for free, not even a specular effect.

Okay, I was afraid of that… so exactly what is needed to set things up properly? you need to set up a good value for glShininess right? And then ofcourse your specular lighting components (RGB)… am I leaving anything out?

Dont forget to subdivide objects on great amounts of polies. That helps. Or use NURBS.

As there is less performance hit using a texture than doubling the poly’s, I would recommend using sphere map/multitexturing to achieve the shininess. You will have to use a lot of poly’s to get the same effect without textures.

Multitexturing isn’t too slow: if you apply 2 textures, it’s just like you draw the object twice.
If you mean sphere environment mapping, I did it yesterday!
You only have to draw the object as usual, then set the depth buffer masked (readonly) and the func less or equal.
Then enable sphere coordinates generation, enable blending (glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)) and set up an alpha factor for the current color or material from 0.0 (no reflection) to 1.0 (total environment refl) and redraw the object with the environment texture.
I think this isn’t the only way to do it, but it works very well and it lets you to set how reflective is your object simply changing alpha value.
Hope this helps you!

According to my own findings, sphere mapping generally halves performance. (Well it did on a 83,000 poly gremlin I experiemnted with)

That was on a Geforce 256.

Isn’t there an extenion on geforces called Seperate Specular Color or something too?

I downloaded something somewhere that used this, and had an excellent specular effect.
I think it was that Delphi Environment mapping demo posted up on the news board a week or two back.

  • Nutty

Originally posted by Nutty:
Isn’t there an extenion on geforces called Seperate Specular Color or something too?

Yes, there is GL_EXT_separate_specular_color but its sole purpose it to have specular highlighting while texturing (using core OpenGL, you have to do it in two passes).

So you still have the problem of tesselating the objet quite a lot for having a proper effect !

I must say, it seems to me that using texturing is a much better solution than using more detailed objects… The problem with this technique is to have the proper textures…

Well, up to you !



[This message has been edited by Eric (edited 02-13-2001).]

So how does that work then?
Just get a texture which is black, with a white highlight in the centre, and just blend that onto the geometry in a second pass with sphere mapping?

ahem… my last post was a question you know. Or has no-one actually done this?

  • Nutty


If you just want to use a lightmap, why would you use sphere mapping ?

I mean, say you have your original texture and your lightmap (the black&white thing but could be with colors as well !). You just have to first render the original one and then the second one with GL_MODULATE. No need for sphere mapping here…

Or perhaps I did not get your question right ?



Hmmm… why was someone mentioning sphere mapping used in conjunction with multi-texturing above, when the discussion is about specular effects?

Would what I described not give a specular effect?

I’ll have to have a fiddle about sometime. Might stick a demo up on me site if I get it working nicely. Wonder if I could work that into a vertex program…

Oops my fault…

I guess what I describe would do like diffuse lighting… Usually, with specular effects, you want the “shiny bit” to move when you change your view angle ! That’s probably why Punchey mentioned sphere mapping…

I should wake up now…



I’ve got a kind of specular effect going now.
Doing 2 passes.

First pass I use GL_DECAL with the main texture, then on the second pass I use GL_MODULATE with a black texture with a white highlight in the middle of it, with sphere mapping.

But it doesn’t work very well. I have to disable depth testing for the 2nd pass to render, and then I dont see any of the original textured object at all. It’s very strange. Maybe I’m doing something completly wrong.

Do I have to enable blending when using MODULATE?

Enable the depth test, set glDepthFunc to GL_EQUAL (or GL_LEQUAL should work too, if your test was GL_LESS before).
To add some specular highlight with multiple passes you’ll have to do some blending on the frame buffer. WIth a black and white texture for the highlight glBlendFunc GL_DST_COLOR, GL_ZERO); should work.

If you are doing two passes with only one texture per pass, why not use multitexturing?

The first textureunit is the base texture set to GL_REPLACE or something, or GL_MODULATE if you want to use the triangle’s basecolor aswell. This will be the diffuse color of the object.

The second textureunit should be the specular map, with the textureunit set to additive blending, i.e. add the two textures together. If you use GL_MODULATE, it will multiply the exisiting color with the incomming color. When the texture is black, the resulting color will be black. If the texture is white, the color will be unchanged. There is no way you can get the highlight effect with GL_MODULATE.

I changed the depth function, and all is well!

GL_DST_COLOR, GL_ZERO dont give the right results, the entire object is darkened, and only normal color at the highlight.

I’m using GL_SRC_ALPHA, GL_ONE, to give a nice additive white out specular.

Might put it on me website tonight.

  • Nutty

>>GL_DST_COLOR, GL_ZERO dont give the right results<<

Well, was just a first approximation without coding.
Fine that you got it right.

Nutty, what’s the URL for your site?

Also, if modulate makes only the highlight the normal color, I suppose then that you could use that characteristic to do some cool kind of spot lighting or something using textures. Like maybe a star-shaped (or whatever) spot light.

Originally posted by Punchey:
Nutty, what’s the URL for your site?