简体   繁体   中英

JOGL use 2d texture on a 3d Cube

I have created a 3d cube in JOGL using the following code:

/**
 * Function used to draw a cube
 */
public void drawCube(GL gl) {


    gl.glBegin(gl.GL_QUADS);

    //gl.glColor3f(1, 0, 0);
        gl.glTexCoord3f(0, 0, 0);
        gl.glVertex3f(0, 0, 0);
        gl.glTexCoord3f(4, 0, 0);
        gl.glVertex3f(1, 0, 0);
        gl.glTexCoord3f(4, 4, 0);
        gl.glVertex3f(1, 1, 0);
        gl.glTexCoord3f(0, 4, 0);
        gl.glVertex3f(0, 1, 0);
    gl.glEnd();



    gl.glBegin(gl.GL_QUADS);
        gl.glTexCoord3f(0, 0, 0);
    // gl.glColor3f(0, 1, 0);
        gl.glVertex3f(0, 0, 0);
        gl.glTexCoord3f(0, 4, 0);
        gl.glVertex3f(0, 1, 0);
        gl.glTexCoord3f(0, 4, 4);
        gl.glVertex3f(0, 1, 1);
        gl.glTexCoord3f(0, 0, 4);
        gl.glVertex3f(0, 0, 1);
    gl.glEnd();



    gl.glBegin(gl.GL_QUADS);
    //  gl.glColor3f(0, 0, 1);
        gl.glTexCoord3f(0, 4, 0);
        gl.glVertex3f(0, 1, 0);
        gl.glTexCoord3f(4, 4, 0);
        gl.glVertex3f(1, 1, 0);
        gl.glTexCoord3f(4, 4, 4);
        gl.glVertex3f(1, 1, 1);
        gl.glTexCoord3f(0, 4, 4);
        gl.glVertex3f(0, 1, 1);
      gl.glEnd();


    gl.glBegin(gl.GL_QUADS);
    //  gl.glColor3f(1, 1, 0);
        gl.glTexCoord3f(4, 4, 0);
        gl.glVertex3f(1, 1, 0);
        gl.glTexCoord3f(4, 0, 0);
        gl.glVertex3f(1, 0, 0);
        gl.glTexCoord3f(4, 0, 4);
        gl.glVertex3f(1, 0, 1);
        gl.glTexCoord3f(4, 4, 4);
        gl.glVertex3f(1, 1, 1);
    gl.glEnd();

     gl.glBegin(gl.GL_QUADS);
    // gl.glColor3f(1, 0, 1);
        gl.glTexCoord3f(4, 0, 0);
        gl.glVertex3f(1, 0, 0);
        gl.glTexCoord3f(0, 0, 0);
        gl.glVertex3f(0, 0, 0);
        gl.glTexCoord3f(0, 0, 4);
        gl.glVertex3f(0, 0, 1);
        gl.glTexCoord3f(4, 0, 4);
        gl.glVertex3f(1, 0, 1);
    gl.glEnd();

     gl.glBegin(gl.GL_QUADS);
    //   gl.glColor3f(0, 1, 1);
        gl.glTexCoord3f(0, 4, 4);
        gl.glVertex3f(0, 1, 1);
        gl.glTexCoord3f(4, 4, 4);
        gl.glVertex3f(1, 1, 1);
        gl.glTexCoord3f(4, 0, 4);
        gl.glVertex3f(1, 0, 1);
        gl.glTexCoord3f(0, 0, 4);
        gl.glVertex3f(0, 0, 1);

     gl.glEnd();

The cube is on the screen, and now I'm trying to place a texture on each faces like this:

public void init(GLAutoDrawable gld) {
    //Init the gl
    GL gl = gld.getGL();

    //Init the glu
    GLU glu = new GLU();

    gl.glMatrixMode(GL.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluPerspective(90, 1.6, 0.6, 30.0);
    glu.gluLookAt(0, 0, 10, 0, 0, 0, 0, 2, 0);


    gl.glEnable(gl.GL_TEXTURE_2D);


    int id_textura = Gen_Textura(gl);


    gl.glBindTexture(gl.GL_TEXTURE_2D, id_textura);


    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR);
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR);
}

The problem is that the texture shows up only on one face of the cube, not on all 6.

How can I make the texture to be drawn on all the faces of the cube?

Answer based on my comment above.

You should use glTexCoord2f , since you're using 2D textures. Also, texture coordinates are usually in the range [0, 1] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM