aboutsummaryrefslogtreecommitdiff
path: root/engine/src/core/com/jme3/renderer/RenderContext.java
blob: ee9ff9c0f1beb92a025e20b7241c7a9098f80a55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright (c) 2009-2010 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.jme3.renderer;

import com.jme3.material.RenderState;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Mesh;
import com.jme3.scene.VertexBuffer;
import com.jme3.texture.FrameBuffer;
import com.jme3.texture.Image;

/**
 * Represents the current state of the graphics library. This class is used
 * internally to reduce state changes. NOTE: This class is specific to OpenGL.
 */
public class RenderContext {

    /**
     * @see RenderState#setFaceCullMode(com.jme3.material.RenderState.FaceCullMode)
     */
    public RenderState.FaceCullMode cullMode = RenderState.FaceCullMode.Off;

    /**
     * @see RenderState#setDepthTest(boolean) 
     */
    public boolean depthTestEnabled = false;

    /**
     * @see RenderState#setAlphaTest(boolean) 
     */
    public boolean alphaTestEnabled = false;

    /**
     * @see RenderState#setDepthWrite(boolean) 
     */
    public boolean depthWriteEnabled = true;

    /**
     * @see RenderState#setColorWrite(boolean) 
     */
    public boolean colorWriteEnabled = true;

    /**
     * @see Renderer#setClipRect(int, int, int, int) 
     */
    public boolean clipRectEnabled = false;

    /**
     * @see RenderState#setPolyOffset(float, float) 
     */
    public boolean polyOffsetEnabled = false;
    
    /**
     * @see RenderState#setPolyOffset(float, float) 
     */
    public float polyOffsetFactor = 0;
    
    /**
     * @see RenderState#setPolyOffset(float, float) 
     */
    public float polyOffsetUnits = 0;

    /**
     * For normals only. Uses GL_NORMALIZE.
     * 
     * @see VertexBuffer#setNormalized(boolean) 
     */
    public boolean normalizeEnabled = false;

    /**
     * For glMatrixMode.
     * 
     * @see Renderer#setWorldMatrix(com.jme3.math.Matrix4f) 
     * @see Renderer#setViewProjectionMatrices(com.jme3.math.Matrix4f, com.jme3.math.Matrix4f) 
     */
    public int matrixMode = -1;

    /**
     * @see Mesh#setPointSize(float) 
     */
    public float pointSize = 1;
    
    /**
     * @see Mesh#setLineWidth(float) 
     */
    public float lineWidth = 1;

    /**
     * @see RenderState#setBlendMode(com.jme3.material.RenderState.BlendMode) 
     */
    public RenderState.BlendMode blendMode = RenderState.BlendMode.Off;

    /**
     * @see RenderState#setWireframe(boolean) 
     */
    public boolean wireframe = false;

    /**
     * @see RenderState#setPointSprite(boolean) 
     */
    public boolean pointSprite = false;

    /**
     * @see Renderer#setShader(com.jme3.shader.Shader) 
     */
    public int boundShaderProgram;

    /**
     * @see Renderer#setFrameBuffer(com.jme3.texture.FrameBuffer) 
     */
    public int boundFBO = 0;

    /**
     * Currently bound Renderbuffer
     * 
     * @see Renderer#setFrameBuffer(com.jme3.texture.FrameBuffer) 
     */
    public int boundRB = 0;

    /**
     * Currently bound draw buffer
     * -2 = GL_NONE
     * -1 = GL_BACK
     *  0 = GL_COLOR_ATTACHMENT0
     *  n = GL_COLOR_ATTACHMENTn
     *  where n is an integer greater than 1
     * 
     * @see Renderer#setFrameBuffer(com.jme3.texture.FrameBuffer) 
     * @see FrameBuffer#setTargetIndex(int) 
     */
    public int boundDrawBuf = -1;

    /**
     * Currently bound read buffer
     *
     * @see RenderContext#boundDrawBuf
     * @see Renderer#setFrameBuffer(com.jme3.texture.FrameBuffer) 
     * @see FrameBuffer#setTargetIndex(int) 
     */
    public int boundReadBuf = -1;

    /**
     * Currently bound element array vertex buffer.
     * 
     * @see Renderer#renderMesh(com.jme3.scene.Mesh, int, int) 
     */
    public int boundElementArrayVBO;

    /**
     * @see Renderer#renderMesh(com.jme3.scene.Mesh, int, int) 
     */
    public int boundVertexArray;

