Search code examples
javaopengljoglglcanvas

Java OpenGL draw textures


I can draw one texture on com.jogamp.opengl.awt.GLCanvas but i want to update texture when bufferedimage changed. I wrote display method like this:

    public void display(GLAutoDrawable glAutoDrawable) {
        try {
            if (gl2 == null) {
                gl2 = glAutoDrawable.getGL().getGL2();  // get the OpenGL 2 graphics context
            }
            gl2.glClear(gl2.GL_COLOR_BUFFER_BIT | gl2.GL_DEPTH_BUFFER_BIT); // clear color and depth buffers
            gl2.glLoadIdentity();  // reset the model-view matrix


            if (image != null) {

                if (image != null) {
                    int[] pixels_raw = new int[this.camPanel.width * this.camPanel.height];
                    pixels_raw = image.getRGB(0, 0, this.camPanel.width, this.camPanel.height, null, 0, this.camPanel.width);

                    pixels.rewind();
                    for (int j = this.camPanel.height - 1; j >= 0; j--) {
                        for (int i = 0; i < this.camPanel.width; i++) {
                            int pixel = pixels_raw[j * this.camPanel.width + i];
                            pixels.put((byte) ((pixel >> 16) & 0xFF));//RED
                            pixels.put((byte) ((pixel >> 8) & 0xFF));//GREEN
                            pixels.put((byte) ((pixel >> 0) & 0xFF));//BLUE
                            pixels.put((byte) ((pixel >> 24) & 0xFF));//ALPHA
                        }
                    }

                    pixels.flip();

//                    gl2.glBindTexture(gl2.GL_TEXTURE_2D, 1);
//                    gl2.glTexParameterf(gl2.GL_TEXTURE_2D, gl2.GL_TEXTURE_WRAP_S, gl2.GL_REPEAT);
//                    gl2.glTexParameterf(gl2.GL_TEXTURE_2D, gl2.GL_TEXTURE_WRAP_T, gl2.GL_REPEAT);
//                    gl2.glTexParameterf(gl2.GL_TEXTURE_2D, gl2.GL_TEXTURE_MIN_FILTER, gl2.GL_NEAREST);
//                    gl2.glTexParameterf(gl2.GL_TEXTURE_2D, gl2.GL_TEXTURE_MAG_FILTER, gl2.GL_NEAREST);
                    gl2.glTexImage2D(gl2.GL_TEXTURE_2D, 0, gl2.GL_RGBA, this.camPanel.width, this.camPanel.height, 0, gl2.GL_RGBA, gl2.GL_UNSIGNED_BYTE, pixels);
//                    gl2.glFlush();
                    System.out.println("Error " + gl2.glGetError());

                    pixels.clear();
                }
                gl2.glEnd();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

When image changed, i am calling display metod but there is always black screen and gives error : "JAWTWindow: surface change 0x17011262 -> 0x7e011c18" "Error 1282"


Solution

  • The error is thrown by the call to glEnd(). The docs states:

    GL_INVALID_OPERATION is generated if glEnd is executed without being preceded by a glBegin.

    Since the code only contains a glEnd() but no glBegin, this call is invalid.