/** * Initialize this ProjectedDepthTexture with the renderer * * @param r */ public void init(final Renderer r) { // already initialized ? if (textureRenderer != null) { return; } textureRenderer = ImageUtil.createTextureRenderer(0, 0, r, true); if (isParallel) { textureRenderer.getCamera().setProjectionMode(Camera.ProjectionMode.Parallel); } // Enforce performance enhancing states on the renderer. // No textures or colors are required since we're only // interested in recording depth. // Also only need front faces when rendering the shadow maps // turn off clipping ClipState noClip = new ClipState(); noClip.setEnabled(false); textureRenderer.enforceState(noClip); // turn off texturing TextureState noTexture = new TextureState(); noTexture.setEnabled(false); textureRenderer.enforceState(noTexture); // turn off colors ColorMaskState colorDisabled = new ColorMaskState(); colorDisabled.setAll(false); textureRenderer.enforceState(colorDisabled); // cull back faces CullState cullFace = new CullState(); cullFace.setEnabled(true); cullFace.setCullFace(CullState.Face.Back); textureRenderer.enforceState(cullFace); // turn off lights LightState noLights = new LightState(); noLights.setEnabled(false); textureRenderer.enforceState(noLights); // use flat shading ShadingState flat = new ShadingState(); flat.setShadingMode(ShadingMode.Flat); textureRenderer.enforceState(flat); // disable GLSLShaderObjectsState GLSLShaderObjectsState glsl = new GLSLShaderObjectsState(); glsl.setEnabled(false); textureRenderer.enforceState(glsl); // enforce the shadow offset parameters textureRenderer.enforceState(offsetState); if (texture == null) { createTexture(); } textureRenderer.setupTexture(texture); }
public static void apply(final JoglRenderer renderer, final GLSLShaderObjectsState state) { final GL gl = GLU.getCurrentGL(); final RenderContext context = ContextManager.getCurrentContext(); final ContextCapabilities caps = context.getCapabilities(); if (caps.isGLSLSupported()) { // Ask for the current state record final ShaderObjectsStateRecord record = (ShaderObjectsStateRecord) context.getStateRecord(StateType.GLSLShader); context.setCurrentState(StateType.GLSLShader, state); if (state.isEnabled()) { if (state._needSendShader) { sendToGL(state); } if (state._shaderDataLogic != null) { state._shaderDataLogic.applyData(state, state._mesh, renderer); } } if (!record.isValid() || record.getReference() != state || state.needsRefresh()) { record.setReference(state); if (state.isEnabled()) { if (state._programID != -1) { gl.glUseProgramObjectARB(state._programID); final List<ShaderVariable> attribs = state.getShaderAttributes(); for (int i = attribs.size(); --i >= 0; ) { final ShaderVariable shaderVariable = attribs.get(i); if (shaderVariable.needsRefresh) { JoglShaderUtil.updateAttributeLocation(shaderVariable, state._programID); shaderVariable.needsRefresh = false; } JoglShaderUtil.updateShaderAttribute(shaderVariable); } final List<ShaderVariable> uniforms = state.getShaderUniforms(); for (int i = uniforms.size(); --i >= 0; ) { final ShaderVariable shaderVariable = uniforms.get(i); if (shaderVariable.needsRefresh) { JoglShaderUtil.updateUniformLocation(shaderVariable, state._programID); JoglShaderUtil.updateShaderUniform(shaderVariable); shaderVariable.needsRefresh = false; } } } } else { gl.glUseProgramObjectARB(0); } } if (!record.isValid()) { record.validate(); } } }
protected static void sendToGL( final GLSLShaderObjectsState state, final ContextCapabilities caps) { final GL gl = GLContext.getCurrentGL(); if (state.getVertexShader() == null && state.getFragmentShader() == null) { logger.warning("Could not find shader resources!" + "(both inputbuffers are null)"); state._needSendShader = false; return; } if (state._programID == -1) { if (gl.isGL2()) { state._programID = gl.getGL2().glCreateProgramObjectARB(); } else { if (gl.isGL2ES2()) { state._programID = gl.getGL2ES2().glCreateProgram(); } } } if (state.getVertexShader() != null) { if (state._vertexShaderID != -1) { removeVertShader(state); } if (gl.isGL2()) { state._vertexShaderID = gl.getGL2().glCreateShaderObjectARB(GL2ES2.GL_VERTEX_SHADER); } else { if (gl.isGL2ES2()) { state._vertexShaderID = gl.getGL2ES2().glCreateShader(GL2ES2.GL_VERTEX_SHADER); } } // Create the sources final byte array[] = new byte[state.getVertexShader().limit()]; state.getVertexShader().rewind(); state.getVertexShader().get(array); if (gl.isGL2()) { gl.getGL2() .glShaderSourceARB( state._vertexShaderID, 1, new String[] {new String(array)}, new int[] {array.length}, 0); } else { if (gl.isGL2ES2()) { gl.getGL2ES2() .glShaderSource( state._vertexShaderID, 1, new String[] {new String(array)}, new int[] {array.length}, 0); } } // Compile the vertex shader final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext(); final IntBuffer compiled = context.getDirectNioBuffersSet().getSingleIntBuffer(); compiled.clear(); if (gl.isGL2()) { gl.getGL2().glCompileShaderARB(state._vertexShaderID); gl.getGL2() .glGetObjectParameterivARB( state._vertexShaderID, GL2.GL_OBJECT_COMPILE_STATUS_ARB, compiled); } else { if (gl.isGL2ES2()) { gl.getGL2ES2().glCompileShader(state._vertexShaderID); gl.getGL2ES2().glGetShaderiv(state._vertexShaderID, GL2ES2.GL_COMPILE_STATUS, compiled); } } checkProgramError(compiled.get(0), state._vertexShaderID, state._vertexShaderName); // Attach the program if (gl.isGL2()) { gl.getGL2().glAttachObjectARB(state._programID, state._vertexShaderID); } else { if (gl.isGL2ES2()) { gl.getGL2ES2().glAttachShader(state._programID, state._vertexShaderID); } } } else if (state._vertexShaderID != -1) { removeVertShader(state); state._vertexShaderID = -1; } if (state.getFragmentShader() != null) { if (state._fragmentShaderID != -1) { removeFragShader(state); } if (gl.isGL2()) { state._fragmentShaderID = gl.getGL2().glCreateShaderObjectARB(GL2ES2.GL_FRAGMENT_SHADER); } else { if (gl.isGL2ES2()) { state._fragmentShaderID = gl.getGL2ES2().glCreateShader(GL2ES2.GL_FRAGMENT_SHADER); } } // Create the sources final byte array[] = new byte[state.getFragmentShader().limit()]; state.getFragmentShader().rewind(); state.getFragmentShader().get(array); if (gl.isGL2()) { gl.getGL2() .glShaderSourceARB( state._fragmentShaderID, 1, new String[] {new String(array)}, new int[] {array.length}, 0); } else { if (gl.isGL2ES2()) { gl.getGL2ES2() .glShaderSource( state._fragmentShaderID, 1, new String[] {new String(array)}, new int[] {array.length}, 0); } } // Compile the fragment shader final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext(); final IntBuffer compiled = context.getDirectNioBuffersSet().getSingleIntBuffer(); compiled.clear(); if (gl.isGL2()) { gl.getGL2().glCompileShaderARB(state._fragmentShaderID); gl.getGL2() .glGetObjectParameterivARB( state._fragmentShaderID, GL2.GL_OBJECT_COMPILE_STATUS_ARB, compiled); } else { if (gl.isGL2ES2()) { gl.getGL2ES2().glCompileShader(state._fragmentShaderID); gl.getGL2ES2().glGetShaderiv(state._fragmentShaderID, GL2ES2.GL_COMPILE_STATUS, compiled); } } checkProgramError(compiled.get(0), state._fragmentShaderID, state._vertexShaderName); // Attach the program if (gl.isGL2()) { gl.getGL2().glAttachObjectARB(state._programID, state._fragmentShaderID); } else { if (gl.isGL2ES2()) { gl.getGL2ES2().glAttachShader(state._programID, state._fragmentShaderID); } } } else if (state._fragmentShaderID != -1) { removeFragShader(state); state._fragmentShaderID = -1; } if (caps.isGeometryShader4Supported()) { if (state.getGeometryShader() != null) { if (state._geometryShaderID != -1) { removeGeomShader(state); } if (gl.isGL2()) { state._geometryShaderID = gl.getGL2().glCreateShaderObjectARB(GL3.GL_GEOMETRY_SHADER); } else { if (gl.isGL2ES2()) { state._geometryShaderID = gl.getGL2ES2().glCreateShader(GL3.GL_GEOMETRY_SHADER); } } // Create the sources final byte array[] = new byte[state.getGeometryShader().limit()]; state.getGeometryShader().rewind(); state.getGeometryShader().get(array); if (gl.isGL2()) { gl.getGL2() .glShaderSourceARB( state._geometryShaderID, 1, new String[] {new String(array)}, new int[] {array.length}, 0); } else { if (gl.isGL2ES2()) { gl.getGL2ES2() .glShaderSource( state._geometryShaderID, 1, new String[] {new String(array)}, new int[] {array.length}, 0); } } // Compile the geometry shader final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext(); final IntBuffer compiled = context.getDirectNioBuffersSet().getSingleIntBuffer(); compiled.clear(); if (gl.isGL2()) { gl.getGL2().glCompileShaderARB(state._geometryShaderID); gl.getGL2() .glGetObjectParameterivARB( state._geometryShaderID, GL2.GL_OBJECT_COMPILE_STATUS_ARB, compiled); } else { if (gl.isGL2ES2()) { gl.getGL2ES2().glCompileShader(state._geometryShaderID); gl.getGL2ES2() .glGetShaderiv(state._geometryShaderID, GL2ES2.GL_COMPILE_STATUS, compiled); } } checkProgramError(compiled.get(0), state._geometryShaderID, state._geometryShaderName); // Attach the program if (gl.isGL2()) { gl.getGL2().glAttachObjectARB(state._programID, state._geometryShaderID); } else { if (gl.isGL2ES2()) { gl.getGL2ES2().glAttachShader(state._programID, state._geometryShaderID); } } } else if (state._geometryShaderID != -1) { removeGeomShader(state); state._geometryShaderID = -1; } } if (caps.isTessellationShadersSupported()) { if (state.getTessellationControlShader() != null) { if (state._tessellationControlShaderID != -1) { removeTessControlShader(state); } if (gl.isGL2()) { state._tessellationControlShaderID = gl.getGL2().glCreateShaderObjectARB(GL4.GL_TESS_CONTROL_SHADER); } else { if (gl.isGL2ES2()) { state._tessellationControlShaderID = gl.getGL2ES2().glCreateShader(GL4.GL_TESS_CONTROL_SHADER); } } // Create the sources final byte array[] = new byte[state.getTessellationControlShader().limit()]; state.getTessellationControlShader().rewind(); state.getTessellationControlShader().get(array); if (gl.isGL2()) { gl.getGL2() .glShaderSourceARB( state._tessellationControlShaderID, 1, new String[] {new String(array)}, new int[] {array.length}, 0); } else { if (gl.isGL2ES2()) { gl.getGL2ES2() .glShaderSource( state._tessellationControlShaderID, 1, new String[] {new String(array)}, new int[] {array.length}, 0); } } // Compile the tessellation control shader final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext(); final IntBuffer compiled = context.getDirectNioBuffersSet().getSingleIntBuffer(); compiled.clear(); if (gl.isGL2()) { gl.getGL2().glCompileShaderARB(state._tessellationControlShaderID); gl.getGL2() .glGetObjectParameterivARB( state._tessellationControlShaderID, GL2.GL_OBJECT_COMPILE_STATUS_ARB, compiled); } else { if (gl.isGL2ES2()) { gl.getGL2ES2().glCompileShader(state._tessellationControlShaderID); gl.getGL2ES2() .glGetShaderiv( state._tessellationControlShaderID, GL2ES2.GL_COMPILE_STATUS, compiled); } } checkProgramError( compiled.get(0), state._tessellationControlShaderID, state._tessellationControlShaderName); // Attach the program if (gl.isGL2()) { gl.getGL2().glAttachObjectARB(state._programID, state._tessellationControlShaderID); } else { if (gl.isGL2ES2()) { gl.getGL2ES2().glAttachShader(state._programID, state._tessellationControlShaderID); } } } else if (state._tessellationControlShaderID != -1) { removeTessControlShader(state); state._tessellationControlShaderID = -1; } if (state.getTessellationEvaluationShader() != null) { if (state._tessellationEvaluationShaderID != -1) { removeTessEvalShader(state); } if (gl.isGL2()) { state._tessellationEvaluationShaderID = gl.getGL2().glCreateShaderObjectARB(GL4.GL_TESS_CONTROL_SHADER); } else { if (gl.isGL2ES2()) { state._tessellationEvaluationShaderID = gl.getGL2ES2().glCreateShader(GL4.GL_TESS_CONTROL_SHADER); } } // Create the sources final byte array[] = new byte[state.getTessellationEvaluationShader().limit()]; state.getTessellationEvaluationShader().rewind(); state.getTessellationEvaluationShader().get(array); if (gl.isGL2()) { gl.getGL2() .glShaderSourceARB( state._tessellationEvaluationShaderID, 1, new String[] {new String(array)}, new int[] {array.length}, 0); } else { if (gl.isGL2ES2()) { gl.getGL2ES2() .glShaderSource( state._tessellationEvaluationShaderID, 1, new String[] {new String(array)}, new int[] {array.length}, 0); } } // Compile the tessellation control shader final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext(); final IntBuffer compiled = context.getDirectNioBuffersSet().getSingleIntBuffer(); compiled.clear(); if (gl.isGL2()) { gl.getGL2().glCompileShaderARB(state._tessellationEvaluationShaderID); gl.getGL2() .glGetObjectParameterivARB( state._tessellationEvaluationShaderID, GL2.GL_OBJECT_COMPILE_STATUS_ARB, compiled); } else { if (gl.isGL2ES2()) { gl.getGL2ES2().glCompileShader(state._tessellationEvaluationShaderID); gl.getGL2ES2() .glGetShaderiv( state._tessellationEvaluationShaderID, GL2ES2.GL_COMPILE_STATUS, compiled); } } checkProgramError( compiled.get(0), state._tessellationEvaluationShaderID, state._tessellationEvaluationShaderName); // Attach the program if (gl.isGL2()) { gl.getGL2().glAttachObjectARB(state._programID, state._tessellationEvaluationShaderID); } else { if (gl.isGL2ES2()) { gl.getGL2ES2().glAttachShader(state._programID, state._tessellationEvaluationShaderID); } } } else if (state._tessellationEvaluationShaderID != -1) { removeTessEvalShader(state); state._tessellationEvaluationShaderID = -1; } } if (gl.isGL2()) { gl.getGL2().glLinkProgramARB(state._programID); } else { if (gl.isGL2ES2()) { gl.getGL2ES2().glLinkProgram(state._programID); } } checkLinkError(state._programID); state.setNeedsRefresh(true); state._needSendShader = false; }
protected static void sendToGL(final GLSLShaderObjectsState state) { final GL gl = GLU.getCurrentGL(); if (state.getVertexShader() == null && state.getFragmentShader() == null) { logger.warning("Could not find shader resources!" + "(both inputbuffers are null)"); state._needSendShader = false; return; } if (state._programID == -1) { state._programID = gl.glCreateProgramObjectARB(); } if (state.getVertexShader() != null) { if (state._vertexShaderID != -1) { removeVertShader(state); } state._vertexShaderID = gl.glCreateShaderObjectARB(GL.GL_VERTEX_SHADER_ARB); // Create the sources final byte array[] = new byte[state.getVertexShader().limit()]; state.getVertexShader().rewind(); state.getVertexShader().get(array); gl.glShaderSourceARB( state._vertexShaderID, 1, new String[] {new String(array)}, new int[] {array.length}, 0); // Compile the vertex shader final IntBuffer compiled = BufferUtils.createIntBuffer(1); gl.glCompileShaderARB(state._vertexShaderID); gl.glGetObjectParameterivARB( state._vertexShaderID, GL.GL_OBJECT_COMPILE_STATUS_ARB, compiled); checkProgramError(compiled, state._vertexShaderID); // Attach the program gl.glAttachObjectARB(state._programID, state._vertexShaderID); } else if (state._vertexShaderID != -1) { removeVertShader(state); state._vertexShaderID = -1; } if (state.getFragmentShader() != null) { if (state._fragmentShaderID != -1) { removeFragShader(state); } state._fragmentShaderID = gl.glCreateShaderObjectARB(GL.GL_FRAGMENT_SHADER_ARB); // Create the sources final byte array[] = new byte[state.getFragmentShader().limit()]; state.getFragmentShader().rewind(); state.getFragmentShader().get(array); gl.glShaderSourceARB( state._fragmentShaderID, 1, new String[] {new String(array)}, new int[] {array.length}, 0); // Compile the fragment shader final IntBuffer compiled = BufferUtils.createIntBuffer(1); gl.glCompileShaderARB(state._fragmentShaderID); gl.glGetObjectParameterivARB( state._fragmentShaderID, GL.GL_OBJECT_COMPILE_STATUS_ARB, compiled); checkProgramError(compiled, state._fragmentShaderID); // Attach the program gl.glAttachObjectARB(state._programID, state._fragmentShaderID); } else if (state._fragmentShaderID != -1) { removeFragShader(state); state._fragmentShaderID = -1; } gl.glLinkProgramARB(state._programID); checkLinkError(state._programID); state.setNeedsRefresh(true); state._needSendShader = false; }
public void reloadShader() { if (useProjectedShader) { if (useRefraction) { currentShaderStr = projectedShaderRefractionStr; } else { currentShaderStr = projectedShaderStr; } } else { if (useRefraction) { currentShaderStr = simpleShaderRefractionStr; } else { currentShaderStr = simpleShaderStr; } } try { logger.info("loading " + currentShaderStr); waterShader.setVertexShader( ResourceLocatorTool.getClassPathResourceAsStream( WaterNode.class, currentShaderStr + ".vert")); waterShader.setFragmentShader( ResourceLocatorTool.getClassPathResourceAsStream( WaterNode.class, currentShaderStr + ".frag")); } catch (final IOException e) { logger.log(Level.WARNING, "Error loading shader", e); return; } waterShader.setUniform("normalMap", 0); waterShader.setUniform("reflection", 1); waterShader.setUniform("dudvMap", 2); if (useRefraction) { waterShader.setUniform("refraction", 3); waterShader.setUniform("depthMap", 4); } if (useProjectedShader) { if (useRefraction) { waterShader.setUniform("foamMap", 5); } else { waterShader.setUniform("foamMap", 3); } } waterShader._needSendShader = true; try { blurShaderVertical.setVertexShader( ResourceLocatorTool.getClassPathResourceAsStream( WaterNode.class, "com/ardor3d/extension/effect/bloom/bloom_blur.vert")); blurShaderVertical.setFragmentShader( ResourceLocatorTool.getClassPathResourceAsStream( WaterNode.class, "com/ardor3d/extension/effect/bloom/bloom_blur_vertical5_down.frag")); } catch (final IOException ex) { logger.logp( Level.SEVERE, getClass().getName(), "init(Renderer)", "Could not load shaders.", ex); } blurShaderVertical.setUniform("RT", 0); blurShaderVertical.setUniform("sampleDist", blurSampleDistance); blurShaderVertical._needSendShader = true; logger.info("Shader reloaded..."); }
@Override public void draw(final Renderer r) { initialize(r); updateTranslations(); final double camWaterDist = waterPlane.pseudoDistance(cam.getLocation()); aboveWater = camWaterDist >= 0; if (isSupported()) { waterShader.setUniform("tangent", tangent); waterShader.setUniform("binormal", binormal); waterShader.setUniform("useFadeToFogColor", useFadeToFogColor); waterShader.setUniform("waterColor", waterColorStart); waterShader.setUniform("waterColorEnd", waterColorEnd); waterShader.setUniform("normalTranslation", (float) normalTranslation); waterShader.setUniform("refractionTranslation", (float) refractionTranslation); waterShader.setUniform("abovewater", aboveWater); if (useProjectedShader) { waterShader.setUniform("cameraPos", cam.getLocation()); waterShader.setUniform("waterHeight", (float) waterPlane.getConstant()); waterShader.setUniform("amplitude", (float) waterMaxAmplitude); waterShader.setUniform("heightFalloffStart", (float) heightFalloffStart); waterShader.setUniform("heightFalloffSpeed", (float) heightFalloffSpeed); } final double heightTotal = clipBias + waterMaxAmplitude - waterPlane.getConstant(); final Vector4 clipPlane = Vector4.fetchTempInstance(); if (useReflection) { clipPlane.set( waterPlane.getNormal().getX(), waterPlane.getNormal().getY(), waterPlane.getNormal().getZ(), heightTotal); renderReflection(clipPlane); } if (useRefraction && aboveWater) { clipPlane.set( -waterPlane.getNormal().getX(), -waterPlane.getNormal().getY(), -waterPlane.getNormal().getZ(), -waterPlane.getConstant()); renderRefraction(clipPlane); } } if (fallbackTextureState != null) { fallbackTextureStateMatrix.setM31(normalTranslation); fallbackTexture.setTextureMatrix(fallbackTextureStateMatrix); } super.draw(r); }