![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Example of using "Canvas" form "Graphics" library to display 3D graphics.
Dependencies: BSP_DISCO_F746NG Graphics mbed
Models/Cube.cpp@1:4a5e329e617b, 2016-11-10 (annotated)
- Committer:
- karpent
- Date:
- Thu Nov 10 15:38:50 2016 +0000
- Revision:
- 1:4a5e329e617b
- Child:
- 3:aca7fe2d44b3
New models added - Cube and Sphere
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
karpent | 1:4a5e329e617b | 1 | // |
karpent | 1:4a5e329e617b | 2 | // Cube.cpp - example of usage graphics commands to create 3D graphics. |
karpent | 1:4a5e329e617b | 3 | // |
karpent | 1:4a5e329e617b | 4 | // This example is based on code written by |
karpent | 1:4a5e329e617b | 5 | // Fabio de Albuquerque Dela Antonio (fabio914 at gmail.com) |
karpent | 1:4a5e329e617b | 6 | // See : https://github.com/fabio914/arduinogl/blob/master/examples |
karpent | 1:4a5e329e617b | 7 | // |
karpent | 1:4a5e329e617b | 8 | |
karpent | 1:4a5e329e617b | 9 | #include "Cube.h" |
karpent | 1:4a5e329e617b | 10 | |
karpent | 1:4a5e329e617b | 11 | // Constructor |
karpent | 1:4a5e329e617b | 12 | Cube::Cube(Display * display) |
karpent | 1:4a5e329e617b | 13 | : Angle() |
karpent | 1:4a5e329e617b | 14 | { |
karpent | 1:4a5e329e617b | 15 | glUseCanvas(display); |
karpent | 1:4a5e329e617b | 16 | |
karpent | 1:4a5e329e617b | 17 | _perspectiveAspect = display->DisplayWidth() / display->DisplayHeight(); |
karpent | 1:4a5e329e617b | 18 | } |
karpent | 1:4a5e329e617b | 19 | |
karpent | 1:4a5e329e617b | 20 | /** |
karpent | 1:4a5e329e617b | 21 | * @brief Model setup. |
karpent | 1:4a5e329e617b | 22 | * @retval None |
karpent | 1:4a5e329e617b | 23 | */ |
karpent | 1:4a5e329e617b | 24 | void Cube::Setup() |
karpent | 1:4a5e329e617b | 25 | { |
karpent | 1:4a5e329e617b | 26 | glPointSize(4); |
karpent | 1:4a5e329e617b | 27 | |
karpent | 1:4a5e329e617b | 28 | glMatrixMode(GL_PROJECTION); |
karpent | 1:4a5e329e617b | 29 | glLoadIdentity(); |
karpent | 1:4a5e329e617b | 30 | |
karpent | 1:4a5e329e617b | 31 | gluPerspective(30.0, _perspectiveAspect, 0.1f, 9999.f); |
karpent | 1:4a5e329e617b | 32 | |
karpent | 1:4a5e329e617b | 33 | glMatrixMode(GL_MODELVIEW); |
karpent | 1:4a5e329e617b | 34 | } |
karpent | 1:4a5e329e617b | 35 | |
karpent | 1:4a5e329e617b | 36 | /** |
karpent | 1:4a5e329e617b | 37 | * @brief Renders a square for given view angle and scale. |
karpent | 1:4a5e329e617b | 38 | * @param None |
karpent | 1:4a5e329e617b | 39 | * @retval None |
karpent | 1:4a5e329e617b | 40 | */ |
karpent | 1:4a5e329e617b | 41 | void Cube::Render() |
karpent | 1:4a5e329e617b | 42 | { |
karpent | 1:4a5e329e617b | 43 | const float scale = 2.5; |
karpent | 1:4a5e329e617b | 44 | |
karpent | 1:4a5e329e617b | 45 | // Put the initial matrix at glMatrices[glmatrixMode == GL_PROJECTION] |
karpent | 1:4a5e329e617b | 46 | glLoadIdentity(); |
karpent | 1:4a5e329e617b | 47 | |
karpent | 1:4a5e329e617b | 48 | // Transform matrix |
karpent | 1:4a5e329e617b | 49 | gluLookAt(10, 8, -10, 0, 0, 0, 0, 1, 0); |
karpent | 1:4a5e329e617b | 50 | |
karpent | 1:4a5e329e617b | 51 | glRotatef(angle, 0.f, 1.f, 0.f); |
karpent | 1:4a5e329e617b | 52 | glScalef(scale, scale, scale); |
karpent | 1:4a5e329e617b | 53 | |
karpent | 1:4a5e329e617b | 54 | DrawCube(); |
karpent | 1:4a5e329e617b | 55 | } |
karpent | 1:4a5e329e617b | 56 | |
karpent | 1:4a5e329e617b | 57 | |
karpent | 1:4a5e329e617b | 58 | void Cube::DrawCube() |
karpent | 1:4a5e329e617b | 59 | { |
karpent | 1:4a5e329e617b | 60 | glBegin(GL_POLYGON); |
karpent | 1:4a5e329e617b | 61 | glVertex3f(-1, -1, -1); |
karpent | 1:4a5e329e617b | 62 | glVertex3f(1, -1, -1); |
karpent | 1:4a5e329e617b | 63 | glVertex3f(1, 1, -1); |
karpent | 1:4a5e329e617b | 64 | glVertex3f(-1, 1, -1); |
karpent | 1:4a5e329e617b | 65 | glEnd(); |
karpent | 1:4a5e329e617b | 66 | |
karpent | 1:4a5e329e617b | 67 | glBegin(GL_POLYGON); |
karpent | 1:4a5e329e617b | 68 | glVertex3f(1, -1, -1); |
karpent | 1:4a5e329e617b | 69 | glVertex3f(1, -1, 1); |
karpent | 1:4a5e329e617b | 70 | glVertex3f(1, 1, 1); |
karpent | 1:4a5e329e617b | 71 | glVertex3f(1, 1, -1); |
karpent | 1:4a5e329e617b | 72 | glEnd(); |
karpent | 1:4a5e329e617b | 73 | |
karpent | 1:4a5e329e617b | 74 | glBegin(GL_POLYGON); |
karpent | 1:4a5e329e617b | 75 | glVertex3f(1, -1, 1); |
karpent | 1:4a5e329e617b | 76 | glVertex3f(-1, -1, 1); |
karpent | 1:4a5e329e617b | 77 | glVertex3f(-1, 1, 1); |
karpent | 1:4a5e329e617b | 78 | glVertex3f(1, 1, 1); |
karpent | 1:4a5e329e617b | 79 | glEnd(); |
karpent | 1:4a5e329e617b | 80 | |
karpent | 1:4a5e329e617b | 81 | glBegin(GL_POLYGON); |
karpent | 1:4a5e329e617b | 82 | glVertex3f(-1, -1, 1); |
karpent | 1:4a5e329e617b | 83 | glVertex3f(-1, -1, -1); |
karpent | 1:4a5e329e617b | 84 | glVertex3f(-1, 1, -1); |
karpent | 1:4a5e329e617b | 85 | glVertex3f(-1, 1, 1); |
karpent | 1:4a5e329e617b | 86 | glEnd(); |
karpent | 1:4a5e329e617b | 87 | |
karpent | 1:4a5e329e617b | 88 | glBegin(GL_POLYGON); |
karpent | 1:4a5e329e617b | 89 | glVertex3f(-1, -1, 1); |
karpent | 1:4a5e329e617b | 90 | glVertex3f(1, -1, 1); |
karpent | 1:4a5e329e617b | 91 | glVertex3f(1, -1, -1); |
karpent | 1:4a5e329e617b | 92 | glVertex3f(-1, -1, -1); |
karpent | 1:4a5e329e617b | 93 | glEnd(); |
karpent | 1:4a5e329e617b | 94 | |
karpent | 1:4a5e329e617b | 95 | glBegin(GL_POLYGON); |
karpent | 1:4a5e329e617b | 96 | glVertex3f(-1, 1, -1); |
karpent | 1:4a5e329e617b | 97 | glVertex3f(1, 1, -1); |
karpent | 1:4a5e329e617b | 98 | glVertex3f(1, 1, 1); |
karpent | 1:4a5e329e617b | 99 | glVertex3f(-1, 1, 1); |
karpent | 1:4a5e329e617b | 100 | glEnd(); |
karpent | 1:4a5e329e617b | 101 | } |