GlutViewer.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 6743 $                                                       *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2009-08-05 11:03:10 +0200 (Mi, 05. Aug 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 //=============================================================================
00046 //
00047 //  CLASS GlutViewer - IMPLEMENTATION
00048 //
00049 //=============================================================================
00050 
00051 //== INCLUDES =================================================================
00052 
00053 
00054 #include "GlutViewer.hh"
00055 #include <stdio.h>
00056 
00057 
00058 //== NAMESPACES ===============================================================
00059 
00060 
00061 namespace ACG {
00062 
00063 
00064 //== IMPLEMENTATION ========================================================== 
00065 
00066 
00067 std::map<int, GlutViewer*>  GlutViewer::windows__;
00068 
00069 
00070 //-----------------------------------------------------------------------------
00071 
00072 
00073 GlutViewer::
00074 GlutViewer(const char* _title, int _width, int _height)
00075   : width_(_width), height_(_height), fullscreen_(false)
00076 {
00077   // create window
00078   glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_ALPHA);
00079   glutInitWindowSize(_width, _height);
00080   glViewport(0, 0, _width, _height); 
00081   windowID_ = glutCreateWindow(_title);
00082   windows__[windowID_] = this;
00083 
00084 
00085   // register callbacks
00086   glutDisplayFunc(display__);
00087   glutKeyboardFunc(keyboard__);
00088   glutSpecialFunc(special__);
00089   glutMouseFunc(mouse__);
00090   glutMotionFunc(motion__);
00091   glutPassiveMotionFunc(passivemotion__);
00092   glutReshapeFunc(reshape__); 
00093   glutVisibilityFunc(visibility__);
00094 
00095 
00096   // init GL
00097   init();
00098 }
00099   
00100 
00101 //-----------------------------------------------------------------------------
00102 
00103 
00104 GlutViewer::
00105 ~GlutViewer()
00106 {
00107   glutDestroyWindow(windowID_);
00108 }
00109 
00110 
00111 //-----------------------------------------------------------------------------
00112 
00113 
00114 void
00115 GlutViewer::init()
00116 {
00117   // init GL state
00118   glstate_.initialize();
00119 
00120 
00121   // OpenGL state
00122   glEnable(GL_DEPTH_TEST);
00123   glEnable(GL_LIGHTING);
00124   glDisable(GL_DITHER);
00125   glShadeModel(GL_FLAT);
00126   glFrontFace(GL_CCW);
00127 
00128 
00129   // light sources
00130   glLoadIdentity();
00131   GLfloat pos[4], col[4];
00132   col[0] = col[1] = col[2] = 0.6;
00133   pos[3] = 0.0;
00134   col[3] = 1.0;
00135 
00136 #define SET_LIGHT(i,x,y,z) {                    \
00137   pos[0]=x; pos[1]=y; pos[2]=z;                 \
00138   glLightfv(GL_LIGHT##i, GL_POSITION, pos);     \
00139   glLightfv(GL_LIGHT##i, GL_DIFFUSE,  col);     \
00140   glLightfv(GL_LIGHT##i, GL_SPECULAR, col);     \
00141   glEnable(GL_LIGHT##i);                        \
00142 }
00143 
00144   SET_LIGHT(0,  0.0,  0.0, 1.0);
00145   SET_LIGHT(1, -1.0,  1.0, 0.7);
00146   SET_LIGHT(2,  1.0,  1.0, 0.7);
00147 
00148 
00149 
00150   // projection
00151   near_ = 0.1;
00152   far_  = 100.0;
00153   fovy_ = 45.0;
00154   update_projection();
00155   glstate_.viewport(0, 0, width_, height_);
00156   glstate_.translate(0,0,-3);
00157 }
00158 
00159 
00160 //-----------------------------------------------------------------------------
00161 
00162 
00163 void 
00164 GlutViewer::update_projection()
00165 {
00166   glstate_.reset_projection();
00167   glstate_.perspective(fovy_, 
00168                        (GLfloat) width_ / (GLfloat) height_,
00169                        near_, far_);
00170 }
00171 
00172 
00173 //-----------------------------------------------------------------------------
00174 
00175 
00176 GlutViewer* GlutViewer::current_window() { 
00177   return windows__[glutGetWindow()]; 
00178 }
00179 
00180 void GlutViewer::display__(void) {
00181   current_window()->display();
00182 }
00183 
00184 void GlutViewer::idle__(void) {
00185   current_window()->idle();
00186 } 
00187 
00188 void GlutViewer::keyboard__(unsigned char key, int x, int y) {
00189   current_window()->keyboard((int)key, x, y);
00190 }
00191 
00192 void GlutViewer::motion__(int x, int y) {
00193   current_window()->motion(x, y);
00194 }
00195 
00196 void GlutViewer::mouse__(int button, int state, int x, int y) {
00197   current_window()->mouse(button, state, x, y);
00198 }
00199 
00200 void GlutViewer::passivemotion__(int x, int y) {
00201   current_window()->passivemotion(x, y);
00202 }
00203 
00204 void GlutViewer::reshape__(int w, int h) {
00205   current_window()->reshape(w, h);
00206 }
00207 
00208 void GlutViewer::special__(int key, int x, int y) {
00209   current_window()->keyboard(key, x, y);
00210 }   
00211 
00212 void GlutViewer::visibility__(int visible) {
00213   current_window()->visibility(visible);
00214 }
00215 
00216 
00217 //-----------------------------------------------------------------------------
00218 
00219 
00220 void GlutViewer::idle(void) {
00221 } 
00222 
00223 void GlutViewer::keyboard(int key, int /* x */ , int /* y */ ) 
00224 {
00225   switch (key)
00226   {
00227     case 27:  
00228     {
00229       exit(0); 
00230       break;
00231     }
00232 
00233 
00234     case GLUT_KEY_F12: 
00235     {
00236       if (!fullscreen_) 
00237       {
00238         bak_left_   = glutGet(GLUT_WINDOW_X);
00239         bak_top_    = glutGet(GLUT_WINDOW_Y);
00240         bak_width_  = glutGet(GLUT_WINDOW_WIDTH);
00241         bak_height_ = glutGet(GLUT_WINDOW_HEIGHT);
00242         glutFullScreen();
00243         fullscreen_ = true;
00244       }
00245       else
00246       {
00247         glutReshapeWindow(bak_width_, bak_height_);
00248         glutPositionWindow(bak_left_, bak_top_);
00249         fullscreen_ = false;
00250       }
00251       break;
00252     }
00253   }
00254 } 
00255 
00256 void GlutViewer::motion(int /* x */ , int /* y */ ) {
00257 }
00258 
00259 void GlutViewer::mouse(int /* button */ , int /* state */ , int /* x */ , int /* y */ ) {
00260 }
00261 
00262 void GlutViewer::passivemotion(int /* x */ , int /* y */ ) {
00263 }
00264 
00265 void GlutViewer::visibility(int /* visible */ ) {
00266 }
00267 
00268 void GlutViewer::reshape(int w, int h) 
00269 {
00270   width_=w; height_=h;
00271   glstate_.viewport(0, 0, width_, height_);
00272 
00273   glstate_.reset_projection();
00274   glstate_.perspective(fovy_, 
00275                        (GLfloat) width_ / (GLfloat) height_,
00276                        near_, far_);
00277 
00278   glutPostRedisplay();
00279 }
00280 
00281 void GlutViewer::display(void) 
00282 {
00283   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00284   draw();
00285   glutSwapBuffers();
00286 }
00287 
00288 
00289 //=============================================================================
00290 } // namespace ACG
00291 //=============================================================================

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .