码迷,mamicode.com
首页 > 其他好文 > 详细

附录四 其他头文件

时间:2017-10-12 01:00:17      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:oca   void   bsp   source   exit   add   linux   res   dac   

cpu_bitmap.h

 1 #ifndef __CPU_BITMAP_H__
 2 #define __CPU_BITMAP_H__
 3 
 4 #include "gl_helper.h"
 5 
 6 struct CPUBitmap
 7 {
 8     unsigned char   *pixels;
 9     int             x, y;
10     void            *dataBlock;
11     void            (*bitmapExit)(void*);
12 
13     CPUBitmap( int width, int height, void *d = NULL )
14     {
15         pixels = new unsigned char[width * height * 4];
16         x = width;
17         y = height;
18         dataBlock = d;
19     }
20 
21     ~CPUBitmap()
22     {
23         delete [] pixels;
24     }
25 
26     unsigned char* get_ptr( void ) const   { return pixels; }
27     long image_size( void ) const { return x * y * 4; }
28 
29     void display_and_exit( void(*e)(void*) = NULL )
30     {
31         CPUBitmap**   bitmap = get_bitmap_ptr();
32         *bitmap = this;
33         bitmapExit = e;
34         // a bug in the Windows GLUT implementation prevents us from
35         // passing zero arguments to glutInit()
36         int c=1;
37         char* dummy = "";
38         glutInit( &c, &dummy );
39         glutInitDisplayMode( GLUT_SINGLE | GLUT_RGBA );
40         glutInitWindowSize( x, y );
41         glutCreateWindow( "bitmap" );
42         glutKeyboardFunc(Key);
43         glutDisplayFunc(Draw);
44         glutMainLoop();
45     }
46 
47      // static method used for glut callbacks
48     static CPUBitmap** get_bitmap_ptr( void )
49     {
50         static CPUBitmap   *gBitmap;
51         return &gBitmap;
52     }
53 
54    // static method used for glut callbacks
55     static void Key(unsigned char key, int x, int y)
56     {
57         switch (key)
58         {
59             case 27:
60                 CPUBitmap*   bitmap = *(get_bitmap_ptr());
61                 if (bitmap->dataBlock != NULL && bitmap->bitmapExit != NULL)
62                     bitmap->bitmapExit( bitmap->dataBlock );
63             exit(0);
64         }
65     }
66 
67     // static method used for glut callbacks
68     static void Draw( void )
69     {
70         CPUBitmap*   bitmap = *(get_bitmap_ptr());
71         glClearColor( 0.0, 0.0, 0.0, 1.0 );
72         glClear( GL_COLOR_BUFFER_BIT );
73         glDrawPixels( bitmap->x, bitmap->y, GL_RGBA, GL_UNSIGNED_BYTE, bitmap->pixels );
74         glFlush();
75     }
76 };
77 
78 #endif  // __CPU_BITMAP_H__

 

cpu_anim.h

  1 #ifndef __CPU_ANIM_H__
  2 #define __CPU_ANIM_H__
  3 
  4 #include "D:\Code\CUDA\book\common\gl_helper.h"
  5 #include <iostream>
  6 
  7 
  8 struct CPUAnimBitmap
  9 {
 10     unsigned char    *pixels;
 11     int     width, height;
 12     void    *dataBlock;
 13     void (*fAnim)(void*,int);
 14     void (*animExit)(void*);
 15     void (*clickDrag)(void*,int,int,int,int);
 16     int     dragStartX, dragStartY;
 17 
 18     CPUAnimBitmap( int w, int h, void *d = NULL )
 19     {
 20         width = w;
 21         height = h;
 22         pixels = new unsigned char[width * height * 4];
 23         dataBlock = d;
 24         clickDrag = NULL;
 25     }
 26 
 27     ~CPUAnimBitmap()
 28     {
 29         delete [] pixels;
 30     }
 31 
 32     unsigned char* get_ptr( void ) const   { return pixels; }
 33     long image_size( void ) const { return width * height * 4; }
 34 
 35     void click_drag(void(*f)(void*, int, int, int, int))
 36     {
 37         clickDrag = f;
 38     }
 39 
 40     void anim_and_exit(void(*f)(void*, int), void(*e)(void*))
 41     {
 42         CPUAnimBitmap**   bitmap = get_bitmap_ptr();
 43         *bitmap = this;
 44         fAnim = f;
 45         animExit = e;
 46         // a bug in the Windows GLUT implementation prevents us from
 47         // passing zero arguments to glutInit()
 48         int c=1;
 49         char* dummy = "";
 50         glutInit( &c, &dummy );
 51         glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
 52         glutInitWindowSize( width, height );
 53         glutCreateWindow( "bitmap" );
 54         glutKeyboardFunc(Key);
 55         glutDisplayFunc(Draw);
 56         if (clickDrag != NULL)
 57             glutMouseFunc( mouse_func );
 58         glutIdleFunc( idle_func );
 59         glutMainLoop();
 60     }
 61 
 62     // static method used for glut callbacks
 63     static CPUAnimBitmap** get_bitmap_ptr(void)
 64     {
 65         static CPUAnimBitmap*   gBitmap;
 66         return &gBitmap;
 67     }
 68 
 69     // static method used for glut callbacks
 70     static void mouse_func(int button, int state, int mx, int my)
 71     {
 72         if (button == GLUT_LEFT_BUTTON)
 73         {
 74             CPUAnimBitmap*   bitmap = *(get_bitmap_ptr());
 75             if (state == GLUT_DOWN)
 76             {
 77                 bitmap->dragStartX = mx;
 78                 bitmap->dragStartY = my;
 79             }
 80             else if (state == GLUT_UP)
 81                 bitmap->clickDrag( bitmap->dataBlock,bitmap->dragStartX,bitmap->dragStartY,mx, my );
 82         }
 83     }
 84 
 85     // static method used for glut callbacks
 86     static void idle_func( void )
 87     {
 88         static int ticks = 1;
 89         CPUAnimBitmap*   bitmap = *(get_bitmap_ptr());
 90         bitmap->fAnim( bitmap->dataBlock, ticks++ );
 91         glutPostRedisplay();
 92     }
 93 
 94     // static method used for glut callbacks
 95     static void Key(unsigned char key, int x, int y)
 96     {
 97         switch (key)
 98         {
 99             case 27:
100                 CPUAnimBitmap*   bitmap = *(get_bitmap_ptr());
101                 bitmap->animExit( bitmap->dataBlock );
102                 //delete bitmap;
103             exit(0);
104         }
105     }
106 
107     // static method used for glut callbacks
108     static void Draw( void )
109     {
110         CPUAnimBitmap*   bitmap = *(get_bitmap_ptr());
111         glClearColor( 0.0, 0.0, 0.0, 1.0 );
112         glClear( GL_COLOR_BUFFER_BIT );
113         glDrawPixels( bitmap->width, bitmap->height, GL_RGBA, GL_UNSIGNED_BYTE, bitmap->pixels );
114         glutSwapBuffers();
115     }
116 };
117 
118 #endif  // __CPU_ANIM_H__

 

