Results 1 to 5 of 5
  1. #1
    ShoeLace1291 is offline Member
    Join Date
    Dec 2015
    Posts
    6
    Rep Power
    0

    Default How do I correctly map a texture to a Hexagon with LWJGL?

    I have a script that renders a 2D hexagon-shaped grid of hexagon tiles. The grid loads fine, but my textures are missing huge bits of color. What is wrong with my code? Have I mapped the coordinates incorrectly? PS: I wasn't sure if this was the right board for this or not.

    Here is the texture image that I am using:
    How do I correctly map a texture to a Hexagon with LWJGL?-wasteland.png

    Here is how my game appears:
    How do I correctly map a texture to a Hexagon with LWJGL?-missingtexture.png

    Here is my code:
    Java Code:
    public class LWJGLHelloWorld {
        
        	public static int SCREEN_WIDTH;
        	public static int SCREEN_HEIGHT;
        	public static int WINDOW_WIDTH;
        	public static int WINDOW_HEIGHT;
        	public double WIDTH;
        	public double HEIGHT;
        	public ArrayList<Hexagon> hexagons = new ArrayList<Hexagon>();
        	public ArrayList<String> resources = new ArrayList<String>();
        	public Texture brick;
        	public Texture stone;
        	public Texture lumber;
        	public Texture wool;
        	public Texture wheat;
        	public Texture wasteland;
        	public int textureID;
        	private float[][] textureCoords = { 
    			{0.5f, 0.5f},
                {0.5f, 0.0f},
                {0.0f, 0.25f},
                {0.0f, 0.75f},
                {0.5f, 0.0f},
                {1.0f, 0.75f},
                {1.0f, 0.25f}
        };
        	private static enum State {
        		INTRO, MAIN_MENU, GAME;
        	}
        	
        	private State state = State.INTRO;
        	
        	public LWJGLHelloWorld(){
        		
        		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        		double SCREEN_WIDTH = screenSize.getWidth();
        		double SCREEN_HEIGHT = screenSize.getHeight();
        		double WIDTH = SCREEN_WIDTH * .85;
        		double HEIGHT = SCREEN_HEIGHT * .85;
        		
        		try {
        			Display.setDisplayMode(new DisplayMode((int)WIDTH, (int)HEIGHT));
        			Display.setTitle("Hello, LWJGL!");;
        			Display.create();
        		} catch (LWJGLException e){
        			e.printStackTrace();
        		}
        		resetResources();
        		
        
        		brick = loadTexture("brick");
        		stone = loadTexture("stone");
        		lumber = loadTexture("lumber");
        		//Texture wheat = loadTexture("wheat");
        		wool = loadTexture("wool");
        		wasteland = loadTexture("wasteland");
        		
        		glMatrixMode(GL_PROJECTION);
        		glLoadIdentity();
        		glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
        		glMatrixMode(GL_MODELVIEW);
        		glEnable(GL_TEXTURE_2D);
        		
        		int originX = (int)(Display.getDisplayMode().getWidth() / 2);
        		int originY = (int)(Display.getDisplayMode().getHeight() / 2);
        		int radius = (int)(HEIGHT * .1);
        		int padding = (int)(HEIGHT * .005);
        		
        		findHexCoords(originX, originY, 5, radius, padding);
        		
        		while(!Display.isCloseRequested()){
        			checkInput();
        			glClear(GL_COLOR_BUFFER_BIT);
        			for(int h = 0; h < hexagons.size(); h++){
        				String rsrc = resources.get(h);
        				switch(rsrc){
        				case "brick":
        					brick.bind();
        					break;
        				case "stone":
        					stone.bind();
        					break;
        				case "lumber":
        					lumber.bind();
        					break;
        				case "wheat":
        					//wheat.bind();
        					break;
        				case "wool":
        					wool.bind();
        					break;
        				case "wasteland":
        					wasteland.bind();
        					break;	
        				}
        				glBegin(GL_POLYGON);
        				Hexagon hex = hexagons.get(h);
        				for(int p = 0; p < hex.points.length; p++){
        					Point point = hex.points[p];
        					glVertex2f(point.x, point.y);
        					glTexCoord2f(textureCoords[p][0], textureCoords[p][0]);
        					GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
        					GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
        				}
        				glEnd();
        			}
        			
        			Display.update();
        			Display.sync(60);
        		}
        		
        		Display.destroy();
        	}
        	
        	private void bindTexture(String rsrc){
        		
        	}
        	
        	private void findHexCoords(int x, int y, int size, int radius, int padding) {
                
                Point origin = new Point(x, y);
                double ang30 = Math.toRadians(30);
                double xOff = Math.cos(ang30) * (radius + padding);
                double yOff = Math.sin(ang30) * (radius + padding);
                int half = size / 2;
                
                int i = 0;
                for (int row = 0; row < size; row++) {
                    
                    int cols = size - Math.abs(row - half);
        
                    for (int col = 0; col < cols; col++) {
                                     
                        int xLbl = row < half ? col - row : col - half;
                        int yLbl = row - half;
                        int centerX = (int) (origin.x + xOff * (col * 2 + 1 - cols));
                        int centerY = (int) (origin.y + yOff * (row - half) * 3);
                        
                        Hexagon hex = new Hexagon(centerX, centerY, radius);
                        System.out.println(centerX+","+centerY);
                        hexagons.add(hex);
                        i++;
                    }
                }
            }
        	
        	public void checkInput(){
        		switch(state){
        		case INTRO:
        			if(Keyboard.isKeyDown(Keyboard.KEY_S)){
        				state = State.MAIN_MENU;
        			}
        			if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
        				Display.destroy();
        				System.exit(0);;
        			}
        			break;
        		case GAME:
        			if(Keyboard.isKeyDown(Keyboard.KEY_BACK)){
        				state = State.MAIN_MENU;
        			}
        			break;
        		case MAIN_MENU:
        			if(Keyboard.isKeyDown(Keyboard.KEY_RETURN)){
        				state = State.GAME;
        			}
        			if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
        				state = State.INTRO;
        			}
        			break;
        		}
        	}
        	
        	private Texture loadTexture(String key){
        		try {
        			return TextureLoader.getTexture("PNG", new FileInputStream(new File("img/" + key + ".png")));
        		} catch (FileNotFoundException e) {
        			e.printStackTrace();
        		} catch (IOException e) {
        			e.printStackTrace();
        		}
        		return null;
        	}
        	public static void main(String[] args) {
        		new LWJGLHelloWorld();
        
        	}
        	
        	public void resetResources(){
        		resources.clear();
        		resources.add("Brick");
                resources.add("Brick");
                resources.add("Brick");
                resources.add("Wool");
                resources.add("Wool");
                resources.add("Wool");
                resources.add("Wool");
                resources.add("Lumber");
                resources.add("Lumber");
                resources.add("Lumber");
                resources.add("Lumber");
                resources.add("Stone");
                resources.add("Stone");
                resources.add("Stone");
                resources.add("Wheat");
                resources.add("Wheat");
                resources.add("Wheat");
                resources.add("Wheat");
                long seed = System.nanoTime();
        		Collections.shuffle(resources, new Random(seed));
        		int randomIndex = ThreadLocalRandom.current().nextInt(0, 19);
        		resources.add(randomIndex, "Wasteland");
        		for(int r = 0; r < resources.size(); r++){
        			System.out.println(resources.get(r));
        		}
        	}
        
        }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How do I correctly map a texture to a Hexagon with LWJGL?

    Can't you just blit the base hex rather than drawing it and then blitting a texture?

    I don't think I've seen a game that didn't have the hexes as the graphical resource.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    ShoeLace1291 is offline Member
    Join Date
    Dec 2015
    Posts
    6
    Rep Power
    0

    Default Re: How do I correctly map a texture to a Hexagon with LWJGL?

    Quote Originally Posted by Tolls View Post
    Can't you just blit the base hex rather than drawing it and then blitting a texture?

    I don't think I've seen a game that didn't have the hexes as the graphical resource.
    I'm not sure what you mean by blit.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How do I correctly map a texture to a Hexagon with LWJGL?

    Sorry, draw.
    https://en.wikipedia.org/wiki/Bit_blit

    It's how individual images get overlaid to produce the final screen.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    ShoeLace1291 is offline Member
    Join Date
    Dec 2015
    Posts
    6
    Rep Power
    0

    Default Re: How do I correctly map a texture to a Hexagon with LWJGL?

    So I took your advice and just drew quads with transparency enabled on the images. I have run into a new problem, however. My script always applies the same texture to every tile. I thought texture.bind() was supposed to apply the texture to the quad that's being rendered. Maybe I'm mistaking. I am using the same code that I have in my original post, but this is the new render code:
    Java Code:
                                   glBegin(GL_QUADS);
    					glTexCoord2f(0, 0);
    					glVertex2f(hex.center.x - radius, hex.center.y - radius);
    					glTexCoord2f(1, 0);
    					glVertex2f(hex.center.x + radius, hex.center.y - radius);
    					glTexCoord2f(1, 1);
    					glVertex2f(hex.center.x + radius, hex.center.y + radius);
    					glTexCoord2f(0, 1);
    					glVertex2f(hex.center.x - radius, hex.center.y + radius);
    					GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
                                            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
    				glEnd();
    The image itself is displaying fine.
    Last edited by ShoeLace1291; 02-25-2017 at 05:35 PM.

Similar Threads

  1. 2D Circle Texture Rotation?
    By exonaut in forum New To Java
    Replies: 8
    Last Post: 01-12-2015, 05:12 PM
  2. LWJGL Texture Rendering
    By zFollette in forum Advanced Java
    Replies: 5
    Last Post: 01-06-2014, 09:40 PM
  3. point in hexagon
    By DoyleBrunson in forum Java 2D
    Replies: 5
    Last Post: 10-25-2012, 08:15 PM
  4. One big texture file?
    By jammas615 in forum New To Java
    Replies: 1
    Last Post: 09-27-2011, 06:41 AM
  5. Java 3D QuadArray Texture Coordinates
    By ajtgarber in forum Advanced Java
    Replies: 0
    Last Post: 02-13-2011, 05:46 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
  •