Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/opengl/opengl.cpp
16337 views
1
#include <iostream>
2
3
#ifdef _WIN32
4
#define WIN32_LEAN_AND_MEAN 1
5
#define NOMINMAX 1
6
#include <windows.h>
7
#endif
8
9
#if defined(__APPLE__)
10
#include <OpenGL/gl.h>
11
#include <OpenGL/glu.h>
12
#else
13
#include <GL/gl.h>
14
#include <GL/glu.h>
15
#endif
16
17
#include "opencv2/core.hpp"
18
#include "opencv2/core/opengl.hpp"
19
#include "opencv2/core/cuda.hpp"
20
#include "opencv2/highgui.hpp"
21
22
using namespace std;
23
using namespace cv;
24
using namespace cv::cuda;
25
26
const int win_width = 800;
27
const int win_height = 640;
28
29
struct DrawData
30
{
31
ogl::Arrays arr;
32
ogl::Texture2D tex;
33
ogl::Buffer indices;
34
};
35
36
void draw(void* userdata);
37
38
void draw(void* userdata)
39
{
40
DrawData* data = static_cast<DrawData*>(userdata);
41
42
glRotated(0.6, 0, 1, 0);
43
44
ogl::render(data->arr, data->indices, ogl::TRIANGLES);
45
}
46
47
int main(int argc, char* argv[])
48
{
49
string filename;
50
if (argc < 2)
51
{
52
cout << "Usage: " << argv[0] << " image" << endl;
53
filename = "../data/lena.jpg";
54
}
55
else
56
filename = argv[1];
57
58
Mat img = imread(filename);
59
if (img.empty())
60
{
61
cerr << "Can't open image " << filename << endl;
62
return -1;
63
}
64
65
namedWindow("OpenGL", WINDOW_OPENGL);
66
resizeWindow("OpenGL", win_width, win_height);
67
68
Mat_<Vec2f> vertex(1, 4);
69
vertex << Vec2f(-1, 1), Vec2f(-1, -1), Vec2f(1, -1), Vec2f(1, 1);
70
71
Mat_<Vec2f> texCoords(1, 4);
72
texCoords << Vec2f(0, 0), Vec2f(0, 1), Vec2f(1, 1), Vec2f(1, 0);
73
74
Mat_<int> indices(1, 6);
75
indices << 0, 1, 2, 2, 3, 0;
76
77
DrawData data;
78
79
data.arr.setVertexArray(vertex);
80
data.arr.setTexCoordArray(texCoords);
81
data.indices.copyFrom(indices);
82
data.tex.copyFrom(img);
83
84
glMatrixMode(GL_PROJECTION);
85
glLoadIdentity();
86
gluPerspective(45.0, (double)win_width / win_height, 0.1, 100.0);
87
88
glMatrixMode(GL_MODELVIEW);
89
glLoadIdentity();
90
gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
91
92
glEnable(GL_TEXTURE_2D);
93
data.tex.bind();
94
95
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
96
glTexEnvi(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_REPLACE);
97
98
glDisable(GL_CULL_FACE);
99
100
setOpenGlDrawCallback("OpenGL", draw, &data);
101
102
for (;;)
103
{
104
updateWindow("OpenGL");
105
char key = (char)waitKey(40);
106
if (key == 27)
107
break;
108
}
109
110
setOpenGlDrawCallback("OpenGL", 0, 0);
111
destroyAllWindows();
112
113
return 0;
114
}
115
116