Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glew.h>
- #include <gl/freeglut.h>
- #include <cuda_gl_interop.h>
- #include <cuda_runtime.h>
- #include <rendercheckGL.h>
- GLuint pointer_to_buffer = NULL;
- GLuint texture_ID = NULL;
- void* gimg;
- int full = 0;
- int W = 0, H = 0, CH = 0, PICH = 0;
- int current_width = 0, current_height = 0;
- char* winTitle = "Image";
- int waitKeyCalled = 0;
- int timeDelay = 0;
- int pressed_key = -1;
- unsigned short channel_type = 0;
- unsigned short channel_sequence = 0;
- unsigned short image_depth = 0;
- void CreateTexture(GLuint* textureId, int width, int height, int channels)
- {
- glEnable(GL_TEXTURE_2D);
- glGenTextures(1,textureId);
- glBindTexture(GL_TEXTURE_2D,*textureId);
- glTexImage2D(GL_TEXTURE_2D,0,channel_type,width,height,0,channel_sequence,image_depth,NULL);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- }
- void CreatePBO(GLuint* pbo)
- {
- if(pbo)
- {
- int data_size = W * H * CH * sizeof(GLubyte);
- glGenBuffers(1,pbo);
- glBindBuffer(GL_PIXEL_UNPACK_BUFFER,*pbo);
- glBufferData(GL_PIXEL_UNPACK_BUFFER,data_size,NULL,GL_DYNAMIC_COPY);
- cudaGLRegisterBufferObject(*pbo);
- }
- }
- void RunCUDA()
- {
- void* dptr = NULL;
- size_t size = W * H * CH;
- size_t pitch = W * CH;
- CreatePBO(&pointer_to_buffer);
- cudaGLMapBufferObject((void**)&dptr,pointer_to_buffer);
- cudaMemcpy2D(dptr,pitch,gimg,PICH,pitch,H,cudaMemcpyDeviceToDevice);
- cudaGLUnmapBufferObject(pointer_to_buffer);
- }
- void TimerFunction(int value)
- {
- if(value == 1)
- glutLeaveMainLoop();
- glutPostRedisplay();
- }
- void DisplayFunction()
- {
- glutTimerFunc(timeDelay,TimerFunction,timeDelay!=0);
- glPixelStorei(GL_UNPACK_ALIGNMENT,1);
- glBindBuffer(GL_PIXEL_UNPACK_BUFFER,pointer_to_buffer);
- glBindTexture(GL_TEXTURE_2D,texture_ID);
- glTexSubImage2D(GL_TEXTURE_2D,0,0,0,W,H,channel_sequence,image_depth,NULL);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0f,1.0f); glVertex2f(0.0f,0.0f);
- glTexCoord2f(0.0f,0.0f); glVertex2f(0.0f,1.0f);
- glTexCoord2f(1.0f,0.0f); glVertex2f(1.0f,1.0f);
- glTexCoord2f(1.0f,1.0f); glVertex2f(1.0f,0.0f);
- glEnd();
- glutSwapBuffers();
- }
- void MouseWheelFunc(int wheel, int direction, int x, int y)
- {
- if(!full)
- {
- if(direction > 0)
- {
- current_width /= 0.97f;
- current_height /= 0.97f;
- }
- else
- {
- current_width *= 0.97f;
- current_height *= 0.97f;
- }
- glutReshapeWindow(current_width,current_height);
- glutPostRedisplay();
- }
- }
- void KeyBoardFunction(unsigned char key, int x, int y)
- {
- pressed_key = key;
- int mod = glutGetModifiers();
- switch(key)
- {
- case 102:
- glutFullScreenToggle();
- full = 1 - full;
- break;
- default:
- glutLeaveMainLoop();
- break;
- }
- glutPostRedisplay();
- }
- void ClosingFuntion()
- {
- cudaGLUnregisterBufferObject(pointer_to_buffer);
- glDeleteTextures(1,&texture_ID);
- glDeleteBuffers(1,&pointer_to_buffer);
- glDisable(GL_TEXTURE_2D);
- }
- void InitCUDA()
- {
- cudaGLSetGLDevice(0);
- CreateTexture(&texture_ID,W,H,CH);
- RunCUDA();
- }
- void InitGL()
- {
- glutInit(&__argc,__argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
- glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);
- unsigned int desk_w = 0, desk_h = 0;
- desk_w = glutGet(GLUT_SCREEN_WIDTH);
- desk_h = glutGet(GLUT_SCREEN_HEIGHT);
- if(W > desk_w || H > desk_h)
- glutInitWindowPosition(0,0);
- else
- glutInitWindowPosition((desk_w - W)/2,(desk_h-H)/2);
- glutInitWindowSize(W,H);
- glutCreateWindow(winTitle);
- glutDisplayFunc(DisplayFunction);
- glutKeyboardFunc(KeyBoardFunction);
- glutMouseWheelFunc(MouseWheelFunc);
- glutCloseFunc(ClosingFuntion);
- glewInit();
- glViewport(0,0,W,H);
- glDisable(GL_DEPTH_TEST);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0,1.0,0.0,1.0,0.0,1.0);
- }
- void _showImage()
- {
- InitGL();
- InitCUDA();
- glutDisplayFunc(DisplayFunction);
- glutMainLoop();
- }
- //data = image data in GPU Global memory
- int ShowRawImage(void* data, int width, int height, size_t pitch, int channels)
- {
- winTitle = "Image";
- W = width;
- H = height;
- current_width = W;
- current_height = H;
- CH = channels;
- PICH = pitch;
- gimg = data;
- switch(channels)
- {
- case 1:
- channel_type = GL_LUMINANCE;
- channel_sequence = GL_LUMINANCE;
- image_depth = GL_UNSIGNED_BYTE;
- case 3:
- channel_type = GL_RGB;
- channel_sequence = GL_RGB;
- image_depth = GL_UNSIGNED_BYTE;
- break;
- case 4:
- channel_type = GL_RGBA;
- channel_sequence = GL_RGBA;
- image_depth = GL_UNSIGNED_BYTE;
- break;
- default:
- return -1;
- }
- _showImage();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement