UPDATE: The code below is good. The code I was actually using in my project had texenvi statements being called inside the glBegin which AFAIK isn’t allowed. I blame this on the feature not working, because now everything works fine.
Hello,
A buddy and I are working on a 2D side-scroller. I’m currently experimenting with making the sprites for our enemy entities “Flash” white when hit with a bullet. I’ve done extensive searching and encountered a lot of information on making a texture brighter using the COMBINE and ADD functionality in GL tex env. So far all I have achieved is making the sprites darken. The color range seems to “Cap” at 1.0f, although the posts and tutorials that suggested “ADD” seem to suggest that the range is higher. Here’s our initialization for the GL object, which occurs once at the start of the game:
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(0.0, 1440, 0.0, 900);
//set up the viewports
gl.glViewport(0, 0, 1440,900 );
//set up some options
gl.glClearColor(1, 1, 1, 0);
gl.glDisable(gl.GL_DEPTH_TEST);
gl.glEnable(gl.GL_TEXTURE_2D);
gl.glBlendFunc(gl.GL_SRC_ALPHA,gl.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(gl.GL_BLEND);
And here is the code that is responsible for texturing and drawing the entities:
if (flashLength > 0)
{
float brightness = 1.5f;
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE,GL.GL_COMBINE); //Need to be in this mode
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_COLOR, GL.GL_SRC2_RGB); //Use 0-2 scale???
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_RGB_SCALE, GL.GL_OPERAND2_RGB); //Use 0-2 scale???
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_COMBINE_RGB, GL.GL_ADD); //Add mode needed
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_COMBINE_ALPHA, GL.GL_REPLACE); //Don't use add mode for alpha
gl.glColor4f(brightness - 1.0f, brightness - 1.0f, brightness - 1.0f, 1.0f); //Add brightness to RGB, keep alpha the same
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_SRC0_RGB, GL.GL_TEXTURE); //Set source 1
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_SRC1_RGB, GL.GL_PRIMARY_COLOR); //Set source 2
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_SRC0_ALPHA, GL.GL_TEXTURE); //Get texture's alpha
flashLength--; //Decrement Flash Length
}
else
{
//If no flash, modulate and draw the default
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE,GL.GL_MODULATE);
gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
//Draw the quad
gl.glBegin(GL.GL_QUADS);
//set the texture coordinates for the next vertex
gl.glTexCoord2d(0.0, 1.0);
gl.glVertex2f(x - width/2, y - height/2); //bottom left
gl.glTexCoord2d(1.0, 1.0);
gl.glVertex2f(x + width/2,y - height/2); //bottom right
gl.glTexCoord2d(1.0, 0.0);
gl.glVertex2f(x + width/2,y + height/2); //top right
gl.glTexCoord2d(0.0, 0.0);
gl.glVertex2f(x - width/2,y + height/2); //top left
gl.glEnd();
flashLength is assigned by a collision event in our physics engine and is the duration that the new color settings (white flash) will be applied. Using modulate I can achieve a red tint, but I’m capped in brightness by the texture brightness.
There’s a lot going on there, and I suspect that there’s something wrong with the graphics object not having its settings reset between drawing. We’re completely new to this and have no idea about:
- Whether the GL_TEXTURE_ENV adjustments need to be made inside the glBegin
- How the ADD setting actually works and if this code is executing that functionality correctly
- Any pre-defined environment variables we have forgotten to set in our initialization that make this functionality possible
- A whole host of other things, but we’re getting there.
Any help or at least a nudge in the right direction would be appreciated. We’re trying to adjust the brightness of a texture.
EDIT: Should probably clarify that we are using JOGL but were using LWJGL, not sure what was lost in the conversion.