Hello everybody, i would like to do a AVI player with OpenGL. I already done it but i just began coding with OpenGL :
In fact i would like to know if my code takes the good way to do it or if i use useless part of code.
This is my code, a little bit heavy i’m sorry :
#include <stdio.h>
#include <stdlib.h>
#include <cutil_inline.h>
#include <math.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include <cuda_gl_interop.h>
#include <rendercheck_gl.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cxtypes.h>
#include <cxcore.h>
#include <cv.h>
#include <highgui.h>
#include <ml.h>
#include <string.h>
#include <iostream>
#include <time.h>
using namespace std;
char *video_filename = "eddy.avi";
IplImage* frame;
CvCapture* capture=NULL;
int nbrframes=1;
clock_t debut,fin;
char *imgData;
unsigned int *imgBase;
unsigned int *imgFrame;
unsigned int width, height;
unsigned int * img_base = NULL;
unsigned int * img_frame = NULL;
int tblock=0;
int nbrblk=0;
int nbroctets=0;
dim3 DimBlock(512,1,1);
dim3 DimGrid;
GLuint pbo;
GLuint texid;
GLuint shader;
CheckRender *g_CheckRender = NULL;
//=================== Display result
void goExit()
{
fin=clock();
double duree = (double) (fin-debut)/ CLOCKS_PER_SEC;
cout<<"Time : "<<duree<<endl;
duree = nbrframes/duree;
cout<<"FPS : "<<duree<<endl;
system("PAUSE");
exit(0);
}
//===============================================================
//==================== OPENGL play loop
void display()
{
unsigned int *d_result;
glClear(GL_COLOR_BUFFER_BIT);
frame=cvQueryFrame(capture); // next frame
if (!frame) goExit();
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, width*height*sizeof(GLubyte)*3, frame->imageData, GL_STREAM_DRAW_ARB);
//************************************************************************************* CODE GPU
//CUDA_SAFE_CALL(cudaGLMapBufferObject((void**)&img_frame,
pbo))
//Sobel a placer ici
//CUDA_SAFE_CALL(cudaGLUnmapBufferObject(pbo));
//************************************************************************************* end CODE GPU
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
glBindTexture(GL_TEXTURE_2D, texid);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, 0);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
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();
nbrframes++;
}
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); //l'angle de vue.
}
//==================== CUDA
void initCuda()
{
CUDA_SAFE_CALL (cudaMalloc( (void**) &img_base, (nbroctets*4) ));
cudaMemcpy(img_base,imgBase,nbroctets*4,cudaMemcpyHostToDevice);
}
//==================== CUDA
void cleanup()
{
free(imgData);
CUDA_SAFE_CALL(cudaFree(img_base));
glDeleteBuffersARB(1, &pbo);
glDeleteTextures(1, &texid);
glDeleteProgramsARB(1, &shader);
if (g_CheckRender) {
delete g_CheckRender; g_CheckRender = NULL;
}
}
//================================================================================================================ SHADERS
// shader for displaying floating-point texture
static const char *shader_code =
"!!ARBfp1.0
"
"TEX result.color, fragment.texcoord, texture[0], 2D;
"
"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
%s
", (int)error_pos, error_string);
return 0;
}
return program_id;
}
//================================================================================================================
void initOpenGL()
{
glGenBuffersARB(1, &pbo); //crée 1 pixel buffer object ->pbo pointe dessus.
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo); //chargement depuis pbo
glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, width*height*sizeof(GLubyte)*3, frame->imageData, GL_STREAM_DRAW_ARB);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0); //-----??? efface?
CUDA_SAFE_CALL(cudaGLRegisterBufferObject(pbo));//on enregistre avec cuda
// create texture for display
glGenTextures(1, &texid);
glBindTexture(GL_TEXTURE_2D, texid);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, 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)
{
cudaSetDevice(0);
capture = cvCreateFileCapture(video_filename);
frame=cvQueryFrame(capture);
printf("Loaded %d x %d pixels
", frame->width, frame->height);
width=frame->width;
height=frame->height;
cout<<frame->nChannels<<endl;
cout<<frame->colorModel<<endl;
cout<<frame->channelSeq<<endl;
nbroctets=(int)ceil((double)frame->imageSize/4);
imgData= (char*) malloc (nbroctets*4);
imgBase=(unsigned int*)imgData;
nbrblk=(int)ceil((double)nbroctets/512);
tblock=(int)ceil((double)pow(nbrblk,0.5));
DimGrid.x=tblock;
DimGrid.y=tblock;
DimGrid.z=1;
for (int i=0;i<nbroctets;i++) imgBase[i]=0;
for (int i=0;i<frame->imageSize;i++)
{
imgData[i]=frame->imageData[i];
}
frame=cvQueryFrame(capture);
//initialize GLUT
//CREER FENETRE
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(width, height);
glutCreateWindow("CUDA Box Filter");
//allocation memoire
initCuda();
//initialisation des fonctions OGL
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
");
fprintf(stderr, "This sample requires:
");
fprintf(stderr, " OpenGL version 1.5
");
fprintf(stderr, " GL_ARB_vertex_buffer_object
");
fprintf(stderr, " GL_ARB_pixel_buffer_object
");
exit(-1);
}
initOpenGL();
fflush(stdout);
atexit(cleanup);
debut=clock();
glutMainLoop();
CUT_EXIT(argc, argv);
}
Really big Thanks!