glScissors produces me flickering with animations (JOGL)

301 views Asked by At

I am doing an animation with JOGL. I need to draw some elements but some of them are always the same ones with the same properties and no other object is going to move in that regions. That is why I though in implementing glScissor. So my idea is: I declare a boolean variable as true and I do an if statment that will be only true for the first frame. Inside the if statement I call a function that will paint all what I need as constant and then I enable glScissors. My problem is that although I see that only in the area with scissor's the image is being changed, in the outer region I can see a flickering. It's like if there were black frames between others with the correct image. Why? What am I doing wrong?

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

There are 1 answers

0
Learning from masters On BEST ANSWER

Following http://anirudhsasikumar.net/blog/2006.03.04.html you just need to:

a) write glDrawBuffer(GL_FRONT_AND_BACK) in init()

OR

b) copy the front buffer to the back buffer. If you change the default drawing buffer or the default logic operation, you'd want to save and restore those as well. The code as shown in that page is:

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); 
}