Skip to content

Instantly share code, notes, and snippets.

@nowhere-girl
Created March 25, 2020 06:05
Show Gist options
  • Save nowhere-girl/e6e4df607f361f3f9192f264ff7372d0 to your computer and use it in GitHub Desktop.
Save nowhere-girl/e6e4df607f361f3f9192f264ff7372d0 to your computer and use it in GitHub Desktop.
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include <iostream>
#include <string>
#include <sstream>
#include <GL/glut.h>
using namespace cv;
using namespace std;
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();
//Load Texture
glBindTexture(GL_TEXTURE_2D, texture);
//Rotate plane before draw
glRotatef(angle, 1.0f, 1.0f, 1.0f);
//Create the plane and set the texture coordinate
glBegin(GL_QUADS);
//First point and coordinate texture
glTexCoord2d(0.0,0.0);
glVertex2d(-1.0,-1.0);
//Second point and coordinate texture
glTexCoord2d(1.0,0.0);
glVertex2d(+1.0,-1.0);
//Third point and coordinate texture
glTexCoord2d(1.0,1.0);
glVertex2d(+1.0,+1.0);
//Fourth point and coordinate texture
glTexCoord2d(0.0,1.0);
glVertex2d(-1.0,+1.0);
glEnd;
}
int main(int argc, const char** argv){
//Open webcam
int deviceID = 2;
int apiID = 0;
camera.open(deviceID + apiID);
if (!camera.isOpened())
{
cout << "ERROR : Can't open camera" << endl;
return -1;
}
//Create new windows
namedWindow("OpenGL Camera", WINDOW_OPENGL);
//Enable texture
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture);
setOpenGlDrawCallback("OpenGL Camera", on_opengl);
while(waitKey(300) !='q'){
camera >> frame;
//Create first texture
loadTexture();
updateWindow("OpenGL Camera");
angle = angle+4;
}
//Destroy windows
destroyWindow("OpenGL Camera");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment