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

GraphicsA general purpose graphics library built on top of the MobileLCD interface, providing 2D and wireframe 3D functionality.
Functions
GraphicsInstantiate the graphics library.
lineDraw a coloured line between two points.
line3dDraws a coloured line in 3D space.
x_rotate3dRotates a point in 3D space about a specified origin along the x-axis.
y_rotate3dRotates a point in 3D space about a specified origin along the y-axis.
z_rotate3dRotates a point in 3D space about a specified origin along the z-axis.
circleDraw a coloured circle.
class Graphics : public MobileLCD
A general purpose graphics library built on top of the MobileLCD interface, providing 2D and wireframe 3D functionality.
Graphics(PinName mosi,
PinName miso,
PinName clk,
PinName cs,
PinName rst)
Instantiate the graphics library.
void line(int x0,
int y0,
int x1,
int y1,
int colour)
Draw a coloured line between two points.
void line3d(int x0,
int y0,
int z0,
int x1,
int y1,
int z0,
int colour)
Draws a coloured line in 3D space.
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 x-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 y-axis.
void z_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 circle(int cx,
int cy,
int radius,
int colour)
Draw a coloured circle.

Object3DGeneral purpose 3D base object, new 3D objects should inherit from this class.
Functions
positionSpecify the position of the object in 3D space.
rotateRotate the object about it’s centre point.
scaleModify the size of the object.
colourSets the colour of the object.
renderDraws the object to the specified graphical context.
class Object3D
General purpose 3D base object, new 3D objects should inherit from this class.
void position(int x,
int y,
int z)
Specify the position of the object in 3D space.
void rotate(double rx,
double ry,
double rz)
Rotate the object about it’s centre point.
void scale(double sx,
double sy,
double sz)
Modify the size of the object.
void colour(int colour)
Sets the colour of the object.
void render(Graphics g)
Draws the object to the specified graphical context.

CuboidDisplays a 3D wireframe cuboid.
Functions
renderDraws the cuboid to the specified graphical context.
class Cuboid : public Object3D
Displays a 3D wireframe cuboid.
void render(Graphics g)
Draws the cuboid to the specified graphical context.

TrimeshObjectConstructs 3D objects from triangle meshes specified by lists of vertices and faces.
Functions
TrimeshObjectInstantiate a trimesh object.
renderDraws the trimesh object to the specified graphical context.
class TrimeshObject : public Object3D
Constructs 3D objects from triangle meshes specified by lists of vertices and faces.
TrimeshObject(int vertices[][3],
int faces[][3],
int num_faces)
Instantiate a trimesh object.
void render(Graphics g)
Draws the trimesh object to the specified graphical context.


Example

The following example will render a 3D Tie Fighter and rotate it about the centre of the screen.

View a video of this example.

#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