所以我的 opengl 游戏中有几个不同的对象。如果我加载纹理或为顶点着色,我在游戏中的所有其他对象都会被赋予该纹理或它们的顶点被着色。所以,例如。我有一堆立方体,我将纹理“a”应用到。然后我有一个天空盒,我将纹理“b”应用到。如果我然后将两者都渲染出来,它们出于某种原因都具有纹理“b”。我调用glPushMatrix()
并且glPopMatrix()
在渲染立方体时,为什么它们具有天空盒的纹理?这是一些代码:
public void render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glLoadIdentity();
glRotatef(input.xrot, 1.0f, 0, 0);
glRotatef(input.yrot, 0, 1.0f, 0);
glTranslatef(-input.xpos, -input.ypos - 19, -input.zpos - 5);
box.render();
chunk.render();
BitMapFont.drawString("X: " + (int)-input.xpos + " Y: " + (int)-input.ypos + " Z: " + (int)-input.zpos, 10, 0);
}
我的块类中的渲染方法:
public void render() {
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboV);
glVertexPointer(vertSize, GL_FLOAT, 0, 0L);
GL15.glBindBuffer(GL15.GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING, vboT);
glTexCoordPointer(3, GL_FLOAT, 0, 0L);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glPushMatrix();
glDrawArrays(GL_QUADS, 0, vertSize * vertAmount * faceAmount * currentBlockCount);
glPopMatrix();
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
然后是我的天空盒类:
public class SkyBox {
Texture tex;
int x, z, y, offSet;
public void bindTex() {
tex.bind();
}
public SkyBox(int x, int z, int y) {
this.x = x;
this.z = z;
this.y = y;
this.offSet = 128;
try {
tex = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/Blocks/air.png")));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void render() {
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(x, z, y);
glTexCoord2f(1, 0);
glVertex3f(x + offSet, z, y);
glTexCoord2f(1, 1);
glVertex3f(x + offSet, z, y + offSet);
glTexCoord2f(0, 1);
glVertex3f(x, z, y + offSet);
glTexCoord2f(1, 0);
glVertex3f(x, z, y + offSet);
glTexCoord2f(1, 0);
glVertex3f(x + offSet, z, y + offSet);
glTexCoord2f(1, 1);
glVertex3f(x + offSet, z + offSet, y + offSet);
glTexCoord2f(0, 1);
glVertex3f(x, z + offSet, y + offSet);
glTexCoord2f(0, 0);
glVertex3f(x + offSet, z, y);
glTexCoord2f(0, 1);
glVertex3f(x + offSet, z + offSet, y);
glTexCoord2f(1, 1);
glVertex3f(x + offSet, z + offSet, y + offSet);
glTexCoord2f(0, 1);
glVertex3f(x + offSet, z, y + offSet);
glTexCoord2f(0, 0);
glVertex3f(x, z + offSet, y);
glTexCoord2f(1, 0);
glVertex3f(x, z, y);
glTexCoord2f(1, 1);
glVertex3f(x, z, y + offSet);
glTexCoord2f(0, 1);
glVertex3f(x, z + offSet, y + offSet);
glTexCoord2f(0, 0);
glVertex3f(x, z + offSet, y + offSet);
glTexCoord2f(1, 0);
glVertex3f(x + offSet, z + offSet, y + offSet);
glTexCoord2f(1, 1);
glVertex3f(x + offSet, z + offSet, y);
glTexCoord2f(0, 1);
glVertex3f(x, z + offSet, y);
glTexCoord2f(0, 0);
glVertex3f(x, z + offSet, y);
glTexCoord2f(1, 0);
glVertex3f(x + offSet, z + offSet, y);
glTexCoord2f(1, 1);
glVertex3f(x + offSet, z, y);
glTexCoord2f(0, 1);
glVertex3f(x, z, y);
glEnd();
}
所以真的,为什么一切都搞砸了?