#include "include/raven.h" using namespace std; // RAVEN_OPENGL is the only available mode for now #define VIDEOMODETYPE RAVEN_OPENGL bool hdr_demo=false; bool allow_hdr=true; #define MODESTRING(a) (a)&&allow_hdr?" to change. Using HDR (three passes).":" to change. LDR only (one pass)." Core *core; Renderer *renderer; Pixelmap *stupid; Texture *stupidtex; FontBase *font; VertexBuffer *cube; ShaderProgram *hipass; ShaderProgram *lighting; ShaderProgram *bloom; RenderTarget *target_scene; RenderTarget *target_hipass; //RenderTarget *target_bloom; RenderMaterial blitmaterial(NULL); void blit_surface(Renderer *thisrenderer,Texture *surface,const Vector2 &surfacesize,ShaderProgram *program=NULL); void blit_surface2surface(Renderer *thisrenderer,Texture *surface,const Vector2 &surfacesize,ShaderProgram *program=NULL); void build_cube(VertexBuffer *buf); int main(int argc,char *argv[]) { cout.sync_with_stdio(); /* this should pretty much ALWAYS be used in my opinion. It ensures that switching between printf and cout does NOT disrupt the stream. Since C/C++ are used together very often this is a must. */ // set up the core factory CoreFactory::Singleton().Set("Test App",640,480,16,16,RAVEN_FULLSCREEN); // create the system core. this has our video context and handles OS specific junk. if (!(core=CoreFactory::Singleton().Create(VIDEOMODETYPE))) { cerr<<"Failed to create system core!"<State( RAVEN_STATE_LIGHT|RAVEN_STATE_MATERIAL|RAVEN_STATE_TEXTURE| RAVEN_STATE_GPU_PROGRAM|RAVEN_STATE_USE_HW_VERTEX_MEMORY| RAVEN_STATE_USE_HW_PIXEL_MEMORY,true); // load a texture image stupid=PixelmapLoader::Singleton().LoadPixmap("stupidpplinternet.png"); if (!stupid) { cerr<<"Failed to load \"stupidpplinternet.png\"!"<Stop()) cerr<<"Failed to stop system core!"<CreateTexture(stupid,SamplerState(2),0); if (!stupidtex) { cerr<<"Failed to create texture!"<Stop()) cerr<<"Failed to stop system core!"<CreateVertexBuffer( RAVEN_VERTEX_BUFFER_VERTICES|RAVEN_VERTEX_BUFFER_NORMALS| RAVEN_VERTEX_BUFFER_COLORS|RAVEN_VERTEX_BUFFER_TEXCOORDS); // create materials! RenderMaterial thematerial(stupidtex,Vector3(0.3f),Vector3(1.0f),Vector3(2.5f),false,NULL); // create a light DirectionalLight light0(Vector3(0.75f),Vector3(2.75f),Vector3(3.0f)); // really fucking bright light0.Direction(Vector3(1.0f,-1.0f,1.0f)); light0.Attenuation(0.0f); // build the cube from mesh data build_cube(cube); // create the render targets target_scene=renderer->CreateRenderTargetTexture(SamplerState(0,RAVEN_NEAREST_SAMPLE,RAVEN_NEAREST_SAMPLE),10,true); target_hipass=renderer->CreateRenderTargetTexture(SamplerState(0,RAVEN_LINEAR_SAMPLE,RAVEN_NEAREST_SAMPLE),9,true); if (!(target_hipass->IsValid()&&target_hipass->IsFloatingPointFormat())) { cerr<<"Could not create hipass render target (probably no float texture support), HDR is not available."<LoadShader("shaders","hipass_blit",true))) { cerr<<"Could not load hipass shader, HDR is not available."<AddUniform(shadervar); if (!(lighting=renderer->LoadShader("shaders","simple_lighting",true))) { cerr<<"Could not load lighting shader, lighting will be per vertex and HDR will not be available."<AddUniform(shadervar); if (!(bloom=renderer->LoadShader("shaders","bloom_blit",true))) { cerr<<"Could not load bloom shader, HDR is not available."<AddUniform(shadervar); thematerial.Shader(lighting); // lastly, load the font font=renderer->LoadFont("small.ttf",11); // no blend fo' yoo, foo! renderer->Blend(RAVEN_BLEND_NONE); bool run=true,spacedown=false; float ang=0.0f; while (run) { ang+=0.03f; if (hdr_demo&&allow_hdr) // do image space HDR filtering on float buffers! { // render the scene renderer->BindRenderTarget(target_scene); // bind target as our scene render target renderer->RenderViewport(Viewport(renderer->FrameBufferTarget())); renderer->BeginScene(true,true,Vector4(0.2f)); // begin scene, clear color/depth { renderer->State(RAVEN_STATE_NODEPTH,false); renderer->State(RAVEN_STATE_LIGHT,true); renderer->PushProjection(); renderer->PushModelView(); renderer->LoadProjection(Projection(640.0f/480.0f,(float)PI/4.0f,1.0f,512.0f)); renderer->EnableLight(0,&light0); renderer->MultModelView(translate(0.0f,0.0f,-4.0f)); renderer->MultModelView(rotateZXY(0.0f,ang,0.0f)); renderer->MultModelView(rotateZXY(ang,ang,ang)); // render the cube renderer->Material(thematerial); renderer->RenderVertexBuffer(cube,RAVEN_GEOMETRY_QUADS); renderer->DisableLight(0); renderer->PopProjection(); renderer->PopModelView(); } renderer->EndScene(false); // end scene, don't flip // draw the result to a bloom target using a hipass program renderer->BindRenderTarget(target_hipass); // bind target as hipass target renderer->RenderViewport(Viewport(target_hipass)); renderer->BeginScene(true,true,Vector4(0.0f)); // begin scene, clear color/depth { renderer->State(RAVEN_STATE_NODEPTH,true); renderer->State(RAVEN_STATE_LIGHT,false); renderer->PushProjection(); renderer->LoadProjection(orthoMatrixX(0.0f,(float)target_hipass->GetWidth(),0.0f,(float)target_hipass->GetHeight(),-1.0f,1.0f)); /*renderer->Blend(RAVEN_BLEND_ALPHA); renderer->BeginGeometry(RAVEN_GEOMETRY_QUADS); renderer->GeometryColor(Vector4(Vector3(0.0f),0.65f)); renderer->GeometryVertex(Vector3(0.0f)); renderer->GeometryColor(Vector4(Vector3(0.0f),0.65f)); renderer->GeometryVertex(Vector3(0.0f,target_hipass->GetHeight(),0.0f)); renderer->GeometryColor(Vector4(Vector3(0.0f),0.65f)); renderer->GeometryVertex(Vector3(target_hipass->GetSize(),0.0f)); renderer->GeometryColor(Vector4(Vector3(0.0f),0.65f)); renderer->GeometryVertex(Vector3(target_hipass->GetWidth(),0.0f,0.0f)); renderer->EndGeometry(); renderer->Blend(RAVEN_BLEND_NONE);*/ blit_surface2surface(renderer,target_scene->GetTexture(),target_hipass->GetSize(),hipass); renderer->PopProjection(); } renderer->EndScene(false); // end scene, don't flip // put everything together in the framebuffer renderer->BindRenderTarget(renderer->FrameBufferTarget()); // bind target as main framebuffer renderer->RenderViewport(Viewport(renderer->FrameBufferTarget())); renderer->BeginScene(true,true,Vector4(0.0f)); // begin scene, clear color/depth { renderer->State(RAVEN_STATE_NODEPTH,true); renderer->State(RAVEN_STATE_LIGHT,false); renderer->PushProjection(); renderer->LoadProjection(orthoMatrixX(0.0f,(float)renderer->GetCore()->GetWidth(),0.0f,(float)renderer->GetCore()->GetHeight(),-1.0f,1.0f)); renderer->Blend(RAVEN_BLEND_ADD); blit_surface(renderer,target_scene->GetTexture(),target_scene->GetSize()); blit_surface(renderer,target_hipass->GetTexture(),target_scene->GetSize(),bloom); renderer->Blend(RAVEN_BLEND_NONE); renderer->PopProjection(); renderer->RenderText(font,5,5,"The fonts are totally working."); renderer->RenderText(font,5,25,"Bitches most definitely be crazy."); renderer->RenderText(font,5,45,MODESTRING(hdr_demo)); renderer->RenderText(font,5,65,"LDR - final=(frag0=clamp(frag,0,1))"); renderer->RenderText(font,5,85,"HDR - final=clamp(frag1=blur_filter(frag0=hi_pass(frag))+frag,0,1)"); } renderer->EndScene(true); // end scene, flip if (renderer->GetCore()->KeyDown(RAVEN_KEY_SPACE)) spacedown=true; else if (spacedown) { hdr_demo=false; spacedown=false; } } else // render simple with no HDR { // render the scene renderer->BindRenderTarget(target_scene); // bind target as our scene render target renderer->RenderViewport(Viewport(renderer->FrameBufferTarget())); renderer->BeginScene(true,true,Vector4(0.2f)); // begin scene, clear color/depth { renderer->State(RAVEN_STATE_NODEPTH,false); renderer->State(RAVEN_STATE_LIGHT,true); renderer->PushProjection(); renderer->PushModelView(); renderer->LoadProjection(Projection(640.0f/480.0f,(float)PI/4.0f,1.0f,512.0f)); renderer->EnableLight(0,&light0); renderer->MultModelView(translate(0.0f,0.0f,-4.0f)); renderer->MultModelView(rotateZXY(0.0f,ang,0.0f)); renderer->MultModelView(rotateZXY(ang,ang,ang)); // render the cube renderer->Material(thematerial); renderer->RenderVertexBuffer(cube,RAVEN_GEOMETRY_QUADS); renderer->DisableLight(0); renderer->PopProjection(); renderer->PopModelView(); } renderer->EndScene(false); // end scene, don't flip // put everything together in the framebuffer renderer->BindRenderTarget(renderer->FrameBufferTarget()); // bind target as main framebuffer renderer->RenderViewport(Viewport(renderer->FrameBufferTarget())); renderer->BeginScene(true,true,Vector4(0.0f)); // begin scene, clear color/depth { renderer->State(RAVEN_STATE_NODEPTH,true); renderer->State(RAVEN_STATE_LIGHT,false); renderer->PushProjection(); renderer->LoadProjection(orthoMatrixX(0.0f,(float)renderer->GetCore()->GetWidth(),0.0f,(float)renderer->GetCore()->GetHeight(),-1.0f,1.0f)); blit_surface(renderer,target_scene->GetTexture(),target_scene->GetSize()); renderer->PopProjection(); renderer->RenderText(font,5,5,"The fonts are totally working."); renderer->RenderText(font,5,25,"Bitches most definitely be crazy."); renderer->RenderText(font,5,45,MODESTRING(hdr_demo)); renderer->RenderText(font,5,65,"LDR - final=(frag0=clamp(frag,0,1))"); renderer->RenderText(font,5,85,"HDR - final=clamp(frag1=blur_filter(frag0=hi_pass(frag))+frag,0,1)"); } renderer->EndScene(true); // end scene, flip if (renderer->GetCore()->KeyDown(RAVEN_KEY_SPACE)) spacedown=true; else if (spacedown) { hdr_demo=true; spacedown=false; } } if (renderer->GetCore()->KeyDown(RAVEN_KEY_ESCAPE)||renderer->GetCore()->IsAppTerm()) run=false; } delete font; delete hipass; delete lighting; delete bloom; delete target_scene; delete target_hipass; delete stupid; delete stupidtex; delete cube; delete renderer; if (!core->Stop()) { cerr<<"Failed to stop system core!"<BoundRenderTarget()->GetWidth()/surfacesize.x; float surfcoefy=(float)thisrenderer->BoundRenderTarget()->GetHeight()/surfacesize.y; blitmaterial.Tex(surface); blitmaterial.Shader(program); thisrenderer->Material(blitmaterial); thisrenderer->BeginGeometry(RAVEN_GEOMETRY_QUADS); { thisrenderer->GeometryTexCoord(Vector2(0.0f,0.0f),0); thisrenderer->GeometryVertex(Vector3(0.0f,0.0f,0.0f)); thisrenderer->GeometryTexCoord(Vector2(0.0f,surfcoefy),0); thisrenderer->GeometryVertex(Vector3(0.0f,(float)thisrenderer->BoundRenderTarget()->GetHeight(),0.0f)); thisrenderer->GeometryTexCoord(Vector2(surfcoefx,surfcoefy),0); thisrenderer->GeometryVertex(Vector3((float)thisrenderer->BoundRenderTarget()->GetWidth(),(float)thisrenderer->BoundRenderTarget()->GetHeight(),0.0f)); thisrenderer->GeometryTexCoord(Vector2(surfcoefx,0.0f),0); thisrenderer->GeometryVertex(Vector3((float)thisrenderer->BoundRenderTarget()->GetWidth(),0.0f,0.0f)); } thisrenderer->EndGeometry(); } void blit_surface2surface(Renderer *thisrenderer,Texture *surface,const Vector2 &surfacesize,ShaderProgram *program) { float surfcoefx=(float)thisrenderer->BoundRenderTarget()->GetWidth()/surfacesize.x; float surfcoefy=(float)thisrenderer->BoundRenderTarget()->GetHeight()/surfacesize.y; blitmaterial.Tex(surface); blitmaterial.Shader(program); thisrenderer->Material(blitmaterial); thisrenderer->BeginGeometry(RAVEN_GEOMETRY_QUADS); { thisrenderer->GeometryTexCoord(Vector2(0,surfcoefx),0); thisrenderer->GeometryVertex(Vector3(0.0f,0.0f,0.0f)); thisrenderer->GeometryTexCoord(Vector2(0.0f,0.0f),0); thisrenderer->GeometryVertex(Vector3(0.0f,(float)thisrenderer->BoundRenderTarget()->GetHeight(),0.0f)); thisrenderer->GeometryTexCoord(Vector2(surfcoefx,0),0); thisrenderer->GeometryVertex(Vector3((float)thisrenderer->BoundRenderTarget()->GetWidth(),(float)thisrenderer->BoundRenderTarget()->GetHeight(),0.0f)); thisrenderer->GeometryTexCoord(Vector2(surfcoefx,surfcoefy),0); thisrenderer->GeometryVertex(Vector3((float)thisrenderer->BoundRenderTarget()->GetWidth(),0.0f,0.0f)); } thisrenderer->EndGeometry(); } void build_cube(VertexBuffer *buf) { buf->NumTextures(1); buf->NumVerts(24); buf->Set(RAVEN_VERTEX_BUFFER_VERTICES,0,cubeverts); buf->Set(RAVEN_VERTEX_BUFFER_NORMALS,0,cubenormals); buf->Set(RAVEN_VERTEX_BUFFER_COLORS,0,cubecolors); buf->Set(RAVEN_VERTEX_BUFFER_TEXCOORDS,0,cubetexcoords); }