0

I'm trying to create several 3D blocks with a 2D texture, but is not working because some sides of the cubes are transparent to others. Here is a part of code from the class where I define the blocks:

    public void Render(){

            try {
        this.texture = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/gold1.png")));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    squareDisplayList = glGenLists(1);
    glNewList(squareDisplayList, GL_COMPILE);
    {

        glBegin(GL_QUADS);
        //trás
        glTexCoord2f(1, 0);
        glVertex3f(x2, y1, z1);
        glTexCoord2f(1, 1);
        glVertex3f(x2, y2, z1);
        glTexCoord2f(0, 1);
        glVertex3f(x1, y2, z1);
        glTexCoord2f(0, 0);
        glVertex3f(x1, y1, z1);

        //cima
        glTexCoord2f(1, 0);
        glVertex3f(x2, y1, z2);
        glTexCoord2f(1, 1);
        glVertex3f(x1, y1, z2);
        glTexCoord2f(0, 1);
        glVertex3f(x1, y1, z1);
        glTexCoord2f(0, 0);
        glVertex3f(x2, y1, z1);

        //baixo
        glTexCoord2f(1, 0);
        glVertex3f(x2, y2, z1);
        glTexCoord2f(1, 1);
        glVertex3f(x2, y2, z2);
        glTexCoord2f(0, 1);
        glVertex3f(x1, y2, z2);
        glTexCoord2f(0, 0);
        glVertex3f(x1, y2, z1);

        //direito
        glTexCoord2f(1, 0);
        glVertex3f(x2, y1, z2);
        glTexCoord2f(1, 1);
        glVertex3f(x2, y2, z2);
        glTexCoord2f(0, 1);
        glVertex3f(x2, y2, z1);
        glTexCoord2f(0, 0);
        glVertex3f(x2, y1, z1);

        //esquerdo
        glTexCoord2f(1, 0);
        glVertex3f(x1, y1, z2);
        glTexCoord2f(1, 1);
        glVertex3f(x1, y2, z2);
        glTexCoord2f(0, 1);
        glVertex3f(x1, y2, z1);
        glTexCoord2f(0, 0);
        glVertex3f(x1, y1, z1);

        //frente
        glTexCoord2f(1, 0);
        glVertex3f(x2, y1, z2);
        glTexCoord2f(1, 1);
        glVertex3f(x2, y2, z2);
        glTexCoord2f(0, 1);
        glVertex3f(x1, y2, z2);
        glTexCoord2f(0, 0);
        glVertex3f(x1, y1, z2);
        glEnd();

    }
    glEndList();

}

 public void Draw(){

    glCallList(squareDisplayList);

}

And here is the class here I call the several blocks:

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective((float) 30, 880f / 580f, 0.001f,100);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_TEXTURE_2D);

    for(int ix = 0; ix < world_width; ix++){

        for(int iy = 0; iy < world_hight; iy++){    

            for(int iz = 0; iz < world_height; iz++){

                render[ix][iy][iz].Render();

            }

        }

    }   

    while(!Display.isCloseRequested()){

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glTranslatef(xspeed, -jumpsd, zspeed);

        for(int ix = 0; ix < world_width; ix++){

            for(int iy = 0; iy < world_hight; iy++){    

                for(int iz = 0; iz < world_height; iz++){

                    render[ix][iy][iz].Draw();

                }

            }

        }

        Display.update();
        Display.sync(35);

    }

How can I solve the transparency problem?

1 Answer 1

2

Put the following code in your initialisation block:

glEnable(GL_DEPTH_TEST);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.