// // Computergraphik II // Prof. Dr. Juergen Doellner // Wintersemester 2001/02 // // Rahmenprogramm zu Aufgabenzettel 7 // #include "cgtexture.h" #include "vector.h" #include #include #ifndef PI const double PI = 3.14159265358979323846; #endif const unsigned int BUF_SIZE = 1024; const unsigned int PATCH_SIZE = 20; CGTexture::CGTexture(char* filename) { filename_ = filename; run_ = false; rotate_ = false; zoom_ = 1.5; list_ = NULL; buildPatch(PATCH_SIZE); } CGTexture::~CGTexture() { } void CGTexture::drawScene() { glPushMatrix(); glRotatef(60,1,0,0); glScaled(zoom_, zoom_, zoom_); drawObject(); glPopMatrix(); } void CGTexture::handleVertex(const Vector& v) { // Hier müssen die Texturkoordinaten berechnet werden glTexCoord2f(v[0]+0.5, v[2]+0.5); // draw patch glVertex3dv(v.rep()); } void CGTexture::drawObject() { // draw geometry glBindTexture(GL_TEXTURE_2D, textureID_); int c=0; for(int j=0; j> binary; #endif // Read "P6" char ppm; in >> ppm; if(!in.good() || ppm != 'P') { cerr << "ppm format error" << endl; } in >> ppm; if(!in.good() || ppm != '6') { cerr << "ppm format error" << endl; } // forward to next line in.getline(buf, BUF_SIZE); // normally read comments, but we assume that no comments are there // Read width and height in >> texWidth_; if(!in.good()) { cerr << "ppm format error" << endl; } in >> texHeight_; if(!in.good()) { cerr << "ppm format error" << endl; } // Read 255 unsigned int res; in >> res; if(!in.good() || res != 255) { cerr << "ppm format error" << endl; } // forward to next line in.getline(buf, BUF_SIZE); // read image data image_ = new GLubyte [3 * texWidth_ * texHeight_]; in.read(image_, 3 * texWidth_ * texHeight_); if(!in.good()) { cerr << "ppm format error" << endl; } in.close(); } void CGTexture::onInit() { // Tiefen Test aktivieren glEnable(GL_DEPTH_TEST); // Smooth Schattierung aktivieren glShadeModel(GL_SMOOTH); // Projection glMatrixMode(GL_PROJECTION); gluPerspective(60.0, 1.0, 2.0, 50.0); // LookAt glMatrixMode(GL_MODELVIEW); gluLookAt( 0.0, 0.0, 4.0, // from (0,0,4) 0.0, 0.0, 0.0, // to (0,0,0) 0.0, 1.0, 0.); // up glClearColor(0.9,0.9,0.9,1.0); // liest ein ppm-bild im format RGB ein (kein alpha-kanal!) readImage(); ////////////////////////////////// // create texture // generate a unique ID glGenTextures(1, &textureID_); // use this texture ID glBindTexture(GL_TEXTURE_2D, textureID_); // repeat texture if necessary glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // use filter "linear" both for magnification and minification glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // bind image to texture object glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth_, texHeight_, 0, GL_RGB, GL_UNSIGNED_BYTE, image_); // enable texture mapping glEnable(GL_TEXTURE_2D); } void CGTexture::onSize(unsigned int newWidth,unsigned int newHeight) { width_ = newWidth; height_ = newHeight; glMatrixMode(GL_PROJECTION); glViewport(0, 0, width_ - 1, height_ - 1); glLoadIdentity(); gluPerspective(40.0,float(width_)/float(height_),2.0, 100.0); glMatrixMode(GL_MODELVIEW); } void CGTexture::onKey(unsigned char key) { switch (key) { case 27: { exit(0); break; } case '+': { zoom_*= 1.1; break; } case '-': { zoom_*= 0.9; break; } case ' ': { run_ = !run_; break; } case 'a': { rotate_ = !rotate_; break; }; } onDraw(); } void CGTexture::onIdle() { // rotate object if (run_) glRotatef(.5, 0.0, 1.0, 0.0); // rotate texture if (rotate_) { // use texture matrix stack glMatrixMode(GL_TEXTURE); // move origin glTranslatef(0.5, 0.5, 0); // rotate glRotatef(-1, 0, 0, 1); // move back glTranslatef(-0.5, -0.5, 0); // switch back to model view matrix stack glMatrixMode(GL_MODELVIEW); } onDraw(); } void CGTexture::onDraw() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); drawScene(); swapBuffers(); } // Hauptprogramm int main(int argc, char* argv[]) { // Erzeuge eine Instanz der Beispiel-Anwendung: CGTexture sample("church_spiral.ppm"); //CGTexture sample("deep_spiral.ppm"); cout << "Tastenbelegung:" << endl << "ESC Programm beenden" << endl << "Leertaste Objekt drehen" << endl << "+ Hineinzoomen" << endl << "- Herauszoomen" << endl << "a Textur rotieren" << endl; // Starte die Beispiel-Anwendung: sample.start("Stephan Brumme, 702544", true, 512, 512); return(0); }