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

📄 sunflowapi.java

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

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;

import org.codehaus.janino.ClassBodyEvaluator;
import org.codehaus.janino.CompileException;
import org.codehaus.janino.Scanner;
import org.codehaus.janino.Parser.ParseException;
import org.codehaus.janino.Scanner.ScanException;
import org.sunflow.core.Camera;
import org.sunflow.core.CameraLens;
import org.sunflow.core.Display;
import org.sunflow.core.Geometry;
import org.sunflow.core.ImageSampler;
import org.sunflow.core.Instance;
import org.sunflow.core.LightSource;
import org.sunflow.core.Modifier;
import org.sunflow.core.Options;
import org.sunflow.core.ParameterList;
import org.sunflow.core.PrimitiveList;
import org.sunflow.core.RenderObject;
import org.sunflow.core.Scene;
import org.sunflow.core.SceneParser;
import org.sunflow.core.Shader;
import org.sunflow.core.Tesselatable;
import org.sunflow.core.ParameterList.InterpolationType;
import org.sunflow.core.parser.RA2Parser;
import org.sunflow.core.parser.RA3Parser;
import org.sunflow.core.parser.SCParser;
import org.sunflow.core.parser.ShaveRibParser;
import org.sunflow.core.parser.TriParser;
import org.sunflow.core.renderer.BucketRenderer;
import org.sunflow.core.renderer.ProgressiveRenderer;
import org.sunflow.core.renderer.SimpleRenderer;
import org.sunflow.image.Color;
import org.sunflow.math.BoundingBox;
import org.sunflow.math.Matrix4;
import org.sunflow.math.Point3;
import org.sunflow.math.Vector3;
import org.sunflow.system.SearchPath;
import org.sunflow.system.Timer;
import org.sunflow.system.UI;
import org.sunflow.system.UI.Module;

/**
 * This API gives a simple interface for creating scenes procedurally. This is
 * the main entry point to Sunflow. To use this class, extend from it and
 * implement the build method which may execute arbitrary code to create a
 * scene.
 */
public class SunflowAPI {
    public static final String VERSION = "0.07.2";
    public static final String DEFAULT_OPTIONS = "::options";

    private Scene scene;
    private BucketRenderer bucketRenderer;
    private ProgressiveRenderer progressiveRenderer;
    private SearchPath includeSearchPath;
    private SearchPath textureSearchPath;
    private ParameterList parameterList;
    private RenderObjectMap renderObjects;
    private int currentFrame;

    /**
     * This is a quick system test which verifies that the user has launched
     * Java properly.
     */
    public static void runSystemCheck() {
        final long RECOMMENDED_MAX_SIZE = 800;
        long maxMb = Runtime.getRuntime().maxMemory() / 1048576;
        if (maxMb < RECOMMENDED_MAX_SIZE)
            UI.printError(Module.API, "JVM available memory is below %d MB (found %d MB only).\nPlease make sure you launched the program with the -Xmx command line options.", RECOMMENDED_MAX_SIZE, maxMb);
        String compiler = System.getProperty("java.vm.name");
        if (compiler == null || !(compiler.contains("HotSpot") && compiler.contains("Server")))
            UI.printError(Module.API, "You do not appear to be running Sun's server JVM\nPerformance may suffer");
        UI.printDetailed(Module.API, "Java environment settings:");
        UI.printDetailed(Module.API, "  * Max memory available : %d MB", maxMb);
        UI.printDetailed(Module.API, "  * Virtual machine name : %s", compiler == null ? "<unknown" : compiler);
        UI.printDetailed(Module.API, "  * Operating system     : %s", System.getProperty("os.name"));
        UI.printDetailed(Module.API, "  * CPU architecture     : %s", System.getProperty("os.arch"));
    }

    /**
     * Creates an empty scene.
     */
    public SunflowAPI() {
        reset();
    }

    /**
     * Reset the state of the API completely. The object table is cleared, and
     * all search paths areset back to their default values.
     */
    public final void reset() {
        scene = new Scene();
        bucketRenderer = new BucketRenderer();
        progressiveRenderer = new ProgressiveRenderer();
        includeSearchPath = new SearchPath("include");
        textureSearchPath = new SearchPath("texture");
        parameterList = new ParameterList();
        renderObjects = new RenderObjectMap();
        currentFrame = 1;
    }

    /**
     * Returns a name currently not being used by any other object. The returned
     * name is of the form "prefix_n" where n is an integer starting at 1. Only
     * a simple linear search is performed, so this method should be used only
     * when there is no other way to guarentee uniqueness.
     * 
     * @param prefix name prefix
     * @return a unique name not used by any rendering object
     */
    public final String getUniqueName(String prefix) {
        // generate a unique name based on the given prefix
        int counter = 1;
        String name;
        do {
            name = String.format("%s_%d", prefix, counter);
            counter++;
        } while (renderObjects.has(name));
        return name;
    }

