《jogl简明教程》学习笔记3

双缓冲
在1.2.3节的时候有一个示例
代码如下
import javax.media.opengl.*;
import com.sun.opengl.util.Animator;
import java.awt.event.*;
//built on J1_O_Point class
public class J1_1_Point extends J1_0_Point {
static Animator animator; // drive display() in a loop
public J1_1_Point() { 
// use super's constructor to initialize drawing
//1. specify using only a single buffer
capabilities.setDoubleBuffered(false);
//2. add a listener for window closing
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
animator.stop(); // stop animation
System.exit(0);
}
});
}
// called one-time for OpenGL initialization
public void init(GLAutoDrawable drawable) {
// specify a drawing color: red
gl.glColor3f(1.0f, 0.0f, 0.0f);
//3. clear the background to black
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
//4. drive the display() in a loop
animator = new Animator(canvas);
animator.start(); // start animator thread
}
// called for OpenGL rendering every reshape
public void display(GLAutoDrawable drawable) {
//5. generate a random point
double x = Math.random()*WIDTH;
double y = Math.random()*HEIGHT;
// specify to draw a point
gl.glBegin(GL.GL_POINTS);
gl.glVertex2d(x, y);
gl.glEnd();
}
public static void main(String[] args) {
J1_1_Point f = new J1_1_Point();
//6. add a title on the frame
f.setTitle("JOGL J1_1_Point");
f.setSize(WIDTH, HEIGHT);
f.setVisible(true);
}
}
注意用红色标出的代码
原书对于双缓冲的说明
JOGL uses capabilities.setDoubleBuffered(true)to specify the display with double 
buffers. Animator drives the display()method in a loop. When it is running in double 
buffer mode, it swaps the front and back buffers automatically by default, displaying 
the results of the rendering. You can turn automatic swapping off by the following 
method:  drawable.setAutoSwapBufferMode(false). Then, the programmer is 
responsible for calling drawable.swapBuffers()manually.

大致意思是jogl使用capabilities.setDoubleBuffered(true)开始启用双缓冲模式。Animator会循环调用display来绘制动画,默认的它会自动交换两个缓冲区。可以调用drawable.setAutoSwapBufferMode(false)方法停止自动交换缓冲区。手动交换缓冲区使用drawable.swapBuffers()。
另外还有一个方法gl.setSwapInterval(11);可以用来控制交换缓冲区的速率

你可能感兴趣的:(JAVA)