BrickApplication.cpp
Default mainpageExamplesBrickApplicationBrickApplication.cpp
Description Overview Included files Included by Source
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "BrickApplication.hpp"

// This is a macro that creates the main entry point into the
// application. The name of the extended application class is
// sent to the macro and the main function is created.
IMPLEMENT_APPLICATION(BrickApplication);

BrickApplication::BrickApplication()
: Application("Brick Demo", 100, 100, 640, 480)
{
    rotation = 0.0f;

    // Set the type of shape to use
    shape = TEAPOT;
}

BrickApplication::~BrickApplication()
{

}

void BrickApplication::onEntry()
{
    // The brick shader classes are created here
    brickVertex   = new VertexShader();
    brickFragment = new FragmentShader();
    brickShader   = new ProgramObject();

    // The shader code is loaded and compiled here
    // Place the shader code where the executable is
    brickVertex->loadShader("brick.vert");
    brickFragment->loadShader("brick.frag");

    // The vertex and fragment are added to the program object
    brickShader->addShader(brickVertex);
    brickShader->addShader(brickFragment);

    // After adding all the vertex and fragments shaders the
    // program object must be linked before its able to be used
    brickShader->link();
}

void BrickApplication::onClose()
{
    // Here all the shader classes are cleaned up
    delete(brickVertex);
    delete(brickFragment);
    delete(brickShader);
}

void BrickApplication::onIdle()
{
    rotation += 0.2f;

    // Call the parent classes onIdle loop
    Application::onIdle();
}

void BrickApplication::onDisplay()
{
    glScissor(xPosition, yPosition, width, height);
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);
    glStencilMask(~0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
    glViewport(0, 0, width, height);
    glScissor (0, 0, width, height);

    gluPerspective(45.0, 1.0, 1.5, 20.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -5.0f);

    glPushMatrix();
        glRotatef(rotation, 0.0f, 1.0f, 0.0f);

        // Begins the use of the shader
        brickShader->begin();
            // Set all the uniforms after begining the shader
            brickShader->setUniform3f("BrickColor", 1.0f, 0.3f, 0.2f);
            brickShader->setUniform3f("MortarColor", 0.85f, 0.86f, 0.84f);
            brickShader->setUniform2f("BrickSize", 0.30f, 0.15f);
            brickShader->setUniform2f("BrickPct", 0.90f, 0.85f);
            brickShader->setUniform3f("LightPosition", 0.0f, 0.0f, 4.0f);

            switch (shape)
            {
                case SPHERE : glutSolidSphere(1, 24, 24); break;
                case CUBE   : glutSolidCube(1.5);   break;
                case TEAPOT : glutSolidTeapot(1); break;
            }

        // Ends the use of the shader
        brickShader->end();
    glPopMatrix();

    // Show back buffer
    glutSwapBuffers();

    glDisable(GL_SCISSOR_TEST);
}

void BrickApplication::onKeyDown(unsigned char key, int x, int y)
{
    switch(key)
    {
        case '1' : shape = TEAPOT; break;
        case '2' : shape = SPHERE; break;
        case '3' : shape = CUBE;   break;
    }
}