    /**
     * Declare a parameter with the specified name and value. This parameter
     * will be added to the currently active parameter list.
     * 
     * @param name parameter name
     * @param value parameter value
     */
    public final void parameter(String name, String value) {
        parameterList.addString(name, value);
    }

    /**
     * Declare a parameter with the specified name and value. This parameter
     * will be added to the currently active parameter list.
     * 
     * @param name parameter name
     * @param value parameter value
     */
    public final void parameter(String name, boolean value) {
        parameterList.addBoolean(name, value);
    }

    /**
     * Declare a parameter with the specified name and value. This parameter
     * will be added to the currently active parameter list.
     * 
     * @param name parameter name
     * @param value parameter value
     */
    public final void parameter(String name, int value) {
        parameterList.addInteger(name, value);
    }

    /**
     * Declare a parameter with the specified name and value. This parameter
     * will be added to the currently active parameter list.
     * 
     * @param name parameter name
     * @param value parameter value
     */
    public final void parameter(String name, float value) {
        parameterList.addFloat(name, value);
    }

    /**
     * Declare a parameter with the specified name and value. This parameter
     * will be added to the currently active parameter list.
     * 
     * @param name parameter name
     * @param value parameter value
     */
    public final void parameter(String name, Color value) {
        parameterList.addColor(name, value);
    }

    /**
     * Declare a parameter with the specified name and value. This parameter
     * will be added to the currently active parameter list.
     * 
     * @param name parameter name
     * @param value parameter value
     */
    public final void parameter(String name, Point3 value) {
        parameterList.addPoints(name, InterpolationType.NONE, new float[] {
                value.x, value.y, value.z });
    }

    /**
     * Declare a parameter with the specified name and value. This parameter
     * will be added to the currently active parameter list.
     * 
     * @param name parameter name
     * @param value parameter value
     */
    public final void parameter(String name, Vector3 value) {
        parameterList.addVectors(name, InterpolationType.NONE, new float[] {
                value.x, value.y, value.z });
    }

    /**
     * Declare a parameter with the specified name and value. This parameter
     * will be added to the currently active parameter list.
     * 
     * @param name parameter name
     * @param value parameter value
     */
    public final void parameter(String name, Matrix4 value) {
        parameterList.addMatrices(name, InterpolationType.NONE, value.asRowMajor());
    }

    /**
     * Declare a parameter with the specified name and value. This parameter
     * will be added to the currently active parameter list.
     * 
     * @param name parameter name
     * @param value parameter value
     */
    public final void parameter(String name, int[] value) {
        parameterList.addIntegerArray(name, value);
    }

    /**
     * Declare a parameter with the specified name and value. This parameter
     * will be added to the currently active parameter list.
     * 
     * @param name parameter name
     * @param value parameter value
     */
    public final void parameter(String name, String[] value) {
        parameterList.addStringArray(name, value);
    }

    /**
     * Declare a parameter with the specified name. The type may be one of the
     * follow: "float", "point", "vector", "texcoord", "matrix". The
     * interpolation determines how the parameter is to be interpreted over
     * surface (see {@link InterpolationType}). The data is specified in a
     * flattened float array.
     * 
     * @param name parameter name
     * @param type parameter data type
     * @param interpolation parameter interpolation mode
     * @param data raw floating point data
     */
    public final void parameter(String name, String type, String interpolation, float[] data) {
        InterpolationType interp;
        try {
            interp = InterpolationType.valueOf(interpolation.toUpperCase());
        } catch (IllegalArgumentException e) {
            UI.printError(Module.API, "Unknown interpolation type: %s -- ignoring parameter \"%s\"", interpolation, name);
            return;
        }
        if (type.equals("float"))
            parameterList.addFloats(name, interp, data);
        else if (type.equals("point"))
            parameterList.addPoints(name, interp, data);
        else if (type.equals("vector"))
            parameterList.addVectors(name, interp, data);
        else if (type.equals("texcoord"))
            parameterList.addTexCoords(name, interp, data);
        else if (type.equals("matrix"))
            parameterList.addMatrices(name, interp, data);
        else
            UI.printError(Module.API, "Unknown parameter type: %s -- ignoring parameter \"%s\"", type, name);
    }

    /**
     * Remove the specified render object. Note that this may cause the removal
     * of other objects which depended on it.
     * 
     * @param name name of the object to remove

⌨️ 快捷键说明

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