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