/** * @param glRenderContext */ public void disable( RenderContext glRenderContext ) { if ( shaderProgram != null ) { shaderProgram.disable( glRenderContext.getContext() ); } }
@Override public void render( RenderContext glRenderContext ) { drawCube( glRenderContext.getContext() ); }
/** * Render the fragments with a colormap * * @param glRenderContext * @param colorMap */ private void render( RenderContext glRenderContext, Colormap colorMap ) { colorMap.enable( glRenderContext ); render( glRenderContext.getContext() ); colorMap.disable( glRenderContext ); }
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; }
/** * 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." ); } } } } }
private void renderCopyright() { Texture copyImage = TexturePool.getTexture( glRenderContext, copyrightID ); if ( copyImage != null ) { float tH = copyImage.getHeight(); float tW = copyImage.getWidth(); float quadWidth = tW; float quadHeight = tH; glRenderContext.getContext().glEnable( GL.GL_ALPHA_TEST ); glRenderContext.getContext().glAlphaFunc( GL.GL_GREATER, 0.4f ); draw2D( 0, 0, quadWidth, quadHeight, copyImage, true ); glRenderContext.getContext().glDisable( GL.GL_ALPHA_TEST ); glRenderContext.getContext().glAlphaFunc( GL.GL_ALWAYS, 1 ); } }
@Override protected void enableArrays( RenderContext glRenderContext, DirectGeometryBuffer geomBuffer ) { super.enableArrays( glRenderContext, geomBuffer ); if ( texturePosition >= 0 && textureBuffer == null && geomBuffer != null ) { textureBuffer = geomBuffer.getTextureCoordinates( texturePosition, textureOrdinatesCount ); if ( textureBuffer == null ) { texturePosition = -1; } } if ( textureBuffer != null ) { glRenderContext.getContext().glEnable( GL.GL_TEXTURE_2D ); TexturePool.loadTexture( glRenderContext, texture ); glRenderContext.getContext().glEnableClientState( GL.GL_TEXTURE_COORD_ARRAY ); glRenderContext.getContext().glTexCoordPointer( 2, GL.GL_FLOAT, 0, textureBuffer ); } }
/** * @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 ); } }
public void renderPrepared( RenderContext glRenderContext, DirectGeometryBuffer geomBuffer ) { enableArrays( glRenderContext, geomBuffer ); GL context = glRenderContext.getContext(); normalBuffer.rewind(); context.glPushAttrib( GL.GL_CURRENT_BIT | GL.GL_LIGHTING_BIT ); // context.glMaterialfv( GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT, ambientColor, 0 ); context.glMaterialfv( GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE, diffuseColor, 0 ); context.glMaterialfv( GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR, specularColor, 0 ); context.glMaterialfv( GL.GL_FRONT_AND_BACK, GL.GL_EMISSION, emmisiveColor, 0 ); context.glMaterialf( GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, style.getShininess() ); context.glDrawArrays( openGLType, 0, vertexCount ); context.glPopAttrib(); disableArrays( glRenderContext ); }
@Override public void disableArrays( RenderContext glRenderContext ) { super.disableArrays( glRenderContext ); LOG.trace( "Disabling array state and texture 2d" ); glRenderContext.getContext().glDisableClientState( GL.GL_TEXTURE_COORD_ARRAY ); glRenderContext.getContext().glDisable( GL.GL_TEXTURE_2D ); }
/** * Load the dem fragment on the GPU, in other words, enable the VBO's, of the normals, vertices and indizes. * * @param glRenderContext */ private void loadDEMOnGPU( RenderContext glRenderContext ) { long begin = System.currentTimeMillis(); try { fragmentManager.requireOnGPU( activeLOD, glRenderContext.getContext() ); } catch ( IOException e ) { LOG.debug( "Could not load the fragments on the gpu, stack.", e ); LOG.error( "Could not load the fragments on the gpu because: " + e.getMessage() ); } LOG.debug( "GPU upload of " + activeLOD.size() + " fragments: " + ( System.currentTimeMillis() - begin ) + " milliseconds." ); }
private void debug( RenderContext context ) { GL gl = context.getContext(); gl.glPushAttrib( GL.GL_CURRENT_BIT | GL.GL_LIGHTING_BIT ); gl.glMaterialfv( GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT, new float[] { 1, 0, 0 }, 0 ); gl.glMaterialfv( GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE, new float[] { 1, 0, 0 }, 0 ); float[] bbox = getModelBBox(); gl.glBegin( GL.GL_QUADS ); // Front face gl.glNormal3f( 0, 0, 1 ); gl.glVertex3f( bbox[0], bbox[1], bbox[2] ); gl.glVertex3f( bbox[3], bbox[4], bbox[2] ); gl.glVertex3f( bbox[3], bbox[4], bbox[5] ); gl.glVertex3f( bbox[0], bbox[1], bbox[5] ); gl.glEnd(); gl.glPopAttrib(); } }
/** * Link this program with all it's attached shaders. * * @param gl * @return true if the linkage was successful and the validation was positive. * @throws RuntimeException * if an OGL exception occurred. */ public boolean linkProgram( GL gl ) throws RuntimeException { boolean result = false; try { linkShaderProgram( gl ); result = true; } catch ( RuntimeException r ) { LOG.error( "Could not link shader because: " + r.getLocalizedMessage(), r ); throw ( r ); } // if ( LOG.isDebugEnabled() ) { try { validateShaderProgram( gl ); } catch ( RuntimeException r ) { LOG.warn( "Shader program source: was not valid because: " + r.getLocalizedMessage() ); result = false; } return result; }
/** * @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 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; }
/** * 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; }
/** * @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 gl */ private void renderCopyright( RenderContext context ) { Texture copyImage = TexturePool.getTexture( context, copyrightID ); if ( copyImage != null ) { float tH = copyImage.getHeight(); float tW = copyImage.getWidth(); float quadWidth = tW; float quadHeight = tH; /** * rb: Scaling of copyright image could be realized with following code. */ // if ( width > height ) { // quadWidth = (float) ( width * copyrightScale ); // quadHeight = quadWidth * ( ( tW > tH ) ? ( tH / tW ) : ( tW / tH ) ); // } else { // quadHeight = (float) ( height * copyrightScale ); // // if the width of the texture is larger then the height, use the scalediff to calculate the texwidth. // quadWidth = quadHeight * ( ( tW > tH ) ? ( tW / tH ) : ( tH / tW ) ); // } context.getContext().glEnable( GL.GL_ALPHA_TEST ); context.getContext().glAlphaFunc( GL.GL_GREATER, 0.4f ); draw2D( context, 0, 0, quadWidth, quadHeight, copyImage, true ); context.getContext().glDisable( GL.GL_ALPHA_TEST ); context.getContext().glAlphaFunc( GL.GL_ALWAYS, 1 ); } }
/** * @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 ); }
/** * @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; }