-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.cpp
More file actions
364 lines (297 loc) · 9.39 KB
/
sample.cpp
File metadata and controls
364 lines (297 loc) · 9.39 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// The sample box model. You should build a file
// very similar to this for when you make your model in
// order to plug in to the animator project.
#pragma warning (disable : 4305)
#pragma warning (disable : 4244)
#pragma warning (disable : 4786)
#pragma warning (disable : 4312)
#include "modelerview.h"
#include "modelerdraw.h"
#include "modelerui.h"
#include "vault.h"
#include "FL/glut.h"
#include "model.h"
#include "cameras.h"
#include <cmath>
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);
class RobotArm : public Model
{
RangeProperty baseRotation, lowerTilt, upperTilt, clawRotation, baseLength,
lowerLength, upperLength;
public:
RobotArm() :
Model("Robot Arm"),
baseRotation("base rotation (theta)", -180, 180, 0,0.1),
lowerTilt("lower arm tilt (phi)", 15, 95, 55, .1),
upperTilt("upper arm tilt (psi)", 0, 135, 30, .1),
clawRotation("claw rotation (cr)", -30, 180, 0, .1),
baseLength("base height (h1)", .5, 10, .8, .1),
lowerLength("lower arm length (h2)", 1, 10, 3, .1),
upperLength("upper arm length (h3)", 1, 10, 2.5, .1)
{
properties.add(&baseRotation)
.add(&lowerTilt)
.add(&upperTilt)
.add(&clawRotation)
.add(&baseLength)
.add(&lowerLength)
.add(&upperLength);
}
virtual void draw();
};
// 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 = baseRotation.getValue();
float phi = lowerTilt.getValue();
float psi = upperTilt.getValue();
float cr = clawRotation.getValue();
float h1 = baseLength.getValue();
float h2 = lowerLength.getValue();
float h3 = upperLength.getValue();
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);
}
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();
}
/** The scene, which includes the lights and models. */
class Scene : public Model {
protected:
///////////////////////////////// TEXTURES ////////////////////////////////////
Texture2D texture;
///////////////////////////////// SHADERS /////////////////////////////////////
ShaderProgram shader;
//////////////////////////////// PROPERTIES ///////////////////////////////////
// Scene lights
PointLight pointLight;
DirectionalLight directionalLight;
// A robot arm model
RobotArm robotArm;
public:
ParticleSystem ps;
/** Modeler calls this to get our particle system. */
ParticleSystem* getParticleSystem() {
return &ps;
}
/** Construct the scene */
Scene() :
// You have to call the parent class's constructor, to provide a
// name for the model.
Model("Scene"),
// Construct textures and shaders.
// They won't be loaded until the model is drawn for the first time.
texture("checkers.png"),
shader("shader.vert", "shader.frag", NULL),
// Call the constructors for the lights
pointLight("Point Light", GL_LIGHT1, /**direction part**/ -5, 5, 5, /**diffuse part**/ 1.0, 0.5, 0.5,
/**specular part**/ 1.0, 0.5, 0.5, /**ambient part**/ .2f, 0.1, 0.1 /**attenuation part**/, 0.4, 0.7, 0),
directionalLight("Directional Light", GL_LIGHT0, /**direction part**/ 5, 5, 5, /**diffuse part**/ 0.0f, 0.5, 0.5f,
/**specular part**/ 0.0f, 0.5f, 0.5f )
// Now, call the constructors for each Property:
{
// If you have child Models, like the MobileLight model from model.h,
// you can add their property groups, and they will appear in the list
// in the top left corner of Modeler, under this model's entry:
properties.add(pointLight.getProperties())
.add(directionalLight.getProperties());
properties.add(robotArm.getProperties());
// Finally, add all the properties to this model's PropertyGroup.
}
/**
* Modeler calls this method once an OpenGL context becomes available,
* (which happens before the first time draw() is called), and whenever
* you click the "Reload Textures and Shaders" menu option.
*
* Make sure you call the load() methods of each of your textures and
* shaders here.
*/
void load() {
texture.load();
shader.load();
}
/**
* Modeler calls this method many times a second when the Animate
* checkbox is checked.
*/
void tick() {
// You can put code here to increment animation counters for
// extra credit.
}
/** Draw the scene. */
void draw() {
// The lights must be drawn FIRST, so the other scene elements
// can get lit!
pointLight.draw();
directionalLight.draw();
// Draw robot arm
robotArm.draw();
}
};
/**
* The program starts here.
*/
int main()
{
// Tell the FLTK library to let us use OpenGL
Fl::gl_visual(FL_RGB);
init_load_curve_file();
// Instantiate Modeler
ModelerUserInterface ui;
// Give Modeler your scene.
// Modeler will free this pointer when it exits.
ui.setModel(new Scene());
// Run the application
return ui.run();
}