/** * Create the dem compositing texture shader programs for the given context * * @param gl */ public void createCompositingTextureShaderPrograms( GL gl ) { LOG.debug( "building " + numberOfTextureUnits + " shader programs" ); synchronized ( LOCK ) { if ( this.compositeTextureShaderPrograms == null ) { this.compositeTextureShaderPrograms = new ShaderProgram[this.numberOfTextureUnits]; for ( int i = 0; i < compositeTextureShaderPrograms.length; i++ ) { String shaderSource = CompositingShader.getGLSLCode( i + 1 ); compositeTextureShaderPrograms[i] = new ShaderProgram(); int shaderId = compositeTextureShaderPrograms[i].createFragmentShader( gl, shaderSource ); if ( compositeTextureShaderPrograms[i].attachShader( gl, shaderId ) ) { compositeTextureShaderPrograms[i].linkProgram( gl ); } else { LOG.warn( "Could not attach compositing texture shader program: " + i + " error messages should have been supplied before this message." ); } } } } }
/** * @param glContext * the context for which this shader program was created. * @param fragmentShaderSource * the program to attach and link against this open gl program. * @return the id of the linked vertex shader. * @throws RuntimeException * if the shader could not be compiled or linked. */ public int createFragmentShader( GL glContext, String fragmentShaderSource ) throws RuntimeException { LOG.debug( "Adding fragment shader source: " + fragmentShaderSource ); return compileShader( glContext, GL.GL_FRAGMENT_SHADER, fragmentShaderSource ); }
/** * @param glRenderContext */ public void disable( RenderContext glRenderContext ) { if ( shaderProgram != null ) { shaderProgram.disable( glRenderContext.getContext() ); } }
/** * @param glRenderContext */ public void enable( RenderContext glRenderContext ) { GL gl = glRenderContext.getContext(); if ( shaderProgram == null ) { if ( !setupShaderProgram( gl ) ) { this.shaderProgram = null; return; } } if ( shaderProgram.useProgram( gl ) ) { int mCLoc = gl.glGetUniformLocation( shaderProgram.getOGLId(), UNIFORM_MIN_COLOR ); int maxCLoc = gl.glGetUniformLocation( shaderProgram.getOGLId(), UNIFORM_HEIGHT_COLOR ); int dVLoc = gl.glGetUniformLocation( shaderProgram.getOGLId(), UNIFORM_DIFF_VEC ); int miZLoc = gl.glGetUniformLocation( shaderProgram.getOGLId(), UNIFORM_MIN_Z ); if ( mCLoc == -1 || maxCLoc == -1 || dVLoc == -1 || miZLoc == -1 ) { LOG.warn( "Could not get uniform location of Vertex shader, no color map is enabled: {},{},{},{}.", new Object[] { mCLoc, maxCLoc, dVLoc, miZLoc, } ); return; } gl.glUniform4f( mCLoc, minColor[0], minColor[1], minColor[2], 1 ); gl.glUniform4f( maxCLoc, heightColor[0], heightColor[1], heightColor[2], 1 ); gl.glUniform4f( dVLoc, difVec[0], difVec[1], difVec[2], 1 ); gl.glUniform4f( miZLoc, zMin, invDifZ, 0, 0 ); } }
/** * @param gl * the context of this shader program. * @param shaderId * to be detached from this program. * @return true iff the current program id and the given shaderId are valid and the shader could be detached from * this program. */ public boolean detachShader( GL gl, int shaderId ) { boolean result = false; if ( isShaderIdValid( gl, shaderId ) ) { gl.glDetachShader( oglProgramId, shaderId ); result = true; } else { LOG.warn( "Either the program id: " + oglProgramId + " or the given shader id: " + shaderId + " are not valid, cannot detach the shader." ); } return result; }
/** * Creates, Compiles, links and validates the given shader source. * * @param gl * to be link against * @param GL_SHADER_ID * either {@link GL#GL_VERTEX_SHADER} or {@link GL#GL_FRAGMENT_SHADER} * @param shaderSource * to be used * @return the id of the shader program and the id of teh shader. */ private int compileShader( GL gl, int GL_SHADER_ID, String shaderSource ) throws RuntimeException { if ( oglProgramId == -1 ) { oglProgramId = gl.glCreateProgram(); } int shaderId = -1; try { shaderId = compileShaderProgram( gl, GL_SHADER_ID, shaderSource ); } catch ( RuntimeException r ) { LOG.error( "Could not compile " + ( ( GL.GL_VERTEX_SHADER == GL_SHADER_ID ) ? "vertex" : "fragment" ) + " shader from source: \n" + shaderSource + " \nbecause: " + r.getLocalizedMessage(), r ); throw ( r ); } return shaderId; }
shaderProgram.useProgram( gl ); for ( int i = 0; i < textures.size(); i++ ) { if ( textures.get( i ) != null ) { int texSampler = gl.glGetUniformLocation( shaderProgram.getOGLId(), "tex" + i ); gl.glUniform1i( texSampler, i );
/** * @param gl * the context of this shader program. * @param shaderId * to attach to this program. * @return true iff this program and the given shader id are valid and the id could be attached to the program * (attaching is not linking!). */ public boolean attachShader( GL gl, int shaderId ) { boolean result = false; if ( isShaderIdValid( gl, shaderId ) ) { gl.glAttachShader( oglProgramId, shaderId ); result = true; } else { LOG.warn( "Either the program id: " + oglProgramId + " or the given shader id: " + shaderId + " are not valid, cannot attach the shader." ); } return result; }
private boolean setupShaderProgram( GL gl ) { this.shaderProgram = new ShaderProgram(); int vertShaderId = shaderProgram.createVertexShader( gl, createVertexShader() ); int fragShaderId = shaderProgram.createFragmentShader( gl, createFragmentShader( 1 ) ); if ( !shaderProgram.attachShader( gl, vertShaderId ) ) { // rb: unable to attach the given shader id, ignoring. LOG.warn( "Could not attach the vertex part to the color map shader program, ignoring color map." ); return false; } if ( !shaderProgram.attachShader( gl, fragShaderId ) ) { LOG.warn( "Could not attach the fragment part to the color map shader program, ignoring color map." ); return false; } if ( !shaderProgram.linkProgram( gl ) ) { LOG.warn( "Could not link the shaders to the color map shader program, ignoring color map." ); return false; } return true; }
/** * @param gl * @param textures * @param shaderProgram */ private void disableTextureStates( GL gl, List<FragmentTexture> textures, ShaderProgram shaderProgram ) { // reset non-standard OpenGL states if ( textures != null && textures.size() > 0 ) { for ( int i = textures.size() - 1; i >= 0; --i ) { int textureUnitId = JOGLUtils.getTextureUnitConst( i ); gl.glClientActiveTexture( textureUnitId ); gl.glActiveTexture( textureUnitId ); gl.glDisable( GL.GL_TEXTURE_2D ); gl.glDisableClientState( GL.GL_TEXTURE_COORD_ARRAY ); } // gl.glActiveTexture( GL.GL_TEXTURE0 ); // gl.glClientActiveTexture( GL.GL_TEXTURE0 ); } // rb: shader program can not be null. shaderProgram.disable( gl ); }
/** * @param glContext * the context for which this shader program was created. * @param vertexShaderSource * the program to attach and link against this open gl program. * @return the id of the linked vertex shader. * @throws RuntimeException * if the shader could not be compiled or linked. */ public int createVertexShader( GL glContext, String vertexShaderSource ) throws RuntimeException { LOG.debug( "Adding vertex shader source: " + vertexShaderSource ); return compileShader( glContext, GL.GL_VERTEX_SHADER, vertexShaderSource ); }