JAVA OpenGL 3D立方体多重纹理

时间:2015-07-07 18:35:55

标签: java opengl textures jogl

我在OpenGL中使用JAVA创建旋转骰子动画时遇到问题。立方体是可见的,有色的和旋转的,但我还需要在每一侧不同的纹理。现在,我无法看到任何纹理,但是当我使用println时,似乎所有纹理都被启动和缓冲。

public class Renderer implements GLEventListener, MouseListener,
        MouseMotionListener, KeyListener {

    private int rotationAngle;

    private boolean isClicked = false;

    private static int[] textures = new int[6];

    private String textureList[] = { "Dice1.jpg","Dice2.jpg","Dice3.jpg","Dice4.jpg","Dice5.jpg","Dice6.jpg" };

    private Texture texture;
     @Override
        public void display(GLAutoDrawable drawable) {
            GL2 gl = drawable.getGL().getGL2();
            gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
            gl.glLoadIdentity();

            glu.gluLookAt(
                    0f, 0f, 3f,
                    0f, 0f, 0f,
                    0f, 1f, 0f
            );
            gl.glRotatef(rotationAngle, 1f, 0f, 1f);
            draw(gl);

            if(!isClicked){
                rotationAngle += 3f;
            } else  {
                 rotationAngle +=  0f;
            }  
                if (rotationAngle >= 360f)
                    rotationAngle %= 360f;

        }




        @Override
        public void init(GLAutoDrawable drawable) {
            GL2 gl = drawable.getGL().getGL2();




             gl.glGenTextures(6, textures, 0);
             for (int i=0;i<6;i++)
                {     

                    //...and bind it to our array
                    gl.glBindTexture(GL2.GL_TEXTURE_2D, textures[i]);
                    gl.glEnable(GL2.GL_TEXTURE_2D);
                    File file = new File(textureList[i]);
                    BufferedImage image = null;
                    try {
                        image = ImageIO.read(file);

                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    try {
                        texture = TextureIO.newTexture(file, false);                        
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    if(image!=null && texture != null){
                        ByteBuffer buf = TextureLoader.convertImageData(image);
                        gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, 3, 100, 100, 0, GL2.GL_BGR, GL2.GL_UNSIGNED_BYTE, buf);
                        //Create Nearest Filtered Texture
                        gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR);
                        gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_NEAREST);

                        //Different possible texture parameters, e.g. GL2.GL_CLAMP_TO_EDGE
                        gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_REPEAT);
                        gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_REPEAT);


                        file = null;
                    }
                }

             gl.glEnable(GL2.GL_DEPTH_TEST);
            gl.glDepthFunc(GL2.GL_LEQUAL);
            gl.glClearDepthf(1.0f);
        }

        @Override
        public void reshape(GLAutoDrawable drawable, int x, int y, int width,
                int height) {
            GL2 gl = drawable.getGL().getGL2();
            gl.glViewport(x, y, width, height);

            gl.glMatrixMode(GL2.GL_PROJECTION);
            gl.glLoadIdentity();
            glu.gluPerspective(60.0f, (float) width/height, 1f, 10f);

            gl.glMatrixMode(GL2.GL_MODELVIEW);
        }

        private GLUgl2 glu = new GLUgl2();  

        public static void draw(GL2 gl){
            //Point to our buffers
            gl.glBindTexture(GL2.GL_TEXTURE_2D, textures[0]); // Texture 0 = 0.bmp
            gl.glBegin(GL2.GL_QUADS);

                // Front Face Of Cube
                gl.glColor3f(0.0f, 0.0f, 1.0f);
                gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f,  1.0f);
                gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f,  1.0f);
                gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f,  1.0f,  1.0f);
                gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f,  1.0f,  1.0f);

            gl.glEnd();

            gl.glBindTexture(GL2.GL_TEXTURE_2D, textures[1]); // Texture 1 = 1.bmp              
            gl.glBegin(GL2.GL_QUADS);

                // Back Face Of Cube
                gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
                gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f,  1.0f, -1.0f);
                gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f,  1.0f, -1.0f);
                gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);

            gl.glEnd();

            gl.glBindTexture(GL2.GL_TEXTURE_2D, textures[2]); // Texture 2 = 2.bmp     
            gl.glBegin(GL2.GL_QUADS);

                // Top Face Of Cube
                gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f,  1.0f, -1.0f);
                gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f,  1.0f,  1.0f);
                gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f,  1.0f,  1.0f);
                gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f,  1.0f, -1.0f);

            gl.glEnd();

            gl.glBindTexture(GL2.GL_TEXTURE_2D, textures[3]); // Texture 3 = 3.bmp
            gl.glBegin(GL2.GL_QUADS);

                // Bottom Face Of Cube
                gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
                gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
                gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f,  1.0f);
                gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f,  1.0f);

            gl.glEnd();

            gl.glBindTexture(GL2.GL_TEXTURE_2D, textures[4]); // Texture 4 = = 4.bmp
            gl.glBegin(GL2.GL_QUADS);

                // Right face Of Cube
                gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
                gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f,  1.0f, -1.0f);
                gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f,  1.0f,  1.0f);
                gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f,  1.0f);

            gl.glEnd();

            gl.glBindTexture(GL2.GL_TEXTURE_2D, textures[5]); // Texture 5 = 5.bmp
            gl.glBegin(GL2.GL_QUADS);

                // Left Face Of Cube
                gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
                gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f,  1.0f);
                gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f,  1.0f,  1.0f);
                gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f,  1.0f, -1.0f);

            gl.glEnd();
        }

        @Override  public void dispose(GLAutoDrawable drawable) {}      
    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}
    public void mouseClicked(MouseEvent e) { isClicked = true;  }
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseMoved(MouseEvent e) {}
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}
    @Override public void keyPressed(KeyEvent arg0) {}
    @Override public void mouseDragged(MouseEvent arg0) {}
    @Override public void mousePressed(MouseEvent arg0) {}
}

1 个答案:

答案 0 :(得分:0)

首先,您可以看一下移植到JOGL 2的OpenGL Red Book的这个简单示例: texbind.java

您应该提前致电gl.glEnable(GL2.GL_TEXTURE_2D)(参见上面引用的代码)。

此外,您将高级JOGL API与低级API混合,这是一个坏主意。如果你想使用Texture类,调用enable(),bind()和disable(),看看its online Java documentation,不要通过调用glGenTextures生成第二个纹理标识符,它已经是在Texture类中完成。

TextureLoader.convertImageData()没用,它不是来自JOGL。 TextureData,TextureIO,AWTTextureData和AWTTextureIO就够了,做得很好。

最后,绑定正确的纹理,包含一些纹理数据的纹理,调用Texture.bind()。

(可选)使用直接NIO缓冲区,使用Buffers.newDirect * Buffer()创建它们,否则JOGL将不得不这样做。避免在整数数组中存储纹理标识符,而是使用小型直接NIO缓冲区。可以使用包含所有现有图像的单个图像文件,然后创建单个纹理并修改纹理坐标但我假设您的纹理非常简单,您可以使用纹理继续每张脸。