⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 quadmesh.java

📁 Sunflow是一个照片级的渲染系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.sunflow.core.primitive;

import java.io.FileWriter;
import java.io.IOException;

import org.sunflow.SunflowAPI;
import org.sunflow.core.Instance;
import org.sunflow.core.IntersectionState;
import org.sunflow.core.ParameterList;
import org.sunflow.core.PrimitiveList;
import org.sunflow.core.Ray;
import org.sunflow.core.ShadingState;
import org.sunflow.core.ParameterList.FloatParameter;
import org.sunflow.core.ParameterList.InterpolationType;
import org.sunflow.math.BoundingBox;
import org.sunflow.math.MathUtils;
import org.sunflow.math.Matrix4;
import org.sunflow.math.OrthoNormalBasis;
import org.sunflow.math.Point3;
import org.sunflow.math.Vector3;
import org.sunflow.system.UI;
import org.sunflow.system.UI.Module;

public class QuadMesh implements PrimitiveList {
    protected float[] points;
    protected int[] quads;
    private FloatParameter normals;
    private FloatParameter uvs;
    private byte[] faceShaders;

    public QuadMesh() {
        quads = null;
        points = null;
        normals = uvs = new FloatParameter();
        faceShaders = null;
    }

    public void writeObj(String filename) {
        try {
            FileWriter file = new FileWriter(filename);
            file.write(String.format("o object\n"));
            for (int i = 0; i < points.length; i += 3)
                file.write(String.format("v %g %g %g\n", points[i], points[i + 1], points[i + 2]));
            file.write("s off\n");
            for (int i = 0; i < quads.length; i += 4)
                file.write(String.format("f %d %d %d %d\n", quads[i] + 1, quads[i + 1] + 1, quads[i + 2] + 1, quads[i + 3] + 1));
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean update(ParameterList pl, SunflowAPI api) {
        {
            int[] quads = pl.getIntArray("quads");
            if (quads != null) {
                this.quads = quads;
            }
        }
        if (quads == null) {
            UI.printError(Module.GEOM, "Unable to update mesh - quad indices are missing");
            return false;
        }
        if (quads.length % 4 != 0)
            UI.printWarning(Module.GEOM, "Quad index data is not a multiple of 4 - some quads may be missing");
        pl.setFaceCount(quads.length / 4);
        {
            FloatParameter pointsP = pl.getPointArray("points");
            if (pointsP != null)
                if (pointsP.interp != InterpolationType.VERTEX)
                    UI.printError(Module.GEOM, "Point interpolation type must be set to \"vertex\" - was \"%s\"", pointsP.interp.name().toLowerCase());
                else {
                    points = pointsP.data;
                }
        }
        if (points == null) {
            UI.printError(Module.GEOM, "Unabled to update mesh - vertices are missing");
            return false;
        }
        pl.setVertexCount(points.length / 3);
        pl.setFaceVertexCount(4 * (quads.length / 4));
        FloatParameter normals = pl.getVectorArray("normals");
        if (normals != null)
            this.normals = normals;
        FloatParameter uvs = pl.getTexCoordArray("uvs");
        if (uvs != null)
            this.uvs = uvs;
        int[] faceShaders = pl.getIntArray("faceshaders");
        if (faceShaders != null && faceShaders.length == quads.length / 4) {
            this.faceShaders = new byte[faceShaders.length];
            for (int i = 0; i < faceShaders.length; i++) {
                int v = faceShaders[i];
                if (v > 255)
                    UI.printWarning(Module.GEOM, "Shader index too large on quad %d", i);
                this.faceShaders[i] = (byte) (v & 0xFF);
            }
        }
        return true;
    }

    public float getPrimitiveBound(int primID, int i) {
        int quad = 4 * primID;
        int a = 3 * quads[quad + 0];
        int b = 3 * quads[quad + 1];
        int c = 3 * quads[quad + 2];
        int d = 3 * quads[quad + 3];
        int axis = i >>> 1;
        if ((i & 1) == 0)
            return MathUtils.min(points[a + axis], points[b + axis], points[c + axis], points[d + axis]);
        else
            return MathUtils.max(points[a + axis], points[b + axis], points[c + axis], points[d + axis]);
    }

    public BoundingBox getWorldBounds(Matrix4 o2w) {
        BoundingBox bounds = new BoundingBox();
        if (o2w == null) {
            for (int i = 0; i < points.length; i += 3)
                bounds.include(points[i], points[i + 1], points[i + 2]);
        } else {
            // transform vertices first
            for (int i = 0; i < points.length; i += 3) {
                float x = points[i];
                float y = points[i + 1];
                float z = points[i + 2];
                float wx = o2w.transformPX(x, y, z);
                float wy = o2w.transformPY(x, y, z);
                float wz = o2w.transformPZ(x, y, z);
                bounds.include(wx, wy, wz);
            }
        }
        return bounds;
    }

    public void intersectPrimitive(Ray r, int primID, IntersectionState state) {
        // ray/bilinear patch intersection adapted from "Production Rendering:
        // Design and Implementation" by Ian Stephenson (Ed.)
        int quad = 4 * primID;
        int p0 = 3 * quads[quad + 0];
        int p1 = 3 * quads[quad + 1];
        int p2 = 3 * quads[quad + 2];
        int p3 = 3 * quads[quad + 3];
        // transform patch into Hilbert space
        final float A[] = {
                points[p2 + 0] - points[p3 + 0] - points[p1 + 0] + points[p0 + 0],
                points[p2 + 1] - points[p3 + 1] - points[p1 + 1] + points[p0 + 1],
                points[p2 + 2] - points[p3 + 2] - points[p1 + 2] + points[p0 + 2] };
        final float B[] = { points[p1 + 0] - points[p0 + 0],
                points[p1 + 1] - points[p0 + 1],
                points[p1 + 2] - points[p0 + 2] };
        final float C[] = { points[p3 + 0] - points[p0 + 0],
                points[p3 + 1] - points[p0 + 1],
                points[p3 + 2] - points[p0 + 2] };
        final float R[] = { r.ox - points[p0 + 0], r.oy - points[p0 + 1],
                r.oz - points[p0 + 2] };
        final float Q[] = { r.dx, r.dy, r.dz };

        // pick major direction
        float absqx = Math.abs(r.dx);
        float absqy = Math.abs(r.dy);
        float absqz = Math.abs(r.dz);

        int X = 0, Y = 1, Z = 2;
        if (absqx > absqy && absqx > absqz) {
            // X = 0, Y = 1, Z = 2
        } else if (absqy > absqz) {
            // X = 1, Y = 0, Z = 2
            X = 1;
            Y = 0;
        } else {
            // X = 2, Y = 1, Z = 0
            X = 2;
            Z = 0;
        }

        float Cxz = C[X] * Q[Z] - C[Z] * Q[X];
        float Cyx = C[Y] * Q[X] - C[X] * Q[Y];
        float Czy = C[Z] * Q[Y] - C[Y] * Q[Z];
        float Rxz = R[X] * Q[Z] - R[Z] * Q[X];
        float Ryx = R[Y] * Q[X] - R[X] * Q[Y];
        float Rzy = R[Z] * Q[Y] - R[Y] * Q[Z];
        float Bxy = B[X] * Q[Y] - B[Y] * Q[X];
        float Byz = B[Y] * Q[Z] - B[Z] * Q[Y];
        float Bzx = B[Z] * Q[X] - B[X] * Q[Z];
        float a = A[X] * Byz + A[Y] * Bzx + A[Z] * Bxy;
        if (a == 0) {
            // setup for linear equation
            float b = B[X] * Czy + B[Y] * Cxz + B[Z] * Cyx;
            float c = C[X] * Rzy + C[Y] * Rxz + C[Z] * Ryx;
            float u = -c / b;
            if (u >= 0 && u <= 1) {
                float v = (u * Bxy + Ryx) / Cyx;
                if (v >= 0 && v <= 1) {
                    float t = (B[X] * u + C[X] * v - R[X]) / Q[X];
                    if (r.isInside(t)) {
                        r.setMax(t);
                        state.setIntersection(primID, u, v);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -