1 #include <iostream>
2 
3 #include <GL/glew.h>
4 
5 #include <pangolin/pangolin.h>
6 #include <pangolin/gl/glcuda.h>
7 #include <pangolin/gl/glvbo.h>
8 
9 #include <cuda_runtime.h>
10 #include <cuda_gl_interop.h>
11 #include <vector_types.h>
12 
13 using namespace pangolin;
14 using namespace std;
15 
16 // Mesh size
17 const int mesh_width=256;
18 const int mesh_height=256;
19 
20 extern "C" void launch_kernel(float4* dVertexArray, uchar4* dColourArray, unsigned int width, unsigned int height, float time);
21 
main(int,char * argv[])22 int main( int /*argc*/, char* argv[] )
23 {
24 //  cudaGLSetGLDevice(0);
25 
26   pangolin::CreateWindowAndBind("Main",640,480);
27   glewInit();
28 
29   // 3D Mouse handler requires depth testing to be enabled
30   glEnable(GL_DEPTH_TEST);
31 
32   // Create vertex and colour buffer objects and register them with CUDA
33   GlBufferCudaPtr vertex_array(
34       GlArrayBuffer, mesh_width*mesh_height, GL_FLOAT, 4,
35       cudaGraphicsMapFlagsWriteDiscard, GL_STREAM_DRAW
36   );
37   GlBufferCudaPtr colour_array(
38       GlArrayBuffer, mesh_width*mesh_height, GL_UNSIGNED_BYTE, 4,
39       cudaGraphicsMapFlagsWriteDiscard, GL_STREAM_DRAW
40   );
41 
42   // Define Camera Render Object (for view / scene browsing)
43   pangolin::OpenGlRenderState s_cam(
44     ProjectionMatrix(640,480,420,420,320,240,0.1,1000),
45     ModelViewLookAt(-0,2,-2, 0,0,0, AxisY)
46   );
47   const int UI_WIDTH = 180;
48 
49   // Add named OpenGL viewport to window and provide 3D Handler
50   View& d_cam = pangolin::Display("cam")
51     .SetBounds(0.0, 1.0, Attach::Pix(UI_WIDTH), 1.0, -640.0f/480.0f)
52     .SetHandler(new Handler3D(s_cam));
53 
54   // Add named Panel and bind to variables beginning 'ui'
55   // A Panel is just a View with a default layout and input handling
56   View& d_panel = pangolin::CreatePanel("ui")
57       .SetBounds(0.0, 1.0, 0.0, Attach::Pix(UI_WIDTH));
58 
59   // Default hooks for exiting (Esc) and fullscreen (tab).
60   for(int frame=0; !pangolin::ShouldQuit(); ++frame)
61   {
62     static double time = 0;
63     static Var<double> delta("ui.time delta", 0.001, 0, 0.005);
64 
65     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
66 
67     d_cam.Activate(s_cam);
68     glColor3f(1.0,1.0,1.0);
69 
70     {
71       CudaScopedMappedPtr var(vertex_array);
72       CudaScopedMappedPtr car(colour_array);
73       launch_kernel((float4*)*var,(uchar4*)*car,mesh_width,mesh_height,time);
74       time += delta;
75     }
76 
77     pangolin::RenderVboCbo(vertex_array, colour_array);
78 
79     // Swap frames and Process Events
80     pangolin::FinishFrame();
81   }
82 
83   return 0;
84 }
85