Exemplo n.º 1
0
void Renderer::drawGround() {
    // Active the ProgramObject
    _groundProgram->activate();

    // Bind the ground texture in the first available texture unit
    TextureUnit groundTextureUnit;
    groundTextureUnit.activate();
    _groundTexture->enable();
    _groundTexture->bind();

    // Bind the normal texture in the next free texture unit
    TextureUnit groundTextureNormalUnit;
    groundTextureNormalUnit.activate();
    _groundTextureNormal->enable();
    _groundTextureNormal->bind();

    // We are using 'fragColor' as the output variable from the FragmentShader
    _groundProgram->bindFragDataLocation("fragColor", 0);

    // Enable and bind the VBO holding the vertices for the ground and assign them a location
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, _groundVBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    _groundProgram->bindAttributeLocation("in_position", _groundVBO);
    
    // Set the rest of the uniforms
    // It would be faster to cache the uniform location and reuse that, but this is more readable
    _groundProgram->setUniform("_viewProjectionMatrix", _viewProjectionMatrix);
    _groundProgram->setUniform("_cameraPosition", _position);
    _groundProgram->setUniform("_lightPosition", _lightPosition);
    _groundProgram->setUniform("_texture", groundTextureUnit.unitNumber());
    _groundProgram->setUniform("_textureNormal", groundTextureNormalUnit.unitNumber());

    // Draw one quad 
    glDrawArrays(GL_QUADS, 0, 4);

    // And disable everything again to be a good citizen
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    _groundTexture->disable();
    _groundTextureNormal->disable();
    _groundProgram->deactivate();
}
Exemplo n.º 2
0
void Renderer::drawParticles() {
    // We want to be able to set the point size from the shader
    // and let OpenGL generate texture coordinates for each point
    glEnable(GL_PROGRAM_POINT_SIZE);
    glEnable(GL_POINT_SPRITE); // Deprecated in OpenGL 3.2, but necessary

    // Activate the ProgramObject
    _particleProgram->activate();

    // Bind the only one texture that is used as the color and normal texture
    TextureUnit textureUnit;
    textureUnit.activate();
    _particleTexture->enable();
    _particleTexture->bind();

    // We are using 'fragColor' as the output variable from the FragmentShader
    _particleProgram->bindFragDataLocation("fragColor", 0);

    // Enable and bind the VBO holding the vertices for the ground and assign them a location
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, _particleVBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_TRUE, 0, 0);
    _particleProgram->bindAttributeLocation("in_position", _particleVBO);

    // Set the rest of the uniforms
    // It would be faster to cache the uniform location and reuse that, but this is more readable
    _particleProgram->setUniform("_viewProjectionMatrix", _viewProjectionMatrix);
    _particleProgram->setUniform("_cameraPosition", _position);
    _particleProgram->setUniform("_lightPosition", _lightPosition);
    _particleProgram->setUniform("_texture", textureUnit.unitNumber());

    // _particleData holds the xyz coordinates, so it has thrice the amount of data
    // than it has points. And since we test for the correct amount of data elsewhere,
    // it is safe to assume that everything is fine
    glDrawArrays(GL_POINTS, 0, _numberOfParticles);

    // Be a good citizen and disable everything again
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    _particleTexture->disable();
    _particleProgram->deactivate();
    glDisable(GL_POINT_SPRITE);
    glDisable(GL_PROGRAM_POINT_SIZE);
}
Exemplo n.º 3
0
void Renderer::drawSkybox() {
    // Active the ProgramObject
    _skyboxProgram->activate();

    // Bind the cube map texture into the first texture unit
    TextureUnit cubeMapTextureUnit;
    cubeMapTextureUnit.activate();
    glEnable(GL_TEXTURE_CUBE_MAP);
    glBindTexture(GL_TEXTURE_CUBE_MAP, _skyboxTexture);

    // We are using 'fragColor' as the output variable from the FragmentShader
    _skyboxProgram->bindFragDataLocation("fragColor", 0);

    // Enable and bind the VBO holding the vertices for the ground and assign them a location
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, _skyboxVBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    _skyboxProgram->bindAttributeLocation("in_position", _skyboxVBO);

    // Use _skyboxIBO as our element array buffer to handle the indexed rendering
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _skyboxIBO);

    // Set the rest of the uniforms
    // It would be faster to cache the uniform location and reuse that, but this is more readable
    _skyboxProgram->setUniform("_viewProjectionMatrix", _viewProjectionMatrix);
    _skyboxProgram->setUniform("_texture", cubeMapTextureUnit.unitNumber());

    // Render the 4 quads
    glDrawElements(GL_QUADS, _numSkyboxIndices, GL_UNSIGNED_SHORT, 0);

    // And disable everything again to be a good citizen
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glDisable(GL_TEXTURE_CUBE_MAP);
    _skyboxProgram->deactivate();
}