Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By gimbal2

Thread: LWJGL Texture Rendering

  1. #1
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default LWJGL Texture Rendering

    Well I started to learn about LWJGL, so I decided to make a texture cube, which is relatively easy. The only problem is that my textures are rendering weird. Here is all of the code you should need to help:

    EDIT: The problem was not call
    Java Code:
    glEnable(GL_DEPTH_TEST);
    Java Code:
    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);
        }
    Java Code:
    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));
                    }
                }
            }
        }
    Java Code:
    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);
            }
        }
    Java Code:
    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;
        }
    
    }
    Java Code:
    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();
        }
    Java Code:
    private void top(int x, int z) { //Nevermind the name of the void, my plans changed and I never changed the name
    
            glColor3f(0, 50, 0);
            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);
            
            glTexCoord2f(0, 0);
            glVertex3i(x, -playerHeight, z);
            glTexCoord2f(1, 0);
            glVertex3i(x + tileSize, -playerHeight, z);
            glTexCoord2f(1, 1);
            glVertex3i(x + tileSize, -playerHeight - tileSize, z);
            glTexCoord2f(0, 1);
            glVertex3i(x, -playerHeight - tileSize, z);
            
            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);
        }
    And Here is my output:

    LWJGL Texture Rendering-capture.png

    As, you can see, the back wall renders over top of the top wall. I don't know what is causing this.
    edit: not just textres are doing this.
    Last edited by zFollette; 01-05-2014 at 12:06 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: LWJGL Texture Rendering

    Have you found java-gaming.org yet? Its a forum where any and all Java game developers congregate. Its a more suited forum to ask (Java) game programming or OpenGL related questions because you'll have the entire forum as a possible source of an answer and not just 1 or 2 people.

    Pretty cool side effect by the way, it has a very M.C. Escher kind of feel to it. I have never worked with cube textures myself, but what I do know from 2D textures is that problems tend to come from providing either the wrong texture coordinates, or by providing them in the wrong order (for example: as a side effect of passing the actual vertex coordinates in the wrong order). I'd check the theory behind cube textures against your code to see if there is a mismatch there.
    Last edited by gimbal2; 01-06-2014 at 11:31 AM.
    KevinWorkman likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: LWJGL Texture Rendering

    Where did you start learning about LWJGL?

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: LWJGL Texture Rendering

    I can't speak for zfollette, but me personally I started learning OpenGL, not LWJGL directly. I learned it all when I was still doing C/C++ programming and I do believe that made my learning path easier than people that have to start with LWJGL and then have to start to understand the innards and theory of an API that is very much aimed and designed for native programming and all its wonders and mysteries that you simply don't have when programming something as high level as Java.

    LWJGL is a very effective wrapper around that thin native API, but it does tend to shy people away from applying very rich information and understanding resources: (OpenGL) game programming books which tend to target C/C++ or more recently C#, but not Java. That's a shame because the books really helped me a lot to master this difficult subject matter.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: LWJGL Texture Rendering

    Is OpenGL just a graphics API that you can use on JPanels and Canvases, or is there another set of "windows" that OpenGL uses?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: LWJGL Texture Rendering

    Quote Originally Posted by pj6444 View Post
    Is OpenGL just a graphics API that you can use on JPanels and Canvases, or is there another set of "windows" that OpenGL uses?
    Best way to answer this kind of question: https://www.google.com/search?q=opengl

    Also, people checking out LWJGL might also want to check out libGDX.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. One big texture file?
    By jammas615 in forum New To Java
    Replies: 1
    Last Post: 09-27-2011, 05:41 AM
  2. Using Images In Java As A Texture Pack?
    By Alerhau in forum New To Java
    Replies: 6
    Last Post: 08-22-2011, 05:36 PM
  3. Java 3D QuadArray Texture Coordinates
    By ajtgarber in forum Advanced Java
    Replies: 0
    Last Post: 02-13-2011, 04:46 PM
  4. Replies: 5
    Last Post: 05-11-2009, 04:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •