How do I put different textures on different walls? LWJGL
Posted
by
lehermj
on Game Development
See other posts from Game Development
or by lehermj
Published on 2014-05-27T13:45:57Z
Indexed on
2014/05/27
16:03 UTC
Read the original article
Hit count: 227
So far I have it so you are running around in a box, but all of the walls are the same texture! I've loaded up other textures for the walls (I want the walls a different texture than the floor) but it seems as if its being ignored... Here's my code:
int floorTexture = glGenTextures();
{
InputStream in = null;
try {
in = new FileInputStream("floor.png");
PNGDecoder decoder = new PNGDecoder(in);
ByteBuffer buffer = BufferUtils.createByteBuffer(4 * decoder.getWidth() * decoder.getHeight());
decoder.decode(buffer, decoder.getWidth() * 4, Format.RGBA);
buffer.flip();
glBindTexture(GL_TEXTURE_2D, floorTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA,
GL_UNSIGNED_BYTE, buffer);
glBindTexture(GL_TEXTURE_2D, floorTexture);
} catch (FileNotFoundException ex) {
System.err.println("Failed to find the texture files.");
ex.printStackTrace();
Display.destroy();
System.exit(1);
} catch (IOException ex) {
System.err.println("Failed to load the texture files.");
ex.printStackTrace();
Display.destroy();
System.exit(1);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
int wallTexture = glGenTextures();
{
InputStream in = null;
try {
in = new FileInputStream("walls.png");
PNGDecoder decoder = new PNGDecoder(in);
ByteBuffer buffer = BufferUtils.createByteBuffer(4 * decoder.getWidth() * decoder.getHeight());
decoder.decode(buffer, decoder.getWidth() * 4, Format.RGBA);
buffer.flip();
glBindTexture(GL_TEXTURE_2D, wallTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA,
GL_UNSIGNED_BYTE, buffer);
glBindTexture(GL_TEXTURE_2D, wallTexture);
} catch (FileNotFoundException ex) {
System.err.println("Failed to find the texture files.");
ex.printStackTrace();
Display.destroy();
System.exit(1);
} catch (IOException ex) {
System.err.println("Failed to load the texture files.");
ex.printStackTrace();
Display.destroy();
System.exit(1);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
int ceilingDisplayList = glGenLists(1);
glNewList(ceilingDisplayList, GL_COMPILE);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(-gridSize, ceilingHeight, -gridSize);
glTexCoord2f(gridSize * 10 * tileSize, 0);
glVertex3f(gridSize, ceilingHeight, -gridSize);
glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
glVertex3f(gridSize, ceilingHeight, gridSize);
glTexCoord2f(0, gridSize * 10 * tileSize);
glVertex3f(-gridSize, ceilingHeight, gridSize);
glEnd();
glEndList();
int wallDisplayList = glGenLists(1);
glNewList(wallDisplayList, GL_COMPILE);
glBegin(GL_QUADS);
// North wall
glTexCoord2f(0, 0);
glVertex3f(-gridSize, floorHeight, -gridSize);
glTexCoord2f(0, gridSize * 10 * tileSize);
glVertex3f(gridSize, floorHeight, -gridSize);
glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
glVertex3f(gridSize, ceilingHeight, -gridSize);
glTexCoord2f(gridSize * 10 * tileSize, 0);
glVertex3f(-gridSize, ceilingHeight, -gridSize);
// West wall
glTexCoord2f(0, 0);
glVertex3f(-gridSize, floorHeight, -gridSize);
glTexCoord2f(gridSize * 10 * tileSize, 0);
glVertex3f(-gridSize, ceilingHeight, -gridSize);
glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
glVertex3f(-gridSize, ceilingHeight, +gridSize);
glTexCoord2f(0, gridSize * 10 * tileSize);
glVertex3f(-gridSize, floorHeight, +gridSize);
// East wall
glTexCoord2f(0, 0);
glVertex3f(+gridSize, floorHeight, -gridSize);
glTexCoord2f(gridSize * 10 * tileSize, 0);
glVertex3f(+gridSize, floorHeight, +gridSize);
glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
glVertex3f(+gridSize, ceilingHeight, +gridSize);
glTexCoord2f(0, gridSize * 10 * tileSize);
glVertex3f(+gridSize, ceilingHeight, -gridSize);
// South wall
glTexCoord2f(0, 0);
glVertex3f(-gridSize, floorHeight, +gridSize);
glTexCoord2f(gridSize * 10 * tileSize, 0);
glVertex3f(-gridSize, ceilingHeight, +gridSize);
glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
glVertex3f(+gridSize, ceilingHeight, +gridSize);
glTexCoord2f(0, gridSize * 10 * tileSize);
glVertex3f(+gridSize, floorHeight, +gridSize);
glEnd();
glEndList();
int floorDisplayList = glGenLists(1);
glNewList(floorDisplayList, GL_COMPILE);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(-gridSize, floorHeight, -gridSize);
glTexCoord2f(0, gridSize * 10 * tileSize);
glVertex3f(-gridSize, floorHeight, gridSize);
glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
glVertex3f(gridSize, floorHeight, gridSize);
glTexCoord2f(gridSize * 10 * tileSize, 0);
glVertex3f(gridSize, floorHeight, -gridSize);
glEnd();
glEndList();
© Game Development or respective owner