textures.cpp

This is an example of how to use the CIvfTexture class.

00001 // ------------------------------------------------------------
00002 //
00003 // Ivf++ Texture mapping example
00004 //
00005 // ------------------------------------------------------------
00006 //
00007 // Author: Jonas Lindemann
00008 //
00009 
00010 // ------------------------------------------------------------
00011 // Include files
00012 // ------------------------------------------------------------
00013 
00014 #ifdef WIN32
00015 #include <ivf/ivfwin32libs.h>
00016 #endif
00017 
00018 #include <ivfui/IvfApplication.h>
00019 #include <ivfui/IvfWindow.h>
00020 
00021 #include <ivf/IvfCamera.h>
00022 #include <ivf/IvfComposite.h>
00023 #include <ivf/IvfLighting.h>
00024 #include <ivf/IvfMaterial.h>
00025 #include <ivf/IvfTexture.h>
00026 #include <ivf/IvfQuadPlane.h>
00027 
00028 #include <ivfimage/IvfPngImage.h>
00029 
00030 // ------------------------------------------------------------
00031 // Window class definition
00032 // ------------------------------------------------------------
00033 
00034 IvfSmartPointer(CExampleWindow);
00035 
00036 class CExampleWindow: public CIvfWindow {
00037 private:
00038         CIvfCameraPtr           m_camera;
00039         CIvfCompositePtr  m_scene;
00040         CIvfLightPtr            m_light;
00041 
00042         CIvfTexturePtr  m_logoTexture;
00043         
00044         double m_angleX;
00045         double m_angleY;
00046         double m_moveX;
00047         double m_moveY;
00048         double m_zoomX;
00049         double m_zoomY;
00050         
00051         int m_beginX;
00052         int m_beginY;
00053         
00054 public:
00055         CExampleWindow(int X, int Y, int W, int H)
00056                 :CIvfWindow(X, Y, W, H) {};
00057         
00058         virtual void onInit(int width, int height);
00059         virtual void onResize(int width, int height);
00060         virtual void onRender();
00061         virtual void onDestroy();
00062         virtual void onMouseDown(int x, int y);
00063         virtual void onMouseMove(int x, int y);
00064         virtual void onMouseUp(int x, int y);
00065         virtual void onKeyboard(int key, int x, int y);
00066 };
00067 
00068 // ------------------------------------------------------------
00069 // Window class implementation
00070 // ------------------------------------------------------------
00071 
00072 void CExampleWindow::onInit(int width, int height)
00073 {
00074         // Initialize variables
00075         
00076         m_angleX = 0.0;
00077         m_angleY = 0.0;
00078         m_moveX = 0.0;
00079         m_moveY = 0.0;
00080         m_zoomX = 0.0;
00081         m_zoomY = 0.0;
00082         
00083         // Initialize Ivf++ camera
00084         
00085         m_camera = new CIvfCamera();
00086         m_camera->setPosition(0.0, 0.0, 6.0);
00087         
00088         // Create a materials
00089         
00090         CIvfMaterialPtr material = new CIvfMaterial();
00091         material->setDiffuseColor(1.0f, 0.0f, 0.0f, 1.0f);
00092         material->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
00093         material->setAmbientColor(0.3f, 0.3f, 0.3f, 1.0f);
00094         
00095         // Create scene composite
00096         
00097         m_scene = new CIvfComposite();
00098         
00099         // Create images
00100         
00101         CIvfPngImagePtr logoImage = new CIvfPngImage();
00102         logoImage->setFileName("images/ivf.png");
00103         logoImage->read();
00104         
00105         // Create a texture
00106         
00107         m_logoTexture = new CIvfTexture();
00108         m_logoTexture->setImage(logoImage);
00109         m_logoTexture->setFilters(GL_NEAREST, GL_NEAREST);
00110         m_logoTexture->setGenerateMipmaps(true);
00111 
00112         // Create a quad plane
00113         
00114         CIvfQuadPlanePtr logo = new CIvfQuadPlane();
00115         logo->setSize(1.8, 1.8);;
00116         logo->setMaterial(material);
00117         logo->setTexture(m_logoTexture);
00118         
00119         m_scene->addChild(logo);
00120         
00121         // Create a light
00122         
00123         CIvfLightingPtr lighting = CIvfLighting::getInstance();
00124 
00125         m_light = lighting->getLight(0);
00126         m_light->setType(CIvfLight::LT_DIRECTIONAL);
00127         m_light->setDirection(1.0, 1.0, 1.0);
00128         m_light->setAmbientColor(0.2f, 0.2f, 0.2f, 1.0f); 
00129         m_light->enable();
00130 }
00131 
00132 // ------------------------------------------------------------
00133 void CExampleWindow::onResize(int width, int height)
00134 {
00135         m_camera->setPerspective(45.0, 0.1, 100.0);
00136         m_camera->setViewPort(width, height);
00137         m_camera->initialize();
00138 }
00139 
00140 // ------------------------------------------------------------
00141 void CExampleWindow::onRender()
00142 {
00143         m_light->render();
00144         m_camera->render();
00145         m_scene->render();
00146 }
00147 
00148 // ------------------------------------------------------------
00149 void CExampleWindow::onDestroy()
00150 {
00151         delete m_camera;
00152         delete m_light;
00153         delete m_scene;
00154 }
00155 
00156 // ------------------------------------------------------------
00157 void CExampleWindow::onKeyboard(int key, int x, int y)
00158 {
00159         switch (key) {
00160         case '1':
00161                 m_logoTexture->setFilters(GL_NEAREST, GL_NEAREST);
00162                 break;
00163         case '2':
00164                 m_logoTexture->setFilters(GL_LINEAR,  GL_LINEAR);
00165                 break;
00166         case '3':
00167                 m_logoTexture->setFilters(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
00168                 break;
00169         case 'd':
00170                 m_logoTexture->setMode(GL_DECAL);
00171                 break;
00172         case 'b':
00173                 m_logoTexture->setMode(GL_BLEND);
00174                 break;
00175         case 'm':
00176                 m_logoTexture->setMode(GL_MODULATE);
00177                 break;
00178         default:
00179 
00180                 break;
00181         }
00182 
00183         m_logoTexture->bind();
00184 
00185         redraw();
00186 }
00187 
00188 // ------------------------------------------------------------
00189 void CExampleWindow::onMouseDown(int x, int y)
00190 {
00191         m_beginX = x;
00192         m_beginY = y;
00193 }
00194 
00195 // ------------------------------------------------------------
00196 void CExampleWindow::onMouseMove(int x, int y)
00197 {
00198         m_angleX = 0.0;
00199         m_angleY = 0.0;
00200         m_moveX = 0.0;
00201         m_moveY = 0.0;
00202         m_zoomX = 0.0;
00203         m_zoomY = 0.0;
00204         
00205         if (isLeftButtonDown())
00206         {
00207                 m_angleX = (x - m_beginX);
00208                 m_angleY = (y - m_beginY);
00209                 m_beginX = x;
00210                 m_beginY = y;
00211                 
00212                 m_camera->rotatePositionY(m_angleX/100.0);
00213                 m_camera->rotatePositionX(m_angleY/100.0);
00214                 
00215                 redraw();
00216         }
00217         
00218         if (isRightButtonDown())
00219         {
00220                 if (getModifierKey() == CIvfWidgetBase::MT_SHIFT)
00221                 {
00222                         m_zoomX = (x - m_beginX);
00223                         m_zoomY = (y - m_beginY);
00224                 }
00225                 else
00226                 {
00227                         m_moveX = (x - m_beginX);
00228                         m_moveY = (y - m_beginY);
00229                 }
00230                 m_beginX = x;
00231                 m_beginY = y;
00232                 
00233                 m_camera->moveSideways(m_moveX/100.0);
00234                 m_camera->moveVertical(m_moveY/100.0);
00235                 m_camera->moveDepth(m_zoomY/50.0);
00236 
00237                 redraw();
00238         }
00239 }
00240 
00241 // ------------------------------------------------------------
00242 void CExampleWindow::onMouseUp(int x, int y)
00243 {
00244         m_angleX = 0.0;
00245         m_angleY = 0.0;
00246         m_moveX = 0.0;
00247         m_moveY = 0.0;
00248         m_zoomX = 0.0;
00249         m_zoomY = 0.0;
00250 }
00251 
00252 // ------------------------------------------------------------
00253 // Main program
00254 // ------------------------------------------------------------
00255 
00256 int main(int argc, char **argv) 
00257 {
00258         // Create Ivf++ application object.
00259         
00260         CIvfApplicationPtr app = new CIvfApplication(IVF_DOUBLE|IVF_RGB);
00261         
00262         // Create a window
00263         
00264         CExampleWindowPtr window = new CExampleWindow(0, 0, 512, 512);
00265         
00266         // Set window title and show window
00267         
00268         window->setWindowTitle("Ivf++ Texture mapping example");
00269         window->show();
00270         
00271         // Enter main application loop
00272         
00273     app->run();
00274         
00275         return 0;
00276 }
00277 
00278 

Generated on Fri Sep 1 15:36:45 2006 for Interactive Visualisation Framework - Ivf++ by  doxygen 1.4.6-NO