Laser Sensing Display for UI interfaces in the real world

Dependencies:   mbed

Fork of skinGames_forktest by Alvaro Cassinelli

Committer:
mbedalvaro
Date:
Thu Apr 17 08:04:14 2014 +0000
Revision:
47:199042980678
Parent:
43:1dd4cfc30788
publishing for sharing with Ken Iwasaki

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedalvaro 40:3ba2b0ea9f33 1 /* Name: WrapperFunctions.h
mbedalvaro 40:3ba2b0ea9f33 2
mbedalvaro 40:3ba2b0ea9f33 3 * DESCRIPTION:
mbedalvaro 40:3ba2b0ea9f33 4
mbedalvaro 40:3ba2b0ea9f33 5 The following file defines a set of "WRAPPER" functions to simplify creating, rendering and displaying objects and scenes.
mbedalvaro 40:3ba2b0ea9f33 6 Basically, to display something we have to:
mbedalvaro 40:3ba2b0ea9f33 7
mbedalvaro 40:3ba2b0ea9f33 8 (1) BUILD A "SCENE" by creating and adding Objects. The rendering stack (lsr) will be used to modify the "modeling transformation RT".
mbedalvaro 40:3ba2b0ea9f33 9 (2) Set the PERSPECTIVE TRANSFORMATION, and the VIEWING TRANSFORMATION (again, this is just playing with lsr), and then DRAW THE SCENE.
mbedalvaro 40:3ba2b0ea9f33 10
mbedalvaro 40:3ba2b0ea9f33 11 * NOTE THAT:
mbedalvaro 40:3ba2b0ea9f33 12
mbedalvaro 40:3ba2b0ea9f33 13 (1) BUILDING the SCENE is done ONLY ONCE (or at least, each time we have a completely new scene to show), and not all the time as
mbedalvaro 40:3ba2b0ea9f33 14 in OpenGL (well, the equivalent in OpenGL is the vertex arrays sent to the graphic card).
mbedalvaro 40:3ba2b0ea9f33 15 (2) DRAWING THE SCENE is ALSO DONE ONLY WHEN WE GET A NEW VIEWING TRANSFORMATION (i.e., a new "POSE"). This is because
mbedalvaro 40:3ba2b0ea9f33 16
mbedalvaro 40:3ba2b0ea9f33 17 * HOW DOES IT WORK:
mbedalvaro 40:3ba2b0ea9f33 18
mbedalvaro 40:3ba2b0ea9f33 19 (1) (a) Building a scene start by a call to "beginScene()". This routine first stoping the displaying engine (lsd) if it was working, and then deleting all data in it.
mbedalvaro 40:3ba2b0ea9f33 20 The reason is that we will modify the number of points/objects in it, and the "laser sensing display" engine (lsd) would fail trying to access arrays that have been deleted.
mbedalvaro 40:3ba2b0ea9f33 21 (b) then, we can add objects (using the objects primitives, or begin/end keywords). In so doing, we can use the modelview matrix stack liberaly, to place things
mbedalvaro 40:3ba2b0ea9f33 22 where they need to be. Note that the objects are NOT yet rendered.
mbedalvaro 40:3ba2b0ea9f33 23 (C) We finish by calling "updateScene()", which has an important function: it tells the (dormant) displaying engine to update the size and structure of the "scene" in its
mbedalvaro 40:3ba2b0ea9f33 24 internal variables. BUT WE DON'T REATTACH THE INTERRUPT for the lsd again, because we did NOT yet render anything!
mbedalvaro 40:3ba2b0ea9f33 25
mbedalvaro 40:3ba2b0ea9f33 26 (2) Time for drawing: the first thing is to set the PROJECTION MATRIX (this in principle can be done once, at the very start of the program and is likely not to change).
mbedalvaro 40:3ba2b0ea9f33 27 Then, each time we have a new POSE, we need to set the VIEWING TRANSFORMATION (again, using the modelview stack lsr).
mbedalvaro 40:3ba2b0ea9f33 28 When both things are done, a call to "drawScene()" will apply the global viewing transformation to each point in the scene, and RENDER them too (using the projection matrix).
mbedalvaro 40:3ba2b0ea9f33 29 By DEFAULT, a call to "drawScene()" will ACTIVATE the DISPLAYING ENGINE if it was not working.
mbedalvaro 40:3ba2b0ea9f33 30
mbedalvaro 40:3ba2b0ea9f33 31 IMPORTANT: Note that we don't NEED to STOP the displaying interrupt when just changing the pose or even the projection matrix - this is because the STRUCTURE of the scene
mbedalvaro 40:3ba2b0ea9f33 32 did not change, and then the displaying engine will not fail. However, there may be some strange graphical "anomalies" (the equivalent of problems with
mbedalvaro 40:3ba2b0ea9f33 33 vertical synch on normal displays). This is because I am not using DOUBLE BUFFERING, but I will address this in future versions.
mbedalvaro 40:3ba2b0ea9f33 34
mbedalvaro 40:3ba2b0ea9f33 35 * AN EXAMPLE:
mbedalvaro 40:3ba2b0ea9f33 36
mbedalvaro 40:3ba2b0ea9f33 37 (a) In a setting function:
mbedalvaro 40:3ba2b0ea9f33 38
mbedalvaro 40:3ba2b0ea9f33 39 //(A) First, set the modelview and projection matrices:
mbedalvaro 40:3ba2b0ea9f33 40 // Typically, the projection matrix is set from the PC side, or loaded by default (from file system).
mbedalvaro 40:3ba2b0ea9f33 41 // The modelview matrix may be, say, the identity:
mbedalvaro 40:3ba2b0ea9f33 42 lsr.setIdentityPose(); // we could use a wrapper, but I won't for the time being.
mbedalvaro 40:3ba2b0ea9f33 43
mbedalvaro 40:3ba2b0ea9f33 44 //(B) Start building the "scene" with objects in it (now I will use wrapper functions - note that "objectPrimitives" are sort of wrapper functions too):
mbedalvaro 40:3ba2b0ea9f33 45
mbedalvaro 40:3ba2b0ea9f33 46 beginScene(); // stop displaying (in the future we can have double buffering...). then clear the scene.
mbedalvaro 40:3ba2b0ea9f33 47 // we can start adding objects:
mbedalvaro 40:3ba2b0ea9f33 48
mbedalvaro 40:3ba2b0ea9f33 49 // (a) in OpenGL like style:
mbedalvaro 40:3ba2b0ea9f33 50 begin(1); // start creating object with identifier = 1
mbedalvaro 40:3ba2b0ea9f33 51 vertex(10,10,0); vertex(10,100,0); vertex(100,100,0); vertex(10,100,0); vertex(10,10,0); // closed square
mbedalvaro 40:3ba2b0ea9f33 52 end();
mbedalvaro 40:3ba2b0ea9f33 53
mbedalvaro 40:3ba2b0ea9f33 54 // (b) example of adding an object using objectPrimitives:
mbedalvaro 40:3ba2b0ea9f33 55 buildLine(10,10,0,100,100,0,10, 1);
mbedalvaro 40:3ba2b0ea9f33 56
mbedalvaro 40:3ba2b0ea9f33 57 endScene(); // this is important: rendering is done here, and the display engine (lsd) is reattached to the interrupt timer.
mbedalvaro 40:3ba2b0ea9f33 58
mbedalvaro 40:3ba2b0ea9f33 59
mbedalvaro 40:3ba2b0ea9f33 60 */
mbedalvaro 40:3ba2b0ea9f33 61
mbedalvaro 40:3ba2b0ea9f33 62 #include "Scene.h"
mbedalvaro 40:3ba2b0ea9f33 63 #include "laserSensingDisplay.h"
mbedalvaro 40:3ba2b0ea9f33 64 #include "LaserRenderer.h"
mbedalvaro 40:3ba2b0ea9f33 65
mbedalvaro 40:3ba2b0ea9f33 66 #include <string>
mbedalvaro 40:3ba2b0ea9f33 67 //using namespace std; // should not be in a header (we will bring to any other cpp that includes this file!!
mbedalvaro 40:3ba2b0ea9f33 68
mbedalvaro 40:3ba2b0ea9f33 69 // Special classes of derived objects:
mbedalvaro 40:3ba2b0ea9f33 70 // #include ...
mbedalvaro 40:3ba2b0ea9f33 71
mbedalvaro 40:3ba2b0ea9f33 72 //class laserSensingDisplay; // will produce "incomplete type not allowed"
mbedalvaro 40:3ba2b0ea9f33 73
mbedalvaro 40:3ba2b0ea9f33 74 // ====================================================================================================================================================
mbedalvaro 40:3ba2b0ea9f33 75
mbedalvaro 40:3ba2b0ea9f33 76 // enum typeObject {GENERIC_OBJECT, LETTER}; // this will be useful for adding derived objects from BaseObject using the being() method, such as letters, and in the future objects
mbedalvaro 40:3ba2b0ea9f33 77 // that can have an "update" method of their own (and move around...)
mbedalvaro 40:3ba2b0ea9f33 78
mbedalvaro 40:3ba2b0ea9f33 79 // ================================= BASIC "OPENGL-like" object/scene builders and query functions ("sense") ==========================================
mbedalvaro 40:3ba2b0ea9f33 80
mbedalvaro 40:3ba2b0ea9f33 81 // Creating a "scene" (a collection of "objects").
mbedalvaro 40:3ba2b0ea9f33 82 // NOTE: the advantage of having clustered points into "objects" and then a "scene" is that we may have handles for each of them, to modify their appearance, or query for touch, etc.
mbedalvaro 40:3ba2b0ea9f33 83 void clearScene();
mbedalvaro 40:3ba2b0ea9f33 84 void updateScene();
mbedalvaro 40:3ba2b0ea9f33 85
mbedalvaro 40:3ba2b0ea9f33 86 // Create objects "a-la" OpenGL (for other ways, see objectPrimitives.h).
mbedalvaro 40:3ba2b0ea9f33 87 //BaseObject* begin(typeObject _type, unsigned char _id); // This will add a new object with or without identifier _id. In the future, we can have modes - lines, bezier, and points.
mbedalvaro 40:3ba2b0ea9f33 88 BaseObject* begin(unsigned char _id); // This will add a new object with or without identifier _id. In the future, we can have modes - lines, bezier, and points.
mbedalvaro 40:3ba2b0ea9f33 89 void vertex(float x, float y, float z); // add a point, but do not RENDER it yet (no final projection computed)
mbedalvaro 40:3ba2b0ea9f33 90 void vertex(V3& _v3);
mbedalvaro 40:3ba2b0ea9f33 91 void end(); // (this is meaningless if there is no polygon or bezier mode... but even now it is good to have for clarity - and to look like opengl)
mbedalvaro 40:3ba2b0ea9f33 92
mbedalvaro 40:3ba2b0ea9f33 93 // Delete object from current scenembed_reset (in the future, let's use also strings as IDs):
mbedalvaro 40:3ba2b0ea9f33 94 void deleteObject(int _id);
mbedalvaro 40:3ba2b0ea9f33 95
mbedalvaro 40:3ba2b0ea9f33 96 // Rendering methods:
mbedalvaro 40:3ba2b0ea9f33 97 void drawObject(int _id) ;
mbedalvaro 40:3ba2b0ea9f33 98 void drawObject(BaseObject* ptr_object) ;
mbedalvaro 40:3ba2b0ea9f33 99 void drawScene();
mbedalvaro 40:3ba2b0ea9f33 100
mbedalvaro 40:3ba2b0ea9f33 101 // Sensing methods (query and process sensed data):
mbedalvaro 40:3ba2b0ea9f33 102 bool senseObject(int _id);
mbedalvaro 40:3ba2b0ea9f33 103 bool senseScene();
mbedalvaro 40:3ba2b0ea9f33 104
mbedalvaro 40:3ba2b0ea9f33 105 // ================================= OTHER auxiliary GLOBAL SCENE/OBJECT TRANSFORMATIONS ==========================================
mbedalvaro 40:3ba2b0ea9f33 106
mbedalvaro 40:3ba2b0ea9f33 107 void trasformObject(int _id) ;
mbedalvaro 47:199042980678 108 void translateObject(int _id, int xx, int yy, int zz=0);
mbedalvaro 47:199042980678 109 void rotateObjectX(int _id, float alpha);
mbedalvaro 47:199042980678 110 void rotateObjectY(int _id, float alpha);
mbedalvaro 47:199042980678 111 void rotateObjectZ(int _id, float alpha);
mbedalvaro 47:199042980678 112
mbedalvaro 40:3ba2b0ea9f33 113 void transformObject(BaseObject* ptr_object) ;
mbedalvaro 40:3ba2b0ea9f33 114 void transformScene();
mbedalvaro 40:3ba2b0ea9f33 115 void changeColorScene(unsigned char _color);
mbedalvaro 40:3ba2b0ea9f33 116
mbedalvaro 40:3ba2b0ea9f33 117 // ================================= OBJECT PRIMITIVES (this could be in a different file) ============================================================
mbedalvaro 40:3ba2b0ea9f33 118
mbedalvaro 40:3ba2b0ea9f33 119 // These are some primitive sensing-displaying objects (in the future, kind of UIButtons - would be nice if they could produce "events"...)
mbedalvaro 40:3ba2b0ea9f33 120 // Note that the only way (for now) to query if some object has been "touched" is through the knowledge of its "unique" ID... which acts as a handler. In fact,
mbedalvaro 40:3ba2b0ea9f33 121 // we can keep track of its pointer in the main program - and this means it will be exactly a handler...
mbedalvaro 40:3ba2b0ea9f33 122 void line(float x0, float y0, float z0, float x1, float y1, float z1, int npoints);
mbedalvaro 40:3ba2b0ea9f33 123 void line(V3& v0, V3& v1, int npoints);
mbedalvaro 40:3ba2b0ea9f33 124 void square(float sideSize, int npointsSide);
mbedalvaro 40:3ba2b0ea9f33 125 void rectangle(float sideSizeX, float sideSizeY, int interPointDistance);
mbedalvaro 40:3ba2b0ea9f33 126 void circle(float radius, int numpoints);
mbedalvaro 40:3ba2b0ea9f33 127 void cube(float sideSize, int npointsSide); // not really a cube...
mbedalvaro 40:3ba2b0ea9f33 128 void grid(int nx, int ny, int repeatpoint); // normalized (0-1)x(0-1)
mbedalvaro 40:3ba2b0ea9f33 129 void grid(float sizeX, float sizeY, int nx, int ny, int repeatpoint);
mbedalvaro 40:3ba2b0ea9f33 130 void gridCircles(int nx, int ny, float radius, int nbpointsCircle);
mbedalvaro 40:3ba2b0ea9f33 131 void gridCircles(float sizeX, float sizeY, int nx, int ny, float radius, int nbpointsCircle);
mbedalvaro 40:3ba2b0ea9f33 132
mbedalvaro 40:3ba2b0ea9f33 133 // LETTERS and STRINGS:
mbedalvaro 40:3ba2b0ea9f33 134 void letter3d(char _letter, float width, float height);
mbedalvaro 40:3ba2b0ea9f33 135 void string3d(string text, float fontSize, float spaceWidth);
mbedalvaro 40:3ba2b0ea9f33 136
mbedalvaro 40:3ba2b0ea9f33 137 // ================================= WRAPPERS TO LOAD OBJECTS FROM SYSTEM FILE ========================================================================
mbedalvaro 40:3ba2b0ea9f33 138 // ... to do
mbedalvaro 40:3ba2b0ea9f33 139 //void buildRawTrajectory(float * auxbuffer, int arraySize);
mbedalvaro 40:3ba2b0ea9f33 140
mbedalvaro 40:3ba2b0ea9f33 141 // ================================= WRAPPERS TO LOAD MATRICES FROM SYSTEM FILE =======================================================================
mbedalvaro 40:3ba2b0ea9f33 142 // ...to do
mbedalvaro 40:3ba2b0ea9f33 143
mbedalvaro 40:3ba2b0ea9f33 144 // ================================= WRAPPERS FOR MORE BASIC IO FUNCTIONS =================================
mbedalvaro 40:3ba2b0ea9f33 145 void scanSerial(unsigned short pointsPerLine);
mbedalvaro 40:3ba2b0ea9f33 146 void showLimitsMirrors(unsigned short pointsPerLine, unsigned short durationSecs);
mbedalvaro 40:3ba2b0ea9f33 147 void recomputeLookUpTable();
mbedalvaro 40:3ba2b0ea9f33 148 void startDisplay();
mbedalvaro 40:3ba2b0ea9f33 149 void stopDisplay();
mbedalvaro 43:1dd4cfc30788 150 void toggleDisplay();
mbedalvaro 40:3ba2b0ea9f33 151 void resumeDisplay();
mbedalvaro 40:3ba2b0ea9f33 152 // =============================== HARDWARE KNOBS: switches, potentiometers... =================================================
mbedalvaro 40:3ba2b0ea9f33 153 void hardwareKnobs();
mbedalvaro 40:3ba2b0ea9f33 154