Java tutorial
/* * Copyright (C) 2013 Clemens-Alexander Brust IT-Dienstleistungen * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package de.ikosa.mars.viewer.glviewer.engine; import de.ikosa.mars.util.ML; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL13; import org.lwjgl.opengl.GL30; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.FileInputStream; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.ShortBuffer; public class GLTextureBuilder { public String name; public String filePath; public GLTextureBuilder(String name, String filePath) { this.name = name; this.filePath = filePath; } public static GLTexture createEmptyTexture(String name, int width, int height, byte r, byte g, byte b, byte a) { try { int textureId = GL11.glGenTextures(); ByteBuffer buffer = ByteBuffer.allocateDirect(width * height * 4); for (int i = 0; i < width * height; i++) { buffer.put(r); buffer.put(g); buffer.put(b); buffer.put(a); } buffer.flip(); GL13.glActiveTexture(GL13.GL_TEXTURE0); GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId); GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1); GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer); GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D); GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); return new GLTexture(name, textureId); } catch (Exception e) { ML.f(e); } return null; } public static GLTexture createEmptyTexture(String name, int width, int height, float initial) { try { int textureId = GL11.glGenTextures(); ByteBuffer buffer = ByteBuffer.allocateDirect(width * height * 4); for (int i = 0; i < width * height * 4; i++) { buffer.put((byte) 0); } buffer.flip(); GL13.glActiveTexture(GL13.GL_TEXTURE0); GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId); GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1); GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_DEPTH_COMPONENT, width, height, 0, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, buffer); GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D); GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); return new GLTexture(name, textureId); } catch (Exception e) { ML.f(e); } return null; } public GLTexture createTexture() { try { InputStream inputStream = new FileInputStream(filePath); BufferedImage image = ImageIO.read(inputStream); byte[] x = GLPNGLoader.loadPNG(image); boolean grayScale = (image.getWidth() * image.getHeight() * 4) > x.length; int textureId = grayScale ? createGrayscaleTexture(image, x) : createRGBTexture(image, x); return new GLTexture(name, textureId); } catch (Exception e) { ML.f(e); } return null; } private int createRGBTexture(BufferedImage image, byte[] x) { ByteBuffer buffer; buffer = ByteBuffer.allocateDirect(x.length); for (int i = 0; i < x.length; i++) { buffer.put(x[i]); } buffer.flip(); int textureId = GL11.glGenTextures(); GLRenderer2Stage.errorCheck("generating texture id"); GL13.glActiveTexture(GL13.GL_TEXTURE0); GLRenderer2Stage.errorCheck("activating texture image unit"); GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId); GLRenderer2Stage.errorCheck("binding 2d texture"); GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1); GLRenderer2Stage.errorCheck("setting unpack aligment"); GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer); GLRenderer2Stage.errorCheck("storing 2d texture data"); GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D); GLRenderer2Stage.errorCheck("generating 2d texture array mipmaps"); GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); GLRenderer2Stage.errorCheck("unbinding 2d texture array"); return textureId; } private int createGrayscaleTexture(BufferedImage image, byte[] x) { ShortBuffer buffer; buffer = BufferUtils.createShortBuffer(image.getHeight() * image.getWidth()); for (int i = 0; i < x.length; i += 2) { short val = (short) (fromByte(x[i]) + (fromByte(x[i + 1]) << 8)); buffer.put(val); } buffer.flip(); int textureId = GL11.glGenTextures(); GLRenderer2Stage.errorCheck("generating texture id"); GL13.glActiveTexture(GL13.GL_TEXTURE0); GLRenderer2Stage.errorCheck("activating texture image unit"); GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId); GLRenderer2Stage.errorCheck("binding 2d texture"); GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 2); GLRenderer2Stage.errorCheck("setting unpack aligment"); GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL30.GL_R16, image.getWidth(), image.getHeight(), 0, GL11.GL_RED, GL11.GL_UNSIGNED_SHORT, buffer); GLRenderer2Stage.errorCheck("storing 2d texture data"); GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D); GLRenderer2Stage.errorCheck("generating 2d texture array mipmaps"); GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); GLRenderer2Stage.errorCheck("unbinding 2d texture array"); return textureId; } private int fromByte(byte b) { return (b >= 0) ? b : ((int) b) + 256; } }