Repository navigation
Expand file tree
/
Copy pathsample.cpp
More file actions
410 lines (338 loc) · 11.5 KB
/
Copy pathsample.cpp
File metadata and controls
410 lines (338 loc) · 11.5 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// 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>
#include <time.h>
/** The scene, which includes the lights and models. */
class Scene : public Model {
protected:
///////////////////////////////// TEXTURES ////////////////////////////////////
Texture2D texture;
///////////////////////////////// SHADERS /////////////////////////////////////
ShaderProgram shader;
ShaderProgram toonShader;
ShaderProgram schlickShader;
ShaderProgram textShader;
ShaderProgram spotLightShader;
//////////////////////////////// PROPERTIES ///////////////////////////////////
// Switches for spheres
BooleanProperty useTexture;
// Switch for showing/hiding reference unit sphere
BooleanProperty showReferenceUnitSphere;
// Lets you pick what shapes to use for the default model!
ChoiceProperty shapeChoice;
ChoiceProperty shaderChoice;
BooleanProperty useShader;
BooleanProperty useToonShader;
BooleanProperty useSchlickShader;
BooleanProperty useTextShader;
BooleanProperty useSpotLightShader;
// Some slider properties
RangeProperty rotateX, rotateY;
RangeProperty brightness;
RangeProperty N1;
RangeProperty N2;
// Diffuse color picker
RGBProperty diffuse;
// Scene lights
PointLight pointLight;
DirectionalLight directionalLight;
SpotLight spotLight;
// A robot arm model
//RobotArm robotArm;
MyModel myModel;
public:
ParticleSystem ps;
Cloth cloth;
/** Modeler calls this to get our particle system. */
ParticleSystem* getParticleSystem() {
return &ps;
}
// Cloth* getCloth() {
// return &cloth;
// }
RangeProperty sphereCenterX, sphereCenterY, sphereCenterZ;
/** Construct the scene */
Scene() :
// You have to call the parent class's constructor, tso 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),
toonShader("toonShader.vert", "toonShader.frag", NULL),
schlickShader("schlickShader.vert", "schlickShader.frag", NULL),
textShader("textShader.vert", "textShader.frag", NULL),
spotLightShader("spotLightShader.vert", "spotLightShader.frag", NULL),
// Call the constructors for the lights
spotLight("Spot Light", GL_LIGHT2, /**direction part**/ -5, 5, 5, /**diffuse part**/ 1.0, 0.0, 0.9,
/**specular part**/ 1.0, 0.5, 0.5, /**ambient part**/ .2f, 0.1, 0.1 /**attenuation part**/, 0.65, 0.33, 0.38,
/**spotX/Y/Z part**/ -7, 10, 8, /**spotlight angle**/ 5, 30 ),
pointLight("Point Light", GL_LIGHT1, /**direction part**/ 0.54, -0.54, -3.29, /**diffuse part**/ 0.666, 0.940, 1.0,
/**specular part**/ 1.0, 0.409, 0.550, /**ambient part**/ .740f, 0.246, 0.290 /**attenuation part**/, 0.4, 0.7, 0),
directionalLight("Directional Light", GL_LIGHT0, /**direction part**/ 5, 5, 5, /**diffuse part**/ 0.5f, 0.071f, 0.172f,
/**specular part**/ 0.289f, 0.75f, 0.0f )
// Now, call the constructors for each Property:
, useTexture("Use Checkered Texture", true),
showReferenceUnitSphere("Show Reference Unit Sphere", false),
shapeChoice("Model Shape:", "Sphere|Cube|Cylinder|Torus|Icosahedron|Teapot|Revolution|My Model", 0), //557 animator UI allows shapes + Model
shaderChoice("Shape To Use:", "None|Cartoon Shader|Schlick Shader|Texture Blinn Phong Shader|Spotlight Shader", 0),
useShader("Use My Shader", true),
useToonShader("Use Cartoon Shader", false),
useSchlickShader("Use Schlick Shader", false),
useTextShader("Use Texture Blinn Phong Shader", false),
useSpotLightShader("Use Spot Light Shader", false),
rotateX("Rotate Basic Shape X", -180, 180, 0, 1),
rotateY("Rotate Basic Shape Y", -180, 180, 0, 1),
brightness("Brightness", 0.0f, 1.0f, 1.0f, 0.1f),
N1("Schlick N1", 0.0f, 1.0f, 1.0f, 0.1f),
N2("Schlick N2", 0.0f, 1.0f, 1.0f, 0.1f),
diffuse("Diffuse Color", 1.0, 0.7, .4)
,
sphereCenterX("Collision Sphere Center (X)", -6.0, 6.0, 2.0, 0.1),
sphereCenterY("Collision Sphere Center (Y)", -6.0, 6.0, -2.0, 0.1),
sphereCenterZ("Collision Sphere Center (Z)", -6.0, 6.0, 2.0, 0.1)
{
// 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())
.add(spotLight.getProperties())
.add(myModel.getProperties())
;
//properties.add(robotArm.getProperties());
// Finally, add all the properties to this model's PropertyGroup.
properties.add(&useTexture)
;
properties.add(&showReferenceUnitSphere)
.add(&shapeChoice)
.add(&shaderChoice);
properties.add(&rotateX)
.add(&rotateY)
.add(&useShader)
.add(&useToonShader)
.add(&useSchlickShader)
.add(&useTextShader)
.add(&useSpotLightShader)
;
properties.add(&sphereCenterX)
.add(&sphereCenterY)
.add(&sphereCenterZ);
properties.add(&brightness)
.add(&N1)
.add(&N2)
.add(&diffuse)
;
}
/**
* 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();
toonShader.load();
schlickShader.load();
textShader.load();
spotLightShader.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 model. */
void drawModel() {
int divisions=16;
switch(ModelerDrawState::Instance()->m_quality)
{
case HIGH:
divisions = 64; break;
case MEDIUM:
divisions = 20; break;
case LOW:
divisions = 12; break;
case POOR:
divisions = 8; break;
}
// Remember our current modelview matrix, which is in world space.
glPushMatrix();
// Rotate around the X-axis
glRotatef(rotateX.getValue(), 1, 0, 0);
// Rotate around the Y-axis
glRotatef(rotateY.getValue(), 0, 1, 0);
// Draw some object at the origin.
switch (shapeChoice.getValue()) {
case 0: // sphere
// glRotatef(-90.f,1.f,0.f,0.f);
// glutSolidSphere(1.0, divisions, divisions);
// glRotatef(90.f,1.f,0.f,0.f);
break;
case 1: // box
glRotatef(-45.f,0.f,1.f,0.f);
glutSolidCube(2);
glRotatef(45.f,0.f,1.f,0.f);
break;
case 2: // cylinder
glRotatef(90.f,1.f,0.f,0.f);
drawCylinder(1.8,1.0,1.8);
glRotatef(-90.f,1.f,0.f,0.f);
break;
case 3: // torus
glutSolidTorus(.5,1,divisions,divisions);
break;
case 4: // icosahedron
glutSolidIcosahedron();
break;
case 5: // teapot
glutSolidTeapot(1);
break;
case 6:
drawRevolution(1);
break;
case 7:
// Draw the default sphere for collision
glPopMatrix();
// draw();
myModel.draw();
break;
}
// if (showReferenceUnitSphere.getValue() ) {
// // Now go 1 units in the +y direction,
// // and 1 units in the NEGATIVE x direction.
// glTranslated(-2, -1, 0);
// // Now go 1 units in the +y direction,
// // and 1 units in the NEGATIVE x direction.
// glRotatef(-90.f,1.f,0.f,0.f);
// drawSphere(.5);
// glRotatef(90.f,1.f,0.f,0.f);
// glPopMatrix();
// }
// Go back to "world space"
glPopMatrix();
// if (shapeChoice.getValue() != 7)
// {
// // Draw the floor. Here's a basic example of texture mapping.
// glBegin(GL_QUADS);
// glTexCoord2f(0, 1); // specify the texture coordinate
// glNormal3f(0, 1, 0); // specify the surface's normal at this vertex
// glVertex3f(-10, -2, -10); // both before its corresponding vertex
// glTexCoord2f(1, 1);
// glNormal3f(0, 1, 0);
// glVertex3f(10, -2, -10);
// glTexCoord2f(1, 0);
// glNormal3f(0, 1, 0);
// glVertex3f(10, -2, 10);
// glTexCoord2f(0, 0);
// glNormal3f(0, 1, 0);
// glVertex3f(-10, -2, 10);
// glEnd();
// }
}
/** Draw the scene. */
void draw() {
// The lights must be drawn FIRST, so the other scene elements
// can get lit!
pointLight.draw();
directionalLight.draw();
spotLight.draw();
//reset colors
setDiffuseColor(
diffuse.getRed(),
diffuse.getGreen(),
diffuse.getBlue());
setAmbientColor(0.2, 0.2, 0.2);
setSpecularColor(1,1,1);
setShininess(pow(2.71828183f, 3));
// Use the texture if desired.
if (useTexture.getValue()) {
texture.use();
} else {
glBindTexture(GL_TEXTURE_2D, 0);
}
// Use the shader if desired.
if (useShader.getValue()) {
// glUseProgram(0);
GLint brightnessVariableLocation;
GLint N1VariableLocation;
GLint N2VariableLocation;
switch (shaderChoice.getValue()) {
case 0: // None
shader.use();
brightnessVariableLocation = glGetUniformLocation( shader.getID(), "brightness" );
glUniform1f(brightnessVariableLocation, brightness.getValue());
break;
case 1: // Cartoon Shader
// useShader.setValue(false);
toonShader.use();
brightnessVariableLocation = glGetUniformLocation( toonShader.getID(), "brightness" );
glUniform1f(brightnessVariableLocation, brightness.getValue());
break;
case 2: // Schlick Shader
// shader.use();
schlickShader.use();
brightnessVariableLocation = glGetUniformLocation( schlickShader.getID(), "brightness");
N1VariableLocation = glGetUniformLocation( schlickShader.getID(), "N1");
N2VariableLocation = glGetUniformLocation( schlickShader.getID(), "N2");
glUniform1f(brightnessVariableLocation, brightness.getValue());
glUniform1f(N1VariableLocation, N1.getValue());
glUniform1f(N2VariableLocation, N2.getValue());
break;
case 3: // Texture Blinn Phong
glUseProgram(0);
break;
case 4:
spotLightShader.use();
brightnessVariableLocation = glGetUniformLocation(spotLightShader.getID(), "brightness");
glUniform1f(brightnessVariableLocation, brightness.getValue());
break;
}
// Call a class that draws our model.
drawModel();
// Stop applying shaders to objects.
// This if-statement makes sure that glUseProgram is not a null
// function pointer (which it will be if GLEW couldn't initialize).
if (glUseProgram) {
glUseProgram(0);
}
// Stop applying textures to objects
glBindTexture(GL_TEXTURE_2D, 0);
}
}
};
/**
* 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();
}