Example of using "Canvas" form "Graphics" library to display 3D graphics.

Dependencies:   BSP_DISCO_F746NG Graphics mbed

Models/Cube.cpp

Committer:
karpent
Date:
2016-11-10
Revision:
1:4a5e329e617b
Child:
3:aca7fe2d44b3

File content as of revision 1:4a5e329e617b:

//
// Cube.cpp - example of usage graphics commands to create 3D graphics.
//
// This example is based on code written by
// Fabio de Albuquerque Dela Antonio (fabio914 at gmail.com)
// See : https://github.com/fabio914/arduinogl/blob/master/examples
//

#include "Cube.h"

// Constructor
Cube::Cube(Display * display)
    : Angle()
{
    glUseCanvas(display);

    _perspectiveAspect = display->DisplayWidth() / display->DisplayHeight();
}

/**
* @brief  Model setup.
* @retval None
*/
void Cube::Setup()
{
    glPointSize(4);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(30.0, _perspectiveAspect, 0.1f, 9999.f);

    glMatrixMode(GL_MODELVIEW);
}

/**
* @brief  Renders a square for given view angle and scale.
* @param  None
* @retval None
*/
void Cube::Render()
{
    const float scale = 2.5;

    // Put the initial matrix at glMatrices[glmatrixMode == GL_PROJECTION]
    glLoadIdentity();
    
    // Transform matrix
    gluLookAt(10, 8, -10, 0, 0, 0, 0, 1, 0);

    glRotatef(angle, 0.f, 1.f, 0.f);
    glScalef(scale, scale, scale);

    DrawCube();
}


void Cube::DrawCube()
{
    glBegin(GL_POLYGON);
    glVertex3f(-1, -1, -1);
    glVertex3f(1, -1, -1);
    glVertex3f(1, 1, -1);
    glVertex3f(-1, 1, -1);
    glEnd();

    glBegin(GL_POLYGON);
    glVertex3f(1, -1, -1);
    glVertex3f(1, -1, 1);
    glVertex3f(1, 1, 1);
    glVertex3f(1, 1, -1);
    glEnd();

    glBegin(GL_POLYGON);
    glVertex3f(1, -1, 1);
    glVertex3f(-1, -1, 1);
    glVertex3f(-1, 1, 1);
    glVertex3f(1, 1, 1);
    glEnd();

    glBegin(GL_POLYGON);
    glVertex3f(-1, -1, 1);
    glVertex3f(-1, -1, -1);
    glVertex3f(-1, 1, -1);
    glVertex3f(-1, 1, 1);
    glEnd();

    glBegin(GL_POLYGON);
    glVertex3f(-1, -1, 1);
    glVertex3f(1, -1, 1);
    glVertex3f(1, -1, -1);
    glVertex3f(-1, -1, -1);
    glEnd();

    glBegin(GL_POLYGON);
    glVertex3f(-1, 1, -1);
    glVertex3f(1, 1, -1);
    glVertex3f(1, 1, 1);
    glVertex3f(-1, 1, 1);
    glEnd();
}