gpt4 book ai didi

Java Opengl 3D 渲染

转载 作者:行者123 更新时间:2023-12-02 07:43:28 24 4
gpt4 key购买 nike

我正在尝试创建几个具有 2D 纹理的 3D block ,但不起作用,因为立方体的某些面对其他面是透明的。以下是我定义 block 的类的部分代码:

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

}

这是我称之为几个 block 的类:

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

}

如何解决透明度问题?

最佳答案

将以下代码放入初始化 block 中:

glEnable(GL_DEPTH_TEST);

关于Java Opengl 3D 渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11264976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com