1
votes

i have an issue with creating rotating dice animation in JAVA in OpenGL. Cube is visible, colored, and rotating, but i also need on each side different texture. For now, i can't see any texture there, but when i use println, it seems, that all textures were initiated and buffered.

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
It would be better to post a sscce: sscce.org Rather call GLU.createGLU(GL) so that the right GLU subclass is used even when you use another profile with another GL implementation, it's more future proof. - gouessej

1 Answers

0
votes

At first, you can look at this simple example of the OpenGL Red Book ported to JOGL 2: texbind.java

You should call gl.glEnable(GL2.GL_TEXTURE_2D) earlier (see the code quoted above).

Moreover, you mix the high level JOGL API with the low level API, it's a bad idea. If you want to use the Texture class, call enable(), bind() and disable(), look at its online Java documentation, don't generate a second texture identifier by calling glGenTextures, it's already done in the Texture class.

TextureLoader.convertImageData() is useless, it doesn't come from JOGL. TextureData, TextureIO, AWTTextureData and AWTTextureIO are enough and do a good job.

Finally, bind the correct textures, those containing some texture data, call Texture.bind().

(optional) Rather use direct NIO buffers, use Buffers.newDirect*Buffer() to create them, otherwise JOGL will have to do so under the hood. Avoid storing texture identifiers in integer arrays, rather use a small direct NIO buffer. It's possible to use a single image file that contains all your existing images, then to create a single texture and to modify your texture coordinates but I assume that your textures are very simple, you can go on that way with a texture per face.