    /**
     * Currently bound array vertex buffer.
     * 
     * @see Renderer#renderMesh(com.jme3.scene.Mesh, int, int) 
     */
    public int boundArrayVBO;

    public int numTexturesSet = 0;

    /**
     * Current bound texture IDs for each texture unit.
     * 
     * @see Renderer#setTexture(int, com.jme3.texture.Texture) 
     */
    public Image[] boundTextures = new Image[16];

    /**
     * IDList for texture units
     * 
     * @see Renderer#setTexture(int, com.jme3.texture.Texture) 
     */
    public IDList textureIndexList = new IDList();

    /**
     * Currently bound texture unit
     * 
     * @see Renderer#setTexture(int, com.jme3.texture.Texture) 
     */
    public int boundTextureUnit = 0;

    /**
     * Stencil Buffer state
     */
    public boolean stencilTest = false;
    public RenderState.StencilOperation frontStencilStencilFailOperation = RenderState.StencilOperation.Keep;
    public RenderState.StencilOperation frontStencilDepthFailOperation = RenderState.StencilOperation.Keep;
    public RenderState.StencilOperation frontStencilDepthPassOperation = RenderState.StencilOperation.Keep;
    public RenderState.StencilOperation backStencilStencilFailOperation = RenderState.StencilOperation.Keep;
    public RenderState.StencilOperation backStencilDepthFailOperation = RenderState.StencilOperation.Keep;
    public RenderState.StencilOperation backStencilDepthPassOperation = RenderState.StencilOperation.Keep;
    public RenderState.TestFunction frontStencilFunction = RenderState.TestFunction.Always;
    public RenderState.TestFunction backStencilFunction = RenderState.TestFunction.Always;

    /**
     * Vertex attribs currently bound and enabled. If a slot is null, then
     * it is disabled.
     */
    public VertexBuffer[] boundAttribs = new VertexBuffer[16];

    /**
     * IDList for vertex attributes
     */
    public IDList attribIndexList = new IDList();
    
    /**
     * Ambient color (GL1 only)
     */
    public ColorRGBA ambient;
    
    /**
     * Diffuse color (GL1 only)
     */
    public ColorRGBA diffuse;
    
    /**
     * Specular color (GL1 only)
     */
    public ColorRGBA specular;
    
    /**
     * Material color (GL1 only)
     */
    public ColorRGBA color;
    
    /**
     * Shininess (GL1 only)
     */
    public float shininess;
    
    /**
     * Use vertex color (GL1 only)
     */
    public boolean useVertexColor;

    /**
     * Reset the RenderContext to default GL state
     */
    public void reset(){
        cullMode = RenderState.FaceCullMode.Off;
        depthTestEnabled = false;
        alphaTestEnabled = false;
        depthWriteEnabled = false;
        colorWriteEnabled = false;
        clipRectEnabled = false;
        polyOffsetEnabled = false;
        polyOffsetFactor = 0;
        polyOffsetUnits = 0;
        normalizeEnabled = false;
        matrixMode = -1;
        pointSize = 1;
        blendMode = RenderState.BlendMode.Off;
        wireframe = false;
        boundShaderProgram = 0;
        boundFBO = 0;
        boundRB = 0;
        boundDrawBuf = -1; 
        boundReadBuf = -1;
        boundElementArrayVBO = 0;
        boundVertexArray = 0;
        boundArrayVBO = 0;
        numTexturesSet = 0;
        for (int i = 0; i < boundTextures.length; i++)
            boundTextures[i] = null;

        textureIndexList.reset();
        boundTextureUnit = 0;
        for (int i = 0; i < boundAttribs.length; i++)
            boundAttribs[i] = null;

        attribIndexList.reset();
        
        stencilTest = false;
        frontStencilStencilFailOperation = RenderState.StencilOperation.Keep;
        frontStencilDepthFailOperation = RenderState.StencilOperation.Keep;
        frontStencilDepthPassOperation = RenderState.StencilOperation.Keep;
        backStencilStencilFailOperation = RenderState.StencilOperation.Keep;
        backStencilDepthFailOperation = RenderState.StencilOperation.Keep;
        backStencilDepthPassOperation = RenderState.StencilOperation.Keep;
        frontStencilFunction = RenderState.TestFunction.Always;
        backStencilFunction = RenderState.TestFunction.Always;
        
        ambient = diffuse = specular = color = null;
        shininess = 0;
        useVertexColor = false;
    }
}