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
| #include <iostream> #include <sstream> #include <string> using namespace std;
#ifdef __APPLE__ #include <OpenGL/gl.h> #else #include <GL/gl.h> #endif
#include "opencv2/core.hpp" #include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" using namespace cv;
Mat frame; GLfloat angle = 0.0; GLuint texture; VideoCapture camera;
int loadTexture() {
if (frame.data == NULL) return -1;
glBindTexture(GL_TEXTURE_2D, texture); 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, frame.cols, frame.rows, 0, GL_BGR, GL_UNSIGNED_BYTE, frame.data); return 0; }
void on_opengl(void *param) { glLoadIdentity(); glBindTexture(GL_TEXTURE_2D, texture); glRotatef(angle, 1.0f, 1.0f, 1.0f); glBegin(GL_QUADS); glTexCoord2d(0.0, 0.0); glVertex2d(-1.0, -1.0); glTexCoord2d(1.0, 0.0); glVertex2d(+1.0, -1.0); glTexCoord2d(1.0, 1.0); glVertex2d(+1.0, +1.0); glTexCoord2d(0.0, 1.0); glVertex2d(-1.0, +1.0); glEnd(); }
int main(int argc, const char **argv) { camera.open(0); if (!camera.isOpened()) return -1;
namedWindow("OpenGL Camera", WINDOW_OPENGL);
glEnable(GL_TEXTURE_2D); glGenTextures(1, &texture);
setOpenGlDrawCallback("OpenGL Camera", on_opengl);
while (waitKey(30) != 'q') { camera >> frame; loadTexture(); updateWindow("OpenGL Camera"); angle = angle + 4; }
return 0; }
|