Hello Guest

Just starting LWJGL

  • 2 Replies
  • 2240 Views
Just starting LWJGL
« on: January 05, 2014, 01:12:18 »
I am just starting to learn the library so I decided to make an x, y, z arrow of cubes, this works and all, but the textures are all messed up, I've tried loading BMP, GIF, PNG and JPG of the same 512x512 image using slick-util and I get the same result.



Here is some of the code I am using:
Code: [Select]
public Game() throws LWJGLException {
        Display.setDisplayMode(new DisplayMode(width, height));
        Display.setTitle("3D");
        Display.create();
 
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(30, width / height, 0.1f, 1500f);
        glMatrixMode(GL_MODELVIEW);
        glEnable(GL_TEXTURE_2D);
         
 
        initBlocks();
        loadTextures();
 
        while (!Display.isCloseRequested()) {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
            textures.get("grass").bind();
 
            drawBlocks();
            keys();
 
            Display.update();
            Display.sync(FPS);
        }
 
        Display.destroy();
        System.exit(0);
    }

Code: [Select]
public void initBlocks() {
        for (int x = 0; x < blocks.length; x++) {
            for (int y = 0; y < blocks[0].length; y++) {
                for (int z = 0; z < 1; z++) {
                    blocks[x][y][z] = new Block(textures.get("grass"), new Vector3f(x * 50, y * 50, z * 50));
                }
            }
        }
    }

Code: [Select]
public void loadTextures() {
        TextureLoader load = new TextureLoader();
        try {
            textures.put("grass", load.getTexture("grass, gif"));
        } catch (IOException e) {
            System.out.println("Failed to load texture.");
        } catch (NullPointerException | URISyntaxException ex) {
            Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Code: [Select]
public class TextureLoader {
 
    public Texture getTexture(String name) throws IOException, NullPointerException, URISyntaxException {
        String ext = name.split(", ")[1];
        String name1 = name.split(", ")[0] + "." + ext;
        System.out.println("Loading texture: " + name1);
        Texture text = org.newdawn.slick.opengl.TextureLoader.getTexture(name.split(", ")[1], this.getClass().getClassLoader().getResourceAsStream("net/textures/" + name1));
        return text;
    }
 
}

Code: [Select]
public void drawBlocks() {
        glBegin(GL_QUADS);
 
        for (int x = 0; x < 1; x++) {
            for (int z = 0; z < 1; z++) {
                top(x * tileSize, z * tileSize);
            }
        }
 
        glEnd();
    }

Code: [Select]
    private void top(int x, int z) {

        glColor3f(0, 0, 255);
       
        glTexCoord2f(0, 0);
        glVertex3i(x, -playerHeight, z);
        glTexCoord2f(1, 0);
        glVertex3i(x + tileSize, -playerHeight, z);
        glTexCoord2f(1, 1);
        glVertex3i(x + tileSize, -playerHeight, z + tileSize);
        glTexCoord2f(0, 1);
        glVertex3i(x, -playerHeight, z + tileSize);

        glTexCoord2f(0, 0);
        glVertex3i(x, -playerHeight - tileSize, z);
        glTexCoord2f(1, 0);
        glVertex3i(x + tileSize, -playerHeight - tileSize, z);
        glTexCoord2f(1, 1);
        glVertex3i(x + tileSize, -playerHeight - tileSize, z + tileSize);
        glTexCoord2f(0, 1);
        glVertex3i(x, -playerHeight - tileSize, z + tileSize);

        glVertex3i(x, -playerHeight, z);
        glTexCoord2f(0, 0);
        glVertex3i(x + tileSize, -playerHeight, z);
        glTexCoord2f(1, 0);
        glVertex3i(x + tileSize, -playerHeight - tileSize, z);
        glTexCoord2f(1, 1);
        glVertex3i(x, -playerHeight - tileSize, z);
        glTexCoord2f(0, 1);
       

        glTexCoord2f(0, 0);
        glVertex3i(x, -playerHeight, z + tileSize);
        glTexCoord2f(1, 0);
        glVertex3i(x + tileSize, -playerHeight, z + tileSize);
        glTexCoord2f(1, 1);
        glVertex3i(x + tileSize, -playerHeight - tileSize, z + tileSize);
        glTexCoord2f(0, 1);
        glVertex3i(x, -playerHeight - tileSize, z + tileSize);

        glTexCoord2f(0, 0);
        glVertex3i(0, (z - playerHeight) - tileSize, x);
        glTexCoord2f(1, 0);
        glVertex3i(0, (z - playerHeight) - tileSize, x + tileSize);
        glTexCoord2f(1, 1);
        glVertex3i(0, (z + tileSize - playerHeight) - tileSize, x + tileSize);
        glTexCoord2f(0, 1);
        glVertex3i(0, (z + tileSize - playerHeight) - tileSize, x);

        glTexCoord2f(0, 0);
        glVertex3i(tileSize, (z - playerHeight) - tileSize, x);
        glTexCoord2f(1, 0);
        glVertex3i(tileSize, (z - playerHeight) - tileSize, x + tileSize);
        glTexCoord2f(1, 1);
        glVertex3i(tileSize, (z + tileSize - playerHeight) - tileSize, x + tileSize);
        glTexCoord2f(0, 1);
        glVertex3i(tileSize, (z + tileSize - playerHeight) - tileSize, x);
    }

I hope someone can help me.

EDIT: I just realized that I added textures to blocks before I loaded the textures. Although, switching the order did nothing. As I never call the textures until they're loaded.
« Last Edit: January 05, 2014, 01:15:03 by zFollette »

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: Just starting LWJGL
« Reply #1 on: January 06, 2014, 12:33:14 »
So this is as far as I can see, a problem with slick. I've never used slick so I cannot comment but it might help if you posted what the texture should be looking like.

Also you have not enabled the depth test which is why the back face is rendered "above" the left face. Call glEnable(GL_DEPTH_TEST); in your initialization code.

Re: Just starting LWJGL
« Reply #2 on: January 07, 2014, 06:11:06 »
Thanks.