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

📄 joglrenderer.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*
 * 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.renderer.jogl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.Arrays;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.media.opengl.GL;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLException;
import javax.media.opengl.glu.GLU;

import com.jme.curve.Curve;
import com.jme.image.Image;
import com.jme.image.Texture;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.RenderContext;
import com.jme.renderer.RenderQueue;
import com.jme.renderer.Renderer;
import com.jme.scene.Geometry;
import com.jme.scene.Line;
import com.jme.scene.Point;
import com.jme.scene.QuadMesh;
import com.jme.scene.Spatial;
import com.jme.scene.TexCoords;
import com.jme.scene.Text;
import com.jme.scene.TriMesh;
import com.jme.scene.VBOInfo;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.ClipState;
import com.jme.scene.state.ColorMaskState;
import com.jme.scene.state.CullState;
import com.jme.scene.state.FogState;
import com.jme.scene.state.FragmentProgramState;
import com.jme.scene.state.GLSLShaderObjectsState;
import com.jme.scene.state.LightState;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.ShadeState;
import com.jme.scene.state.StateRecord;
import com.jme.scene.state.StencilState;
import com.jme.scene.state.TextureState;
import com.jme.scene.state.VertexProgramState;
import com.jme.scene.state.WireframeState;
import com.jme.scene.state.ZBufferState;
import com.jme.scene.state.jogl.JOGLBlendState;
import com.jme.scene.state.jogl.JOGLClipState;
import com.jme.scene.state.jogl.JOGLColorMaskState;
import com.jme.scene.state.jogl.JOGLCullState;
import com.jme.scene.state.jogl.JOGLFogState;
import com.jme.scene.state.jogl.JOGLFragmentProgramState;
import com.jme.scene.state.jogl.JOGLLightState;
import com.jme.scene.state.jogl.JOGLMaterialState;
import com.jme.scene.state.jogl.JOGLShadeState;
import com.jme.scene.state.jogl.JOGLShaderObjectsState;
import com.jme.scene.state.jogl.JOGLStencilState;
import com.jme.scene.state.jogl.JOGLTextureState;
import com.jme.scene.state.jogl.JOGLVertexProgramState;
import com.jme.scene.state.jogl.JOGLWireframeState;
import com.jme.scene.state.jogl.JOGLZBufferState;
import com.jme.scene.state.jogl.records.LineRecord;
import com.jme.scene.state.jogl.records.RendererRecord;
import com.jme.scene.state.jogl.records.TextureStateRecord;
import com.jme.system.DisplaySystem;
import com.jme.system.JmeException;
import com.jme.system.jogl.JOGLDisplaySystem;
import com.jme.util.Debug;
import com.jme.util.WeakIdentityCache;
import com.jme.util.geom.BufferUtils;
import com.jme.util.stat.StatCollector;
import com.jme.util.stat.StatType;

/**
 * <code>JOGLRenderer</code> provides an implementation of the
 * <code>Renderer</code> interface using the JOGL API.
 *
 * @see com.jme.renderer.Renderer
 * @author Mark Powell - initial implementation, and more.
 * @author Joshua Slack - Further work, Optimizations, Headless rendering
 * @author Tijl Houtbeckers - Small optimizations and improved VBO
 * @author Steve Vaughan - JOGL port
 * @version $Id: JOGLRenderer.java 4096 2009-02-03 07:36:03Z andreas.grabner@gmail.com $
 */
public class JOGLRenderer extends Renderer {
    private static final Logger logger = Logger.getLogger(JOGLRenderer.class
            .getName());

    private final JOGLDisplaySystem display;

    private Vector3f vRot = new Vector3f();

    private JOGLFont font;

    private boolean supportsVBO = false;

    private boolean supportsFogCoords = false;

    private boolean indicesVBO = false;

    private boolean inOrthoMode;

    private Vector3f tempVa = new Vector3f();

    private FloatBuffer prevVerts;

    private FloatBuffer prevFogCoords;

    private FloatBuffer prevNorms;

    private FloatBuffer prevColor;

    private FloatBuffer[] prevTex;

    private int prevNormMode = GL.GL_ZERO;

    private final JOGLContextCapabilities capabilities;

    private int prevTextureNumber = 0;

    private boolean generatingDisplayList = false;

    protected WeakIdentityCache<Buffer, Integer> vboMap = new WeakIdentityCache<Buffer, Integer>();

