-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_robotarm.cpp
291 lines (232 loc) · 7.51 KB
/
_robotarm.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// The sample robotarm model. You should build a file
// very similar to this for when you make your model.
#pragma warning (disable : 4305)
#pragma warning (disable : 4244)
#pragma warning(disable : 4786)
#include "modelerview.h"
#include "modelerapp.h"
#include "modelerdraw.h"
#include "particleSystem.h"
#include <FL/gl.h>
#include <stdlib.h>
#define M_DEFAULT 2.0f
#define M_OFFSET 3.0f
#define P_OFFSET 0.3f
#define MAX_VEL 200
#define MIN_STEP 0.1
// This is a list of the controls for the RobotArm
// We'll use these constants to access the values
// of the controls from the user interface.
enum RobotArmControls
{
BASE_ROTATION=0, LOWER_TILT, UPPER_TILT, CLAW_ROTATION,
BASE_LENGTH, LOWER_LENGTH, UPPER_LENGTH, PARTICLE_COUNT, NUMCONTROLS,
};
void ground(float h);
void base(float h);
void rotation_base(float h);
void lower_arm(float h);
void upper_arm(float h);
void claw(float h);
void y_box(float h);
// To make a RobotArm, we inherit off of ModelerView
class RobotArm : public ModelerView
{
public:
RobotArm(int x, int y, int w, int h, char *label)
: ModelerView(x,y,w,h,label) {}
virtual void draw();
};
// We need to make a creator function, mostly because of
// nasty API stuff that we'd rather stay away from.
ModelerView* createRobotArm(int x, int y, int w, int h, char *label)
{
return new RobotArm(x,y,w,h,label);
}
// We'll be getting the instance of the application a lot;
// might as well have it as a macro.
#define VAL(x) (ModelerApplication::Instance()->GetControlValue(x))
// We are going to override (is that the right word?) the draw()
// method of ModelerView to draw out RobotArm
void RobotArm::draw()
{
/* pick up the slider values */
float theta = VAL( BASE_ROTATION );
float phi = VAL( LOWER_TILT );
float psi = VAL( UPPER_TILT );
float cr = VAL( CLAW_ROTATION );
float h1 = VAL( BASE_LENGTH );
float h2 = VAL( LOWER_LENGTH );
float h3 = VAL( UPPER_LENGTH );
float pc = VAL( PARTICLE_COUNT );
// This call takes care of a lot of the nasty projection
// matrix stuff
ModelerView::draw();
static GLfloat lmodel_ambient[] = {0.4,0.4,0.4,1.0};
// define the model
ground(-0.2);
base(0.8);
glTranslatef( 0.0, 0.8, 0.0 ); // move to the top of the base
glRotatef( theta, 0.0, 1.0, 0.0 ); // turn the whole assembly around the y-axis.
rotation_base(h1); // draw the rotation base
glTranslatef( 0.0, h1, 0.0 ); // move to the top of the base
glRotatef( phi, 0.0, 0.0, 1.0 ); // rotate around the z-axis for the lower arm
glTranslatef( -0.1, 0.0, 0.4 );
lower_arm(h2); // draw the lower arm
glTranslatef( 0.0, h2, 0.0 ); // move to the top of the lower arm
glRotatef( psi, 0.0, 0.0, 1.0 ); // rotate around z-axis for the upper arm
upper_arm(h3); // draw the upper arm
glTranslatef( 0.0, h3, 0.0 );
glRotatef( cr, 0.0, 0.0, 1.0 );
claw(1.0);
//*** DON'T FORGET TO PUT THIS IN YOUR OWN CODE **/
endDraw();
}
void ground(float h)
{
glDisable(GL_LIGHTING);
glColor3f(0.65,0.45,0.2);
glPushMatrix();
glScalef(30,0,30);
y_box(h);
glPopMatrix();
glEnable(GL_LIGHTING);
}
void base(float h) {
setDiffuseColor( 0.25, 0.25, 0.25 );
setAmbientColor( 0.25, 0.25, 0.25 );
glPushMatrix();
glPushMatrix();
glTranslatef(1.0, h / 2.0, 0.75);
drawCylinder(0.25, h / 2.0, h / 2.0);
glPopMatrix();
glPushMatrix();
glTranslatef(1.0, h / 2.0, -1.0);
drawCylinder(0.25, h / 2.0, h / 2.0);
glPopMatrix();
glPushMatrix();
glTranslatef(-1.0, h / 2.0, 0.75);
drawCylinder(0.25, h / 2.0, h / 2.0);
glPopMatrix();
glPushMatrix();
glTranslatef(-1.0, h / 2.0, -1.0);
drawCylinder(0.25, h / 2.0, h / 2.0);
glPopMatrix();
glScalef(4.0f, h, 4.0f);
y_box(1.0f);
glPopMatrix();
}
void rotation_base(float h) {
setDiffuseColor( 0.85, 0.75, 0.25 );
setAmbientColor( 0.95, 0.75, 0.25 );
glPushMatrix();
glPushMatrix();
glScalef(4.0, h, 4.0);
y_box(1.0f); // the rotation base
glPopMatrix();
setDiffuseColor( 0.15, 0.15, 0.65 );
setAmbientColor( 0.15, 0.15, 0.65 );
glPushMatrix();
glTranslatef(-0.5, h, -0.6);
glScalef(2.0, h, 1.6);
y_box(1.0f); // the console
glPopMatrix();
setDiffuseColor( 0.65, 0.65, 0.65 );
setAmbientColor( 0.65, 0.65, 0.65 );
glPushMatrix();
glTranslatef( 0.5, h, 0.6 );
glRotatef( -90.0, 1.0, 0.0, 0.0 );
drawCylinder( h, 0.05, 0.05 ); // the pipe
glPopMatrix();
glPopMatrix();
}
void lower_arm(float h) { // draw the lower arm
setDiffuseColor( 0.85, 0.75, 0.25 );
setAmbientColor( 0.95, 0.75, 0.25 );
y_box(h);
}
void upper_arm(float h) { // draw the upper arm
setDiffuseColor( 0.85, 0.75, 0.25 );
setAmbientColor( 0.95, 0.75, 0.25 );
glPushMatrix();
glScalef( 1.0, 1.0, 0.7 );
y_box(h);
glPopMatrix();
}
void claw(float h) {
setDiffuseColor( 0.25, 0.25, 0.85 );
setAmbientColor( 0.25, 0.25, 0.85 );
glBegin( GL_TRIANGLES );
glNormal3d( 0.0, 0.0, 1.0); // +z side
glVertex3d( 0.5, 0.0, 0.5);
glVertex3d(-0.5, 0.0, 0.5);
glVertex3d( 0.5, h, 0.5);
glNormal3d( 0.0, 0.0, -1.0); // -z side
glVertex3d( 0.5, 0.0, -0.5);
glVertex3d(-0.5, 0.0, -0.5);
glVertex3d( 0.5, h, -0.5);
glEnd();
glBegin( GL_QUADS );
glNormal3d( 1.0, 0.0, 0.0); // +x side
glVertex3d( 0.5, 0.0,-0.5);
glVertex3d( 0.5, 0.0, 0.5);
glVertex3d( 0.5, h, 0.5);
glVertex3d( 0.5, h,-0.5);
glNormal3d( 0.0,-1.0, 0.0); // -y side
glVertex3d( 0.5, 0.0, 0.5);
glVertex3d( 0.5, 0.0,-0.5);
glVertex3d(-0.5, 0.0,-0.5);
glVertex3d(-0.5, 0.0, 0.5);
glEnd();
}
void y_box(float h) {
glBegin( GL_QUADS );
glNormal3d( 1.0 ,0.0, 0.0); // +x side
glVertex3d( 0.25,0.0, 0.25);
glVertex3d( 0.25,0.0,-0.25);
glVertex3d( 0.25, h,-0.25);
glVertex3d( 0.25, h, 0.25);
glNormal3d( 0.0 ,0.0, -1.0); // -z side
glVertex3d( 0.25,0.0,-0.25);
glVertex3d(-0.25,0.0,-0.25);
glVertex3d(-0.25, h,-0.25);
glVertex3d( 0.25, h,-0.25);
glNormal3d(-1.0, 0.0, 0.0); // -x side
glVertex3d(-0.25,0.0,-0.25);
glVertex3d(-0.25,0.0, 0.25);
glVertex3d(-0.25, h, 0.25);
glVertex3d(-0.25, h,-0.25);
glNormal3d( 0.0, 0.0, 1.0); // +z side
glVertex3d(-0.25,0.0, 0.25);
glVertex3d( 0.25,0.0, 0.25);
glVertex3d( 0.25, h, 0.25);
glVertex3d(-0.25, h, 0.25);
glNormal3d( 0.0, 1.0, 0.0); // top (+y)
glVertex3d( 0.25, h, 0.25);
glVertex3d( 0.25, h,-0.25);
glVertex3d(-0.25, h,-0.25);
glVertex3d(-0.25, h, 0.25);
glNormal3d( 0.0,-1.0, 0.0); // bottom (-y)
glVertex3d( 0.25,0.0, 0.25);
glVertex3d(-0.25,0.0, 0.25);
glVertex3d(-0.25,0.0,-0.25);
glVertex3d( 0.25,0.0,-0.25);
glEnd();
}
int main()
{
ModelerControl controls[NUMCONTROLS ];
controls[BASE_ROTATION] = ModelerControl("base rotation (theta)", -180.0, 180.0, 0.1, 0.0 );
controls[LOWER_TILT] = ModelerControl("lower arm tilt (phi)", 15.0, 95.0, 0.1, 55.0 );
controls[UPPER_TILT] = ModelerControl("upper arm tilt (psi)", 0.0, 135.0, 0.1, 30.0 );
controls[CLAW_ROTATION] = ModelerControl("claw rotation (cr)", -30.0, 180.0, 0.1, 0.0 );
controls[BASE_LENGTH] = ModelerControl("base height (h1)", 0.5, 10.0, 0.1, 0.8 );
controls[LOWER_LENGTH] = ModelerControl("lower arm length (h2)", 1, 10.0, 0.1, 3.0 );
controls[UPPER_LENGTH] = ModelerControl("upper arm length (h3)", 1, 10.0, 0.1, 2.5 );
controls[PARTICLE_COUNT] = ModelerControl("particle count (pc)", 0.0, 5.0, 0.1, 5.0 );
// You should create a ParticleSystem object ps here and then
// call ModelerApplication::Instance()->SetParticleSystem(ps)
// to hook it up to the animator interface.
ModelerApplication::Instance()->Init(&createRobotArm, controls, NUMCONTROLS);
return ModelerApplication::Instance()->Run();
}