All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dabasan.jxm.bd1.BD1BufferGenerator Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
package com.github.dabasan.jxm.bd1;

import org.joml.Vector3fc;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * BD1 buffer generator
 *
 * @author maeda6uiui
 */
class BD1BufferGenerator {
    public static List generateBuffers(Map> facesMap,
                                                  boolean flipV) {
        var buffers = new ArrayList();

        for (var entry : facesMap.entrySet()) {
            var indexValues = new ArrayList();
            var posValues = new ArrayList();
            var uvValues = new ArrayList();
            var normValues = new ArrayList();

            int countIndex = 0;
            for (var face : entry.getValue()) {
                Vector3fc[] vertexPositions = face.vertexPositions;
                UV[] uvs = face.uvs;
                Vector3fc normal = face.normal;

                // First triangle
                indexValues.add(countIndex);
                indexValues.add(countIndex + 1);
                indexValues.add(countIndex + 2);
                // Second triangle
                indexValues.add(countIndex + 2);
                indexValues.add(countIndex + 3);
                indexValues.add(countIndex);
                countIndex += 4;

                for (int i = 3; i >= 0; i--) {
                    // Position
                    posValues.add(vertexPositions[i].x());
                    posValues.add(vertexPositions[i].y());
                    posValues.add(vertexPositions[i].z());
                    // UVs
                    uvValues.add(uvs[i].u);
                    if (flipV == true) {
                        uvValues.add(uvs[i].v * (-1.0f));
                    } else {
                        uvValues.add(uvs[i].v);
                    }
                    // Normal
                    normValues.add(normal.x());
                    normValues.add(normal.y());
                    normValues.add(normal.z());
                }
            }

            IntBuffer indexBuffer = IntBuffer.allocate(indexValues.size());
            FloatBuffer posBuffer = FloatBuffer.allocate(posValues.size());
            FloatBuffer uvBuffer = FloatBuffer.allocate(uvValues.size());
            FloatBuffer normBuffer = FloatBuffer.allocate(normValues.size());
            for (var indexValue : indexValues) {
                indexBuffer.put(indexValue);
            }
            for (var posValue : posValues) {
                posBuffer.put(posValue);
            }
            for (var uvValue : uvValues) {
                uvBuffer.put(uvValue);
            }
            for (var normValue : normValues) {
                normBuffer.put(normValue);
            }
            indexBuffer.flip();
            posBuffer.flip();
            uvBuffer.flip();
            normBuffer.flip();

            var buffer = new BD1Buffer();
            buffer.indexBuffer = indexBuffer;
            buffer.posBuffer = posBuffer;
            buffer.uvBuffer = uvBuffer;
            buffer.normBuffer = normBuffer;

            int textureID = entry.getKey();
            buffer.textureID = textureID;

            buffers.add(buffer);
        }

        return buffers;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy