Re: Using OpenGL in Java2d seems broken.
Daniel Pitts wrote:
It seems to me that the following (SSCCE provided below) should draw a
little yellow circle where the user has moved the mouse. It works fine
if I don't enable opengl, but it doesn't work when I do enable it.
What happens for me: A window appears with decoration, but its contents
are "see through". E.g. it looks like only the decoration was drawn, not
any of the contents. If I move the frame around, it appears that some
sort of double buffer is used, but it never gets updated with the
contents that I provide.
Is this a bug with:
a) my code
b) the JDK
c) The latest GeForce Go 6150 Vista 32-bit drivers from HP (downloaded
and installed yesterday).
d) Something else gone wrong?
<SSCCE filename="OpenGLTest" please-snip-in-reply="true">
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
public class OpenGLTest {
// To see "correct" behavior, set to false.
private static final boolean USE_OPENGL = true;
public static void main(String[] args) {
if (USE_OPENGL) {
System.out.println("Using OpenGL");
System.setProperty("sun.java2d.opengl", "True");
} else {
System.out.println("Not using OpenGL");
}
EventQueue.invokeLater(new Initter());
}
private static void draw(Graphics2D g2d, Point mousePosition) {
g2d.setPaint(Color.blue);
g2d.fill(new Rectangle(0, 0, 640, 480));
Ellipse2D.Double circle = new Ellipse2D.Double();
if (mousePosition != null) {
g2d.setPaint(Color.yellow);
circle.setFrame(mousePosition, new Dimension(10, 10));
g2d.fill(circle);
}
}
private static class Initter implements Runnable {
public void run() {
System.out.println("Constructing frame");
JFrame frame = new JFrame("Hello");
final JPanel pane = new PaintlessPanel();
pane.setPreferredSize(new Dimension(640, 480));
pane.addMouseMotionListener(new MouseUpdater(pane));
frame.getContentPane().add(pane);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
System.out.println("Done constructing frame.");
}
private static class MouseUpdater extends MouseMotionAdapter {
private final JPanel pane;
public MouseUpdater(JPanel pane) {
this.pane = pane;
}
public void mouseMoved(MouseEvent e) {
System.out.println("Mouse moved to: " + e.getPoint());
Graphics graphics = pane.getGraphics();
draw((Graphics2D) graphics, e.getPoint());
graphics.dispose();
}
}
private static class PaintlessPanel extends JPanel {
public void paint(Graphics g) {
System.out.println("Paint even called and ignored.");
// draw((Graphics2D) g, getMousePosition(true));
}
}
}
}
</SSCCE>
Thanks,
Daniel.
I'm running 1.6.0_10-rc and I see a very slow yellow circle following
the mouse pointer with OpenGL enabled. Disabled it works fine and
refreshes much faster.
Win XP SP 3
1.6.0_10-rc
Nvidia NVS 285 (with the latest drivers)
I know that a lot of work is being done on rendering in _10. I even
found a bug related to a pixel shader that appears on NVidia cards
similar to mine.
Do you actually need OpenGL or are you trying to improve performance?
There are a lot of issues with different video cards, OpenGL and the d3d
pipeline. Below is the program I used to find a bug in the new _10
pixel shader for my video card. I found that running the program on an
older version of _10 worked almost twice as fast as a later version.
Anyway, if performance is what you are trying to get, run the program
below without and command line options and then with
-Dsun.java2d.opengl=True and then with -Dsun.java2d.d3d=False and see
what sort of speeds you get. Mine are 290, 60 and 450 respectively.
The important thing to note here is that this program tests
AffineTranform rotations. Some other 2D effects may run much faster or
slower. And different hardware will affect this in unpredictable ways.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
public class test2 extends Canvas implements Runnable {
volatile Thread thread;
volatile BufferStrategy bs;
double angle;
long then = System.currentTimeMillis();
int n;
double rate;
public test2() {
setIgnoreRepaint(true);
setPreferredSize(new Dimension(400,300));
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
if (bs == null) {
createBufferStrategy(2);
bs = getBufferStrategy();
System.out.println(bs);
}
}
});
}
public void start() {
then = System.currentTimeMillis();
thread = new Thread(this);
thread.start();
}
public void stop() {
thread.interrupt();
}
public void run() {
while (!thread.interrupted()) {
render();
}
}
public void render() {
do {
do {
int w = getWidth();
int h = getHeight();
Graphics2D g = (Graphics2D)bs.getDrawGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.fillRect(0,0,w,h);
AffineTransform at = g.getTransform();
angle += 0.001;
g.rotate(angle,w/2,h/2);
g.setColor(Color.BLUE);
g.fillRect(w/2 - 100,h/2 - 100,200,200);
if (++n % 100 == 0) {
long now = System.currentTimeMillis();
long time = now - then;
then = now;
rate = 100000.0 / time;
}
g.setTransform(at);
g.setColor(Color.RED);
g.drawString(String.format("%3.1f",rate),10,10);
g.dispose();
} while (bs.contentsRestored()) ;
bs.show();
} while (bs.contentsLost()) ;
}
public static void main(String[] args) {
final test2 t2 = new test2();
final Frame f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent we) {
t2.start();
}
public void windowClosing(WindowEvent we) {
t2.stop();
f.dispose();
}
});
f.add(t2,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
}
--
Knute Johnson
email s/nospam/knute2008/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access