[SOLVED] Texture-based Lighting???

Started by MattCa, August 23, 2011, 19:19:03

Previous topic - Next topic

MattCa

Hi guys!
I'm rather new to OpenGL, and I'm experimenting with a few ideas I have. At the moment, I'm trying to set up a pseudo-dynamic lighting system, my current method involves constantly changing the Ambient lighting setting with glLight(); multiple times a frame. The problem with this is, as you've probably already guessed is that this method is EXTREMELY slow, cutting my FPS from ~500fps to ~40-50fps.

I was thinking earlier, and it seems that one solution would be to modify the textures instead of the lighting, which would mean that I could get rid of the lighting and (hopefully) retain the high frame rate.

Does anyone have any idea how to do this at a fast pace, or does anyone know a way to do this differently?

Thanks for the help!
Matt.

P.S. I'm using the in-built Slick-Util TextureLoader for.. well... texture loading.

P. P. S. If anyone would happen to have a good link to a tutorial that describes how you actually upload vertex and texture data to a VBO, I would be very grateful :).

Fool Running

Using glLight is the way to go. It sounds to me like you might be using glLight() wrong. Could you post some code?
I assume you aren't using shaders, so the only way (I think) that you could drop your framerate so much by using lights is if you were maybe creating too many lights instead of reusing one single light.

P.S. Altering textures in real-time for this would be MUCH slower as that would require the textures to be read from and written to the graphics card.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

MattCa

Quote from: Fool Running on August 24, 2011, 17:11:39
Using glLight is the way to go. It sounds to me like you might be using glLight() wrong. Could you post some code?
I assume you aren't using shaders, so the only way (I think) that you could drop your framerate so much by using lights is if you were maybe creating too many lights instead of reusing one single light.

P.S. Altering textures in real-time for this would be MUCH slower as that would require the textures to be read from and written to the graphics card.

Thanks for the reply! :P
Your right in assuming I'm not using shaders, at the moment I'd prefer to stay with the fixed pipeline whilst I get to grips with the library.
As for an example of how I'm using lighting, heres what I'm doing per frame, per face:

        glLight(GL_LIGHT1, GL_AMBIENT, (FloatBuffer)createFloatBuffer(4).put(new float[] {topLighting, topLighting, topLighting, 1.0f}).flip());
        glCallList(topFace);


Without the glLight call, I get upwards of 400fps, but with glLight I get a huge drop to around 30-40fps.

This is a guess, but could it be the createFloatBuffer() function? Although I was assuming that these would be rather fast...

Thanks,
Matt.

Mickelukas

Create the float buffer once and reuse it, what you're doing there is going to be very expensive.

    private static final FloatBuffer COLORAMBIENT = BufferUtils.createFloatBuffer(4).put(new float[] {0.0f, 0.0f, 0.0f, 0.0f});

in the init code:
COLORAMBIENT.rewind();

And in your draw function (if you need to change it every frame):
COLORAMBIENT.put(0, 0.2f);
glLight(GL_LIGHT1, GL_AMBIENT, COLORAMBIENT);



Mike

MattCa

Quote from: Mickelukas on August 24, 2011, 18:19:35
Create the float buffer once and reuse it, what you're doing there is going to be very expensive.

   private static final FloatBuffer COLORAMBIENT = BufferUtils.createFloatBuffer(4).put(new float[] {0.0f, 0.0f, 0.0f, 0.0f});

in the init code:
COLORAMBIENT.rewind();

And in your draw function (if you need to change it every frame):
COLORAMBIENT.put(0, 0.2f);
glLight(GL_LIGHT1, GL_AMBIENT, COLORAMBIENT);



Mike
Thanks for your reply!
Sadly, the problem persists. The sole cause of the FPS drop must be the fact that I run glLight six times. Once for each side of a cube.

Is there no fast way of applying different light levels to the six different faces of a cube (be it through a VBO, display list etc)?

Thanks,
Matt.

Edit: Does anyone have any experience with glMaterial()? Something I just saw online says something about materials being associated with lighting :S

MattCa

Sorry for the double post, but to those of you who may need help with this in the future I've managed to get it working through the (rather simple!) use of glTexEnvf.

Basically, when you want to apply this kind of 'custom lighting', you need to do the following:

glDisable(GL_LIGHTING); // Disable lighting, doesn't work without this
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // Enable texture modulation - I believe it multiplies the texture by a color
texture.bind(); // Bind your texture either with Slick-Util or glBindTexture();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f); // First three values define the colour you wish to multiply by, 1 being normal 'brightness'


I guess thats a new thing learned for today!

Thanks for the help guys!