glslshaderobjectsstate.java

来自「java 3d game jme 工程开发源代码」· Java 代码 · 共 917 行 · 第 1/3 页

JAVA
917
字号
/*
 * Copyright (c) 2003-2009 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.jme.scene.state;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.jme.math.Matrix3f;
import com.jme.math.Matrix4f;
import com.jme.math.Quaternion;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Geometry;
import com.jme.scene.state.RenderState.StateType;
import com.jme.util.export.InputCapsule;
import com.jme.util.export.JMEExporter;
import com.jme.util.export.JMEImporter;
import com.jme.util.export.OutputCapsule;
import com.jme.util.geom.BufferUtils;
import com.jme.util.shader.ShaderVariable;
import com.jme.util.shader.uniformtypes.ShaderVariableFloat;
import com.jme.util.shader.uniformtypes.ShaderVariableFloat2;
import com.jme.util.shader.uniformtypes.ShaderVariableFloat3;
import com.jme.util.shader.uniformtypes.ShaderVariableFloat4;
import com.jme.util.shader.uniformtypes.ShaderVariableInt;
import com.jme.util.shader.uniformtypes.ShaderVariableInt2;
import com.jme.util.shader.uniformtypes.ShaderVariableInt3;
import com.jme.util.shader.uniformtypes.ShaderVariableInt4;
import com.jme.util.shader.uniformtypes.ShaderVariableMatrix2;
import com.jme.util.shader.uniformtypes.ShaderVariableMatrix3;
import com.jme.util.shader.uniformtypes.ShaderVariableMatrix4;
import com.jme.util.shader.uniformtypes.ShaderVariablePointerByte;
import com.jme.util.shader.uniformtypes.ShaderVariablePointerFloat;
import com.jme.util.shader.uniformtypes.ShaderVariablePointerInt;
import com.jme.util.shader.uniformtypes.ShaderVariablePointerShort;

/**
 * Implementation of the GL_ARB_shader_objects extension.
 *
 * @author Thomas Hourdel
 * @author Rikard Herlitz (MrCoder)
 */
public abstract class GLSLShaderObjectsState extends RenderState {
    private static final Logger logger = Logger
            .getLogger(GLSLShaderObjectsState.class.getName());

    /** Storage for shader uniform values */
    protected ArrayList<ShaderVariable> shaderUniforms =
            new ArrayList<ShaderVariable>();
    /** Storage for shader attribute values */
    protected ArrayList<ShaderVariable> shaderAttributes =
            new ArrayList<ShaderVariable>();
    
    /** Optional logic for setting shadervariables based on the current geom */
    protected GLSLShaderDataLogic shaderDataLogic;

    /** The Geometry this shader currently operates on during rendering */
    protected Geometry geom;
    
    protected static boolean glslSupported = false;
    protected static boolean glslSupportedDetected = false;
    
    protected boolean needSendShader = false;
    
    protected String vertShader, fragShader;
    
    /**
     * Gets the currently loaded vertex shader.
     * @return
     */
    public	String	getVertexShader() {
    	return vertShader;
    }
    
    /**
     * Gets the currently loaded fragment shader.
     * @return
     */
    public	String	getFragmentShader() {
    	return fragShader;
    }
    
    /**
     * Gets all shader uniforms variables.
     * @return
     */
    public	ArrayList<ShaderVariable>	getShaderUniforms() {
    	return shaderUniforms;
    }
    
    /**
     * Retrieves a shader uniform by name.
     * @param uniformName
     * @return
     */
    public	ShaderVariable		getUniformByName(String uniformName) {
    	for(ShaderVariable shaderVar : shaderUniforms) {
    		if(shaderVar.name.equals(uniformName)) {
    			return shaderVar;
    		}
    	}
    	
    	return null;
    }
    
    /**
     * Gets all shader attribute variables.
     * @return
     */
    public	ArrayList<ShaderVariable>	getShaderAttributes() {
    	return shaderAttributes;
    }
    
