de.ikosa.mars.viewer.glviewer.engine.GLTextureArrayBuilder.java Source code

Java tutorial

Introduction

Here is the source code for de.ikosa.mars.viewer.glviewer.engine.GLTextureArrayBuilder.java

Source

/*
 * 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.opengl.GL11;
import org.lwjgl.opengl.GL12;
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;

public class GLTextureArrayBuilder extends GLTextureBuilder {
    String[] filePaths;

    public GLTextureArrayBuilder(String name, String... filePaths) {
        super(name, null);
        this.filePaths = filePaths;
    }

    @Override
    public GLTextureArray createTexture() {
        try {
            int files = filePaths.length;
            byte[][] imageData = new byte[files][];
            int totalSize = 0;
            int imageWidth = 0;
            int imageHeight = 0;
            // read in files
            for (int file = 0; file < files; file++) {
                InputStream inputStream = new FileInputStream(filePaths[file]);
                BufferedImage image = ImageIO.read(inputStream);

                if (file > 0)
                    if (imageWidth != image.getWidth() | imageHeight != image.getHeight())
                        ML.f("Incompatible images in 3D texture...");

                imageWidth = image.getWidth();
                imageHeight = image.getHeight();

                imageData[file] = GLPNGLoader.loadPNG(image);
                totalSize += imageData[file].length;
            }

            // store in consecutive buffer
            ByteBuffer buffer = ByteBuffer.allocateDirect(totalSize);
            for (int file = 0; file < files; file++) {
                byte[] singleImageData = imageData[file];
                for (int i = 0; i < singleImageData.length; i++)
                    buffer.put(singleImageData[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(GL30.GL_TEXTURE_2D_ARRAY, textureId);
            GLRenderer2Stage.errorCheck("binding 2d texture array");

            GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
            GLRenderer2Stage.errorCheck("setting unpack aligment");

            GL12.glTexImage3D(GL30.GL_TEXTURE_2D_ARRAY, 0, GL11.GL_RGBA, imageWidth, imageHeight, files, 0,
                    GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
            GLRenderer2Stage.errorCheck("storing 2d texture array data");

            GL30.glGenerateMipmap(GL30.GL_TEXTURE_2D_ARRAY);
            GLRenderer2Stage.errorCheck("generating 2d texture array mipmaps");

            GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, 0);
            GLRenderer2Stage.errorCheck("unbinding 2d texture array");

            return new GLTextureArray(name, textureId);
        } catch (Exception e) {
            ML.f(e);
        }
        return null;
    }
}