Java OpenGL绘制纹理

时间:2018-03-19 14:09:03

标签: java opengl jogl glcanvas

我可以在com.jogamp.opengl.awt.GLCanvas上绘制一个纹理,但我想在bufferedimage更改时更新纹理。我写了这样的显示方法:

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

当图像改变时,我正在调用显示metod,但总是有黑屏并给出错误: “JAWTWindow:表面变化0x17011262 - &gt; 0x7e011c18” “错误1282”

1 个答案:

答案 0 :(得分:4)

调用glEnd()会引发错误。 docs州:

  

如果执行glEnd而没有前面的glBegin,则会生成GL_INVALID_OPERATION。

由于代码只包含glEnd()但不包含glBegin,因此此调用无效。