    /**
     * Retrieves a shader attribute by name.
     * @param uniformName
     * @return
     */
    public	ShaderVariable		getAttributeByName(String attributeName) {
    	for(ShaderVariable shaderVar : shaderAttributes) {
    		if(shaderVar.name.equals(attributeName)) {
    			return shaderVar;
    		}
    	}
    	
    	return null;
    }
    
    /**
     * 
     * @param geom
     */
    public void setGeometry(Geometry geom) {
        this.geom = geom;
    }
    
    /**
     * Logic to handle setting geom-specific data to a shader before rendering 
     * @param shaderDataLogic
     */
    public void setShaderDataLogic(GLSLShaderDataLogic shaderDataLogic) {
        this.shaderDataLogic = shaderDataLogic;
    }    

    /**
     * <code>isSupported</code> determines if the ARB_shader_objects extension
     * is supported by current graphics configuration. This will only be valid
     * if a renderer has been created.
     * 
     * @return if ARB shader objects are supported
     */
    public static boolean isSupported() {
        return glslSupported;
    }

    /**
     * Overide setting of glsl support.
     * 
     * @param use
     */
    public static void overrideSupport(boolean use) {
        glslSupported = use;
    }

    /**
     * Reset glsl support to driver-detected setting.
     */
    public static void resetSupport() {
        glslSupported = glslSupportedDetected;
    }

    /**
     * Set an uniform value for this shader object.
     *
     * @param name uniform variable to change
     * @param value the new value
     */
    public void setUniform(String name, boolean value) {
        ShaderVariableInt shaderUniform =
                getShaderUniform(name, ShaderVariableInt.class);
        shaderUniform.value1 = value ? 1 : 0;

        setNeedsRefresh(true);
    }

    /**
     * Set an uniform value for this shader object.
     *
     * @param name uniform variable to change
     * @param value the new value
     */
    public void setUniform(String name, int value) {
        ShaderVariableInt shaderUniform =
                getShaderUniform(name, ShaderVariableInt.class);
        shaderUniform.value1 = value;

        setNeedsRefresh(true);
    }

    /**
     * Set an uniform value for this shader object.
     *
     * @param name uniform variable to change
     * @param value the new value
     */
    public void setUniform(String name, float value) {
        ShaderVariableFloat shaderUniform =
                getShaderUniform(name, ShaderVariableFloat.class);
        shaderUniform.value1 = value;

        setNeedsRefresh(true);
    }

    /**
     * Set an uniform value for this shader object.
     *
     * @param name uniform variable to change
     * @param value1 the new value
     * @param value2 the new value
     */
    public void setUniform(String name, boolean value1, boolean value2) {
        ShaderVariableInt2 shaderUniform =
                getShaderUniform(name, ShaderVariableInt2.class);
        shaderUniform.value1 = value1 ? 1 : 0;
        shaderUniform.value2 = value2 ? 1 : 0;

        setNeedsRefresh(true);
    }

    /**
     * Set an uniform value for this shader object.
     *
     * @param name uniform variable to change
     * @param value1 the new value
     * @param value2 the new value
     */
    public void setUniform(String name, int value1, int value2) {
        ShaderVariableInt2 shaderUniform =
                getShaderUniform(name, ShaderVariableInt2.class);
        shaderUniform.value1 = value1;
        shaderUniform.value2 = value2;

        setNeedsRefresh(true);
    }

    /**
     * Set an uniform value for this shader object.
     *
     * @param name uniform variable to change
     * @param value1 the new value
     * @param value2 the new value
     */
    public void setUniform(String name, float value1, float value2) {
        ShaderVariableFloat2 shaderUniform =
                getShaderUniform(name, ShaderVariableFloat2.class);
        shaderUniform.value1 = value1;
        shaderUniform.value2 = value2;

        setNeedsRefresh(true);
    }

    /**
     * Set an uniform value for this shader object.

⌨️ 快捷键说明

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