#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();
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 '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(
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);
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[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;
}