[font=“Times New Roman”][b]Hi
This is my code to display a picture by CUda , it works right .
what i Need is to modify it to display in first time 2 or 3 pictures and in second time to show a video .
Please i need your Help
Thanks.
[/b][/font]
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <GL/glew.h>
#if defined(__APPLE__) || defined(__MACOSX)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <cuda_runtime.h>
#include <cutil.h>
#include <cuda_gl_interop.h>
#include <rendercheck_gl.h>
char *image_filename = "rrr.ppm";
unsigned int width, height;
unsigned int * h_img = NULL;
unsigned int * d_img = NULL;
unsigned int * d_temp = NULL;
GLuint pbo; // OpenGL pixel buffer object
GLuint texid; // texture
GLuint shader;
unsigned int timer = 0;
// CheckFBO/BackBuffer class objects
CheckRender *g_CheckRender = NULL;
// These are CUDA functions to handle allocation and launching the kernels
extern "C" void initTexture(int width, int height, void *pImage);
extern "C" void freeTextures();
// display results using OpenGL
void display()
{
CUT_SAFE_CALL(cutStartTimer(timer));
// execute filter, writing results to pbo
unsigned int *d_result;
CUDA_SAFE_CALL(cudaGLMapBufferObject((void**)&d_result, pbo));
CUDA_SAFE_CALL(cudaGLUnmapBufferObject(pbo));
// display results
glClear(GL_COLOR_BUFFER_BIT);
// load texture from pbo
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
glBindTexture(GL_TEXTURE_2D, texid);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
// fragment program is required to display floating point texture
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader);
glEnable(GL_FRAGMENT_PROGRAM_ARB);
glDisable(GL_DEPTH_TEST);
glBegin(GL_QUADS);
glVertex2f(0, 0); glTexCoord2f(0, 0);
glVertex2f(0, 1); glTexCoord2f(1, 0);
glVertex2f(1, 1); glTexCoord2f(1, 1);
glVertex2f(1, 0); glTexCoord2f(0, 1);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_FRAGMENT_PROGRAM_ARB);
glutSwapBuffers();
CUT_SAFE_CALL(cutStopTimer(timer));
}
void idle()
{
glutPostRedisplay();
}
void reshape(int x, int y)
{
glViewport(0, 0, x, y);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
}
void initCuda()
{
// allocate device memory
CUDA_SAFE_CALL( cudaMalloc( (void**) &d_img, (width * height * sizeof(unsigned int)) ));
CUDA_SAFE_CALL( cudaMalloc( (void**) &d_temp, (width * height * sizeof(unsigned int)) ));
// Refer to boxFilter_kernel.cu for implementation
initTexture(width, height, h_img);
CUT_SAFE_CALL( cutCreateTimer( &timer));
}
void cleanup()
{
CUT_SAFE_CALL( cutDeleteTimer( timer));
// free(h_img);
CUDA_SAFE_CALL(cudaFree(d_img));
CUDA_SAFE_CALL(cudaFree(d_temp));
// Refer to boxFilter_kernel.cu for implementation
freeTextures();
CUDA_SAFE_CALL(cudaGLUnregisterBufferObject(pbo));
glDeleteBuffersARB(1, &pbo);
glDeleteTextures(1, &texid);
glDeleteProgramsARB(1, &shader);
if (g_CheckRender) {
delete g_CheckRender; g_CheckRender = NULL;
}
}
// shader for displaying floating-point texture
static const char *shader_code =
"!!ARBfp1.0\n"
"TEX result.color, fragment.texcoord, texture[0], 2D; \n"
"END";
GLuint compileASMShader(GLenum program_type, const char *code)
{
GLuint program_id;
glGenProgramsARB(1, &program_id);
glBindProgramARB(program_type, program_id);
glProgramStringARB(program_type, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei) strlen(code), (GLubyte *) code);
GLint error_pos;
glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos);
if (error_pos != -1) {
const GLubyte *error_string;
error_string = glGetString(GL_PROGRAM_ERROR_STRING_ARB);
fprintf(stderr, "Program error at position: %d\n%s\n", (int)error_pos, error_string);
return 0;
}
return program_id;
}
void initOpenGL()
{
// create pixel buffer object
glGenBuffersARB(1, &pbo);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, width*height*sizeof(GLubyte)*4, h_img, GL_STREAM_DRAW_ARB);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
CUDA_SAFE_CALL(cudaGLRegisterBufferObject(pbo));
// create texture for display
glGenTextures(1, &texid);
glBindTexture(GL_TEXTURE_2D, texid);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
// load shader program
shader = compileASMShader(GL_FRAGMENT_PROGRAM_ARB, shader_code);
}
////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
int
main( int argc, char** argv)
{
CUT_DEVICE_INIT(argc, argv);
// load image
char* image_path = cutFindFilePath(image_filename, argv[0]);
if (image_path == 0) {
fprintf(stderr, "Error finding image file '%s'\n", image_filename);
exit(EXIT_FAILURE);
}
CUT_SAFE_CALL( cutLoadPPM4ub(image_path, (unsigned char **) &h_img, &width, &height));
if (!h_img) {
printf("Error opening file '%s'\n", image_path);
exit(-1);
}
printf("Loaded '%s', %d x %d pixels\n", image_path, width, height);
// initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(width, height);
glutCreateWindow("CUDA Box Filter");
initCuda();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glewInit();
if (!glewIsSupported( "GL_VERSION_1_5 GL_ARB_vertex_buffer_object GL_ARB_pixel_buffer_object" )) {
fprintf(stderr, "Error: failed to get minimal extensions for demo\n");
fprintf(stderr, "This sample requires:\n");
fprintf(stderr, " OpenGL version 1.5\n");
fprintf(stderr, " GL_ARB_vertex_buffer_object\n");
fprintf(stderr, " GL_ARB_pixel_buffer_object\n");
exit(-1);
}
// initCuda();
initOpenGL();
fflush(stdout);
atexit(cleanup);
glutMainLoop();
CUT_EXIT(argc, argv);
}