glScissors让我动画闪烁(JOGL)

时间:2017-01-04 11:50:50

标签: java animation jogl flicker

我正在用JOGL做动画。我需要绘制一些元素,但是其中一些元素总是具有相同属性的相同元素,并且其他对象不会在该区域中移动。这就是我在实施glScissor时的原因。所以我的想法是:我声明一个布尔变量为true,我做一个if语句,只对第一帧是真的。在if语句中,我调用一个函数,将我需要的所有内容绘制为常量,然后启用glScissors。我的问题是,虽然我看到只有剪刀区域的图像正在改变,但在外部区域我可以看到闪烁。就好像其他人之间有正确的图像。为什么?我做错了什么?

boolean first = true;    

public void init(GLAutoDrawable drawable) {
    final GL2 gl = (GL2) drawable.getGL();

    // Enable z- (depth) buffer for hidden surface removal. 
    gl.glEnable(GL.GL_DEPTH_TEST);
    gl.glDepthFunc(GL.GL_LEQUAL);

    // Enable smooth shading.
    gl.glShadeModel(GL2.GL_SMOOTH);

    // Define "clear" color.
    gl.glClearColor(1f, 1f, 1f, 1f);

    // We want a nice perspective.
    gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);

    // Create GLU.
    glu = new GLU();
    GLProfile profile = GLProfile.get(GLProfile.GL2);

    gl.glPixelStorei(gl.GL_PACK_ALIGNMENT, 1);
}

public void display(GLAutoDrawable drawable) {

    final GL2 gl = (GL2) drawable.getGL();

    // Clear screen.
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

    // Set camera.
    setCamera(gl, glu, 30);

    // Prepare light parameters.
    float SHINE_ALL_DIRECTIONS = 1;
    float[] lightPos = {0, 0, 0, SHINE_ALL_DIRECTIONS};
    float[] lightColorAmbient = {1f, 1f, 1f, 1f};
    float[] lightColorSpecular = {1f, 1f, 1f, 1f};

    // Set light parameters.
    gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, lightPos, 0);
    gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_AMBIENT, lightColorAmbient, 0);
    gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_SPECULAR, lightColorSpecular, 0);

    // Enable lighting in GL.
    gl.glEnable(GL2.GL_LIGHT1);
    gl.glEnable(GL2.GL_LIGHTING);
    if (first==true){
        first=false;
        //inside plot_constant_object I don't change anything related with lighting
        Plot_constant_objects(drawable, gl);
        gl.glScissor(100, 100, 500, 500);
        gl.glEnable(gl.GL_SCISSOR_TEST);
    }
    //Draw other stuff

}

1 个答案:

答案 0 :(得分:0)

关注http://anirudhsasikumar.net/blog/2006.03.04.html您只需:

a)在init()中写glDrawBuffer(GL_FRONT_AND_BACK)

OR

b)将前缓冲区复制到后台缓冲区。如果更改默认绘图缓冲区或默认逻辑操作,则还需要保存和恢复它们。该页面中显示的代码是:

void copy_buffer()
{
  static GLint viewport[4];
  static GLfloat raster_pos[4];

  glGetIntegerv(GL_VIEWPORT, viewport);

  /* set source buffer */
  glReadBuffer(GL_FRONT);

  /* set projection matrix */
  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity() ;
  gluOrtho2D(0, viewport[2], 0, viewport[3]);

  /* set modelview matrix */
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glLoadIdentity();

  /* save old raster position */
  glGetFloatv(GL_CURRENT_RASTER_POSITION, raster_pos);

  /* set raster position */
  glRasterPos4f(0.0, 0.0, 0.0, 1.0);

  /* copy buffer */
  glCopyPixels(0, 0, viewport[2], viewport[3], GL_COLOR);

  /* restore old raster position */
  glRasterPos4fv(raster_pos);

  /* restore old matrices */
  glPopMatrix();
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();
  glMatrixMode(GL_MODELVIEW);

  /* restore source buffer */
  glReadBuffer(GL_BACK); 
}