Search code examples
javagraphicsfragment-shaderjogl

JOGL glDrawArrays doesn't work after calling glClear(GL_COLOR_BUFFER_BIT)


openGL initialization code on reshape method(GLEventListener):

    public void reshape(GLAutoDrawable glautodrawable, int x, int y, int width, int height) {
        final GL2 gl = glautodrawable.getGL().getGL2();

        //gl.glFrontFace(GL2.GL_CW);
        gl.glEnable(GL2.GL_DEPTH_TEST);
        gl.glDepthMask(true);
        gl.glClearDepth(1.0);
        gl.glDepthFunc(GL2.GL_ALWAYS);
        gl.glColorMask(true, true, true, true);
        gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
        gl.glClearColor(0.0f, 1.0f, 0.0f, 0.5f);
        
        //build Buffers, program and extra;
    }

display method without clear color Buffer. I used FPS Animator.

    public void display( GLAutoDrawable drawable ) {
        final GL2 gl = drawable.getGL().getGL2();
        gl.glClear(GL2.GL_DEPTH_BUFFER_BIT);

        float time = (float)(System.currentTimeMillis() - startTime) / 1000.0f;
        System.out.println("Time: " + time);
        gl.glUniform1f(0, time);
        gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, 4);
    }

result: enter image description here



display method with clear buffer:

    public void display( GLAutoDrawable drawable ) {
        final GL2 gl = drawable.getGL().getGL2();
        gl.glClear(GL2.GL_DEPTH_BUFFER_BIT | GL2.GL_COLOR_BUFFER_BIT);

        float time = (float)(System.currentTimeMillis() - startTime) / 1000.0f;
        System.out.println("Time: " + time);
        gl.glUniform1f(0, time);
        gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, 4);
    }

result: enter image description here



So as I think, the results should be same because I cleared the buffer and then drawed a square. But as you can see in the result above, there is only a green clear color.

uniform location 0 is float variable "time" in fragment shader that affect the brightness of the stars in square. But It doesn't change over time as I expected. But only the first call of glUniform1f() is applyed like this(gl.glUniform1f(0, 20)):

enter image description here

Funny thing is, when I tryied example on internet with glClear(GL_COLOR_BUFFER_BIT), It works fine.

   public void display( GLAutoDrawable drawable ) {

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

       gl.glClear(GL2.GL_DEPTH_BUFFER_BIT | GL2.GL_COLOR_BUFFER_BIT);

       gl.glBegin( GL2.GL_TRIANGLES );

       gl.glColor3f( 1.0f, 0.0f, 0.0f );   // Red
       gl.glVertex3f( 0.5f,0.7f,0.0f );    // Top
       gl.glColor3f( 0.0f,1.0f,0.0f );     // blue
       gl.glVertex3f( -0.2f,-0.50f,0.0f ); // Bottom Left
       gl.glColor3f( 0.0f,0.0f,1.0f );     // green
       gl.glVertex3f( 0.5f,-0.5f,0.0f );   // Bottom Right

       gl.glEnd();
       gl.glFlush();
   }

result: enter image description here


what is the problem of my code? or is it version problem or bug? and why example works fine but mine is not?
or is there a way to check what is actually happening/debugging?

Solution

  • looking for some examples using glDrawArrays or glDrawElements, I found this.

    github link

    And the difference with my code was two line:

    //after rendering in display method
    gl.glUseProgram(0);
    gl.glBindVertexArray(0);
    

    After add two line in my code, it works as I intended. A square drawn after glClear(GL_COLOR_BUFFER_BIT) works fine, and uniform value changes overtime as I intended. But I'm not sure about the why this is the solution of my problem. Because when I use openGL in C++, code works fine without two lines of code just added.