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

BambooIsland Abgabe 2

- Main Page
- Related Pages
- Classes
- Files

- File List

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

    00001
    00002 /*
    00003 * main source file for realtime-lu project
    00004 * written by:
    00005 * christian moellinger <ch.moellinger@gmail.com>
    00006 * wolfgang ludwig <wl@ViennaLinux.at>
    00007 * 10/2010
    00008 */
    00009
    00010 #include "common_gl.h"
    00011 #include "Graphic.h"
    00012 #include "ShaderManager.h"
    00013 #include "Shader.h"
    00014 #include "Camera.h"
    00015 #include "Logger.h"
    00016 #include "TextureManager.h"
    00017 #include "SoundManager.h"
    00018
    00019 #include <iostream>
    00020
    00021
    00022 using namespace std;
    00023
    00024 const int resolution_x = 1024;
    00025 const int resolution_y = 768;
    00026
    00027 double current_time, last_time, deltaTime = 0;
    00028 bool stop = false;
    00029 bool handleMouseMove = false;
    00030 bool sound = true;
    00031 SoundManager* soundManager;
    00032
    00035 //double getTimeDelta(){
    00036 //      current_time = glfwGetTime();
    00037 //      deltaTime = current_time - last_time;
    00038 //      last_time = current_time;
    00039 //      return deltaTime;
    00040 //}
    00041
    00042 void GLFWCALL handleKeyboard(int keyIdentifier, int newKeyState)
    00043 {
    00044         double movespeed = 0.01 /* getTimeDelta()*/;
    00045         float factor = 0;
    00046
    00047         if (newKeyState == GLFW_PRESS)
    00048             factor = 1.0;
    00049         else if (newKeyState == GLFW_RELEASE)
    00050             factor = -1.0;
    00051         else
    00052             Logger::instance()->log(Logger::ERROR, "keyboard callback sends unknown keystate");
    00053
    00054         switch (keyIdentifier)
    00055         {
    00056         case 'W':
    00057             Camera::instance()->addToMoveVector(glm::vec3(0.0, 0.0, (factor * movespeed) ));
    00058             break;
    00059         case 'A':
    00060             Camera::instance()->addToMoveVector(glm::vec3( (factor * movespeed), 0.0, 0.0));
    00061             break;
    00062         case 'S':
    00063             Camera::instance()->addToMoveVector(glm::vec3(0.0, 0.0, -(factor * movespeed) ));
    00064             break;
    00065         case 'D':
    00066             Camera::instance()->addToMoveVector(glm::vec3( -(factor * movespeed), 0.0, 0.0));
    00067             break;
    00068         case GLFW_KEY_F5:
    00069             Camera::instance()->setMode(Camera::Record);
    00070             break;
    00071         case GLFW_KEY_F6:
    00072             Camera::instance()->setMode(Camera::Replay);
    00073             break;
    00074         case GLFW_KEY_SPACE:
    00075             Camera::instance()->addToMoveVector(glm::vec3(0.0, (factor * movespeed), 0.0));
    00076             break;
    00077         case GLFW_KEY_LCTRL:
    00078             Camera::instance()->addToMoveVector(glm::vec3(0.0, -(factor * movespeed), 0.0));
    00079             break;
    00080
    00081         case GLFW_KEY_ESC:
    00082             stop = true;
    00083             break;
    00084         }
    00085 }
    00086
    00087 void GLFWCALL handleMousePos(int x, int y)
    00088 {
    00089     if (handleMouseMove)
    00090     {
    00091         int diff_x = (resolution_x / 2) - x;
    00092         int diff_y = (resolution_y / 2) - y;
    00093
    00094         if (diff_x || diff_y)   //only change camera and reset mouse if the reported position is != middle_of_the_screen. That's because glfwSetMousePos() also emits handleMousePos
    00095         {
    00096             Camera::instance()->incRotationX(diff_x / 10.0);
    00097             Camera::instance()->incRotationY(diff_y / 10.0);
    00098
    00099             glfwSetMousePos((resolution_x / 2), (resolution_y / 2));
    00100         }
    00101     }
    00102 }
    00103
    00104 void GLFWCALL handleMouseWheel(int pos)
    00105 {
    00106         // ToDo: react on mousewheel
    00107
    00108 }
    00109
    00110 void GLFWCALL handleMouseButtons(int button, int action)
    00111 {
    00112         /* ToDo:
    00113         * use mouse button1 to print current
    00114         * view vector for camera animation path
    00115         */
    00116
    00117     if (button == 0 && action == 1)
    00118     {
    00119         glfwSetMousePos((resolution_x / 2), (resolution_y / 2));
    00120         handleMouseMove = true;
    00121     }
    00122     else if (button == 0 && action == 0)
    00123         handleMouseMove = false;
    00124
    00125         if (button==0)
    00126         Logger::instance()->log(Logger::TRACE, "mouse button 0");
    00127     if (button==1)
    00128         Logger::instance()->log(Logger::TRACE, "mouse button 1");
    00129     if (button==2)
    00130         Logger::instance()->log(Logger::TRACE, "mouse button 2");
    00131
    00132     if (action==0)
    00133         Logger::instance()->log(Logger::TRACE, "mouse action 0");
    00134     if (action==1)
    00135         Logger::instance()->log(Logger::TRACE, "mouse action 1");
    00136     if (action==2)
    00137         Logger::instance()->log(Logger::TRACE, "mouse action 2");
    00138 }
    00139
    00140 int main()
    00141 {
    00142         /************************************************************************/
    00143         /* SETUP LOGGER                                                         */
    00144         /************************************************************************/
    00145         Logger::instance()->setLogLevelForConsole(Logger::DEBUG);
    00146         Logger::instance()->setLogLevelForFile(Logger::ERROR);
    00147
    00148         if (glfwInit() == GL_TRUE)
    00149                 Logger::instance()->log(Logger::TRACE, "glfw successfully initialized");
    00150         else
    00151                 Logger::instance()->log(Logger::FATAL, "glfw initialization failed");
    00152
    00153         /************************************************************************/
    00154         /* SETUP WINDOW and load shaders                                        */
    00155         /************************************************************************/
    00156         Graphic graphic(resolution_x, resolution_y, false);
    00157
    00158         ShaderManager::instance()->addShader("bloom-step1", "shaders/bloom.vs", "shaders/bloom_step1.fs");
    00159         ShaderManager::instance()->addShader("bloom-step2", "shaders/bloom.vs", "shaders/bloom_step2.fs");
    00160         ShaderManager::instance()->addShader("bloom-step3", "shaders/bloom.vs", "shaders/bloom_step3.fs");
    00161         ShaderManager::instance()->addShader("bloom-combine", "shaders/bloom.vs", "shaders/bloom_combine.fs");
    00162
    00163         ShaderManager::instance()->addShader("phong_shader", "shaders/phong_shading.vs", "shaders/phong_shading.fs");
    00164         ShaderManager::instance()->addShader("phong_shader-reflection", "shaders/phong_shading_water-reflection.vs", "shaders/phong_shading_water-reflection.fs");
    00165         ShaderManager::instance()->addShader("bump_shader", "shaders/bump_shading.vs", "shaders/bump_shading.fs");
    00166         ShaderManager::instance()->addShader("parallax_shader", "shaders/bump_shading.vs", "shaders/parallax_shading.fs");
    00167         ShaderManager::instance()->addShader("simple_shader", "shaders/simple-shading.vs", "shaders/simple-shading.fs");
    00168         ShaderManager::instance()->addShader("skysphere", "shaders/skysphere.vs", "shaders/skysphere.fs");
    00169         ShaderManager::instance()->addShader("water", "shaders/water.vs", "shaders/water.fs");
    00170         ShaderManager::instance()->addShader("heightmap", "shaders/heightmap.vs", "shaders/heightmap.fs");
    00171         ShaderManager::instance()->addShader("heightmap-reflect", "shaders/heightmap-reflect.vs", "shaders/heightmap-reflect.fs");
    00172         ShaderManager::instance()->addShader("heightmap-simple", "shaders/heightmap-simple.vs", "shaders/heightmap-simple.fs");
    00173         ShaderManager::instance()->addShader("light-sphere", "shaders/light-sphere.vs", "shaders/light-sphere.fs");
    00174         ShaderManager::instance()->activateShader("parallax_shader");
    00175
    00176         /************************************************************************/
    00177         /* Load textures                                                        */
    00178         /************************************************************************/
    00179
    00180         TextureManager::instance()->loadTexture("water_bump","textures/waterBump2.JPG", true);
    00181         TextureManager::instance()->loadTexture("skysphere","textures/skysphere2.jpg", true);
    00182
    00183         TextureManager::instance()->loadTexture("heightmap","textures/level1.png", false);
    00184
    00185         TextureManager::instance()->loadTexture("terrain_detail1","textures/terrain_gras.jpg", true);
    00186         TextureManager::instance()->loadTexture("terrain_detail1_normal","textures/terrain_gras_normal.jpg", false);
    00187         TextureManager::instance()->loadTexture("terrain_detail1_bump","textures/terrain_gras_bump.jpg", false);
    00188         TextureManager::instance()->loadTexture("terrain_detail2","textures/terrain_sand.jpg", true);
    00189         TextureManager::instance()->loadTexture("terrain_detail2_normal","textures/terrain_sand_normal.jpg", false);
    00190         TextureManager::instance()->loadTexture("terrain_detail2_bump","textures/terrain_sand_bump.jpg", false);
    00191
    00192         TextureManager::instance()->loadTexture("terrain_detail3","textures/stone09.jpg", true);
    00193         TextureManager::instance()->loadTexture("terrain_detail3_normal","textures/stone09n.jpg", false);
    00194         TextureManager::instance()->loadTexture("terrain_detail3_bump","textures/stone09b.jpg", false);
    00195
    00196         /************************************************************************/
    00197         /* Register Callbacks (Input Handling)                                  */
    00198         /************************************************************************/
    00199         //glfwDisable(GLFW_MOUSE_CURSOR);
    00200         glfwSetKeyCallback(handleKeyboard);
    00201         glfwSetMousePosCallback(handleMousePos);
    00202         glfwSetMouseButtonCallback(handleMouseButtons);
    00203         glfwSetMouseWheelCallback(handleMouseWheel);
    00204
    00205         /************************************************************************/
    00206         /* Setup Sound                                                          */
    00207         /************************************************************************/
    00208         if (sound==true){
    00209         soundManager = new SoundManager();
    00210         soundManager->playSound("sounds/jungleNoise.mp3");
    00211         }
    00212
    00213         /************************************************************************/
    00214         /* MAIN LOOP                                                            */
    00215         /************************************************************************/
    00216         while(stop == false)
    00217         {
    00218                 //stop if window was closed by user
    00219                 if (!glfwGetWindowParam(GLFW_OPENED))
    00220                         stop = true;
    00221                 if(!stop) graphic.draw();
    00222         }
    00223
    00224         /************************************************************************/
    00225         /* CLEAN UP                                                             */
    00226         /************************************************************************/
    00227         if (sound==true)
    00228             delete soundManager; // call soundManager destructor
    00229
    00230         glfwTerminate();
    00231
    00232         exit(EXIT_SUCCESS);
    00233 }

All Classes Functions Variables Enumerations Enumerator