SWE
/import/home/rettenbs/src/SWE/src/opengl/visualization.h
00001 #ifndef VISUALIZATION_H
00002 #define VISUALIZATION_H
00003 // =====================================================================
00004 // This file is part of SWE_CUDA (see file SWE_Block.cu for details).
00005 // 
00006 // Copyright (C) 2010,2011 Tobias Schnabel
00007 // Copyright (C) 2012      Sebastian Rettenberger
00008 // 
00009 // SWE_CUDA is free software: you can redristribute it and/or modify
00010 // it under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 3 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // SWE_CUDA is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with SWE_CUDA.  If not, see <http://www.gnu.org/licenses/>.
00021 // =====================================================================
00022 #include <SDL/SDL.h>
00023 #include <SDL/SDL_opengl.h>
00024 #include <cuda_runtime.h>
00025 #include <cuda_gl_interop.h>
00026 #include "camera.h"
00027 #include "simulation.h"
00028 #include "shader.h"
00029 #include "vbo.h"
00030 #ifdef USESDLTTF
00031 #include "text.h"
00032 #endif // USESDLTTF
00033 
00034 #include "scenarios/SWE_VisInfo.hh"
00035 
00036 void checkCUDAError(const char *msg);
00037 typedef enum RenderMode {
00038    SHADED, WIREFRAME, WATERSHADER
00039 } RenderMode;
00040 
00041 class Visualization {
00042 public:
00043         // Constructor and Destructor
00044         Visualization(int windowWidth, int windowHeight, const char* window_title);
00045         ~Visualization();
00046 
00047         void init(Simulation &sim, SWE_VisInfo *visInfo = 0L);
00048         void cleanUp();
00049         Camera* camera;
00050 
00051         // Access to CUDA VBO pointers
00052         cudaGraphicsResource** getCudaNormalsPtr();
00053         cudaGraphicsResource** getCudaWaterSurfacePtr();
00054 
00055         // Main rendering function
00056         void renderDisplay();
00057         
00058         // Rescale water
00059         void modifyWaterScaling(float factor);
00060 
00061         // Helper functions
00062         void setRenderingMode(RenderMode mode);
00063         void toggleRenderingMode();
00064         int resizeWindow(int newWidth, int newHeight);
00065 
00066         static bool isExtensionSupported(const char* szTargetExtension );
00067 private:
00068         // Init helper functions
00069         void initSDL();
00070         void initGLDefaults();
00071         void initCUDA();
00072 
00073         void setProjection();
00074 
00075         void updateBathymetryVBO(Simulation &sim);
00076 
00077         // Drawing functions
00078         void DrawWaterSurface();
00079         void DrawBathymetry();
00080         int grid_xsize;
00081         int grid_ysize;
00082 
00083         // Vertex Buffer objects
00084         VBO vboBathymetry;
00085         VBO vboVerticesIndex;
00086         VBO vboWaterSurface;
00087         VBO vboNormals;
00088         // Bathymetry color
00089         VBO vboBathColor;
00090 
00095         GLvoid** indicesOffset;
00100         GLsizei* indicesCount;
00101 
00102         struct cudaGraphicsResource* cuda_vbo_watersurface;
00103         struct cudaGraphicsResource* cuda_vbo_normals;
00104 
00105         // VBO management functions
00106         void createIndicesVBO(int xsize, int ysize);
00107         void createVertexVBO(VBO &vbo, struct cudaGraphicsResource *&vbo_res,
00108                 unsigned int vbo_res_flags);
00109 
00110         void deleteCudaResource(struct cudaGraphicsResource *&vbo_res);
00111 
00112         // Rendering mode
00113         RenderMode renderMode;
00114 
00115         // Water/Bathymetry scaling/offset
00116         float wScale, bScale, bOffset;
00117 
00119         GLint wScaleLocation;
00120 
00121         // Shaders
00122         Shader* waterShader;
00123 
00124 #ifdef USESDLTTF
00125         // Text helper class
00126         Text* text;
00127 #endif // USESDLTTF
00128 
00129         int windowWidth;
00130         int windowHeight;
00131 
00132         // Helper function
00133         int coord(int x, int y, int width = -1);
00134 
00135         static void height2Color(float height, GLfloat *color);
00136         static GLfloat mix(GLfloat a, GLfloat b, float factor)
00137         {
00138                 return a * (1-factor) + b * factor;
00139         }
00140 
00141         static PFNGLPRIMITIVERESTARTINDEXNVPROC glPrimitiveRestartIndexNV;
00142 };
00143 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends