我想弄清楚在渲染简单方块时openGL(LWJGL)和swing之间有什么区别。不出所料,openGL赢了,但在某些测试中,结果很接近,甚至有利于摆动。为什么会这样?
我的测试结果:
Testing for a total of 500000 rectangles
Swing time: 0.16367821
OpenGL time: 0.06317429
OpenGL relative to SWING is faster by 159.0899%
Testing for a total of 750000 rectangles
OpenGL time: 0.066062525
Swing time: 0.16988374
OpenGL relative to SWING is faster by 157.156%
Testing for a total of 1000000 rectangles
OpenGL time: 0.0907693
Swing time: 0.15694521
OpenGL relative to SWING is faster by 72.90561%
Testing for a total of 1250000 rectangles
OpenGL time: 0.09591239
Swing time: 0.17011923
OpenGL relative to SWING is faster by 77.369385%
Testing for a total of 1500000 rectangles
OpenGL time: 0.11926948
Swing time: 0.21623997
OpenGL relative to SWING is faster by 81.303696%
Testing for a total of 1750000 rectangles
OpenGL time: 0.16327758
Swing time: 0.25741237
OpenGL relative to SWING is faster by 57.65323%
Testing for a total of 2000000 rectangles
OpenGL time: 0.17265266
Swing time: 0.2788536
OpenGL relative to SWING is faster by 61.511322%
Testing for a total of 2250000 rectangles
OpenGL time: 0.2492242
Swing time: 0.3022127
OpenGL relative to SWING is faster by 21.261368%
Testing for a total of 2500000 rectangles
Swing time: 0.3231118
OpenGL time: 0.29499054
OpenGL relative to SWING is faster by 9.532944%
Testing for a total of 2750000 rectangles
OpenGL time: 0.34329778
Swing time: 0.38381234
OpenGL relative to SWING is faster by 11.801575%
Testing for a total of 3000000 rectangles
OpenGL time: 0.34859535
Swing time: 0.39274055
OpenGL relative to SWING is faster by 12.663734%
Testing for a total of 3250000 rectangles
Swing time: 0.4241282
OpenGL time: 0.44056854
OpenGL relative to SWING is faster by -3.7316208%
Testing for a total of 3500000 rectangles
Swing time: 0.4600469
OpenGL time: 0.4737318
OpenGL relative to SWING is faster by -2.8887482%
Testing for a total of 3750000 rectangles
Swing time: 0.40855232
OpenGL time: 0.25052726
OpenGL relative to SWING is faster by 63.07698%
Testing for a total of 4000000 rectangles
Swing time: 0.5119725
OpenGL time: 0.55266017
OpenGL relative to SWING is faster by -7.362152%
Testing for a total of 4250000 rectangles
OpenGL time: 0.5010328
Swing time: 0.57198834
OpenGL relative to SWING is faster by 14.16185%
Testing for a total of 4500000 rectangles
OpenGL time: 0.53123826
Swing time: 0.5992712
OpenGL relative to SWING is faster by 12.806473%
Testing for a total of 4750000 rectangles
OpenGL time: 0.5412617
Swing time: 0.6458795
OpenGL relative to SWING is faster by 19.328514%
Testing for a total of 5000000 rectangles
OpenGL time: 0.58324844
Swing time: 0.69343716
OpenGL relative to SWING is faster by 18.892242%
我正在使用的代码:
package swingVSopengl;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
public class SwingVsOpenGL {
static int amount = 0;
static float swingtime, opengltime;
public static void main(String[] args) {
for (amount = 500000; amount <= 5000000; amount += 250000) {
System.out.println("Testing for a total of "
+ String.valueOf(amount) + " rectangles");
new SwingWay();
new OpenGLWay();
System.out.println("OpenGL relative to SWING is faster by "
+ String.valueOf(swingtime / opengltime * 100 - 100) + "%");
System.out.println();
}
System.exit(0);
}
private static class SwingWay extends JFrame {
public SwingWay() {
setSize(64, 64);
add(new JPanel() {
@Override
public void paint(Graphics g) {
super.paint(g);
long time = System.nanoTime();
for (int i = 0; i < amount; i++) {
g.fillRect(0, 0, 16, 16);
}
swingtime = (float) (System.nanoTime() - time) / 1000000000;
System.out.print("Swing time: ");
System.out.println(swingtime);
dispose();
}
});
setVisible(true);
}
}
private static class OpenGLWay {
OpenGLWay() {
try {
Display.setDisplayMode(new DisplayMode(64, 64));
Display.setTitle("A fresh display!");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
}
glMatrixMode(GL_PROJECTION);
glOrtho(0, 640, 480, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
long time = System.nanoTime();
for (int i = 0; i < amount; i++) {
glBegin(GL_QUADS);
glVertex2i(0, 0);
glVertex2i(16, 0);
glVertex2i(16, 16);
glVertex2i(0, 16);
glEnd();
}
opengltime = (float) (System.nanoTime() - time) / 1000000000;
System.out.print("OpenGL time: ");
System.out.println(opengltime);
Display.destroy();
}
}
}
答案 0 :(得分:6)
渲染单个方块并不是很困难。从本质上讲,它归结为窗口化一段内存并用特定值填充它。
此外,您使用OpenGL的方式也会遭受巨大的开销。你测量的不是SWING与OpenGL的效率,而是函数调用的效率。 Ditch glBegin
... glEnd
(它们已经超过15年了)并使用顶点数组。也不要通过用绘图命令用勺子喂它来使GPU饿死。而是通过每个绘图批次至少100个基元的顶点数组为它提供一个All-You-Can-Eat。只有这样,你才能离开高地。