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

simpleMRT.cpp

/**********************************************************************
 * 
 *  File: simpleMRT.cpp
 * 
 *  Created:
 *
 *  Version: $Id: simpleMRT.cpp,v 1.6 2005/09/16 14:46:16 trondrh 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 "shallows/GLProgram.hpp"
#include "shallows/OnScreenBuffer.hpp"
#include "shallows/OnScreenRenderTarget.hpp"
#include "boost/shared_ptr.hpp"
#include "shallows/OffScreenBuffer.hpp"
#include "shallows/RenderTexture2D.hpp"
#include "shallows/utils/textureCreation.hpp"
#include "shallows/shallowsexcept.hpp"
#include "shallows/shallows.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> MRTProg;
shared_ptr<GLProgram> displayProg[3];
int window_width;
int window_height;
int RTtodisplay=1;

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

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  //  glScalef(1.0-0.5/width, 1.0-0.5/height, 1.0f);
  //  glTranslatef(-0.5f, -0.5f, 0.0f);
}

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 '0':
      RTtodisplay=0;
      break;
    case '1':
      RTtodisplay=1;
      break;
    case '2':
      RTtodisplay=2;
      break;
    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::createFloatTextureRectangle(4, 4, 4, pixels);


   //   shared_ptr<OffScreenBuffer> fb(new OffScreenBuffer(BUFF_DIM, BUFF_DIM));
   shared_ptr<OffScreenBuffer> fb(new OffScreenBuffer(
      init_tex->getInternalFormat(), BUFF_DIM, BUFF_DIM));

   shared_ptr<RenderTexture2D> rt[3];
   rt[0]=fb->createRenderTextureRectangle();
   rt[1]=fb->createRenderTextureRectangle();
   rt[2]=fb->createRenderTextureRectangle();
   glDisable(GL_DEPTH_TEST);

   MRTProg.reset(new GLProgram);
   MRTProg->readFile("MRT.shader");
   MRTProg->setFrameBuffer(fb);
   MRTProg->setOutputTarget(0, rt[0]);
   MRTProg->setOutputTarget(1, rt[1]);
   MRTProg->setOutputTarget(2, rt[2]);
   MRTProg->setInputTexture("texture", init_tex);
   MRTProg->setTexCoords(0.0f, 0.0f, 4.0f, 4.0f);
   //   MRTProg->setRenderable(init_tex_renderable);

   shared_ptr<OnScreenBuffer> ons_fb(new OnScreenBuffer);
   shared_ptr<OnScreenRenderTarget> ons_rt
      (new OnScreenRenderTarget(GL_BACK_LEFT));
   for (int i=0; i<3; i++)
   {
      displayProg[i].reset(new GLProgram);
      displayProg[i]->readFile("showtexRect.shader");
      displayProg[i]->setFrameBuffer(ons_fb);
      displayProg[i]->setOutputTarget(0, ons_rt);
      displayProg[i]->setInputTexture("texture", rt[i]->getTexture());
      //displayProg->setInputTexture("texture", init_tex);
      //displayProg[i]->setRenderable(renderable);
      displayProg[i]->setTexCoords(0.0f, 0.0f, BUFF_DIM, BUFF_DIM);
   }
}

void display()
{
   try {
      reshape(BUFF_DIM, BUFF_DIM);
      MRTProg->run();
      reshape(window_width, window_height);
      displayProg[RTtodisplay]->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