Example of using "Canvas" form "Graphics" library to display 3D graphics.
Dependencies: BSP_DISCO_F746NG Graphics mbed
Diff: Models/Cube.cpp
- Revision:
- 1:4a5e329e617b
- Child:
- 3:aca7fe2d44b3
diff -r 8acbce46eede -r 4a5e329e617b Models/Cube.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Models/Cube.cpp Thu Nov 10 15:38:50 2016 +0000 @@ -0,0 +1,101 @@ +// +// 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(); +} \ No newline at end of file