main.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 
00003 #include <GL/glew.h>
00004 
00005 #include <pangolin/glcuda.h>
00006 #include <pangolin/pangolin.h>
00007 
00008 #include <cuda_runtime.h>
00009 #include <cuda_gl_interop.h>
00010 #include <vector_types.h>
00011 
00012 #ifdef USE_CUTIL
00013   #include <cutil_inline.h>
00014 #endif // USE_CUTIL
00015 
00016 using namespace pangolin;
00017 using namespace std;
00018 
00019 // Mesh size
00020 const int mesh_width=256;
00021 const int mesh_height=256;
00022 
00023 extern "C" void launch_kernel(float4* dVertexArray, uchar4* dColourArray, unsigned int width, unsigned int height, float time);
00024 
00025 int main( int /*argc*/, char* argv[] )
00026 {
00027 #ifdef USE_CUTIL
00028     cudaGLSetGLDevice(cutGetMaxGflopsDeviceId());
00029 #else
00030     cudaGLSetGLDevice(0);
00031 #endif
00032 
00033   pangolin::CreateGlutWindowAndBind("Main",640,480);
00034   glewInit();
00035 
00036   // Create vertex and colour buffer objects and register them with CUDA
00037   GlBufferCudaPtr vertex_array(
00038       GlArrayBuffer, mesh_width * mesh_height * sizeof(float4),
00039       cudaGraphicsMapFlagsWriteDiscard, GL_STREAM_DRAW
00040   );
00041   GlBufferCudaPtr colour_array(
00042       GlArrayBuffer, mesh_width * mesh_height * sizeof(uchar4),
00043       cudaGraphicsMapFlagsWriteDiscard, GL_STREAM_DRAW
00044   );
00045 
00046   // Define Camera Render Object (for view / scene browsing)
00047   pangolin::OpenGlRenderState s_cam(
00048     ProjectionMatrix(640,480,420,420,320,240,0.1,1000),
00049     ModelViewLookAt(-0,2,-2, 0,0,0, AxisY)
00050   );
00051   const int UI_WIDTH = 180;
00052 
00053   // Add named OpenGL viewport to window and provide 3D Handler
00054   View& d_cam = pangolin::Display("cam")
00055     .SetBounds(0.0, 1.0, Attach::Pix(UI_WIDTH), 1.0, -640.0f/480.0f)
00056     .SetHandler(new Handler3D(s_cam));
00057 
00058   // Add named Panel and bind to variables beginning 'ui'
00059   // A Panel is just a View with a default layout and input handling
00060   View& d_panel = pangolin::CreatePanel("ui")
00061       .SetBounds(0.0, 1.0, 0.0, Attach::Pix(UI_WIDTH));
00062 
00063 #ifdef USE_CUTIL
00064   // Apply timer as used by CUDA samples
00065   // The fps measure they use is actually completely incorrect!
00066   unsigned int timer = 0;
00067   cutCreateTimer(&timer);
00068 #endif
00069 
00070   // Default hooks for exiting (Esc) and fullscreen (tab).
00071   for(int frame=0; !pangolin::ShouldQuit(); ++frame)
00072   {
00073     static double time = 0;
00074     static Var<double> delta("ui.time delta", 0.001, 0, 0.005);
00075 
00076 #ifdef USE_CUTIL
00077     static Var<double> fps("ui.fps");
00078     cutStartTimer(timer);
00079 #endif
00080 
00081     if(HasResized())
00082       DisplayBase().ActivateScissorAndClear();
00083 
00084     d_cam.ActivateScissorAndClear(s_cam);
00085     glEnable(GL_DEPTH_TEST);
00086     glColor3f(1.0,1.0,1.0);
00087 
00088     {
00089       CudaScopedMappedPtr var(vertex_array);
00090       CudaScopedMappedPtr car(colour_array);
00091       launch_kernel((float4*)*var,(uchar4*)*car,mesh_width,mesh_height,time);
00092       time += delta;
00093     }
00094 
00095     vertex_array.Bind();
00096     glVertexPointer(4, GL_FLOAT, 0, 0);
00097     glEnableClientState(GL_VERTEX_ARRAY);
00098 
00099     colour_array.Bind();
00100     glColorPointer(4, GL_UNSIGNED_BYTE, 0, 0);
00101     glEnableClientState(GL_COLOR_ARRAY);
00102 
00103     glDrawArrays(GL_POINTS, 0, mesh_width * mesh_height);
00104 
00105     glDisableClientState(GL_VERTEX_ARRAY);
00106     glDisableClientState(GL_COLOR_ARRAY);
00107 
00108     // Render our UI panel when we receive input
00109     if(!(frame%100))
00110     {
00111 #ifdef USE_CUTIL
00112       fps = 1000.0 / cutGetAverageTimerValue(timer);
00113       cutResetTimer(timer);
00114 #endif
00115     }
00116 
00117     d_panel.Render();
00118 
00119     // Swap frames and Process Events
00120     pangolin::FinishGlutFrame();
00121 
00122 #ifdef USE_CUTIL
00123     cutStopTimer(timer);
00124 #endif
00125   }
00126 
00127 #ifdef USE_CUTIL
00128   cutilCheckError( cutDeleteTimer( timer));
00129 #endif
00130 
00131   return 0;
00132 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


pangolin_wrapper
Author(s): Todor Stoyanov
autogenerated on Wed Feb 13 2013 14:03:25