Texturing a cube with multiple textures (slick)

I have a cube class that can render a textured cube fine, however when I try to bind separate textures to different faces of the cube, it always seems to draw all of them with the last bound texture (rather than the relative bound texture).

I believe my problem is coming from a lack of understanding of what Texture.bind() actually does in the slick.opengl library. (if anyone could provide me a link with actual source code, that would be great!)

So far, I have tried two ways of binding the textures. The first being with a for loop:


for (int i = 0; i < pointlist.length; i++) {
				
				if (texture != null) {
					if (i%4 == 3) {
						glTexCoord2f(0, 0);
					}
					if (i%4 == 0) {
						if (i == 0) {
							relative_texture[0].bind();
						} else if (i == 4) {
							relative_texture[1].bind();
						} else if (i == 8) {
							relative_texture[2].bind();
						} else if (i == 12) {
							relative_texture[3].bind();
						} else if (i == 16) {
							relative_texture[4].bind();
						} else if (i == 20) {
							relative_texture[5].bind();
						}
						glTexCoord2f(0, 1);
					}
					if (i%4 == 1) {
						glTexCoord2f(1, 1);
					}
					if (i%4 == 2) {
						glTexCoord2f(1, 0);
					}
				}
				glVertex3f(pointlist[i].x, pointlist[i].y, pointlist[i].z);
			}

and the second being without a for loop, and just written out (should be going through the same method):


if (relative_texture.length == 6) {
				relative_texture[0].bind();
			}
			glTexCoord2f(0, 1);
			glVertex3f(pointlist[0].x, pointlist[0].y, pointlist[0].z);
			glTexCoord2f(1, 1);
			glVertex3f(pointlist[1].x, pointlist[1].y, pointlist[1].z);
			glTexCoord2f(1, 0);
			glVertex3f(pointlist[2].x, pointlist[2].y, pointlist[2].z);
			glTexCoord2f(0, 0);
			glVertex3f(pointlist[3].x, pointlist[3].y, pointlist[3].z);
			if (relative_texture.length == 6) {
				relative_texture[1].bind();
			}
			glTexCoord2f(0, 1);
			glVertex3f(pointlist[4].x, pointlist[4].y, pointlist[4].z);
			glTexCoord2f(1, 1);
			glVertex3f(pointlist[5].x, pointlist[5].y, pointlist[5].z);
			glTexCoord2f(1, 0);
			glVertex3f(pointlist[6].x, pointlist[6].y, pointlist[6].z);
			glTexCoord2f(0, 0);
			glVertex3f(pointlist[7].x, pointlist[7].y, pointlist[7].z);
			if (relative_texture.length == 6) {
				relative_texture[2].bind();
			}
			glTexCoord2f(0, 1);
			glVertex3f(pointlist[8].x, pointlist[8].y, pointlist[8].z);
			glTexCoord2f(1, 1);
			glVertex3f(pointlist[9].x, pointlist[9].y, pointlist[9].z);
			glTexCoord2f(1, 0);
			glVertex3f(pointlist[10].x, pointlist[10].y, pointlist[10].z);
			glTexCoord2f(0, 0);
			glVertex3f(pointlist[11].x, pointlist[11].y, pointlist[11].z);
			if (relative_texture.length == 6) {
				relative_texture[3].bind();
			}
			glTexCoord2f(0, 1);
			glVertex3f(pointlist[12].x, pointlist[12].y, pointlist[12].z);
			glTexCoord2f(1, 1);
			glVertex3f(pointlist[13].x, pointlist[13].y, pointlist[13].z);
			glTexCoord2f(1, 0);
			glVertex3f(pointlist[14].x, pointlist[14].y, pointlist[14].z);
			glTexCoord2f(0, 0);
			glVertex3f(pointlist[15].x, pointlist[15].y, pointlist[15].z);
			if (relative_texture.length == 6) {
				relative_texture[4].bind();
			}
			glTexCoord2f(0, 1);
			glVertex3f(pointlist[16].x, pointlist[16].y, pointlist[16].z);
			glTexCoord2f(1, 1);
			glVertex3f(pointlist[17].x, pointlist[17].y, pointlist[17].z);
			glTexCoord2f(1, 0);
			glVertex3f(pointlist[18].x, pointlist[18].y, pointlist[18].z);
			glTexCoord2f(0, 0);
			glVertex3f(pointlist[19].x, pointlist[19].y, pointlist[19].z);
			if (relative_texture.length == 6) {
				relative_texture[5].bind();
			}
			glTexCoord2f(0, 1);
			glVertex3f(pointlist[20].x, pointlist[20].y, pointlist[20].z);
			glTexCoord2f(1, 1);
			glVertex3f(pointlist[21].x, pointlist[21].y, pointlist[21].z);
			glTexCoord2f(1, 0);
			glVertex3f(pointlist[22].x, pointlist[22].y, pointlist[22].z);
			glTexCoord2f(0, 0);
			glVertex3f(pointlist[23].x, pointlist[23].y, pointlist[23].z);

the whole draw method looks like this:


public void draw() {
		glPushMatrix();
		if (texture != null) {
			texture.bind();
		}
		glEnable(GL_TEXTURE_2D);
		if (cullEnabled()) {
			glEnable(GL_CULL_FACE);
			glCullFace(cullface);
		}
		glBegin(GL_QUADS);
		{
			glColor3f(color.getR(), color.getG(), color.getB());
		    <INSERT ABOVE CODE EXAMPLES HERE>
		}
		glEnd();
		glDisable(GL_TEXTURE_2D);
		if (cullEnabled()) {
			glDisable(GL_CULL_FACE);
		}
		glPopMatrix();
	}

The way I presume Texture.bind() to work (with limited knowledge of how openGL works) is that it changes the texture state of openGL to this texture using glBindTexture(GL_TEXTURE_2D, ) to draw the next shape(s) and since there is no Texture.unbind(), it would make sense that the next texture bound would be the new texture state and so forth.

Does Texture.bind() only work outside of glBegin(GL_QUADS) and similar methods?

Any help on this subject would be appreciated :slight_smile:

Indeed, you cannot bind a texture while inside glBegin/glEnd

Keep those man pages bookmarked.

For something like this you should use a single texture atlas and specify different texture coordinates for each face.

I’d also highly recommend using an object-oriented approach instead of endless if-else statements. Right now you have lots of repeated code.

Could you give me an example of how how would do it? (pseudo-code is fine)

From this statement: this comes to mind.


create a "Face" class for each face with a texture variable (with texture coordinates) and a draw method. (this will contain texture.bind())
create a "Cube" class that has an array of 6 faces that each call the Face.draw() method.

If you can think of a better way to do it, please let me know! :slight_smile:

P.S. - thank you sproingie, I’ve been looking for those docs…