Partage
  • Partager sur Facebook
  • Partager sur Twitter

LWJGL Textures

    17 juillet 2019 à 22:33:49

    Bonjour, je suis en train de coder un jeu avec des cubes(un peu comme minecraft) et j'aimerais rajouter des textures à mes blocs. Mais j'ai un problème, j'utilise des VBOs, il y a un vbo par Chunk contenant l'entièreté des données nécessaires pour chaque cube du Chunk(position, normales, texture coordinates) mais chaque blocs a une texture différentes. Alors comment faire pour dire à LWJGL de changer de texture pour chaque cube ?

    Je ne sais pas si j'ai été clair, demandez moi si vous avez besoin de plus de précisions.

    Merci d'avance pour votre aide.

    EDIT: 

    C bon j'ai réussi à faire ce que je voulais, voici le code pour ceux qui sont dans le même problème que moi:

    public Main(){
    		blockAtlas = new TextureAtlas(Arrays.asList("grass.png","stone.png","water.png"), "res/textures/blocks/", "atlasBlock", 1024);
    		Block.loadTexture();	
    	}
    public class TextureAtlas {
    
    	public HashMap<String, Vector2f> textures = new HashMap<>();
    	public String fileName;
    	
    	public float textureSize;
    	
    	public TextureAtlas(List<String> textures, String path, String fileName, int textureSize){
    		
    		try{
    			
    			this.fileName = fileName;
    			
    			int size = (int) Math.sqrt(textures.size()) +1;
    			
    			this.textureSize = 1f/(float)size;
    			
    			List<BufferedImage> images = new ArrayList<BufferedImage>();
    			
    			for(String texture : textures){
    				BufferedImage buffer = ImageIO.read(new File(path+""+texture));
    				images.add(buffer);
    				
    				this.textures.put(texture, new Vector2f((float)(textures.indexOf(texture) % size)/(float)size,(float)(textures.indexOf(texture)/size)/(float)size));
    			}
    
    			BufferedImage atlas = new BufferedImage(size*textureSize,size*textureSize, BufferedImage.TYPE_INT_ARGB);
    			
    			Graphics g = atlas.getGraphics();
    			
    			for(BufferedImage img : images){
    				g.drawImage(img, (int)((float)(images.indexOf(img)%size) * (float)textureSize), (int)(images.indexOf(img)/size * (float)textureSize), null);
    			}
    			
    			ImageIO.write(atlas, "PNG", new File(path+""+fileName+".png"));
    			
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    		
    		
    	}
    	
    }
    package fr.terraingeneration.blocks;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    import org.lwjgl.util.vector.Vector2f;
    import org.lwjgl.util.vector.Vector3f;
    import org.newdawn.slick.opengl.Texture;
    import org.newdawn.slick.opengl.TextureLoader;
    
    import fr.terraingeneration.Color4f;
    import fr.terraingeneration.main.Main;
    
    public class Block {
    
    	private Color4f color;
    	public Vector3f pos;
    	public static int textureID;
    	
    	private String textureName;
    	
    	private Vector2f atlasTextureCoords;
    	
    	public Block(Vector3f pos, Color4f color, String textureName){
    		this.color = color;
    		this.pos  = pos;
    		this.textureName = textureName;
    		atlasTextureCoords = texCoords;
    		tSize = Main.blockAtlas.textureSize;
    		texCoords = Main.blockAtlas.textures.get(textureName+".png");
    	}
    	
    	public static void loadTexture(){
    		
    		Texture texture = null;
    		try {
    			texture = TextureLoader.getTexture("PNG", new FileInputStream("res/textures/blocks/"+Main.blockAtlas.fileName+".png"));
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		
    		int textureID = texture.getTextureID();
    		Block.textureID = textureID;
    		
    	}
    	
    	private float tSize;
    	private Vector2f texCoords;
    	
    	public float[] backData(){
    		float x = pos.x;
    		float y = pos.y;
    		float z = pos.z;
    		
    		float s = 1;
    		
    		return new float[]{
    				x, y, z,		color.r, color.g, color.b, color.a, 0,0,-1, 0.5f*tSize + texCoords.x,1f*tSize + texCoords.y,
    				x+s, y, z,		color.r, color.g, color.b, color.a, 0,0,-1, 1f*tSize + texCoords.x,1f*tSize + texCoords.y,
    				x+s, y+s, z,	color.r, color.g, color.b, color.a, 0,0,-1, 1f*tSize + texCoords.x,0.5f*tSize + texCoords.y,
    				x, y+s, z,		color.r, color.g, color.b, color.a, 0,0,-1, 0.5f*tSize + texCoords.x,0.5f*tSize + texCoords.y,
    		};
    	}
    	
    	public float[] frontData(){
    		float x = pos.x;
    		float y = pos.y;
    		float z = pos.z;
    		
    		float s = 1;
    		
    		return new float[]{
    				x+s, y, z+s,		color.r, color.g, color.b, color.a, 0,0,1, 0.5f*tSize + texCoords.x,1f*tSize + texCoords.y,
    				x, y, z+s,			color.r, color.g, color.b, color.a, 0,0,1, 1f*tSize + texCoords.x,1f*tSize + texCoords.y,
    				x, y+s, z+s,		color.r, color.g, color.b, color.a, 0,0,1, 1f*tSize + texCoords.x,0.5f*tSize + texCoords.y,
    				x+s, y+s, z+s,		color.r, color.g, color.b, color.a, 0,0,1, 0.5f*tSize + texCoords.x,0.5f*tSize + texCoords.y,
    		};
    	}
    	
    	public float[] rightData(){
    		float x = pos.x;
    		float y = pos.y;
    		float z = pos.z;
    		
    		float s = 1;
    		
    		return new float[]{
    				x+s, y, z,		color.r, color.g, color.b, color.a, 1,0,0, 0f*tSize + texCoords.x,1f*tSize + texCoords.y,
    				x+s, y, z+s,		color.r, color.g, color.b, color.a, 1,0,0, 1f*tSize + texCoords.x,1f*tSize + texCoords.y,
    				x+s, y+s, z+s,	color.r, color.g, color.b, color.a, 1,0,0, 1f*tSize + texCoords.x,0.5f*tSize + texCoords.y,
    				x+s, y+s, z,		color.r, color.g, color.b, color.a, 1,0,0, 0f*tSize + texCoords.x,0.5f*tSize + texCoords.y,
    		};
    	}
    	
    	public float[] leftData(){
    		float x = pos.x;
    		float y = pos.y;
    		float z = pos.z;
    		
    		float s = 1;
    		
    		return new float[]{
    				x, y, z+s,		color.r, color.g, color.b, color.a, -1,0,0, 0f*tSize + texCoords.x,1f*tSize + texCoords.y,
    				x, y, z,		color.r, color.g, color.b, color.a, -1,0,0, 1f*tSize + texCoords.x,1f*tSize + texCoords.y,
    				x, y+s, z,	color.r, color.g, color.b, color.a, -1,0,0,     1f*tSize + texCoords.x,0.5f*tSize + texCoords.y,
    				x, y+s, z+s,	color.r, color.g, color.b, color.a, -1,0,0, 0f*tSize + texCoords.x,0.5f*tSize + texCoords.y
    		};
    	}
    	
    	public float[] upData(){
    		float x = pos.x;
    		float y = pos.y;
    		float z = pos.z;
    		
    		float s = 1;
    		
    		return new float[]{
    				x, y+s, z,		color.r * 1f, color.g * 1f, color.b * 1f, color.a, 0,1,0,     0f*tSize + texCoords.x,0f*tSize + texCoords.y,
    				x+s, y+s, z,		color.r * 1f, color.g * 1f, color.b * 1f, color.a, 0,1,0, 0.5f*tSize + texCoords.x,0f*tSize + texCoords.y,
    				x+s, y+s, z+s,	color.r * 1f, color.g * 1f, color.b * 1f, color.a, 0,1,0,     0.5f*tSize + texCoords.x,0.5f*tSize + texCoords.y,
    				x, y+s, z+s,		color.r * 1f, color.g * 1f, color.b * 1f, color.a, 0,1,0, 0f*tSize + texCoords.x,0.5f*tSize + texCoords.y
    		};
    	}
    	
    	public float[] downData(){
    		float x = pos.x;
    		float y = pos.y;
    		float z = pos.z;
    		
    		float s = 1;
    		
    		return new float[]{
    				x+s, y, z,		color.r, color.g, color.b, color.a, 0,-1,0,     0f*tSize + texCoords.x,0*tSize + texCoords.y,
    				x, y, z,		color.r, color.g, color.b, color.a, 0,-1,0,		0.5f*tSize + texCoords.x,0f*tSize + texCoords.y,
    				x, y, z+s,	color.r, color.g, color.b, color.a, 0,-1,0,         0.5f*tSize + texCoords.x,0.5f*tSize + texCoords.y,
    				x+s, y, z+s,		color.r, color.g, color.b, color.a, 0,-1,0, 0f*tSize + texCoords.x,0.5f*tSize + texCoords.y
    		};
    	}
    	
    }
    




    -
    Edité par Rafa34 18 juillet 2019 à 0:44:54

    • Partager sur Facebook
    • Partager sur Twitter

    LWJGL Textures

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown