glClearColor( 0.75f, 0.75f, 1.0f, 1.0f );
glClearDepth( 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
MatrixStack modelMatrix = new MatrixStack();
modelMatrix.setMatrix( viewPole.calcMatrix() );
final Mat4 worldToCamMat = modelMatrix.top();
LightBlock lightData = new LightBlock();
lightData.ambientIntensity = new Vec4( 0.2f, 0.2f, 0.2f, 1.0f );
float lightAttenuation = 1.0f / (halfLightDistance * halfLightDistance);
lightData.lightAttenuation = lightAttenuation;
lightData.lights[0] = new PerLight();
lightData.lights[0].cameraSpaceLightPos = Mat4.mul( worldToCamMat, new Vec4( 0.707f, 0.707f, 0.0f, 0.0f ) );
lightData.lights[0].lightIntensity = new Vec4( 0.6f, 0.6f, 0.6f, 1.0f );
lightData.lights[1] = new PerLight();
lightData.lights[1].cameraSpaceLightPos = Mat4.mul( worldToCamMat, calcLightPosition() );
lightData.lights[1].lightIntensity = new Vec4( 0.4f, 0.4f, 0.4f, 1.0f );
glBindBuffer( GL_UNIFORM_BUFFER, lightUniformBuffer );
glBufferSubData( GL_UNIFORM_BUFFER, 0, lightData.fillAndFlipBuffer( lightBlockBuffer ) );
glBindBuffer( GL_UNIFORM_BUFFER, 0 );
{
glBindBufferRange( GL_UNIFORM_BUFFER, materialBlockIndex, materialTerrainUniformBuffer, 0,
MaterialEntry.SIZE );
Mat3 normMatrix = new Mat3( modelMatrix.top() );
normMatrix = Glm.transpose( Glm.inverse( normMatrix ) );
glUseProgram( litMeshProg.theProgram );
glUniformMatrix4( litMeshProg.modelToCameraMatrixUnif, false,
modelMatrix.top().fillAndFlipBuffer( mat4Buffer ) );
glUniformMatrix3( litMeshProg.normalModelToCameraMatrixUnif, false,
normMatrix.fillAndFlipBuffer( mat3Buffer ) );
planeMesh.render();
glUseProgram( 0 );
glBindBufferBase( GL_UNIFORM_BUFFER, materialBlockIndex, 0 );
}
{
VertexData[] posSizeArray = new VertexData[NUMBER_OF_SPHERES];
posSizeArray[0] = new VertexData();
posSizeArray[0].cameraPosition = new Vec3( Mat4.mul( worldToCamMat, new Vec4( 0.0f, 10.0f, 0.0f, 1.0f ) ) );
posSizeArray[0].sphereRadius = 4.0f;
posSizeArray[1] = new VertexData();
posSizeArray[1].cameraPosition = getSphereOrbitPos( modelMatrix, new Vec3( 0.0f, 10.0f, 0.0f ),
new Vec3( 0.6f, 0.8f, 0.0f ), 20.0f, sphereTimer.getAlpha() );
posSizeArray[1].sphereRadius = 2.0f;
posSizeArray[2] = new VertexData();
posSizeArray[2].cameraPosition = getSphereOrbitPos( modelMatrix, new Vec3( -10.0f, 1.0f, 0.0f ),
new Vec3( 0.0f, 1.0f, 0.0f ), 10.0f, sphereTimer.getAlpha() );
posSizeArray[2].sphereRadius = 1.0f;
posSizeArray[3] = new VertexData();
posSizeArray[3].cameraPosition = getSphereOrbitPos( modelMatrix, new Vec3( 10.0f, 1.0f, 0.0f ),
new Vec3( 0.0f, 1.0f, 0.0f ), 10.0f, sphereTimer.getAlpha() * 2.0f );
posSizeArray[3].sphereRadius = 1.0f;
glBindBuffer( GL_ARRAY_BUFFER, imposterVBO );
{
FloatBuffer vertexDataBuffer = BufferUtils.createFloatBuffer( NUMBER_OF_SPHERES * VertexData.SIZE /
FLOAT_SIZE );
for ( VertexData vertexData : posSizeArray ) {
vertexData.fillBuffer( vertexDataBuffer );
}
vertexDataBuffer.flip();
glBufferData( GL_ARRAY_BUFFER, vertexDataBuffer, GL_STREAM_DRAW );
}
glBindBuffer( GL_ARRAY_BUFFER, 0 );
}
{
glBindBufferRange( GL_UNIFORM_BUFFER, materialBlockIndex, materialArrayUniformBuffer, 0,
MaterialEntry.SIZE * NUMBER_OF_SPHERES );
glUseProgram( litImpProg.theProgram );
glBindVertexArray( imposterVAO );
glDrawArrays( GL_POINTS, 0, NUMBER_OF_SPHERES );
glBindVertexArray( 0 );
glUseProgram( 0 );
glBindBufferBase( GL_UNIFORM_BUFFER, materialBlockIndex, 0 );
}
if ( drawLights ) {
modelMatrix.push();
modelMatrix.translate( new Vec3( calcLightPosition() ) );
modelMatrix.scale( 0.5f );
glUseProgram( unlit.theProgram );
glUniformMatrix4( unlit.modelToCameraMatrixUnif, false, modelMatrix.top().fillAndFlipBuffer( mat4Buffer ) );
Vec4 lightColor = new Vec4( 1.0f );
glUniform4( unlit.objectColorUnif, lightColor.fillAndFlipBuffer( vec4Buffer ) );
cubeMesh.render( "flat" );
modelMatrix.pop();
}
if ( drawCameraPos ) {
modelMatrix.push();
modelMatrix.setIdentity();
modelMatrix.translate( new Vec3( 0.0f, 0.0f, -viewPole.getView().radius ) );
glDisable( GL_DEPTH_TEST );
glDepthMask( false );
glUseProgram( unlit.theProgram );
glUniformMatrix4( unlit.modelToCameraMatrixUnif, false, modelMatrix.top().fillAndFlipBuffer( mat4Buffer ) );
glUniform4f( unlit.objectColorUnif, 0.25f, 0.25f, 0.25f, 1.0f );
cubeMesh.render( "flat" );
glDepthMask( true );
glEnable( GL_DEPTH_TEST );
glUniform4f( unlit.objectColorUnif, 1.0f, 1.0f, 1.0f, 1.0f );
cubeMesh.render( "flat" );
modelMatrix.pop();
}
}