/
gl-demo.cpp
172 lines (144 loc) · 4.42 KB
/
gl-demo.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#ifdef USE_EGL
#include <EGL/egl.h>
#else
#include <GL/glx.h>
#endif
#include <GL/gl.h>
#include <stdio.h>
#include <iostream>
#include "opencv2/opencv.hpp"
#include <omp.h>
const int width=640, height=480;
void drawUnitSquare()
{
glBegin( GL_TRIANGLES );
glNormal3f(0,0,1);
glTexCoord2f(1,1);
glVertex3f(0.5f,0.5f,0);
glTexCoord2f(0,1);
glVertex3f(-0.5f,0.5f,0);
glTexCoord2f(0,0);
glVertex3f(-0.5f,-0.5f,0);
glTexCoord2f(0,0);
glVertex3f(-0.5f,-0.5f,0);
glTexCoord2f(1,0);
glVertex3f(0.5f,-0.5f,0);
glTexCoord2f(1,1);
glVertex3f(0.5f,0.5f,0);
glEnd();
}
void draw_and_save()
{
glViewport(0,0,width,height);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
for(int i=0; i<5; ++i){
double t = omp_get_wtime();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor4f(1,0,0, 0.5f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(i*10, 1,1,0);
drawUnitSquare();
glFinish();
std::cout << (omp_get_wtime()-t)*1000 << "ms.\n";
}
cv::Mat rgb_out(height, width, CV_8UC3);
glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, rgb_out.data);
cv::imwrite("img.png", rgb_out);
std::cout << "img.png saved!\n";
}
int main()
{
#ifdef USE_EGL
EGLDisplay d = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if( d )
printf("eglGetDisplay success!\n");
else
printf("eglGetDisplay failed!\n");
int maj, min;
if( eglInitialize(d, &maj, &min) )
printf("eglInitialize success!\n");
else
printf("eglInitialize failed!\n");
printf("EGL version = %d.%d\n", maj, min);
printf("EGL_VENDOR = %s\n", eglQueryString(d, EGL_VENDOR));
EGLConfig first_config;
EGLint numConfigs;
eglGetConfigs(d, &first_config, 1, &numConfigs);
if( eglBindAPI(EGL_OPENGL_API) )
printf("eglBindAPI success!\n");
else
printf("eglBindAPI failed!\n");
EGLContext ctx = eglCreateContext(d, first_config, EGL_NO_CONTEXT, NULL);
if (ctx != EGL_NO_CONTEXT)
printf("create context success!\n");
else
printf("create context failed!\n");
const EGLint pbufAttribs[] = {
EGL_WIDTH, width,
EGL_HEIGHT, height,
EGL_NONE
};
EGLSurface pbuffer = eglCreatePbufferSurface(d, first_config, pbufAttribs);
if (pbuffer == EGL_NO_SURFACE) {
printf("failed to create pbuffer!\n");
return 0;
}
EGLBoolean b = eglMakeCurrent(d, pbuffer, pbuffer, ctx);
if (b)
printf("make current success!\n");
else
printf("make current failed!\n");
#else
typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
typedef Bool (*glXMakeContextCurrentARBProc)(Display*, GLXDrawable, GLXDrawable, GLXContext);
static glXCreateContextAttribsARBProc
glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc) glXGetProcAddressARB( (const GLubyte *)"glXCreateContextAttribsARB");
static glXMakeContextCurrentARBProc
glXMakeContextCurrentARB = (glXMakeContextCurrentARBProc) glXGetProcAddressARB( (const GLubyte *)"glXMakeContextCurrent");
if(!glXCreateContextAttribsARB)
std::cout << "glXGetProcAddressARB \"glXCreateContextAttribsARB\" failed!\n";
if(!glXMakeContextCurrentARB)
std::cout << "glXGetProcAddressARB \"glXMakeContextCurrentARB\" failed!\n";
const char *displayName = NULL;
Display* display = XOpenDisplay( displayName );
if(!display)
std::cout << "XOpenDisplay failed!\n";
static int visualAttribs[] = { None };
int numberOfFramebufferConfigurations = 0;
GLXFBConfig* fbConfigs = glXChooseFBConfig( display, DefaultScreen(display), visualAttribs, &numberOfFramebufferConfigurations );
if(!fbConfigs)
std::cout << "glXChooseFBConfig failed!\n";
int context_attribs[] = {
None
};
GLXContext openGLContext = glXCreateContextAttribsARB( display, fbConfigs[0], 0, True, context_attribs);
if(!openGLContext)
std::cout << "glXCreateContextAttribsARB failed!\n";
int pbufferAttribs[] = {
GLX_PBUFFER_WIDTH, width,
GLX_PBUFFER_HEIGHT, height,
None
};
GLXPbuffer pbuffer = glXCreatePbuffer( display, fbConfigs[0], pbufferAttribs );
// clean up:
XFree( fbConfigs );
XSync( display, False );
if ( !glXMakeContextCurrent( display, pbuffer, pbuffer, openGLContext ) ){
std::cout << "glXMakeContextCurrent failed!\n";
}
#endif
std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << ".\n";
draw_and_save();
#ifdef USE_EGL
eglDestroySurface(d, pbuffer);
eglDestroyContext(d, ctx);
eglTerminate(d);
#else
glXDestroyPbuffer( display, pbuffer );
glXDestroyContext( display, openGLContext );
#endif
return 0;
}