《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);可以用来控制交换缓冲区的速率
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值