0

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"

Erkan
  • 41
  • 8
  • Which line gives the `GL_INVALID_OPERATION` error? And I don't see any draw command in the code, so it's not surprising that the screens stays black. – BDL Mar 19 '18 at 15:41
  • Your source code is poorly implemented. You don't respect the guidelines. You must NOT store GL instances in class fields as you might be tempted to use them when they have become invalid or on the wrong thread or when the OpenGL context isn't current. Moreover, you could use AWTTextureIO to get a TextureData from your BufferedImage. Of course, maybe there is a much more efficient solution, it depends on your use case. I'm not sure that you should use glTexImage2D here. – gouessej Mar 20 '18 at 09:50
  • Please post a SSCCE. Use an animator. You don't have to call display(GLAutoDrawable) directly. – gouessej Mar 20 '18 at 10:03

1 Answers1

5

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.

BDL
  • 21,052
  • 22
  • 49
  • 55
  • It is true. I changed the code and there is no GL_INVALID_OPERATION error. But black screen problem continues, could it be related to JAWTWindow: surface change 0x7c011681 -> 0x57011dea. – Erkan Mar 19 '18 at 16:27
  • 1
    @Erkan You never draw anything in the code you provided. I'd suggest that you open a new question and show the related rendering code since posting it here will most likely invalidate this answer. – BDL Mar 19 '18 at 21:29