gpu_anim.h

  1 #ifndef __GPU_ANIM_H__
  2 #define __GPU_ANIM_H__
  3 #include "D:\Code\CUDA\book\common\gl_helper.h"
  4 #include "cuda.h"
  5 #include "cuda_gl_interop.h"
  6 #include <iostream>
  7 
  8 PFNGLBINDBUFFERARBPROC    glBindBuffer     = NULL;
  9 PFNGLDELETEBUFFERSARBPROC glDeleteBuffers  = NULL;
 10 PFNGLGENBUFFERSARBPROC    glGenBuffers     = NULL;
 11 PFNGLBUFFERDATAARBPROC    glBufferData     = NULL;
 12 
 13 struct GPUAnimBitmap
 14 {
 15     GLuint  bufferObj;
 16     cudaGraphicsResource *resource;
 17     int     width, height;
 18     void    *dataBlock;
 19     void (*fAnim)(uchar4*,void*,int);
 20     void (*animExit)(void*);
 21     void (*clickDrag)(void*,int,int,int,int);
 22     int     dragStartX, dragStartY;
 23 
 24     GPUAnimBitmap( int w, int h, void *d = NULL )
 25     {
 26         width = w;
 27         height = h;
 28         dataBlock = d;
 29         clickDrag = NULL;
 30 
 31         // first, find a CUDA device and set it to graphic interop
 32         cudaDeviceProp  prop;
 33         int dev;
 34         memset( &prop, 0, sizeof( cudaDeviceProp ) );
 35         prop.major = 1;
 36         prop.minor = 0;
 37          cudaChooseDevice( &dev, &prop ) );
 38         cudaGLSetGLDevice( dev );
 39 
 40         // a bug in the Windows GLUT implementation prevents us from
 41         // passing zero arguments to glutInit()
 42         int c=1;
 43         char* dummy = "";
 44         glutInit( &c, &dummy );
 45         glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
 46         glutInitWindowSize( width, height );
 47         glutCreateWindow( "bitmap" );
 48 
 49         glBindBuffer    = (PFNGLBINDBUFFERARBPROC)GET_PROC_ADDRESS("glBindBuffer");
 50         glDeleteBuffers = (PFNGLDELETEBUFFERSARBPROC)GET_PROC_ADDRESS("glDeleteBuffers");
 51         glGenBuffers    = (PFNGLGENBUFFERSARBPROC)GET_PROC_ADDRESS("glGenBuffers");
 52         glBufferData    = (PFNGLBUFFERDATAARBPROC)GET_PROC_ADDRESS("glBufferData");
 53 
 54         glGenBuffers( 1, &bufferObj );
 55         glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, bufferObj );
 56         glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * 4, NULL, GL_DYNAMIC_DRAW_ARB);
 57 
 58         cudaGraphicsGLRegisterBuffer(&resource, bufferObj, cudaGraphicsMapFlagsNone);
 59     }
 60 
 61     ~GPUAnimBitmap()
 62     {
 63         free_resources();
 64     }
 65 
 66     void free_resources( void )
 67     {
 68         cudaGraphicsUnregisterResource( resource ) );
 69         glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, 0 );
 70         glDeleteBuffers( 1, &bufferObj );
 71     }
 72 
 73 
 74     long image_size( void ) const { return width * height * 4; }
 75 
 76     void click_drag(void(*f)(void*, int, int, int, int))
 77     {
 78         clickDrag = f;
 79     }
 80 
 81     void anim_and_exit(void(*f)(uchar4*, void*, int), void(*e)(void*))
 82     {
 83         GPUAnimBitmap**   bitmap = get_bitmap_ptr();
 84         *bitmap = this;
 85         fAnim = f;
 86         animExit = e;
 87 
 88         glutKeyboardFunc( Key );
 89         glutDisplayFunc( Draw );
 90         if (clickDrag != NULL)
 91             glutMouseFunc( mouse_func );
 92         glutIdleFunc( idle_func );
 93         glutMainLoop();
 94     }
 95 
 96     // static method used for glut callbacks
 97     static GPUAnimBitmap** get_bitmap_ptr( void )
 98     {
 99         static GPUAnimBitmap*   gBitmap;
100         return &gBitmap;
101     }
102 
103     // static method used for glut callbacks
104     static void mouse_func( int button, int state,int mx, int my )
105     {
106         if (button == GLUT_LEFT_BUTTON)
107         {
108             GPUAnimBitmap*   bitmap = *(get_bitmap_ptr());
109             if (state == GLUT_DOWN)
110             {
111                 bitmap->dragStartX = mx;
112                 bitmap->dragStartY = my;
113             } 
114             else if (state == GLUT_UP)
115                 bitmap->clickDrag(bitmap->dataBlock, bitmap->dragStartX, bitmap->dragStartY, mx, my);
116         }
117     }
118 
119     // static method used for glut callbacks
120     static void idle_func( void )
121     {
122         static int ticks = 1;
123         GPUAnimBitmap*  bitmap = *(get_bitmap_ptr());
124         uchar4*         devPtr;
125         size_t  size;
126 
127         cudaGraphicsMapResources(1, &(bitmap->resource), NULL);
128         cudaGraphicsResourceGetMappedPointer((void**)&devPtr, &size, bitmap->resource);
129 
130         bitmap->fAnim(devPtr, bitmap->dataBlock, ticks++);
131 
132         cudaGraphicsUnmapResources(1, &(bitmap->resource), NULL);
133 
134         glutPostRedisplay();
135     }
136 
137     // static method used for glut callbacks
138     static void Key(unsigned char key, int x, int y)
139     {
140         switch (key)
141         {
142             case 27:
143                 GPUAnimBitmap*   bitmap = *(get_bitmap_ptr();
144                 if (bitmap->animExit)
145                     bitmap->animExit( bitmap->dataBlock );
146                 bitmap->free_resources();
147                 exit(0);
148         }
149     }
150 
151     // static method used for glut callbacks
152     static void Draw( void )
153     {
154         GPUAnimBitmap*   bitmap = *(get_bitmap_ptr();
155         glClearColor( 0.0, 0.0, 0.0, 1.0 );
156         glClear( GL_COLOR_BUFFER_BIT );
157         glDrawPixels( bitmap->width, bitmap->height, GL_RGBA,
158                       GL_UNSIGNED_BYTE, 0 );
159         glutSwapBuffers();
160     }
161 };
162 
163 #endif  // __GPU_ANIM_H__

 

gl_helper.h

 1 #ifndef __GL_HELPER_H__
 2 #define __GL_HELPER_H__
 3 
 4 /*
 5    On 64-bit Windows, we need to prevent GLUT from automatically linking against
 6    glut32. We do this by defining GLUT_NO_LIB_PRAGMA. This means that we need to
 7    manually add opengl32.lib and glut64.lib back to the link using pragmas.
 8    Alternatively, this could be done on the compilation/link command-line, but
 9    we chose this so that compilation is identical between 32- and 64-bit Windows.
10 */
11 #ifdef _WIN64
12 #define GLUT_NO_LIB_PRAGMA
13 #pragma comment (lib, "opengl32.lib")  /* link with Microsoft OpenGL lib */
14 #pragma comment (lib, "glut64.lib")    /* link with Win64 GLUT lib */
15 #endif //_WIN64
16 
17 
18 #ifdef _WIN32
19 /* On Windows, include the local copy of glut.h and glext.h */
20 #include "GL/glut.h"
21 #include "GL/glext.h"
22 
23 #define GET_PROC_ADDRESS( str ) wglGetProcAddress( str )
24 
25 #else
26 
27 /* On Linux, include the system‘s copy of glut.h, glext.h, and glx.h */
28 #include <GL/glut.h>
29 #include <GL/glext.h>
30 #include <GL/glx.h>
31 
32 #define GET_PROC_ADDRESS( str ) glXGetProcAddress( (const GLubyte *)str )
33 
34 #endif //_WIN32
35 
36 #endif //__GL_HELPER_H__‘

 

附录四 其他头文件

标签:oca   void   bsp   source   exit   add   linux   res   dac   

原文地址:http://www.cnblogs.com/cuancuancuanhao/p/7653441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!