-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbubbleTools.cpp
230 lines (176 loc) · 7.03 KB
/
bubbleTools.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "bubbleTools.h"
#include <triDVector.h>
#include <resizeBuff.h>
colorObj backColor;
bubbleRender* renderMan; // triDRender is the engine that reads the model to do the 2D drawing.
triDRotation new_angle; // This sets the oreantation of the model's drawing on the screen.
triDRotation my_angle; // Saves the last location.
bitmap bubbleMap(TARGET_DIA,TARGET_DIA); // A bitmap for offscreen drawing.
double radToDeg(double x) { return x*(180.0/PI); } // Humans like degrees. These two do the traslantions.
double degToRad(double x) { return x*(PI/180.0); }
triDTriangle* createCircleThing(int slices,double coneAngle,double radius) {
double slice;
double twoPi;
triDVector vectI;
triDVector vectA;
triDTriangle* facets;
triDRotation rotation;
facets = NULL;
if (resizeBuff(sizeof(triDTriangle)*slices,(byte**)&facets)) {
twoPi = 2*M_PI;
slice = twoPi/(float)slices;
Serial.println(slice,5);
vectI.setVector(0,0,radius);
rotation = setRotation(0,coneAngle,0);
vectI.rotateVect(&rotation);
for (int i=0;i<slices;i++) {
facets[i].corners[0].x = 0; //
facets[i].corners[0].y = 0;
facets[i].corners[0].z = radius;
vectA.setVector(&vectI); // Initial vector.
rotation = setRotation(0,0,i*slice); // How far we'll need to shift the vector..
vectA.rotateVect(&rotation); // Add this much to the rotation.
facets[i].corners[2].x = vectA.getX();
facets[i].corners[2].y = vectA.getY();
facets[i].corners[2].z = vectA.getZ();
rotation = setRotation(0,0,slice); // We've aleready added i*slice. We need one more slice.
vectA.rotateVect(&rotation);
facets[i].corners[1].x = vectA.getX();
facets[i].corners[1].y = vectA.getY();
facets[i].corners[1].z = vectA.getZ();
}
}
return facets;
}
void setupModel(void) {
triDPoint location; // This sets the location of the model's drawing on the screen.
triDTriangle* theModel = createCircleThing(SLICES,degToRad(15),RADIUS-2); // This hand crafts the list of triangles.
bubble* arrayModel = new bubble(theModel,SLICES); // Use the hand crafted list to make an array model.
renderMan = new bubbleRender(BOUND_X,BOUND_Y,BOUND_DIA,BOUND_DIA); // Create the render engine with its screen location. (x,y,w,h)
renderMan->begin(arrayModel); // Slot the model into the render enegine.
renderMan->setObjScale(1); // Set the drawing scale.
new_angle.xRad = 0; // The angle around x axis.
new_angle.yRad = 0; // The angle around y axis.
new_angle.zRad = 0; // The angle around z axis.
renderMan->setObjAngle(&new_angle); // Set the orentation.
my_angle = new_angle;
location.x = 99; // x position offset.
location.y = 99; // y position offset.
location.z = 0; // z position offset.
renderMan->setObjLoc(&location); // Set the location offsets in..
viewList.addObj(renderMan); // Add this render object into the screen list. (causing it to draw itself)
}
bubble::bubble(triDTriangle* inList,long numItems)
: arrayList(inList,numItems) {
centerPt.x = 0;
centerPt.y = 0;
centerPt.z = 0;
haveCenter = true;
}
bubble::~bubble(void) { }
//****************************************************************************************
// bubbleRender:
//
// A It renders the model but also deals with erasing it and drawing it.
//****************************************************************************************
bubbleRender::bubbleRender(int inX,int inY,int inWidth,int inHeight)
: triDRender(inX,inY,inWidth,inHeight) { clearPtList(); }
bubbleRender::~bubbleRender(void) { }
void bubbleRender::clearPtList(void) {
for (int i=0;i<SLICES*3;i++) {
pt[i].x = -1;
pt[i].y = -1;
}
}
void bubbleRender::addTriangle(viewFacet* aFacet) {
int i;
bool done;
for (byte j=1;j<3;j++) {
i = 0;
done = false;
while(!done) {
if (aFacet->corner[j].x==pt[i].x && aFacet->corner[j].y==pt[i].y) {
done = true;
} else if (pt[i].x == -1) {
pt[i].x = aFacet->corner[j].x;
pt[i].y = aFacet->corner[j].y;
done = true;
} else {
i++;
}
}
}
}
void bubbleRender::erasePoints(void) {
int i;
bool done;
i = 0;
done = false;
while(!done) {
if (pt[i].x == -1) {
done = true;
} else {
screen->drawPixel(pt[i].x,pt[i].y,&backColor);
i++;
}
}
}
// This is where it all gets put together. Can we actually draw this thing?
void bubbleRender::drawSelf(void) {
viewFacet aFacet;
colorObj aColor;
bool done;
if (!init) {
screen->drawRect(this,&red);
return;
}
if (ourModel->openList()) {
theGrid->setNeedRefresh();
if (setupChange) {
if (createList()) {
setupChange = false;
}
}
resetList();
erasePoints();
clearPtList();
done = false;
do {
aFacet = getNextViewFacet();
if (aFacet.normalVect.isNullVector()) {
done = true;
} else {
offset2DFacet(&aFacet,x,y);
addTriangle(&aFacet);
//screen->drawPixel(aFacet.corner[0].x,aFacet.corner[0].y,&cyan);
screen->drawPixel(aFacet.corner[1].x,aFacet.corner[1].y,&cyan);
screen->drawPixel(aFacet.corner[2].x,aFacet.corner[2].y,&cyan);
}
} while(!done);
ourModel->closeList();
}
}
//****************************************************************************************
// grid:
//
// The cross hair thing.
//****************************************************************************************
grid::grid(int centerX,int centerY)
:drawObj(BOUND_X,BOUND_Y,BOUND_DIA,BOUND_DIA) {
center.x = centerX;
center.y = centerY;
//gridColor.setColor(LC_CYAN);
gridColor.setColor(&red);
gridColor.blend(&black,50);
}
grid::~grid(void) { }
void grid::setColor(colorObj* inColor) { gridColor.setColor(inColor); }
void grid::drawSelf(void) {
screen->drawCircle(BOUND_X,BOUND_Y,BOUND_DIA,&gridColor);
screen->drawCircle(TARGET_X,TARGET_Y,TARGET_DIA,&gridColor);
screen->drawHLine(BOUND_X,BOUND_CY,(BOUND_DIA-TARGET_DIA)/2,&gridColor);
screen->drawHLine(BOUND_CX+(TARGET_DIA/2),BOUND_CY,(BOUND_DIA-TARGET_DIA)/2,&gridColor);
screen->drawVLine(BOUND_CX,BOUND_Y,(BOUND_DIA-TARGET_DIA)/2,&gridColor);
screen->drawVLine(BOUND_CX,BOUND_CY+(TARGET_DIA/2),(BOUND_DIA-TARGET_DIA)/2,&gridColor);
}
grid* theGrid;