    /**
     * Constructor instantiates a new <code>JOGLRenderer</code> object. The
     * size of the rendering window is passed during construction.
     *
     * @param width
     *            the width of the rendering context.
     * @param height
     *            the height of the rendering context.
     *            
     * TODO Replace all of these fields with one surface reference?
     * TODO If the capabilities are really context specific the field should be dropped.
     */
    public JOGLRenderer(JOGLDisplaySystem display, JOGLContextCapabilities caps, int width, int height) {
        if (width <= 0 || height <= 0) {
            logger.warning("Invalid width and/or height values.");
            throw new JmeException("Invalid width and/or height values.");
        }
        this.display = display;
        this.capabilities = caps;
        this.width = width;
        this.height = height;

        logger.info("JOGLRenderer created. W:  " + width + "H: " + height + "\tVersion: "
                + Package.getPackage( "javax.media.opengl" ).getImplementationVersion() );

        queue = new RenderQueue(this);
        if (TextureState.getNumberOfTotalUnits() == -1)
            createTextureState(); // force units population
        prevTex = new FloatBuffer[TextureState.getNumberOfTotalUnits()];

        supportsVBO = capabilities.GL_ARB_vertex_buffer_object;

        supportsFogCoords = capabilities.GL_EXT_fog_coord;
    }

    /**
     * Reinitialize the renderer with the given width/height. Also calls resize
     * on the attached camera if present.
     *
     * @param width
     *            int
     * @param height
     *            int
     */
    public void reinit(int width, int height) {
        if (width <= 0 || height <= 0) {
            logger.warning("Invalid width and/or height values:" + width + "x" + height);
            throw new JmeException("Invalid width and/or height values:" + width + "x" + height);
        }
        this.width = width;
        this.height = height;
        if (camera != null) {
            camera.resize(width, height);
            camera.apply();
        }
    }

    /**
     * <code>setCamera</code> sets the camera this renderer is using. It
     * asserts that the camera is of type <code>JOGLCamera</code>.
     *
     * @see com.jme.renderer.Renderer#setCamera(com.jme.renderer.Camera)
     */
    public void setCamera(final Camera camera) {
        // Check that this isn't the same camera to avoid unnecessary work.
        if (camera == this.camera)
            return;

        if (camera instanceof JOGLCamera) {
            this.camera = (JOGLCamera) camera;

            // Update dimensions for the newly associated camera and apply the
            // changes.
            ((JOGLCamera) this.camera).resize(width, height, true);
            this.camera.apply();
        }
    }

    /**
     * <code>createCamera</code> returns a default camera for use with the
     * JOGL renderer.
     *
     * @param width
     *            the width of the frame.
     * @param height
     *            the height of the frame.
     * @return a default JOGL camera.
     */
    public Camera createCamera(int width, int height) {
        return new JOGLCamera(width, height);
    }

    /**
     * <code>createBlendState</code> returns a new JOGLBlendState object as a
     * regular BlendState.
     *
     * @return an BlendState object.
     */
    public BlendState createBlendState() {
        return new JOGLBlendState(capabilities);
    }

    /**
     * <code>createCullState</code> returns a new JOGLCullState object as a
     * regular CullState.
     *
     * @return a CullState object.
     * @see com.jme.renderer.Renderer#createCullState()
     */
    public CullState createCullState() {
        return new JOGLCullState();
    }

    /**
     * <code>createFogState</code> returns a new JOGLFogState object as a
     * regular FogState.
     *
     * @return an FogState object.
     */
    public FogState createFogState() {
        return new JOGLFogState(capabilities);
    }

    /**
     * <code>createLightState</code> returns a new JOGLLightState object as a
     * regular LightState.
     *
     * @return an LightState object.
     */
    public LightState createLightState() {
        return new JOGLLightState(capabilities);
    }

    /**
     * <code>createMaterialState</code> returns a new JOGLMaterialState
     * object as a regular MaterialState.
     *
     * @return an MaterialState object.
     */
    public MaterialState createMaterialState() {
        return new JOGLMaterialState();
    }

    /**
     * <code>createShadeState</code> returns a new JOGLShadeState object as a
     * regular ShadeState.
     *
     * @return an ShadeState object.
     */
    public ShadeState createShadeState() {
        return new JOGLShadeState();
    }

    /**
     * <code>createTextureState</code> returns a new JOGLTextureState object
     * as a regular TextureState.
     *
     * @return an TextureState object.
     */
    public TextureState createTextureState() {
        return new JOGLTextureState(capabilities);
    }

    /**
     * <code>createWireframeState</code> returns a new JOGLWireframeState
     * object as a regular WireframeState.
     *
     * @return an WireframeState object.
     */
    public WireframeState createWireframeState() {
        return new JOGLWireframeState();
    }

    /**
     * <code>createZBufferState</code> returns a new JOGLZBufferState object
     * as a regular ZBufferState.
     *
     * @return a ZBufferState object.
     */
    public ZBufferState createZBufferState() {
        return new JOGLZBufferState();
    }

    /**
     * <code>createVertexProgramState</code> returns a new
     * JOGLVertexProgramState object as a regular VertexProgramState.
     *
     * @return a JOGLVertexProgramState object.
     */
    public VertexProgramState createVertexProgramState() {
        return new JOGLVertexProgramState(capabilities);
    }

    /**
     * <code>createFragmentProgramState</code> returns a new

⌨️ 快捷键说明

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