使用slick-util库在LWJGL(OpenGL)中出现纹理问题

时间:2014-07-08 23:25:38

标签: opengl textures lwjgl

我刚刚开始使用3D图形编程,而且我有一个很好的封装Cube对象。这个类有一个render()方法,它继续推送一个新矩阵,执行转换,glBegin,指定所有顶点和纹理坐标,glEnd,然后弹出矩阵。现在,我已经编写了这个Cube类,其中包含在多维数据集的不同面上使用不同纹理的方法,但这不会在运行时发生。我理解纹理就像​​颜色一样,我可以通过在适当的顶点之前调用glColor来轻松地更改每个立方体的面,但使用texture.bind()(来自slick-util库)似乎什么都不做。

这是我的渲染方法:

public void render(){
    glPushMatrix();

    top.bind(); 
    top.setTextureFilter(GL_NEAREST);
    glColor4f(1, 1, 1, 0);
    glTranslatef(x, y, z);
    glRotatef(yaw, 0, 1, 0);

    glBegin(GL_QUADS);
    //Top
    glTexCoord2f(0, 0); glVertex3f(0, size, 0);
    glTexCoord2f(0, 1); glVertex3f(0, size, size);
    glTexCoord2f(1, 1); glVertex3f(size, size, size);
    glTexCoord2f(1, 0); glVertex3f(size, size, 0);

    //Front
    top.release();
    side.bind();
    glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
    glTexCoord2f(0, 1); glVertex3f(0, size, 0);
    glTexCoord2f(1, 1); glVertex3f(size, size, 0);
    glTexCoord2f(1, 0); glVertex3f(size, 0, 0);

    //Left
    glTexCoord2f(0, 0); glVertex3f(0, 0, size);
    glTexCoord2f(0, 1); glVertex3f(0, size, size);
    glTexCoord2f(1, 1); glVertex3f(0, size, 0);
    glTexCoord2f(1, 0); glVertex3f(0, 0, 0);

    //Right
    glTexCoord2f(0, 0); glVertex3f(size, 0, size);
    glTexCoord2f(0, 1); glVertex3f(size, size, size);
    glTexCoord2f(1, 1); glVertex3f(size, size, 0);
    glTexCoord2f(1, 0); glVertex3f(size, 0, 0);

    //Back
    glTexCoord2f(0, 0); glVertex3f(0, 0, size);
    glTexCoord2f(0, 1); glVertex3f(0, size, size);
    glTexCoord2f(1, 1); glVertex3f(size, size, size);
    glTexCoord2f(1, 0); glVertex3f(size, 0, size);

    //Bottom
    side.release();
    bottom.bind();
    glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
    glTexCoord2f(0, 1); glVertex3f(0, 0, size);
    glTexCoord2f(1, 1); glVertex3f(size, 0, size);
    glTexCoord2f(1, 0); glVertex3f(size, 0, 0);

    bottom.release();
    glEnd();
    glPopMatrix();
}

变量topsidebottom是我想要放在多维数据集面上的纹理。

结果是立方体的每个面都具有“顶部”纹理。

无论是否拨打texture.release(),结果都是一样的。

1 个答案:

答案 0 :(得分:4)

您无法在glBegin()glEnd()之间绑定纹理。在glBegin()glEnd()之间只能进行一组有限的总帐通话(有关完整列表,请参阅https://www.opengl.org/sdk/docs/man2/xhtml/glBegin.xml),而glBindTexture()不是其中之一。

要为每一面绑定不同的纹理,您需要为每一面开始一个新的开始/结束对:

//Top
top.bind(); 
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, size, 0);
glTexCoord2f(0, 1); glVertex3f(0, size, size);
glTexCoord2f(1, 1); glVertex3f(size, size, size);
glTexCoord2f(1, 0); glVertex3f(size, size, 0);
glEnd();

//Front
side.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
...
glEnd();

//Bottom
bottom.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
...
glEnd();