Java tutorial
/* * Copyright 2016 Matthew Rogers. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.bossletsplays.duthorix.rendering; import static org.lwjgl.opengl.GL11.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.nio.ByteBuffer; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import org.lwjgl.opengl.GL13; import com.bossletsplays.duthorix.utils.Util; import com.bossletsplays.duthorix.utils.debug.LogHelper; import com.bossletsplays.duthorix.utils.managers.TextureManager; /** * <strong>Project:</strong> DuthorixExploration * <strong>File:</strong> Texture.java * * @author Matthew Rogers */ public class Texture { private static Map<String, TextureManager> textures = new HashMap<String, TextureManager>(); private TextureManager manager; private TextureData data; private String file; public static final String MISSING_TEXTURE_FILE = "missing_texture"; public static final Texture MISSING_TEXTURE = new Texture(MISSING_TEXTURE_FILE); public Texture(String file) { this.file = file; TextureManager oldTexture = textures.get(file); if (oldTexture != null) { manager = oldTexture; manager.addReference(); } else { this.manager = loadTexture(file); textures.put(file, manager); } } @Override protected void finalize() throws Throwable { if (manager.removeReference() && !file.isEmpty()) textures.remove(file); super.finalize(); } public void bind(int samplerSlot) { try { assert (samplerSlot >= 0 && samplerSlot <= 31); GL13.glActiveTexture(GL13.GL_TEXTURE0 + samplerSlot); glBindTexture(GL_TEXTURE_2D, manager.getID()); } catch (Exception e) { System.err.println("Could not bind texture: " + file); System.exit(1); } } public void bind() { bind(0); } public int getID() { return manager.getID(); } private TextureManager loadTexture(String path) { BufferedImage image = null; LogHelper.info("Attempting to load a new texture: <" + "textures/" + path + ".png>"); try { image = ImageIO.read(getClass().getResource("/textures/" + path + ".png")); } catch (IOException e) { LogHelper.warn("Could not find texture: <" + "textures/" + path + ".png>"); return textures.get(MISSING_TEXTURE_FILE); } try { int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth()); ByteBuffer buffer = Util.createByteBuffer(image.getWidth() * image.getHeight() * 4); boolean hasAlpha = image.getColorModel().hasAlpha(); for (int y = 0; y < image.getHeight(); y++) for (int x = 0; x < image.getWidth(); x++) { int pixel = pixels[y * image.getWidth() + x]; buffer.put((byte) ((pixel >> 16) & 0xFF)); buffer.put((byte) ((pixel >> 8) & 0xFF)); buffer.put((byte) ((pixel) & 0xFF)); if (hasAlpha) buffer.put((byte) ((pixel >> 24) & 0xFF)); else buffer.put((byte) 0xFF); } buffer.flip(); this.data = new TextureData(image.getWidth(), image.getHeight(), buffer); TextureManager result = new TextureManager(); glBindTexture(GL_TEXTURE_2D, result.getID()); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer); LogHelper.info("Succesfully loaded texture from file: <textures/" + path + ".png>"); return result; } catch (Exception e) { e.printStackTrace(); LogHelper.error("Could not load texture: <textures/" + path + ".png>", e); } return null; } public TextureData getData() { return data; } public String getFile() { return file; } }