Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: LWJGL texture applying problem

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default LWJGL texture applying problem

    import org.lwjgl.LWJGLException;
    import org.lwjgl.opengl.Display;
    import org.lwjgl.opengl.DisplayMode;
    import static org.lwjgl.opengl.GL11.*;
     
    public class game {
     
    	public final String TITLE = "game";
     
    	public static final int WIDTH = 680;
    	public static final int HEIGHT = 480;
     
    	public world world;
     
    	public game() {
    		try {
    			Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
    			Display.setTitle(TITLE);
    			Display.setResizable(false);
    			Display.create();
    		} catch (LWJGLException e) {
    			System.out.println(e);
    		}
     
    		init();
    		initGL();
    		gameLoop();
    	}
     
    	private void init() {
    		world = new world();
    	}
     
    	private void initGL() {
    		glMatrixMode(GL_PROJECTION);
    		glLoadIdentity();
    		glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
    		glMatrixMode(GL_MODELVIEW);
    		glEnable(GL_TEXTURE_2D);
    	}
     
    	private void gameLoop() {
    		while(!Display.isCloseRequested()) {
     
    			render();
     
    			Display.update();
    			Display.sync(60);
    		}
    	}
     
    	private void render() {
    		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    		world.render();
    	}
     
    	public static void main(String[] args) {
    		new game();
    	}
    }

    public enum blockType {
    	DIRT,GRASS,BRICK,AIR;
    }

    import java.util.ArrayList;
    import java.util.List;
     
     
    public class world {
     
    	private List<block>	blocks = new ArrayList<block>();
     
    	public world() {
    		init();
    	}
     
    	public void init() {
    		blocks.add(new block(100,100,blockType.AIR));
    		blocks.add(new block(10,10,blockType.AIR));
    		blocks.add(new block(200,200,blockType.DIRT));
    	}
     
    	public void render() {
     
    		for(block block: blocks) {
    			block.drawBlock();
    		}
    	}
    }

    import static org.lwjgl.opengl.GL11.*;
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
     
    import org.newdawn.slick.opengl.Texture;
    import org.newdawn.slick.opengl.TextureLoader;
     
    public class block {
     
    	private int x, y;
     
    	// gets the type of block being rendered in world class
    	private blockType type;
    	// setTo the type of block e.g AIR
    	private String fileName;
    	//texture got from filename
    	private Texture texture;
     
    	public block(int x, int y, blockType type) {
    		this.x = x;
    		this.y = y;
    		this.type = type;
     
    		checkType();
    		init();
    	}
     
    	private void init() {
    		texture = loadTexture(fileName);
    	}
     
    	private void checkType() {
    		switch (type) {
     
    		case AIR:
    			fileName = "air2";
    			break;
     
    		case DIRT:
    			fileName = "dirt";
    			break;
     
    		case BRICK:
    			fileName = "brick";
    			break;
     
    		case GRASS:
    			fileName = "grass";
    			break;
    		}
    	}
     
    	public void drawBlock() {
    		texture.bind();
    		glBegin(GL_QUADS);
    		{
    			glTexCoord2f(0,0);
    			glVertex2i(x, y);
     
    			glTexCoord2f(1,0);
    			glVertex2i(x + 20, y);
     
    			glTexCoord2f(1,1);
    			glVertex2i(x + 20, y + 20);
     
    			glTexCoord2f(0,1);
    			glVertex2i(x, y + 20);
    		}
     
    		glEnd();
    	}
     
    	private Texture loadTexture(String img) {
    		try {
    			return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/" + img + ".png")));
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    }

    my code draws the boxes fine without the texure, but when it comes to adding the texture it displays them with bits cut out, i think it something to do with the glTexCoord(float,float); but im not to sure, thanks

    textureError.jpg


  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: LWJGL texture applying problem

    Dude, Minecraft is already made by Notch! Stop copying it!

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: LWJGL texture applying problem

    I'm not copying his code.... im just experimenting to see how everthing works as i am LEARNING JAVA.just because i used the word blocks doesnt mean its minecraft and if you looked at the screenshot you would see that it is 2d. if you dont know dont coment.
    Last edited by DanielJamesCollier; April 15th, 2012 at 03:48 PM.

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: LWJGL texture applying problem

    Sorry Dude. You didnt show any titles or directories. Try checking them. Make sure the folder that has the textures has no spaces in its name.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: LWJGL texture applying problem

    its not that the texture isnt being found, its that the texture is drawing weird

Similar Threads

  1. [SOLVED] (LWJGL)openGL arraylist rendering problem
    By DanielJamesCollier in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 11th, 2012, 02:21 PM
  2. [SOLVED] Problem with OpenGL via LWJGL
    By paulo.carabuena in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 6th, 2011, 02:52 PM
  3. OpenGL Texture won't draw
    By nivangerow in forum Android Development
    Replies: 1
    Last Post: November 26th, 2011, 06:03 AM
  4. Need Help applying discount
    By teeej86 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 22nd, 2011, 08:13 PM
  5. Applying a MIDI instrument change to a Track
    By AnkleSpankle in forum Java Theory & Questions
    Replies: 3
    Last Post: January 23rd, 2011, 03:34 PM

Tags for this Thread