#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;
}