sources:


website:
more info here


screenshot:
studies/grafik2/Computergrafik-Code4/Aufgabe11/material.cpp
download file

  1 //
  2 // Computergraphik II
  3 // Prof. Dr. Juergen Doellner
  4 // Wintersemester 2001/02
  5 //
  6 // Rahmenprogramm zu Aufgabenzettel 4
  7 //
  8
  9 #include "material.h"
 10
 11 CGMaterial::CGMaterial() {
 12     run_ = false;
 13     zoom_ = 0.8;
 14 }
 15
 16 CGMaterial::~CGMaterial( ) {   
 17 }
 18
 19 void CGMaterial::setLight(int x, int y) {
 20     // Hier werden die Lichtquellen definiert
 21 }
 22
 23 void CGMaterial::setMaterial(int x, int y) {
 24     // Hier werden die Materialeigenschaften festgelegt
 25
 26     // ambient
 27     float ambient[4];
 28     ambient[0] = 0.5;
 29     ambient[1] = ambient[2] = ambient[3] = 0;
 30     glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
 31
 32     // diffuse
 33     float diffuse[4];
 34     diffuse[0] = x/(XWindows-1.0);
 35     diffuse[1] = diffuse[2] = diffuse[3] = 0;
 36     glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
 37
 38     // specular
 39     static const float specular[]  = { 0.5, 0.5, 0.5, 0.5 };
 40     glMaterialfv(GL_FRONT, GL_SPECULAR,  specular);
 41     glMaterialf(GL_FRONT, GL_SHININESS, 16*pow(2,y));
 42 }
 43
 44 void CGMaterial::setSmallViewport(int x, int y) {   
 45     int sizeX = (width_/XWindows);
 46     int sizeY = (height_/YWindows);
 47     glViewport(x*sizeX, y*sizeY, sizeX - 1, sizeY - 1);       
 48
 49     glMatrixMode(GL_PROJECTION);
 50     glLoadIdentity();
 51     gluPerspective(36.0,float(sizeX)/float(sizeY),2.0, 100.0);
 52     glMatrixMode(GL_MODELVIEW)
 53 }
 54
 55 void CGMaterial::drawScene() {   
 56
 57     glPushMatrix();
 58     glScaled(zoom_, zoom_, zoom_);
 59     glRotatef(70.0, 1., 0., 0.);
 60
 61     glColor4f(1,0,0,0);
 62
 63     static GLuint cache = 0;
 64     if (cache == 0) {
 65         cache = glGenLists(1);       
 66         glNewList(cache, GL_COMPILE_AND_EXECUTE);       
 67
 68         // Hinweis: Falls das Programm zu langsam läuft, kann man die Tessellation
 69         // des Torus durch Verringern des dritten und vierten Parameters vergröbern.
 70         // Dadurch leidet aber die Darstellungsqualität.
 71         glutSolidTorus(0.5, 1.0, 40, 40);
 72
 73         //glutSolidTeapot(1.0);
 74
 75         glEndList();
 76     } else {                       
 77         glCallList(cache);   
 78     }   
 79
 80     glPopMatrix();
 81 }
 82
 83 void CGMaterial::onInit() {
 84    
 85     // automatische Normalisierung
 86     glEnable(GL_NORMALIZE);
 87
 88     // Tiefen Test aktivieren
 89     glEnable(GL_DEPTH_TEST);
 90
 91     // Beleuchtung aktivieren
 92     glEnable(GL_LIGHTING);
 93     glEnable(GL_LIGHT0);
 94     static const float lightPosition0[] = { -3,
 95                                             3,
 96                                             0, 0 };
 97     glLightfv(GL_LIGHT0, GL_POSITION, lightPosition0);
 98 // glEnable(GL_COLOR_MATERIAL);
 99 // glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
100    
101     // Smooth Schattierung aktivieren
102     glShadeModel(GL_SMOOTH);
103    
104     // LookAt
105     glMatrixMode(GL_MODELVIEW);
106     gluLookAt(0.0, 0.0, 4.0,  // from (0,0,4)
107         0.0, 0.0, 0.0,  // to (0,0,0)
108         0.0, 1.0, 0.)
// up
109    
110     glClearColor(1.0, 1.0, 1.0, 1.0);
111 }
112 void CGMaterial::onSize(unsigned int newWidth,unsigned int newHeight) {         
113     width_ = newWidth;
114     height_ = newHeight;           
115     glViewport(0, 0, width_ - 1, height_ - 1);       
116
117     glMatrixMode(GL_PROJECTION);
118     glLoadIdentity();
119     gluPerspective(40.0,float(width_)/float(height_),2.0, 100.0);
120     glMatrixMode(GL_MODELVIEW);     
121 }
122
123 void CGMaterial::onKey(unsigned char key) {
124     switch (key) {
125     case 27: { exit(0); break; }                       
126     case '+': { zoom_*= 1.1; break; }
127     case '-': { zoom_*= 0.9; break; }
128     case ' ': { run_ = !run_; break; }
129     }
130     onDraw();
131 }
132
133 void CGMaterial::onIdle()
134     if (run_) {
135         glRotatef(5,0.1,1,0.2);         
136         onDraw();
137     }
138 }
139
140 void CGMaterial::onDraw() {
141
142     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
143
144     for (int x=0; x<XWindows; x++)
145         for (int y=0; y<YWindows; y++) {
146 // setLight(x, y);
147             setMaterial(x, y);
148             setSmallViewport(x, y);
149             drawScene();
150             glFlush();
151     }
152
153     // Front- und Back-Buffer tauschen:
154     swapBuffers();
155 }
156
157 // Hauptprogramm
158 int main(int argc, char* argv[]) {
159     // Erzeuge eine Instanz der Beispiel-Anwendung:
160     CGMaterial sample;
161    
162     cout << "Tastenbelegung:" << endl          << "ESC Programm beenden" << endl          << "Leertaste Objekt drehen" << endl          << "+ Hineinzoomen" << endl          << "- Herauszoomen" << endl;
163
164     // Starte die Beispiel-Anwendung:
165     sample.start("Stephan Brumme, 702544", true, 550, 550);
166     return(0);
167 }
168
169