sources:
attribute.cpp (1.1k)
attribute.h (1.5k)
cgapplication.cpp (4.1k)
cgapplication.h (4.8k)
cgrobot.cpp (7.9k)
cgrobot.h (716 bytes)
node.cpp (2.4k)
node.h (2.3k)
shape.cpp (3.2k)
shape.h (966 bytes)
transformation.cpp (2.9k)
transformation.h (2.4k)
vector.cpp (1.4k)
vector.h (5.3k)


website:
more info here


screenshot:
studies/grafik/Computergrafik-Code8/cgrobot.cpp
download file

  1 // Computergraphik I
  2 // Prof. Dr. Juergen Doellner
  3 // Sommersemester 2001
  4 //
  5 // Rahmenprogramm fuer Aufgabenzettel 8
  6
  7 #include "cgrobot.h"
  8
  9 CGRobot::CGRobot()
 10     : pause_(false), constructed_(false), cameraBezier_(true), rootNode_(0) {
 11
 12     // der komplette Szenengraph enthält nur exakt EINE Box
 13     Box* box = new Box(Vector(0,0,0), Vector(1,1,1));
 14     LeafNode* leafBox = new LeafNode(box);
 15
 16     // ein Finger modellieren
 17     fingerRotation_ = new Rotation(-1, Vector(0,0,1));
 18     GroupNode* finger = GroupNode::shareNode(leafBox, 8, new Translation(Vector(1,0,0)),
 19                                    fingerRotation_)
;
 20
 21     // Hand erstellen
 22     GroupNode* hand = GroupNode::shareNode(finger, 5, new Translation(Vector(0,0,1.2)));
 23     hand->setAttribute(0, new Translation(Vector(1,0.1,3)));
 24     hand->setAttribute(1, new Rotation(-90, Vector(0,1,0)));
 25     hand->setAttribute(2, new Scaling(Vector(1/5.8, 1/5.8, 1/5.8)));
 26     hand->setAttribute(3, new Color(0,0,1));
 27
 28     // Arm
 29     GroupNode* oberArm = new GroupNode;
 30     oberArm->setAttribute(0, new Scaling(Vector(1,0.3,3)));
 31     oberArm->setChildNode(0, leafBox);
 32
 33     armRotation_ = new Rotation(45, Vector(1,0,0));
 34     GroupNode* arm = new GroupNode;
 35     arm->setAttribute(0, armRotation_);
 36     arm->setAttribute(1, new Translation(Vector(0,1,0)));
 37     arm->setAttribute(2, new Scaling(Vector(0.6, 0.6, 0.6)));
 38     arm->setChildNode(0, oberArm);
 39     arm->setChildNode(1, hand);
 40
 41     // beide Arme
 42     GroupNode* arme = GroupNode::shareNode(arm, 2, new Translation(Vector(1.6,0,0)));
 43     arme->setAttribute(0, new Translation(Vector(-1.1,0,-1)));
 44     arme->setAttribute(1, new Color(0, 1, 1));
 45
 46     // Sensoren
 47     GroupNode* sensor = new GroupNode;
 48     sensor->setAttribute(0, new Scaling(Vector(0.2, 0.2, 0.1)));
 49     sensor->setChildNode(0, leafBox);
 50
 51     sensorColor_ = new Color(1,0,0);
 52
 53     GroupNode* sensoren = GroupNode::shareNode(sensor, 2, new Translation(Vector(0.75,0,0)));
 54     sensoren->setAttribute(0, new Translation(Vector(0,1,0.5)));
 55     sensoren->setAttribute(1, sensorColor_);
 56
 57     // Gesicht
 58     GroupNode* auge = new GroupNode;
 59     auge->setAttribute(0, new Scaling(Vector(0.2, 0.2, 0.1)));
 60     auge->setChildNode(0, leafBox);
 61     GroupNode* augen = GroupNode::shareNode(auge, 2, new Translation(Vector(0.5,0,0)));
 62     augen->setAttribute(0, new Translation(Vector(0.1,0.7,1)));
 63     augen->setAttribute(1, new Color(0,0,0));
 64    
 65     GroupNode* mund = new GroupNode;
 66     mund->setAttribute(0, new Translation(Vector(0.1,0.2,1)));
 67     mund->setAttribute(1, new Scaling(Vector(0.8, 0.1, 0.1)));
 68     mund->setAttribute(2, new Color(1,0,0));
 69     mund->setChildNode(0, leafBox);
 70
 71     GroupNode* nase = new GroupNode;
 72     nase->setAttribute(0, new Translation(Vector(0.4,0.5,1)));
 73     nase->setAttribute(1, new Scaling(Vector(0.2, 0.2, 0.2)));
 74     nase->setAttribute(2, new Color(1,1,0));
 75     nase->setChildNode(0, leafBox);
 76
 77
 78     // Kopf
 79     GroupNode* kopf = new GroupNode;
 80     kopf->setChildNode(0, leafBox);
 81     kopf->setChildNode(1, sensoren);
 82     kopf->setChildNode(2, augen);
 83     kopf->setChildNode(3, mund);
 84     kopf->setChildNode(4, nase);
 85     kopf->setAttribute(0, new Translation(Vector(-0.35,1,-0.35)));
 86     kopf->setAttribute(1, new Scaling(Vector(0.7,0.7,0.7)));
 87     kopf->setAttribute(2, new Color(1,1,1));
 88
 89     // Standfläche
 90     GroupNode* boden = new GroupNode;
 91     boden->setChildNode(0, leafBox);
 92     boden->setAttribute(0, new Translation(Vector(-1,-3,-1)));
 93     boden->setAttribute(1, new Scaling(Vector(2, 0.1, 2)));
 94     boden->setAttribute(2, new Color(0,1,1));
 95    
 96     // Fuss
 97     GroupNode* fuss = new GroupNode;
 98     fuss->setChildNode(0, leafBox);
 99     fuss->setAttribute(0, new Translation(Vector(-0.35,-3,-0.35)));
100     fuss->setAttribute(1, new Scaling(Vector(0.7, 2, 0.7)));
101     fuss->setAttribute(2, new Color(0,0,1));
102
103     // Rumpf
104     GroupNode* rumpf = new GroupNode;
105     rumpf->setChildNode(0, leafBox);
106     rumpf->setAttribute(0, new Scaling(Vector(1,2,1)));
107     rumpf->setAttribute(1, new Translation(Vector(-0.5,-0.5,-0.5)));
108     rumpf->setAttribute(2, new Color(0.5,0.5,0.5));
109
110     // Roboter zusammensetzen
111     camera_ = new Camera(Vector(-2,-2,3));
112     GroupNode* robot = new GroupNode;
113     robot->setAttribute(0, camera_);
114     robot->setChildNode(0, arme);
115     robot->setChildNode(1, kopf);
116     robot->setChildNode(2, fuss);
117     robot->setChildNode(3, boden);
118     robot->setChildNode(4, rumpf);
119
120     rootNode_ = robot;
121
122     // Szene fertig gebaut, onTimer darf ab jetzt eingreifen
123     constructed_ = true;
124 }
125
126
127
128 void CGRobot::onInit() {
129     glClearColor(0, 0, 0, 1);
130     glEnable(GL_DEPTH_TEST);
131
132     // Setze die Beleuchtungseinstellungen:
133     static const float specular[]  = { 1.0, 1.0, 1.0, 1.0 };
134     static const float shininess[] = { 50.0 };
135     glMaterialfv(GL_FRONT, GL_SPECULAR,  specular);
136     glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
137
138     static const float lightPosition[] = { 1.5, 1.5, -3.5, 0.0 };
139     glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
140
141     glShadeModel(GL_SMOOTH);
142     glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
143
144     glEnable(GL_LIGHTING);
145     glEnable(GL_LIGHT0);
146     glEnable(GL_COLOR_MATERIAL);
147     glEnable(GL_NORMALIZE);
148 }
149
150 void CGRobot::onDraw() {
151     // Loesche den Farb-Speicher und den Speicher
152     // fuer den Tiefen-Test!
153     glClearColor(0.8, 0.8, 0.8, 0.0);
154     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
155
156     /**** überschrieben durch Camera !!! ****/
157    
158     // Wir sind 3 Einheiten vom Nullpunkt entfernt (auf
159     // der z-Achse) und schauen direkt in den Nullpunkt.
160     // Die y-Achse soll nach oben zeigen:
161 /* glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0, 0, 3, // Position 0, 0, 0, // auf diesen Punkt wird geschaut 0, 1, 0); // Up-Vektor zeigt nach oben */   
162
163     glMatrixMode(GL_PROJECTION);
164     glLoadIdentity();
165     glFrustum(-1.5, 1.5, -1.5, 1.5, 1, 6);
166
167     // Zeichne den Szene-Graphen
168     if(rootNode_) { rootNode_->traverse(); }
169
170     // Nicht vergessen!!!
171     swapBuffers();
172 }
173
174 void CGRobot::onSize(unsigned int newWidth, unsigned int newHeight) {
175     glViewport(0, 0, newWidth - 1, newHeight - 1);
176 }
177
178 void CGRobot::onKey(unsigned char key) {
179     const unsigned char ESC   = 27;
180     const unsigned char SPACE = ' ';
181
182     switch (key)
183     {
184     case SPACE: pause_ = !pause_;
185                 break;
186     case 'c':
187     case 'C':    cameraBezier_ = !cameraBezier_;
188                 break;
189
190     case ESC:   exit(0);
191     }
192 }
193
194 void CGRobot::onTimer() {
195     if (!constructed_)
196         return;
197
198     static int nTimerTicks = 0;
199
200     // durchgängiger Timer
201     nTimerTicks++;
202
203     // Bewegung
204     if(!pause_)
205     {
206         // Kamera bewegen
207         if (cameraBezier_)
208         {
209             // auf Bezierkurve
210             const int bezierPoints = 4;
211             Vector v[bezierPoints] = { Vector(-2,-2, 3),
212                                        Vector( 5, 2, 7),
213                                        Vector( 3, 5,-5),
214                                        Vector(-2,-2,-2) };
215
216             static double t = 0.01;
217             static double incT = 0.01;
218
219             if (t<0.01 || t>0.99)
220                 incT = -incT;
221             t += incT;
222             camera_->setFrom(bezier(v,bezierPoints,t));
223         }
224         else         {
225             // auf vordefinierter Bahn
226             double angle = nTimerTicks*3.1415926/180.0;
227             camera_->setFrom(Vector(-3*sin(angle), 3*sin(angle), 3*cos(angle)));
228         }
229        
230         // Finger öffnen/schliessen
231         static double fingerAngleInc = -1;
232         double angle = fingerRotation_->getAngle();
233         if (angle == 340 || angle == 0)
234             fingerAngleInc = -fingerAngleInc;
235         fingerRotation_->setAngle(angle+fingerAngleInc);
236
237         // Arme bewegen
238         static double armAngleInc = -1;
239         angle = armRotation_->getAngle();
240         if (angle == 80 || angle == 30)
241             armAngleInc = -armAngleInc;
242         armRotation_->setAngle(angle+armAngleInc);
243     }
244
245     // blinken
246     delete sensorColor_;
247     if (nTimerTicks % 2 == 0)
248         sensorColor_ = new Color(1,0,0);
249     else         sensorColor_ = new Color(1,0.8,0.8);
250
251     needsRedraw();
252 }
253
254 int main(int argc, char* argv[]) {
255     cout << "SPACE toggles rotation" << endl          << "ESC quits the program" << endl;
256
257     CGRobot CGRobot;
258     CGRobot.start(argc, argv, "CGRobot, Stephan Brumme, 702544",
259                   CGApplication::ColorBuffer                 | CGApplication::DepthBuffer                 | CGApplication::DoubleBuffer,
260                 25)
;
261     return 0;
262 }
263
264