JOGL在3d立方体上使用2d纹理

时间:2012-04-19 19:59:24

标签: java opengl jogl

我使用以下代码在JOGL中创建了一个3d立方体:

/**
 * 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();

立方体在屏幕上,现在我正试图在每个面上放置一个纹理,如下所示:

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

问题是纹理只显示在立方体的一个面上,而不是全部显示在纹身上。

如何在立方体的所有面上绘制纹理?

1 个答案:

答案 0 :(得分:2)

根据我上面的评论回答。

您应该使用glTexCoord2f,因为您正在使用2D纹理。此外,纹理坐标通常在[0, 1]范围内。