texture highlight

hi

i have texture(img1) which i render on the screen(gljpanel), then if user mouseover it i want highlight it with yellow (exemple yellow with 0.5 alpha)
what i think to do that create another texture(img2, yellow with 0.5 alpha) with hight and wight as img1 and render over it
but problem is img1 have transparent pixels(0 alpha) and if i render over it all area of img1 be merget with img2.

Is there a way to set blend rules as if img1 have 0 alpha in pixel then this pixel in img2 don’t merge with img1?

or maybe java image have this functionality and i can do it on bufferedimage(i create texture from bufferedimage)

PS img2 i can only create programmatically

thanks

Why not just change the color of the polygon rendered with the 1st image. If you’re using MODULATE, it should blend the yellow and texture together. I’m not sure if this is the effect you’re looking for.

Instead of drawing two things, you should look into multi-texturing. Then you’d just set up your two textures and draw it once (per frame). There are some pretty advanced texture combinations that can be done using GL_COMBINE.

thx for reply

i attach visual representation of what I wanted to achieve, highlight color maybe any but all pixle with 0 alpha need to stay the same

i try to use GL_MODULATE and GL_COMBINE but this is not what i want

If you create a window that stores alpha values with the pixel colors, you can specify a GL_BLEND mode DST_ALPHA that modifies the incoming color by the alpha. This might work, but not all cards like alpha’d windows.

If you’re positive MODULATE and COMBINE won’t work for you, then I’d recommend computing the highlight texture once, and then just switching back and forth as needed. The color you want for the highlight doesn’t seem to lend itself to blending in a great manor.

GL_EXT_SECONDARY_COLOR will let you tint a sprite at runtime without needing to touch the pixel data manually.

I obtain that effect simply with glColor… If you draw with glColor(1,1,1) you see your texture in the normal state and if you set glColor(1,1,0) before drawing your texture you’ll get the highlighted state…


package test;

import java.awt.Frame;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLException;

import com.sun.opengl.util.Animator;
import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureCoords;
import com.sun.opengl.util.texture.TextureIO;

public class Test {
	Frame frame;
	GLCanvas glCanvas;
	Animator animator;
	Texture texture;
	TextureCoords coords;
	float t;

	public Test() {
		glCanvas = new GLCanvas();
		animator = new Animator(glCanvas);
		frame = new Frame();
		frame.add(glCanvas);
		glCanvas.addGLEventListener(glel);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	public void showGui() {
		frame.setBounds(100, 100, 400, 300);
		frame.setVisible(true);
		animator.start();
	}

	GLEventListener glel = new GLEventListener() {
		@Override
		public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
		}

		@Override
		public void init(GLAutoDrawable drawable) {
			GL gl = drawable.getGL();
			gl.glEnable(GL.GL_BLEND);
			gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
			gl.glEnable(GL.GL_LINE_SMOOTH);
			gl.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_NICEST);
			gl.glEnable(GL.GL_POINT_SMOOTH);
			gl.glHint(GL.GL_POINT_SMOOTH_HINT, GL.GL_NICEST);
			gl.glEnable(GL.GL_POLYGON_SMOOTH);
			gl.glHint(GL.GL_POLYGON_SMOOTH_HINT, GL.GL_NICEST);
			gl.glLineWidth(0.3f);
			try {
				texture = TextureIO.newTexture(getClass().getResourceAsStream("img.png"), true, "png");
				coords = texture.getImageTexCoords();
			} catch (GLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		@Override
		public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
		}

		@Override
		public void display(GLAutoDrawable drawable) {
			GL gl = drawable.getGL();
			gl.glLoadIdentity();
			gl.glOrtho(-3, 3, -2, 2, -1, 1);
			gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

			float blue = (float) (0.5 + Math.sin(t += 0.1));
			if (blue < 0) {
				blue = 0;
			}
			if (blue > 1) {
				blue = 1;
			}

			texture.enable();
			texture.bind();
			gl.glColor3f(1, 1, blue); //set the yellow (when blue==0) or white (when blue==1)
			gl.glBegin(GL.GL_QUADS);
			gl.glTexCoord2f(coords.right(), coords.top());
			gl.glVertex2f(1, 1);
			gl.glTexCoord2f(coords.left(), coords.top());
			gl.glVertex2f(-1, 1);
			gl.glTexCoord2f(coords.left(), coords.bottom());
			gl.glVertex2f(-1, -1);
			gl.glTexCoord2f(coords.right(), coords.bottom());
			gl.glVertex2f(1, -1);
			gl.glEnd();
			texture.disable();
		}
	};

	public static void main(String[] args) {
		new Test().showGui();
	}
}