Home | History | Annotate | Download | only in renderer
      1 /*
      2  * Copyright (c) 2009-2010 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 package com.jme3.renderer;
     34 
     35 import com.jme3.material.RenderState;
     36 import com.jme3.math.ColorRGBA;
     37 import com.jme3.scene.Mesh;
     38 import com.jme3.scene.VertexBuffer;
     39 import com.jme3.texture.FrameBuffer;
     40 import com.jme3.texture.Image;
     41 
     42 /**
     43  * Represents the current state of the graphics library. This class is used
     44  * internally to reduce state changes. NOTE: This class is specific to OpenGL.
     45  */
     46 public class RenderContext {
     47 
     48     /**
     49      * @see RenderState#setFaceCullMode(com.jme3.material.RenderState.FaceCullMode)
     50      */
     51     public RenderState.FaceCullMode cullMode = RenderState.FaceCullMode.Off;
     52 
     53     /**
     54      * @see RenderState#setDepthTest(boolean)
     55      */
     56     public boolean depthTestEnabled = false;
     57 
     58     /**
     59      * @see RenderState#setAlphaTest(boolean)
     60      */
     61     public boolean alphaTestEnabled = false;
     62 
     63     /**
     64      * @see RenderState#setDepthWrite(boolean)
     65      */
     66     public boolean depthWriteEnabled = true;
     67 
     68     /**
     69      * @see RenderState#setColorWrite(boolean)
     70      */
     71     public boolean colorWriteEnabled = true;
     72 
     73     /**
     74      * @see Renderer#setClipRect(int, int, int, int)
     75      */
     76     public boolean clipRectEnabled = false;
     77 
     78     /**
     79      * @see RenderState#setPolyOffset(float, float)
     80      */
     81     public boolean polyOffsetEnabled = false;
     82 
     83     /**
     84      * @see RenderState#setPolyOffset(float, float)
     85      */
     86     public float polyOffsetFactor = 0;
     87 
     88     /**
     89      * @see RenderState#setPolyOffset(float, float)
     90      */
     91     public float polyOffsetUnits = 0;
     92 
     93     /**
     94      * For normals only. Uses GL_NORMALIZE.
     95      *
     96      * @see VertexBuffer#setNormalized(boolean)
     97      */
     98     public boolean normalizeEnabled = false;
     99 
    100     /**
    101      * For glMatrixMode.
    102      *
    103      * @see Renderer#setWorldMatrix(com.jme3.math.Matrix4f)
    104      * @see Renderer#setViewProjectionMatrices(com.jme3.math.Matrix4f, com.jme3.math.Matrix4f)
    105      */
    106     public int matrixMode = -1;
    107 
    108     /**
    109      * @see Mesh#setPointSize(float)
    110      */
    111     public float pointSize = 1;
    112 
    113     /**
    114      * @see Mesh#setLineWidth(float)
    115      */
    116     public float lineWidth = 1;
    117 
    118     /**
    119      * @see RenderState#setBlendMode(com.jme3.material.RenderState.BlendMode)
    120      */
    121     public RenderState.BlendMode blendMode = RenderState.BlendMode.Off;
    122 
    123     /**
    124      * @see RenderState#setWireframe(boolean)
    125      */
    126     public boolean wireframe = false;
    127 
    128     /**
    129      * @see RenderState#setPointSprite(boolean)
    130      */
    131     public boolean pointSprite = false;
    132 
    133     /**
    134      * @see Renderer#setShader(com.jme3.shader.Shader)
    135      */
    136     public int boundShaderProgram;
    137 
    138     /**
    139      * @see Renderer#setFrameBuffer(com.jme3.texture.FrameBuffer)
    140      */
    141     public int boundFBO = 0;
    142 
    143     /**
    144      * Currently bound Renderbuffer
    145      *
    146      * @see Renderer#setFrameBuffer(com.jme3.texture.FrameBuffer)
    147      */
    148     public int boundRB = 0;
    149 
    150     /**
    151      * Currently bound draw buffer
    152      * -2 = GL_NONE
    153      * -1 = GL_BACK
    154      *  0 = GL_COLOR_ATTACHMENT0
    155      *  n = GL_COLOR_ATTACHMENTn
    156      *  where n is an integer greater than 1
    157      *
    158      * @see Renderer#setFrameBuffer(com.jme3.texture.FrameBuffer)
    159      * @see FrameBuffer#setTargetIndex(int)
    160      */
    161     public int boundDrawBuf = -1;
    162 
    163     /**
    164      * Currently bound read buffer
    165      *
    166      * @see RenderContext#boundDrawBuf
    167      * @see Renderer#setFrameBuffer(com.jme3.texture.FrameBuffer)
    168      * @see FrameBuffer#setTargetIndex(int)
    169      */
    170     public int boundReadBuf = -1;
    171 
    172     /**
    173      * Currently bound element array vertex buffer.
    174      *
    175      * @see Renderer#renderMesh(com.jme3.scene.Mesh, int, int)
    176      */
    177     public int boundElementArrayVBO;
    178 
    179     /**
    180      * @see Renderer#renderMesh(com.jme3.scene.Mesh, int, int)
    181      */
    182     public int boundVertexArray;
    183 
    184     /**
    185      * Currently bound array vertex buffer.
    186      *
    187      * @see Renderer#renderMesh(com.jme3.scene.Mesh, int, int)
    188      */
    189     public int boundArrayVBO;
    190 
    191     public int numTexturesSet = 0;
    192 
    193     /**
    194      * Current bound texture IDs for each texture unit.
    195      *
    196      * @see Renderer#setTexture(int, com.jme3.texture.Texture)
    197      */
    198     public Image[] boundTextures = new Image[16];
    199 
    200     /**
    201      * IDList for texture units
    202      *
    203      * @see Renderer#setTexture(int, com.jme3.texture.Texture)
    204      */
    205     public IDList textureIndexList = new IDList();
    206 
    207     /**
    208      * Currently bound texture unit
    209      *
    210      * @see Renderer#setTexture(int, com.jme3.texture.Texture)
    211      */
    212     public int boundTextureUnit = 0;
    213 
    214     /**
    215      * Stencil Buffer state
    216      */
    217     public boolean stencilTest = false;
    218     public RenderState.StencilOperation frontStencilStencilFailOperation = RenderState.StencilOperation.Keep;
    219     public RenderState.StencilOperation frontStencilDepthFailOperation = RenderState.StencilOperation.Keep;
    220     public RenderState.StencilOperation frontStencilDepthPassOperation = RenderState.StencilOperation.Keep;
    221     public RenderState.StencilOperation backStencilStencilFailOperation = RenderState.StencilOperation.Keep;
    222     public RenderState.StencilOperation backStencilDepthFailOperation = RenderState.StencilOperation.Keep;
    223     public RenderState.StencilOperation backStencilDepthPassOperation = RenderState.StencilOperation.Keep;
    224     public RenderState.TestFunction frontStencilFunction = RenderState.TestFunction.Always;
    225     public RenderState.TestFunction backStencilFunction = RenderState.TestFunction.Always;
    226 
    227     /**
    228      * Vertex attribs currently bound and enabled. If a slot is null, then
    229      * it is disabled.
    230      */
    231     public VertexBuffer[] boundAttribs = new VertexBuffer[16];
    232 
    233     /**
    234      * IDList for vertex attributes
    235      */
    236     public IDList attribIndexList = new IDList();
    237 
    238     /**
    239      * Ambient color (GL1 only)
    240      */
    241     public ColorRGBA ambient;
    242 
    243     /**
    244      * Diffuse color (GL1 only)
    245      */
    246     public ColorRGBA diffuse;
    247 
    248     /**
    249      * Specular color (GL1 only)
    250      */
    251     public ColorRGBA specular;
    252 
    253     /**
    254      * Material color (GL1 only)
    255      */
    256     public ColorRGBA color;
    257 
    258     /**
    259      * Shininess (GL1 only)
    260      */
    261     public float shininess;
    262 
    263     /**
    264      * Use vertex color (GL1 only)
    265      */
    266     public boolean useVertexColor;
    267 
    268     /**
    269      * Reset the RenderContext to default GL state
    270      */
    271     public void reset(){
    272         cullMode = RenderState.FaceCullMode.Off;
    273         depthTestEnabled = false;
    274         alphaTestEnabled = false;
    275         depthWriteEnabled = false;
    276         colorWriteEnabled = false;
    277         clipRectEnabled = false;
    278         polyOffsetEnabled = false;
    279         polyOffsetFactor = 0;
    280         polyOffsetUnits = 0;
    281         normalizeEnabled = false;
    282         matrixMode = -1;
    283         pointSize = 1;
    284         blendMode = RenderState.BlendMode.Off;
    285         wireframe = false;
    286         boundShaderProgram = 0;
    287         boundFBO = 0;
    288         boundRB = 0;
    289         boundDrawBuf = -1;
    290         boundReadBuf = -1;
    291         boundElementArrayVBO = 0;
    292         boundVertexArray = 0;
    293         boundArrayVBO = 0;
    294         numTexturesSet = 0;
    295         for (int i = 0; i < boundTextures.length; i++)
    296             boundTextures[i] = null;
    297 
    298         textureIndexList.reset();
    299         boundTextureUnit = 0;
    300         for (int i = 0; i < boundAttribs.length; i++)
    301             boundAttribs[i] = null;
    302 
    303         attribIndexList.reset();
    304 
    305         stencilTest = false;
    306         frontStencilStencilFailOperation = RenderState.StencilOperation.Keep;
    307         frontStencilDepthFailOperation = RenderState.StencilOperation.Keep;
    308         frontStencilDepthPassOperation = RenderState.StencilOperation.Keep;
    309         backStencilStencilFailOperation = RenderState.StencilOperation.Keep;
    310         backStencilDepthFailOperation = RenderState.StencilOperation.Keep;
    311         backStencilDepthPassOperation = RenderState.StencilOperation.Keep;
    312         frontStencilFunction = RenderState.TestFunction.Always;
    313         backStencilFunction = RenderState.TestFunction.Always;
    314 
    315         ambient = diffuse = specular = color = null;
    316         shininess = 0;
    317         useVertexColor = false;
    318     }
    319 }
    320