Transparency Revisited

Started by AGP, September 05, 2012, 01:37:29 AM

Previous topic - Next topic

AGP

Since you already have my draw method, I will send you my image.

EgonOlsen

I made this out of it and it works just fine. I made myself two on/off buttons of 128*128 pixels and added some code to repaint the JFrame on click. To make the buttons appear, i had to add a buffer.clearZBuffer(); between blitting the backdrop and the buttons. That's needed because the software renderer's scaled blit is actually an Overlay and therefor has a depth that will be written into the frame buffer. However, the visuals are fine and look in no way like in your screen shot: http://jpct.de/pix/agp.png


package agp;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import com.threed.jpct.*;

public class OpeningScreen extends JFrame implements MouseListener {
private static final long serialVersionUID = 1L;
private Texture background;
protected boolean hasFinished, quitPressed, introPressed;
private FrameBuffer buffer;
private float widthMul, heightMul;
private Button3d playButton, loadButton, introButton, quitButton;

public static void main(String[] args) {
new OpeningScreen();
}

public OpeningScreen() {
FrameBuffer buffer = new FrameBuffer(1024, 768, FrameBuffer.SAMPLINGMODE_NORMAL);

this.buffer = buffer;
quitPressed = false;
widthMul = ((float) buffer.getOutputWidth()) / 1024f;
heightMul = ((float) buffer.getOutputHeight()) / 768f;
background = new Texture("OpeningScreen.png", false);
hasFinished = false;
playButton = new Button3d(406f * widthMul, 360 * heightMul, 202f * widthMul, 80f * heightMul, new Texture("off.png", true), new Texture("on.png", true));
loadButton = new Button3d(406f * widthMul, 440 * heightMul, 202f * widthMul, 80f * heightMul, new Texture("off.png", true), new Texture("on.png", true));
introButton = new Button3d(406f * widthMul, 520 * heightMul, 202f * widthMul, 80f * heightMul, new Texture("off.png", true), new Texture("on.png", true));
quitButton = new Button3d(406f * widthMul, 600 * heightMul, 202f * widthMul, 80f * heightMul, new Texture("off.png", true), new Texture("on.png", true));

this.setSize(1024, 768);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(this);
}

public void processClick(int x, int y) {
if (playButton.contains(x, y))
hasFinished = true;
else if (introButton.contains(x, y))
introPressed = true;
else if (quitButton.contains(x, y))
hasFinished = quitPressed = true;
this.repaint();
}

public void processPress(int x, int y) {
if (playButton.contains(x, y))
playButton.isPressed = true;
else if (loadButton.contains(x, y))
loadButton.isPressed = true;
else if (introButton.contains(x, y))
introButton.isPressed = true;
else if (quitButton.contains(x, y))
quitButton.isPressed = true;
this.repaint();
}

public void processRelease(int x, int y) {
playButton.isPressed = loadButton.isPressed = introButton.isPressed = quitButton.isPressed = false;
this.repaint();
}

@Override
public void paint(Graphics g) {
update(g);
}

@Override
public void update(Graphics g) {
buffer.clear(java.awt.Color.black);
buffer.update();
buffer.blit(background, 0, 0, 0, 0, background.getWidth(), background.getHeight(), buffer.getOutputWidth(), buffer.getOutputHeight(), -1, false);
buffer.clearZBufferOnly();
playButton.draw(buffer);
loadButton.draw(buffer);
introButton.draw(buffer);
quitButton.draw(buffer);
buffer.display(g);
}

@Override
public void mouseClicked(MouseEvent arg0) {
processClick(arg0.getX(), arg0.getY());
}

@Override
public void mouseEntered(MouseEvent arg0) {
//
}

@Override
public void mouseExited(MouseEvent arg0) {
//
}

@Override
public void mousePressed(MouseEvent arg0) {
processPress(arg0.getX(), arg0.getY());
}

@Override
public void mouseReleased(MouseEvent arg0) {
processRelease(arg0.getX(), arg0.getY());
}
}

class Button3d {
private int x, y, width, height;
private Texture unpressed, pressed;
protected boolean isPressed;

public Button3d(float x, float y, float width, float height, Texture unpressed, Texture pressed) {
this.x = (int) (x + .5f);
this.y = (int) (y + .5f);
this.width = (int) (width + .5f);
this.height = (int) (height + .5f);
this.unpressed = unpressed;
this.pressed = pressed;
isPressed = false;
}

public boolean contains(int x, int y) {
if (x > this.x && x <= this.x + this.width && y > this.y && y <= this.y + this.height) {
return true;
}
return false;
}

public void draw(FrameBuffer buffer) {
if (!isPressed) {
buffer.blit(unpressed, 0, 0, x, y, unpressed.getWidth(), unpressed.getHeight(), width, height, 200, false);
} else {
buffer.blit(pressed, 0, 0, x, y, pressed.getWidth(), pressed.getHeight(), width, height, 200, false);
}
if (buffer.usesRenderer(IRenderer.RENDERER_SOFTWARE)) {
java.awt.Graphics g2 = buffer.getGraphics();
g2.setColor(java.awt.Color.blue);
g2.drawRect((int) x, (int) y, (int) width, (int) height);
}
}
}


AGP

That's very strange. I added the clearZBufferOnly() in between the blits, but I saw no improvement.

EgonOlsen

Quote from: AGP on September 06, 2012, 08:59:37 PM
That's very strange. I added the clearZBufferOnly() in between the blits, but I saw no improvement.
That's just to make the buttons appear. It has nothing do to with the backdrop. Have you tried my test case for yourself?

AGP

No, I'll try it, but as far as I can tell the only difference in yours is the lack of a java.awt.Canvas.

AGP

For the record, yours works. From where I'm sitting, this is either a bug on blit (or otherwise, more likely, Overlay) or an awt one related to the use of the canvas. Could you have a look, please?

EgonOlsen

I don't see how this should be related to using a canvas at all. Displaying the rendered image is a simple drawImage-call and that the only line where the component and the engine interact with each other. You could simply try this by opening an additional JFrame in your application and do the drawing into that one instead.
There might be some problem in Overlay, but i've no idea how to track this down. All my usual test cases and this new one work fine. I've no idea what you are doing different to cause this behaviour. I can only suggest that you try to find the difference between what you are doing and what the test case does. Something has to be different.
I can't help code wise in the next two weeks, because i'll be AFK. I'll browse the forum though if the mobile connection allows it.