BambooIsland: C:/Users/easterbunny/Desktop/Echtzeitgraphik-LU/trunk/src/Graphic.cpp Source File

BambooIsland Abgabe 2

- Main Page
- Related Pages
- Classes
- Files

- File List

# C:/Users/easterbunny/Desktop/Echtzeitgraphik-LU/trunk/src/Graphic.cpp

    00001 /*
    00002 * source file for Graphic class
    00003 * written by: christian moellinger <ch.moellinger@gmail.com>
    00004 * 10/2010
    00005 */
    00006
    00007 #include <string>
    00008 #include <sstream>
    00009 #include <memory>
    00010
    00011 #include <glm/gtc/matrix_projection.hpp>
    00012 #include <glm/gtc/matrix_transform.hpp>
    00013 #include <glm/gtx/transform.hpp>
    00014 #include <glm/gtx/transform2.hpp>
    00015 #include <glm/gtc/type_ptr.hpp>
    00016
    00017 #include "common_gl.h"
    00018 #include "ShaderManager.h"
    00019 #include "Logger.h"
    00020 #include "SceneObject.h"
    00021 #include "SceneObject_Water.h"
    00022 #include "SceneObject_Sphere.h"
    00023 #include "SceneObject_Skysphere.h"
    00024 #include "SceneObject_SimpleQuad.h"
    00025 #include "SceneObject_PostEffect.h"
    00026 #include "SceneObject_RenderFromLight.h"
    00027 #include "SceneObject_LightSphere.h"
    00028 #include "SceneObject_OgreImport.h"
    00029 #include "SceneObject_Terrain.h"
    00030 #include "SceneObject_FBO.h"
    00031 #include "Camera.h"
    00032 #include "Graphic.h"
    00033 #undef DEBUG
    00034
    00035 using namespace std;
    00036
    00037 Graphic::Graphic(int width, int height, bool fullscreen) : m_iWidth(width), m_iHeight(height)
    00038 {
    00039         int flags = (fullscreen) ? GLFW_FULLSCREEN : GLFW_WINDOW;       //if fullscreen is true, flags is set to GLFW_FULLSCREEN, else to GLFW_WINDOW
    00040
    00041         // Set flags so GLFW creates the desired OpenGL context
    00042         glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
    00043         glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
    00044         glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    00045
    00046         //Activate 4x antialiasing
    00047         glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
    00048
    00049         if (glfwOpenWindow(m_iWidth, m_iHeight, 0,0,0,0, 24, 8, flags) != GL_TRUE)
    00050                 Logger::instance()->log(Logger::FATAL, "failed to initialize OpenGL window");
    00051         else
    00052                 Logger::instance()->log(Logger::TRACE, "OpenGL window initialized");
    00053
    00054         glfwSetWindowTitle("Bamboo Island - Realtime Rendering Project written by Christian Moellinger");
    00055
    00056         // Disable the mouse cursor
    00057         //glfwDisable(GLFW_MOUSE_CURSOR);
    00058
    00059         // Disable VSync
    00060         //glfwSwapInterval(0);
    00061
    00062         // start up GLEW
    00063         glewExperimental = GL_TRUE; //set experimental flag to true, needed for correct importing of function pointers when using core-profile
    00064         if (glewInit() != GLEW_OK)
    00065                 Logger::instance()->log(Logger::FATAL, "glew initialization failed");
    00066         else
    00067                 Logger::instance()->log(Logger::TRACE, "glew initialized");
    00068
    00069         Logger::instance()->log(Logger::DEBUG, string("Opened context with OpenGL Version ") + string((char *) glGetString(GL_VERSION)));
    00070
    00071         if (GLEW_VERSION_3_3)
    00072         {
    00073                 GLint profile;
    00074
    00075                 // check if we have a core-profile
    00076                 glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &profile);
    00077                 if (profile == GL_CONTEXT_CORE_PROFILE_BIT)
    00078                         Logger::instance()->log(Logger::TRACE, "got rendering context with core profile");
    00079                 else
    00080                         Logger::instance()->log(Logger::FATAL, "got rendering context with compatibility profile instead of core profile");
    00081         }
    00082         else
    00083                 Logger::instance()->log(Logger::FATAL, "OpenGL version 3.3 is needed but not supported");
    00084
    00085         // Enable sRGB gamma correction for framebuffer output.
    00086         glEnable(GL_FRAMEBUFFER_SRGB);
    00087
    00088         //Enable depth test
    00089         glEnable(GL_DEPTH_TEST);
    00090         glDepthFunc(GL_LEQUAL);
    00091
    00092         //Enable face culling (default: backface culling)
    00093         glEnable(GL_CULL_FACE);
    00094
    00095         glPolygonOffset(1.1f, 4.0f);
    00096
    00097         //set viewport
    00098         glViewport(0,0, m_iWidth, m_iHeight);
    00099
    00100         //clear error status
    00101         glGetError();
    00102
    00103         Logger::instance()->log(Logger::DEBUG, "graphic initialized");
    00104 }
    00105
    00106
    00107 Graphic::~Graphic()
    00108 {
    00109         //nothing to do so far
    00110 }
    00111
    00112 void Graphic::initSceneObjects()
    00113 {
    00114         float fbo_bloom_factor = 0.5f;
    00115         float fbo_refract_reflect_factor = 0.5f;
    00116         float fbo_antialias_factor = 2.0f;
    00117
    00118         //This static vector holds the pointers to the created scene object instances.
    00119         //When the main program quits, the vector destroys each auto_ptr, where each
    00120         //auto_ptr calls the destructor of the hold instance.
    00121         static vector<auto_ptr<SceneObject>> created_instances;
    00122
    00123         /************************************************************************/
    00124         /* FBOs                                                                 */
    00125         /************************************************************************/
    00126         //SceneObject_FBO *fbo_bloom_s1 = new SceneObject_FBO(m_iWidth * fbo_bloom_factor, m_iHeight * fbo_bloom_factor, "bloom-only");
    00127         SceneObject_FBO *fbo_bloom_s2 = new SceneObject_FBO(m_iWidth * fbo_bloom_factor, m_iHeight * fbo_bloom_factor, "bloom-blurred-vertical");
    00128         SceneObject_FBO *fbo_bloom_s3 = new SceneObject_FBO(m_iWidth * fbo_bloom_factor, m_iHeight * fbo_bloom_factor, "bloom-blurred-horizontal");
    00129         SceneObject_FBO *fbo_bloom_s4 = new SceneObject_FBO(m_iWidth * fbo_bloom_factor, m_iHeight * fbo_bloom_factor, "fbo_bloom");
    00130         SceneObject_FBO *fbo_full_scene = new SceneObject_FBO(m_iWidth * fbo_antialias_factor, m_iHeight * fbo_antialias_factor, "fbo_scene", "fbo_scene_Z");
    00131         SceneObject_FBO *fbo_water_reflection = new SceneObject_FBO(m_iWidth * fbo_refract_reflect_factor, m_iHeight * fbo_refract_reflect_factor, "scene_reflection");
    00132         SceneObject_FBO *fbo_water_refraction = new SceneObject_FBO(m_iWidth * fbo_refract_reflect_factor, m_iHeight * fbo_refract_reflect_factor, "scene_refraction");
    00133         SceneObject_FBO *fbo_bloom_done = new SceneObject_FBO(m_iWidth * fbo_antialias_factor, m_iHeight * fbo_antialias_factor, "fbo_bloom_done");
    00134         SceneObject_FBO *fbo_light_z = new SceneObject_FBO(8192, 2048, "fbo_light_color", true, false);
    00135         SceneObject_FBO *fbo_dof_done = new SceneObject_FBO(m_iWidth * fbo_antialias_factor, m_iHeight * fbo_antialias_factor, "fbo_dof_done");
    00136
    00137         //created_instances.push_back(auto_ptr<SceneObject>(fbo_bloom_s1));
    00138         created_instances.push_back(auto_ptr<SceneObject>(fbo_bloom_s2));
    00139         created_instances.push_back(auto_ptr<SceneObject>(fbo_bloom_s3));
    00140         created_instances.push_back(auto_ptr<SceneObject>(fbo_bloom_s4));
    00141         created_instances.push_back(auto_ptr<SceneObject>(fbo_full_scene));
    00142         created_instances.push_back(auto_ptr<SceneObject>(fbo_water_reflection));
    00143         created_instances.push_back(auto_ptr<SceneObject>(fbo_water_refraction));
    00144         created_instances.push_back(auto_ptr<SceneObject>(fbo_bloom_done));
    00145         created_instances.push_back(auto_ptr<SceneObject>(fbo_light_z));
    00146
    00147         /************************************************************************/
    00148         /* Test Objects                                                   */
    00149         /************************************************************************/
    00150
    00151         SceneObject *test = new SceneObject_RenderFromLight();
    00152
    00153         created_instances.push_back(auto_ptr<SceneObject>(test));
    00154
    00155         /************************************************************************/
    00156         /* PostEffect Objects                                                   */
    00157         /************************************************************************/
    00158         SceneObject *posteffect_bloom_s1 = new SceneObject_PostEffect("bloom-step1", "fbo_scene");
    00159         SceneObject *posteffect_bloom_s2 = new SceneObject_PostEffect("bloom-step2", "bloom-blurred-vertical");
    00160         SceneObject *posteffect_bloom_s3 = new SceneObject_PostEffect("bloom-step3", "bloom-blurred-horizontal");
    00161         SceneObject *posteffect_bloom_final = new SceneObject_PostEffect("bloom-combine", "fbo_scene", "fbo_bloom", "fbo_scene_Z");
    00162         //SceneObject *posteffect_dof = new SceneObject_PostEffect("DoF-blur", "fbo_scene", "fbo_scene_Z");
    00163
    00164         created_instances.push_back(auto_ptr<SceneObject>(posteffect_bloom_s1));
    00165         created_instances.push_back(auto_ptr<SceneObject>(posteffect_bloom_s2));
    00166         created_instances.push_back(auto_ptr<SceneObject>(posteffect_bloom_s3));
    00167         created_instances.push_back(auto_ptr<SceneObject>(posteffect_bloom_final));
    00168 //      created_instances.push_back(auto_ptr<SceneObject>(posteffect_dof));
    00169
    00170         /************************************************************************/
    00171         /* SceneObjects                                                         */
    00172         /************************************************************************/
    00173         SceneObject *realRoot = new SceneObject();
    00174         SceneObject *root = new SceneObject();
    00175         SceneObject *scene_root = new SceneObject();
    00176         SceneObject *sky_sphere = new SceneObject_Skysphere();
    00177         SceneObject *water = new SceneObject_Water();
    00178         SceneObject *island = new SceneObject_Terrain();
    00179         SceneObject_OgreImport *bamboo1 = new SceneObject_OgreImport("models/Bamboo.mesh.xml");
    00180         SceneObject_OgreImport *bamboo2 = new SceneObject_OgreImport(bamboo1);
    00181         SceneObject *lightSphere = new SceneObject_LightSphere();
    00182         //SceneObject *parallax_quad = new SceneObject_SimpleQuad();
    00183
    00184         created_instances.push_back(auto_ptr<SceneObject>(realRoot));
    00185         created_instances.push_back(auto_ptr<SceneObject>(root));
    00186         created_instances.push_back(auto_ptr<SceneObject>(scene_root));
    00187         created_instances.push_back(auto_ptr<SceneObject>(sky_sphere));
    00188         created_instances.push_back(auto_ptr<SceneObject>(water));
    00189         created_instances.push_back(auto_ptr<SceneObject>(island));
    00190         created_instances.push_back(auto_ptr<SceneObject>(bamboo1));
    00191         created_instances.push_back(auto_ptr<SceneObject>(bamboo2));
    00192         created_instances.push_back(auto_ptr<SceneObject>(lightSphere));
    00193
    00194         /************************************************************************/
    00195         /* Fill Scenegraph                                                      */
    00196         /************************************************************************/
    00197         realRoot->addChild(posteffect_bloom_final);
    00198
    00199         //realRoot->addChild(scene_root);
    00200
    00201                     fbo_bloom_done->addChild(posteffect_bloom_final);
    00202
    00203                         posteffect_bloom_final->addChild(fbo_full_scene); // before last step blooming is applied
    00204
    00205                                 fbo_full_scene->addChild(fbo_light_z); // render the whole scene into this FBO
    00206                                     fbo_light_z->addChild(test);
    00207                                         test->addChild(root);
    00208                                 fbo_full_scene->addChild(root); // render the whole scene into this FBO
    00209
    00210                         posteffect_bloom_final->addChild(fbo_bloom_s4);
    00211
    00212                                 fbo_bloom_s4->addChild(posteffect_bloom_s3);
    00213                                 posteffect_bloom_s3->addChild(fbo_bloom_s3);
    00214                                 fbo_bloom_s3->addChild(posteffect_bloom_s2);
    00215                                 posteffect_bloom_s2->addChild(fbo_bloom_s2);
    00216                                 fbo_bloom_s2->addChild(posteffect_bloom_s1);
    00217                                 //posteffect_bloom_s1->addChild(fbo_dof_done);
    00218                                 //fbo_dof_done->addChild(posteffect_dof);
    00219
    00220                                 root->addChild(sky_sphere);
    00221
    00222
    00223                                 root->addChild(lightSphere);
    00224                                 root->addChild(scene_root);
    00225                                         // Terrain and bamboos
    00226                                         scene_root->addChild(island);
    00227                                         scene_root->addChild(bamboo1);
    00228                                         scene_root->addChild(bamboo2);
    00229
    00230
    00231                                 root->addChild(fbo_water_refraction);
    00232                                         fbo_water_refraction->addChild(island);
    00233
    00234                                 root->addChild(water);
    00235                                         water->addChild(fbo_water_reflection);
    00236                                             fbo_water_reflection->addChild(scene_root);
    00237                                             fbo_water_reflection->addChild(sky_sphere);
    00238
    00239
    00240         this->root = realRoot;
    00241         this->scene_root = scene_root;
    00242
    00243         /************************************************************************/
    00244         /* setTransformMatrix                                                   */
    00245         /************************************************************************/
    00246         island->setTransformMatrix(glm::scale(25.f,25.f,25.f));
    00247         for (int i=0; i < 63; i++)
    00248             bamboo1->addInstancedTransformMatrix( glm::translate( 9.5f + (rand() % 100) * 0.002f + i / 10.0f, 1.5f + rand() % 10 * 0.01f, 8.8f + (rand() % 100) * 0.002f + (i % 3) / (float)(i % 6 + 1)) * glm::rotate((rand() % 30) - 15.0f, (rand() % 100) * 1.0f, (rand() % 100) * 1.0f, (rand() % 100) * 1.0f));
    00249
    00250         for (int i=0; i < 63; i++)
    00251             bamboo1->addInstancedTransformMatrix( glm::translate( 8.5f + (rand() % 100) * 0.002f + i / 10.0f, 1.5f + rand() % 20 * 0.01f, 13.8f + (rand() % 100) * 0.002f + (i % 3) / (float)(i % 6 + 1)) * glm::rotate((rand() % 30) - 15.0f, (rand() % 100) * 1.0f, (rand() % 100) * 1.0f, (rand() % 100) * 1.0f));
    00252
    00253         for (int i=0; i < 63; i++)
    00254             bamboo2->addInstancedTransformMatrix( glm::translate( 8.5f + (rand() % 100) * 0.002f + i / 10.0f, 1.5f + rand() % 20 * 0.01f, 14.5f + (rand() % 100) * 0.002f + (i % 3) / (float)(i % 6 + 1)) * glm::rotate((rand() % 30) - 15.0f, (rand() % 100) * 1.0f, (rand() % 100) * 1.0f, (rand() % 100) * 1.0f));
    00255
    00256         for (int i=0; i < 63; i++)
    00257             bamboo2->addInstancedTransformMatrix( glm::translate( 8.5f + (rand() % 100) * 0.002f + i / 10.0f, 1.5f + rand() % 20 * 0.01f, 15.2f + (rand() % 100) * 0.002f + (i % 3) / (float)(i % 6 + 1)) * glm::rotate((rand() % 30) - 15.0f, (rand() % 100) * 1.0f, (rand() % 100) * 1.0f, (rand() % 100) * 1.0f));
    00258
    00259         sky_sphere->setTransformMatrix(glm::scale(100.0f, 100.0f, 100.0f) * glm::rotate(270.0f, glm::vec3(1.0f, 0.0f, 0.0f)));
    00260         water->setTransformMatrix(glm::scale(1.0f, -1.0f, 1.0f)); //set y-reflection for all childs of water
    00261 //      parallax_quad->setTransformMatrix(glm::translate(11.5f,0.715f,10.55f) * glm::scale(1.0f, 1.0f, 1.0f));
    00262 }
    00263
    00264 void Graphic::drawSceneObject(SceneObject *obj)
    00265 {
    00266     obj->render(glm::mat4());
    00267 }
    00268
    00269 void Graphic::draw()
    00270 {
    00271         static int fps=0;
    00272         static double last_time;
    00273
    00274         /* test block. rotates the root sceneobject */
    00275         static int count=0;
    00276
    00277         if (count==0)
    00278             this->initSceneObjects();
    00279
    00280         count++;
    00281         fps++;
    00282
    00283         Camera::instance()->move();
    00284
    00285         //clear buffers
    00286         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    00287
    00288         // draw the scene
    00289         this->drawSceneObject(this->root);
    00290
    00291         // swap back and front buffers
    00292         glfwSwapBuffers();
    00293
    00294         if (glfwGetTime() - last_time >= 0.1)
    00295         {
    00296             last_time = glfwGetTime();
    00297             stringstream puffer;
    00298             string puffer2;
    00299             puffer << (fps * 10);
    00300             puffer >> puffer2;
    00301
    00302             string puffer3 = string("BambooIsland - by Christian Moellinger, FPS:") + puffer2;
    00303             glfwSetWindowTitle(puffer3.data());
    00304             fps=0;
    00305         }
    00306
    00307 }

All Classes Functions Variables Enumerations Enumerator