Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Examples

simpleHeat.cpp

/**********************************************************************
 * 
 *  File: Texture2D.hpp
 * 
 *  Created: 23. June 2005
 *
 *  Version: $Id: simpleHeat.cpp,v 1.9 2005/09/26 11:36:29 jonhjelm Exp $
 *
 *  Authors: Trond R. Hagen <trr@sintef.no>, 
 *               Jon Mikkelsen Hjelmervik <jami@sintef.no>,
 *               Johan S. Seland <johans@ifi.uio.no>
 *
 *  This file is part of the Shallows library.
 *  Copyright (C) 2005 by SINTEF.  All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  ("GPL") version 2 as published by the Free Software Foundation.
 *  See the file LICENSE.GPL at the root directory of this source
 *  distribution for additional information about the GNU GPL.
 * 
 *  For using Shallows with software that can not be combined with the
 *  GNU GPL, please contact SINTEF for aquiring a commercial license
 *  and support.
 *
 *  SINTEF, Pb 124 Blindern, N-0314 Oslo, Norway
 *  http://www.sintef.no
 *********************************************************************/
#include <iostream>
#include <stdlib.h>
#include <GL/glut.h>

#include "boost/shared_ptr.hpp"
#include "shallows/shallows.hpp"
#include "shallows/utils/textureCreation.hpp"

using boost::shared_ptr;
using shallows::Texture2D;
using shallows::GLProgram;
using shallows::OffScreenBuffer;
using shallows::RenderTexture2D;
using shallows::OnScreenBuffer;
using shallows::OnScreenRenderTarget;

const unsigned int BUFF_DIM=256;
shared_ptr<GLProgram> heatProg[2];
shared_ptr<GLProgram> displayProg;
int window_width;
int window_height;

void reshape(int width, int height)
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
  glViewport(0,0,width,height);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void reshape_callback(int width, int height)
{
   window_width=width;
   window_height=height;
   reshape(window_width, window_height);
}

void keyboard(unsigned char key, int winx, int winy)
{
  switch(key) {
    case 'q':
      exit(0);
  }
}

void initOpengl()
{
   shallows::init_shallows();
   float pixels[]={0.0, 0.0, 0.0, 0.0, 
                   1.0, 0.0, 0.0, 0.0, 
                             1.0, 0.0, 0.0, 0.0, 
                             1.0, 0.0, 0.0, 0.0, 
                   
                   1.0, 0.0, 0.0, 0.0, 
                   0.0, 0.0, 0.0, 0.0, 
                   1.0, 0.0, 0.0, 0.0, 
                   1.0, 0.0, 0.0, 0.0, 

                   1.0, 0.0, 0.0, 0.0, 
                   1.0, 0.0, 0.0, 0.0, 
                   0.0, 0.0, 0.0, 0.0, 
                   1.0, 0.0, 0.0, 0.0, 

                   1.0, 0.0, 0.0, 0.0, 
                   1.0, 0.0, 0.0, 0.0, 
                   1.0, 0.0, 0.0, 0.0, 
                   0.0, 0.0, 0.0, 0.0};
   boost::shared_ptr<Texture2D> init_tex= 
      shallows::utils::createFloatTexture2D(4, 4, 4, pixels);
   

   shared_ptr<OffScreenBuffer> fb(new OffScreenBuffer(BUFF_DIM, BUFF_DIM));
   shared_ptr<RenderTexture2D> rt[2];
   rt[0]=fb->createRenderTexture2D();
   rt[1]=fb->createRenderTexture2D();
   glDisable(GL_DEPTH_TEST);
   for (int i=0; i<2; i++)
   {
      heatProg[i].reset(new GLProgram);
      heatProg[i]->useNormalizedTexCoords();
      heatProg[i]->readFile("heat.shader");
      heatProg[i]->setFrameBuffer(fb);
      heatProg[i]->setOutputTarget(0, rt[(i+1)%2]);
      heatProg[i]->setInputTexture("texture", rt[i]->getTexture());
      try {heatProg[i]->setParam1f("h", 1.0/BUFF_DIM);} catch (...) { ;}
   }


   GLProgram initProg;
   initProg.useNormalizedTexCoords();
   initProg.readFile("showtex2D.shader");
   initProg.setFrameBuffer(fb);
   initProg.setOutputTarget(0, rt[0]);
   initProg.setInputTexture("texture", init_tex);
   reshape(BUFF_DIM, BUFF_DIM);

   initProg.run();
   initProg.setOutputTarget(0, rt[1]);
   initProg.run();

   shared_ptr<OnScreenBuffer> ons_fb(new OnScreenBuffer);
   shared_ptr<OnScreenRenderTarget> ons_rt
      (new OnScreenRenderTarget(GL_BACK_LEFT));
   displayProg.reset(new GLProgram);
   displayProg->useNormalizedTexCoords();
   displayProg->readFile("showtex2D.shader");
   displayProg->setFrameBuffer(ons_fb);
   displayProg->setOutputTarget(0, ons_rt);
   displayProg->setInputTexture("texture", rt[0]->getTexture());

   reshape(BUFF_DIM, BUFF_DIM);
}

void display()
{
   try {
      reshape(BUFF_DIM, BUFF_DIM);
      heatProg[0]->run();
      heatProg[1]->run();
      reshape(window_width, window_height);
      displayProg->run();
   } catch (std::exception &e)
   {
      std::cout << e.what() << std::endl;
      exit(0);
   }

   glutSwapBuffers();
   glutPostRedisplay();
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitWindowPosition(100, 100);
   glutInitWindowSize(512, 512);
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
   int windowID = glutCreateWindow("SimpleHeat using shallows");
   glutSetWindow(windowID);
   
   glutReshapeFunc(reshape_callback);
   glutKeyboardFunc(keyboard);
   glutDisplayFunc(display);
   try {
      initOpengl();
   } catch (shallows::compile_error &e)
   {
      std::cout << "Compile error:\n " << e.log() << std::endl;
      exit(0);
   } catch (std::exception &e)
   {
      std::cout << e.what() << std::endl;
      exit(0);
   }
   glutMainLoop();

   return 0;
}



Generated on Mon Sep 26 15:35:46 2005 for shallows by  doxygen 1.4.4