001: /*
002: * $RCSfile: JoglContext.java,v $
003: *
004: * Copyright 2006-2008 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
006: *
007: * This code is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License version 2 only, as
009: * published by the Free Software Foundation. Sun designates this
010: * particular file as subject to the "Classpath" exception as provided
011: * by Sun in the LICENSE file that accompanied this code.
012: *
013: * This code is distributed in the hope that it will be useful, but WITHOUT
014: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: * version 2 for more details (a copy is included in the LICENSE file that
017: * accompanied this code).
018: *
019: * You should have received a copy of the GNU General Public License version
020: * 2 along with this work; if not, write to the Free Software Foundation,
021: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
024: * CA 95054 USA or visit www.sun.com if you need additional information or
025: * have any questions.
026: *
027: * $Revision: 1.6 $
028: * $Date: 2008/02/28 20:17:18 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import java.nio.*;
035: import javax.media.opengl.*;
036: import com.sun.opengl.cg.*;
037:
038: /**
039: * Graphics context objects for Jogl rendering pipeline.
040: */
041: class JoglContext implements Context {
042: private GLContext context;
043:
044: // Properties we need to keep track of for efficiency
045: private int maxTexCoordSets;
046: private float alphaClearValue;
047: private int currentTextureUnit;
048: private int currentCombinerUnit;
049: private boolean hasMultisample;
050:
051: // Needed for vertex attribute implementation
052: private JoglShaderObject shaderProgram;
053:
054: // Implementation of vertex attribute methods
055: static interface VertexAttributeImpl {
056: public void vertexAttrPointer(GL gl, int index, int size,
057: int type, int stride, Buffer pointer);
058:
059: public void enableVertexAttrArray(GL gl, int index);
060:
061: public void disableVertexAttrArray(GL gl, int index);
062:
063: public void vertexAttr1fv(GL gl, int index, FloatBuffer buf);
064:
065: public void vertexAttr2fv(GL gl, int index, FloatBuffer buf);
066:
067: public void vertexAttr3fv(GL gl, int index, FloatBuffer buf);
068:
069: public void vertexAttr4fv(GL gl, int index, FloatBuffer buf);
070: }
071:
072: private VertexAttributeImpl vertexAttrImpl;
073:
074: class CgVertexAttributeImpl implements VertexAttributeImpl {
075: public void vertexAttrPointer(GL gl, int index, int size,
076: int type, int stride, Buffer pointer) {
077: JoglCgShaderProgramInfo shaderProgramInfo = (JoglCgShaderProgramInfo) shaderProgram;
078: if (shaderProgramInfo != null
079: && index < shaderProgramInfo
080: .getNumVertexAttributes()) {
081: CgGL.cgGLSetParameterPointer(shaderProgramInfo
082: .getVertexAttributes()[index], size, type,
083: stride, pointer);
084: } else {
085: if (shaderProgramInfo == null) {
086: System.err.println(" shaderProgramInfo is null");
087: } else {
088: System.err.println(" index ("
089: + index
090: + ") out of range: numVtxAttrs = "
091: + shaderProgramInfo
092: .getNumVertexAttributes());
093: }
094: }
095: }
096:
097: public void enableVertexAttrArray(GL gl, int index) {
098: JoglCgShaderProgramInfo shaderProgramInfo = (JoglCgShaderProgramInfo) shaderProgram;
099: if (shaderProgramInfo != null
100: && index < shaderProgramInfo
101: .getNumVertexAttributes()) {
102: CgGL.cgGLEnableClientState(shaderProgramInfo
103: .getVertexAttributes()[index]);
104: } else {
105: if (shaderProgramInfo == null) {
106: System.err.println(" shaderProgramInfo is null");
107: } else {
108: System.err.println(" index ("
109: + index
110: + ") out of range: numVtxAttrs = "
111: + shaderProgramInfo
112: .getNumVertexAttributes());
113: }
114: }
115: }
116:
117: public void disableVertexAttrArray(GL gl, int index) {
118: JoglCgShaderProgramInfo shaderProgramInfo = (JoglCgShaderProgramInfo) shaderProgram;
119: if (shaderProgramInfo != null
120: && index < shaderProgramInfo
121: .getNumVertexAttributes()) {
122: CgGL.cgGLDisableClientState(shaderProgramInfo
123: .getVertexAttributes()[index]);
124: } else {
125: if (shaderProgramInfo == null) {
126: System.err.println(" shaderProgramInfo is null");
127: } else {
128: System.err.println(" index ("
129: + index
130: + ") out of range: numVtxAttrs = "
131: + shaderProgramInfo
132: .getNumVertexAttributes());
133: }
134: }
135: }
136:
137: // NOTE: we should never get here. These functions are only called
138: // when building display lists for geometry arrays with vertex
139: // attributes, and such display lists are disabled in Cg mode.
140: public void vertexAttr1fv(GL gl, int index, FloatBuffer buf) {
141: throw new RuntimeException(
142: "Java 3D ERROR : Assertion failed: invalid call to cgVertexAttr1fv");
143: }
144:
145: public void vertexAttr2fv(GL gl, int index, FloatBuffer buf) {
146: throw new RuntimeException(
147: "Java 3D ERROR : Assertion failed: invalid call to cgVertexAttr2fv");
148: }
149:
150: public void vertexAttr3fv(GL gl, int index, FloatBuffer buf) {
151: throw new RuntimeException(
152: "Java 3D ERROR : Assertion failed: invalid call to cgVertexAttr3fv");
153: }
154:
155: public void vertexAttr4fv(GL gl, int index, FloatBuffer buf) {
156: throw new RuntimeException(
157: "Java 3D ERROR : Assertion failed: invalid call to cgVertexAttr4fv");
158: }
159: }
160:
161: class GLSLVertexAttributeImpl implements VertexAttributeImpl {
162: public void vertexAttrPointer(GL gl, int index, int size,
163: int type, int stride, Buffer pointer) {
164: gl.glVertexAttribPointerARB(index + glslVertexAttrOffset,
165: size, type, false, stride, pointer);
166: }
167:
168: public void enableVertexAttrArray(GL gl, int index) {
169: gl.glEnableVertexAttribArrayARB(index
170: + glslVertexAttrOffset);
171: }
172:
173: public void disableVertexAttrArray(GL gl, int index) {
174: gl.glDisableVertexAttribArrayARB(index
175: + glslVertexAttrOffset);
176: }
177:
178: public void vertexAttr1fv(GL gl, int index, FloatBuffer buf) {
179: gl.glVertexAttrib1fvARB(index + glslVertexAttrOffset, buf);
180: }
181:
182: public void vertexAttr2fv(GL gl, int index, FloatBuffer buf) {
183: gl.glVertexAttrib2fvARB(index + glslVertexAttrOffset, buf);
184: }
185:
186: public void vertexAttr3fv(GL gl, int index, FloatBuffer buf) {
187: gl.glVertexAttrib3fvARB(index + glslVertexAttrOffset, buf);
188: }
189:
190: public void vertexAttr4fv(GL gl, int index, FloatBuffer buf) {
191: gl.glVertexAttrib4fvARB(index + glslVertexAttrOffset, buf);
192: }
193: }
194:
195: // Only used when GLSL shader library is active
196: private int glslVertexAttrOffset;
197:
198: // Only used when Cg shader library is active
199: private CGcontext cgContext;
200: private int cgVertexProfile;
201: private int cgFragmentProfile;
202:
203: JoglContext(GLContext context) {
204: this .context = context;
205: }
206:
207: GLContext getGLContext() {
208: return context;
209: }
210:
211: int getMaxTexCoordSets() {
212: return maxTexCoordSets;
213: }
214:
215: void setMaxTexCoordSets(int val) {
216: maxTexCoordSets = val;
217: }
218:
219: float getAlphaClearValue() {
220: return alphaClearValue;
221: }
222:
223: void setAlphaClearValue(float val) {
224: alphaClearValue = val;
225: }
226:
227: int getCurrentTextureUnit() {
228: return currentTextureUnit;
229: }
230:
231: void setCurrentTextureUnit(int val) {
232: currentTextureUnit = val;
233: }
234:
235: int getCurrentCombinerUnit() {
236: return currentCombinerUnit;
237: }
238:
239: void setCurrentCombinerUnit(int val) {
240: currentCombinerUnit = val;
241: }
242:
243: boolean getHasMultisample() {
244: return hasMultisample;
245: }
246:
247: void setHasMultisample(boolean val) {
248: hasMultisample = val;
249: }
250:
251: // Helpers for vertex attribute methods
252: void initCgVertexAttributeImpl() {
253: if (vertexAttrImpl != null) {
254: throw new RuntimeException(
255: "Should not initialize the vertex attribute implementation twice");
256: }
257: vertexAttrImpl = new CgVertexAttributeImpl();
258: }
259:
260: void initGLSLVertexAttributeImpl() {
261: if (vertexAttrImpl != null) {
262: throw new RuntimeException(
263: "Should not initialize the vertex attribute implementation twice");
264: }
265: vertexAttrImpl = new GLSLVertexAttributeImpl();
266: }
267:
268: void vertexAttrPointer(GL gl, int index, int size, int type,
269: int stride, Buffer pointer) {
270: vertexAttrImpl.vertexAttrPointer(gl, index, size, type, stride,
271: pointer);
272: }
273:
274: void enableVertexAttrArray(GL gl, int index) {
275: vertexAttrImpl.enableVertexAttrArray(gl, index);
276: }
277:
278: void disableVertexAttrArray(GL gl, int index) {
279: vertexAttrImpl.disableVertexAttrArray(gl, index);
280: }
281:
282: void vertexAttr1fv(GL gl, int index, FloatBuffer buf) {
283: vertexAttrImpl.vertexAttr1fv(gl, index, buf);
284: }
285:
286: void vertexAttr2fv(GL gl, int index, FloatBuffer buf) {
287: vertexAttrImpl.vertexAttr2fv(gl, index, buf);
288: }
289:
290: void vertexAttr3fv(GL gl, int index, FloatBuffer buf) {
291: vertexAttrImpl.vertexAttr3fv(gl, index, buf);
292: }
293:
294: void vertexAttr4fv(GL gl, int index, FloatBuffer buf) {
295: vertexAttrImpl.vertexAttr4fv(gl, index, buf);
296: }
297:
298: // Used in vertex attribute implementation
299: JoglShaderObject getShaderProgram() {
300: return shaderProgram;
301: }
302:
303: void setShaderProgram(JoglShaderObject object) {
304: shaderProgram = object;
305: }
306:
307: // Only used when GLSL shaders are in use
308: int getGLSLVertexAttrOffset() {
309: return glslVertexAttrOffset;
310: }
311:
312: void setGLSLVertexAttrOffset(int offset) {
313: glslVertexAttrOffset = offset;
314: }
315:
316: // Only used when Cg shaders are in use
317: CGcontext getCgContext() {
318: return cgContext;
319: }
320:
321: void setCgContext(CGcontext c) {
322: cgContext = c;
323: }
324:
325: int getCgVertexProfile() {
326: return cgVertexProfile;
327: }
328:
329: void setCgVertexProfile(int p) {
330: cgVertexProfile = p;
331: }
332:
333: int getCgFragmentProfile() {
334: return cgFragmentProfile;
335: }
336:
337: void setCgFragmentProfile(int p) {
338: cgFragmentProfile = p;
339: }
340: }
|