lwjgllightstate.java

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

JAVA
383
字号
/*
 * 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.lwjgl;

import java.util.Stack;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GLContext;

import com.jme.light.DirectionalLight;
import com.jme.light.Light;
import com.jme.light.PointLight;
import com.jme.light.SpotLight;
import com.jme.math.Matrix4f;
import com.jme.renderer.AbstractCamera;
import com.jme.renderer.RenderContext;
import com.jme.scene.Geometry;
import com.jme.scene.Spatial;
import com.jme.scene.Spatial.LightCombineMode;
import com.jme.scene.state.LightState;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.StateRecord;
import com.jme.scene.state.lwjgl.records.LightRecord;
import com.jme.scene.state.lwjgl.records.LightStateRecord;
import com.jme.system.DisplaySystem;

/**
 * <code>LWJGLLightState</code> subclasses the Light class using the LWJGL API
 * to access OpenGL for light processing.
 * 
 * @author Mark Powell
 * @author Joshua Slack - reworked for StateRecords.
 * @version $Id: LWJGLLightState.java,v 1.30 2007/09/14 20:53:54 nca Exp $
 */
public class LWJGLLightState extends LightState {
    private static final long serialVersionUID = 1L;

    /**
     * Constructor instantiates a new <code>LWJGLLightState</code>.
     */
    public LWJGLLightState() {
        super();
    }

    /**
     * <code>set</code> iterates over the light queue and processes each
     * individual light.
     * 
     * @see com.jme.scene.state.RenderState#apply()
     */
    public void apply() {        
        RenderContext<?> context = DisplaySystem.getDisplaySystem().getCurrentContext();
        LightStateRecord record = (LightStateRecord) context.getStateRecord(StateType.Light);
        context.currentStates[StateType.Light.ordinal()] = this;

        if (isEnabled() && LIGHTS_ENABLED && getQuantity() > 0) {
            setLightEnabled(true, record);
            setTwoSided(twoSidedOn, record);
            setLocalViewer(localViewerOn, record);
            if (GLContext.getCapabilities().OpenGL12) {
                setSpecularControl(separateSpecularOn, record);
            }

            for (int i = 0, max = getQuantity(); i < max; i++) {
                Light light = get(i);                
                LightRecord lr = record.getLightRecord(i);
                //TODO: use the reference to get the lightrecord - rherlitz

                if (lr == null) {
                    lr = new LightRecord();
                    record.setLightRecord(lr, i);
                }

                if (light == null) {
                    setSingleLightEnabled(false, i, record, lr);
                } else {
                    if (light.isEnabled()) {
                        setLight(i, light, record, lr);
                    } else {
                        setSingleLightEnabled(false, i, record, lr);
                    }
                }
            }

            // disable lights at and above the max count in this state
            for (int i = getQuantity(); i < MAX_LIGHTS_ALLOWED; i++) {
                LightRecord lr = record.getLightRecord(i);

                if (lr == null) {
                    lr = new LightRecord();
                    record.setLightRecord(lr, i);
                }
                setSingleLightEnabled(false, i, record, lr);
            }

            if ((lightMask & MASK_GLOBALAMBIENT) == 0) {
                setModelAmbient(record, globalAmbient[0], globalAmbient[1],
                        globalAmbient[2], globalAmbient[3]);
            } else {
                setDefaultModelAmbient(record);
            }

            if (!record.isValid())
                record.validate();
        } else {
            setLightEnabled(false, record);
        }
    }

    private void setLight(int index, Light light, LightStateRecord record,
            LightRecord lr) {
        setSingleLightEnabled(true, index, record, lr);

        if ((lightMask & MASK_AMBIENT) == 0
                && (light.getLightMask() & MASK_AMBIENT) == 0) {
            setAmbient(index, record, light.getAmbient().r,
                    light.getAmbient().g, light.getAmbient().b, light
                            .getAmbient().a, lr);
        } else {
            setDefaultAmbient(index, record, lr);
        }

        if ((lightMask & MASK_DIFFUSE) == 0
                && (light.getLightMask() & MASK_DIFFUSE) == 0) {

            setDiffuse(index, record, light.getDiffuse().r,
                    light.getDiffuse().g, light.getDiffuse().b, light
                            .getDiffuse().a, lr);
        } else {
            setDefaultDiffuse(index, record, lr);
        }

        if ((lightMask & MASK_SPECULAR) == 0
                && (light.getLightMask() & MASK_SPECULAR) == 0) {

            setSpecular(index, record, light.getSpecular().r, light
                    .getSpecular().g, light.getSpecular().b, light
                    .getSpecular().a, lr);
        } else {
            setDefaultSpecular(index, record, lr);
        }

        if (light.isAttenuate()) {
            setAttenuate(true, index, light, record, lr);

        } else {
            setAttenuate(false, index, light, record, lr);

        }

        switch (light.getType()) {
            case Directional: {
                DirectionalLight pkDL = (DirectionalLight) light;

                setPosition(index, record, -pkDL.getDirection().x, -pkDL
                        .getDirection().y, -pkDL.getDirection().z, 0, lr);
                break;
            }
            case Point:
            case Spot: {
                PointLight pointLight = (PointLight) light;

⌨️ 快捷键说明

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