-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture.cpp
145 lines (121 loc) · 3.37 KB
/
capture.cpp
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
// (Windows) g++ -o simple{,.cpp} -lopengl32 -lfreeglut
// (Linux/Mac) g++ -o simple{,.cpp} -lglut -lGLU -lGL
#include <stdlib.h>
#include <GL/glut.h>
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
using namespace std;
void keyboard(unsigned char key, int x, int y);
void display(void);
void idle(void);
void mouse(int button, int state, int x, int y);
void resize(int w, int h);
GLuint texId;
GLfloat texu, texv, aspect;
cv::VideoCapture cap;
cv::Mat frame;
void releaseResources()
{
cap.release();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case '\x1B':
releaseResources();
exit(EXIT_SUCCESS);
break;
}
}
void display()
{
cap >> frame;
if (frame.empty()) {
releaseResources();
exit(0);
}
glClearColor(0.0f, 0.0f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, texId);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, frame.cols, frame.rows,
GL_BGR, GL_UNSIGNED_BYTE, (void *)frame.ptr(0));
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.5f * aspect, 0.5f);
glTexCoord2f(0.0f, texv); glVertex2f(-0.5f * aspect, -0.5f);
glTexCoord2f(texu, 0.0f); glVertex2f( 0.5f * aspect, 0.5f);
glTexCoord2f(texu, texv); glVertex2f( 0.5f * aspect, -0.5f);
glEnd();
glFlush();
glutSwapBuffers();
}
void idle(void)
{
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
}
void initGL(int w, int h)
{
int i, j;
int width, height;
width = 1;
while(width < w) { width <<= 1; }
height = 1;
while(height < h) { height <<= 1; }
texu = (float)w / (float)width;
texv = (float)h / (float)height;
aspect = (float)w / (float)h;
cout << "texuv = " << texu << ", " << texv << "\n";
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glTexImage2D(GL_TEXTURE_2D, 0, 3, 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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
void resize(int w, int h)
{
static bool first = true;
if (first) {
initGL(w, h);
first = false;
}
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0f, (float)w / (float)h, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
}
int main(int argc, char** argv)
{
cap.open(0);
if (!cap.isOpened())
{
std::cerr << "***Could not initialize capturing...***" << std::endl;
return -1;
}
int cap_w, cap_h;
cap_w = (int)cap.get(cv::CAP_PROP_FRAME_WIDTH);
cap_h = (int)cap.get(cv::CAP_PROP_FRAME_HEIGHT);
std::cout << "Capture w,h = " << cap_w << ", " << cap_h << "\n";
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(cap_w, cap_h);
glutCreateWindow("GLUT Test");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutReshapeFunc(resize);
glutIdleFunc(idle);
glutMouseFunc(mouse);
glutMainLoop();
return EXIT_SUCCESS;
}