Overview
This is a general purpose graphics library built on top of the MobileLCD library.
Library
Library location: http://mbed.org/projects/cookbook/svn/Graphics/trunk/
API
| Graphics | A general purpose graphics library built on top of the MobileLCD interface, providing 2D and wireframe 3D functionality. |
| Functions | |
| Graphics | Instantiate the graphics library. |
| line | Draw a coloured line between two points. |
| line3d | Draws a coloured line in 3D space. |
| x_rotate3d | Rotates a point in 3D space about a specified origin along the x-axis. |
| y_rotate3d | Rotates a point in 3D space about a specified origin along the y-axis. |
| z_rotate3d | Rotates a point in 3D space about a specified origin along the z-axis. |
| circle | Draw a coloured circle. |
A general purpose graphics library built on top of the MobileLCD interface, providing 2D and wireframe 3D functionality.
class Graphics : public MobileLCD
Instantiate the graphics library.
Graphics( PinName mosi, PinName miso, PinName clk, PinName cs, PinName rst )
Draw a coloured line between two points.
void line( int x0, int y0, int x1, int y1, int colour )
Draws a coloured line in 3D space.
void line3d( int x0, int y0, int z0, int x1, int y1, int z0, int colour )
Rotates a point in 3D space about a specified origin along the x-axis.
void x_rotate3d( int * x, int * y, int * z, int ox, int oy, int oz, double angle )
Rotates a point in 3D space about a specified origin along the y-axis.
void y_rotate3d( int * x, int * y, int * z, int ox, int oy, int oz, double angle )
Rotates a point in 3D space about a specified origin along the z-axis.
void z_rotate3d( int * x, int * y, int * z, int ox, int oy, int oz, double angle )
Draw a coloured circle.
void circle( int cx, int cy, int radius, int colour )
| Object3D | General purpose 3D base object, new 3D objects should inherit from this class. |
| Functions | |
| position | Specify the position of the object in 3D space. |
| rotate | Rotate the object about it’s centre point. |
| scale | Modify the size of the object. |
| colour | Sets the colour of the object. |
| render | Draws the object to the specified graphical context. |
General purpose 3D base object, new 3D objects should inherit from this class.
class Object3D
Specify the position of the object in 3D space.
void position( int x, int y, int z )
Rotate the object about it’s centre point.
void rotate( double rx, double ry, double rz )
Modify the size of the object.
void scale( double sx, double sy, double sz )
Sets the colour of the object.
void colour( int colour )
Draws the object to the specified graphical context.
void render( Graphics g )
| Cuboid | Displays a 3D wireframe cuboid. |
| Functions | |
| render | Draws the cuboid to the specified graphical context. |
Displays a 3D wireframe cuboid.
class Cuboid : public Object3D
Draws the cuboid to the specified graphical context.
void render( Graphics g )
| TrimeshObject | Constructs 3D objects from triangle meshes specified by lists of vertices and faces. |
| Functions | |
| TrimeshObject | Instantiate a trimesh object. |
| render | Draws the trimesh object to the specified graphical context. |
Constructs 3D objects from triangle meshes specified by lists of vertices and faces.
class TrimeshObject : public Object3D
Instantiate a trimesh object.
TrimeshObject( int vertices[][3], int faces[][3], int num_faces )
Draws the trimesh object to the specified graphical context.
void render( Graphics g )
Example
The following example will render a 3D Tie Fighter and rotate it about the centre of the screen.
#include "mbed.h" #include "Graphics.h" #include "Cuboid.h" #include "TrimeshObject.h" #include "TieFighter.h" Graphics g(5, 6, 7, 8, 9); TrimeshObject tf(tie_fighter_vertices, tie_fighter_faces, TIE_FIGHTER_NUM_FACES); int main() { double rotx = 0, roty = 0, rotz = 0; while (1) { rotx += 0.1; roty += 0.08; rotz += 0.05; tf.rotate(rotx, roty, rotz); tf.colour(0xffffff); tf.render(g); wait(0.02); g.fill(40, 40, 52, 52, 0x000000); } }
The definition of the Tie Fighter model is as follows:
#ifndef MBED_TIEFIGHTER_H #define MBED_TIEFIGHTER_H int tie_fighter_vertices[48][3] = { // Left wing - v0 {0, 0, 17}, {10, -15, 15}, {-10, -15, 15}, {-15, 0, 15}, {-10, 15, 15}, {-10, 15, 15}, {10, 15, 15}, {15, 0, 15}, // Left inner wing connector - v8 {0, 0, 17}, {4, -4, 15}, {-4, -4, 15}, {-4, 0, 15}, {-4, 4, 15}, {-4, 4, 15}, {4, 4, 15}, {4, 0, 15}, // Right wing - v16 {0, 0, -17}, {10, -15, -15}, {-10, -15, -15}, {-15, 0, -15}, {-10, 15, -15}, {-10, 15, -15}, {10, 15, -15}, {15, 0, -15}, // Right inner wing connector - v24 {0, 0, -17}, {4, -4, -15}, {-4, -4, -15}, {-4, 0, -15}, {-4, 4, -15}, {-4, 4, -15}, {4, 4, -15}, {4, 0, -15}, // Body - v32 {8, 8, -8}, {-8, 8, -8}, {8, 8, 8}, {-8, 8, 8}, {8, -8, -8}, {-8, -8, -8}, {8, -8, 8}, {-8, -8, 8}, // Left wing <-> body connection {-2, -2, 15}, {-2, -2, 8}, {2, 2, 15}, {2, 2, 8}, // Right wing <-> body connection {-2, -2, -15}, {-2, -2, -8}, {2, 2, -15}, {2, 2, -8}, }; #define TIE_FIGHTER_NUM_FACES 32 int tie_fighter_faces[TIE_FIGHTER_NUM_FACES][3] = { // Left wing {0, 1, 2}, {0, 2, 3}, {0, 3, 4}, {0, 4, 5}, {0, 5, 6}, {0, 6, 7}, {0, 7, 1}, // Left inner wing connector /* {8, 9, 10}, {8, 10, 11}, {8, 11, 12}, {8, 12, 13}, {8, 13, 14}, {8, 14, 15}, {8, 15, 9}, */ // Right wing {16, 17, 18}, {16, 18, 19}, {16, 19, 20}, {16, 20, 21}, {16, 21, 22}, {16, 22, 23}, {16, 23, 17}, // Right inner wing connector /* {24, 25, 26}, {24, 26, 27}, {24, 27, 28}, {24, 28, 29}, {24, 29, 30}, {24, 30, 31}, {24, 31, 25}, */ // Body {32, 33, 33}, // Hack to just draw some straight lines so the body doesn't look too cluttered with triangles {33, 35, 35}, {35, 34, 34}, {32, 34, 34}, {36, 37, 37}, {37, 39, 39}, {39, 38, 38}, {36, 38, 38}, {36, 32, 32}, {37, 33, 33}, {39, 35, 35}, {38, 34, 34}, // Left wing <-> body connection {40, 41, 41}, {42, 43, 43}, // Right wing <-> body connection {44, 45, 45}, {46, 47, 47}, }; #endif
