1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <cstdlib>
GLuint texture1; //the array for our texture
IplImage *img,*img2;
CvCapture* capture;
//The IplImage to OpenGl texture function
int loadTexture_Ipl(IplImage *image, GLuint *text) {
if (image==NULL) return -1;
glGenTextures(1, text);
glBindTexture( GL_TEXTURE_2D, *text ); //bind the texture to it's array
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->width, image->height,0, GL_BGR, GL_UNSIGNED_BYTE, image->imageData);
return 0;
}
void display (void) {
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
gluLookAt(0,4,0,0,0,0,0,0,1);
glMatrixMode( GL_TEXTURE );
glLoadIdentity( );
glBindTexture(GL_TEXTURE_2D, texture1);
glPushMatrix();
glBegin(GL_QUADS);
glTexCoord2d(0,0);
glVertex3d(1,1,1);
glTexCoord2d(0,1);
glVertex3d(1,1,-1);
glTexCoord2d(1,1);
glVertex3d(-1,1,-1);
glTexCoord2d(1,0);
glVertex3d(-1,1,1);
glEnd();
glFlush();
SDL_GL_SwapBuffers();
}
void FreeTexture( GLuint texture )
{
glDeleteTextures( 1, &texture );
}
int main (int argc, char **argv)
{
/* création d'une fenêtre OpenGL RVBA avec en simple mémoire tampon
avec un tampon de profondeur */
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
atexit(SDL_Quit);
SDL_WM_SetCaption("SDL GL Application", NULL);
SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective(70,(double)640/480,1,1000);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
display();
Uint32 last_time = SDL_GetTicks();
Uint32 current_time,ellapsed_time;
Uint32 start_time;
CvCapture* capture = cvCreateCameraCapture( -1 );
// On vérifie que la capture est bien initialisée,
// si ce n'est pas le cas, on quitte le programme.
if( !capture )
return 10;
cvNamedWindow("video", CV_WINDOW_AUTOSIZE);
char key = ' ';
// Tant qu'on n'a pas appuyé sur Q, on continue :
while(key != 'q')
{
img2 = cvQueryFrame(capture);
loadTexture_Ipl(img2,&texture1);
cvShowImage( "video", img2 );
// On attend 10 ms : IMPORTANT !
key = cvWaitKey(10);
start_time = SDL_GetTicks();
while (SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
exit(0);
break;
}
}
current_time = SDL_GetTicks();
ellapsed_time = current_time - last_time;
last_time = current_time;
display();
ellapsed_time = SDL_GetTicks() - start_time;
if (ellapsed_time < 10)
{
SDL_Delay(10 - ellapsed_time);
}
}
// On détruit les fenêtres créées : nouvelle fonction. ;)
cvDestroyAllWindows();
// On n'a pas besoin de détruire l'image.
img2 = NULL;
// On détruit la capture
cvReleaseCapture( &capture );
return 0;
} |