sources:
cgapplication.cpp (3.2k)
cgapplication.h (3.5k)
cgtranslucent.cpp (3.0k)
cgtranslucent.h (753 bytes)


binaries:
Release/translucent.exe (20.0k)


website:
more info here


screenshot:
studies/grafik2/Computergrafik-Code2/cgtranslucent.cpp
download file

  1 //
  2 // Computergraphik II
  3 // Prof. Dr. Juergen Doellner
  4 // Wintersemester 2001/02
  5 //
  6 // Rahmenprogramm zu Aufgabenzettel 1
  7 //
  8
  9 #include "cgtranslucent.h"
 10 #include <fstream.h>
 11 #include <stdlib.h>
 12 #include <GL/glut.h>
 13
 14 CGTranslucent::CGTranslucent() {
 15     run_ = true;
 16     moveX_ = 0.0;
 17     dirX_ = 0.005;
 18 }
 19
 20 CGTranslucent::~CGTranslucent( ) {   
 21 }
 22
 23
 24 void CGTranslucent::onInit() {
 25     // OpenGL Lichtquelle
 26     static GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};  /* diffuse light. */
 27     static GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};  /* Infinite light location. */   
 28     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
 29     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
 30     glEnable(GL_LIGHTING);
 31     glEnable(GL_LIGHT0);
 32    
 33     // automatische Normalisierung
 34     glEnable(GL_NORMALIZE);
 35    
 36     glEnable(GL_COLOR_MATERIAL);
 37     glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
 38
 39     // Tiefen Test aktivieren
 40     glEnable(GL_DEPTH_TEST);
 41    
 42     // Smooth Schattierung aktivieren
 43     glShadeModel(GL_SMOOTH);
 44    
 45     // Projection
 46     glMatrixMode(GL_PROJECTION);
 47     gluPerspective(60.0,1.0,2,2000.0);   
 48    
 49     // LookAt
 50     glMatrixMode(GL_MODELVIEW);
 51     gluLookAt(0.0, 0.0, 4.0,  // from (0,0,4)
 52               0.0, 0.0, 0.0,  // to (0,0,0)
 53               0.0, 1.0, 0.)
// up
 54    
 55     glClearColor(1.0,1.0,1.0,1.0);
 56 }
 57 void CGTranslucent::onSize(unsigned int newWidth,unsigned int newHeight) {         
 58     width_ = newWidth;
 59     height_ = newHeight;           
 60     glMatrixMode(GL_PROJECTION);
 61     glViewport(0, 0, width_ - 1, height_ - 1);       
 62     glLoadIdentity();
 63     gluPerspective(40.0,float(width_)/float(height_),2.0, 100.0);
 64     glMatrixMode(GL_MODELVIEW);     
 65 }
 66
 67 void CGTranslucent::onKey(unsigned char key) {
 68     switch (key) {
 69     case 27: { exit(0); break; }                       
 70     case ' ': { run_ = !run_; break; }
 71     }
 72     onDraw();
 73 }
 74
 75 void CGTranslucent::onIdle()
 76     moveX_+= dirX_;
 77     if(moveX_ > 1.4) {
 78         moveX_-=(dirX_);
 79         dirX_ = -dirX_;
 80     } else if(moveX_ < -1.4) {
 81         moveX_-=(dirX_);
 82         dirX_ = -dirX_;
 83     }
 84
 85     if (run_) {
 86         glRotatef(1,0.1,1,0.2);         
 87     }
 88    
 89     onDraw();
 90 }
 91
 92 void CGTranslucent::onDraw() {
 93     // Mechanismus zur Behandlung opaker Objekte
 94     glDepthMask(GL_TRUE);
 95     glDisable(GL_BLEND);
 96
 97     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);               
 98
 99     glColor4f(1,1,0,1); // nicht Transparent (A=1)
100     glPushMatrix();
101         glTranslatef(moveX_,0,0);
102         glutSolidSphere(0.5, 32, 32);
103     glPopMatrix();
104
105     // Mechanismus zur Behandlung transparenter Objekte
106     glEnable(GL_BLEND);   
107     glDepthMask(GL_FALSE);
108
109     glColor4f(1,0,0,0.4); // Transparent (A=0.4)
110     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
111     glutSolidSphere(0.8, 32, 32);
112
113     // Nicht vergessen! Front- und Back-Buffer tauschen:
114     swapBuffers();
115 }
116
117 // Hauptprogramm
118 int main(int argc, char* argv[]) {
119     // Erzeuge eine Instanz der Beispiel-Anwendung:
120     CGTranslucent sample;
121    
122     // Starte die Beispiel-Anwendung:
123     sample.start("CGTranslucent", true, 400, 400);
124     return(0);
125 }
126
127
128
129
130
131
132
133
134
135