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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
/*
Create an OpenGL context without creating an OpenGL Window. for Linux.
based on
http://www.opengl.org/sdk/docs/man/xhtml/glXIntro.xml
http://www.mesa3d.org/brianp/sig97/offscrn.htm
http://glprogramming.com/blue/ch07.html
OffscreenContext.mm (Mac OSX version)
*/
#include "OffscreenContext.h"
#include "printutils.h"
#include "imageutils.h"
#include "fbo.h"
#include <GL/gl.h>
#include <GL/glx.h>
#define REPORTGLERROR(task) { GLenum tGLErr = glGetError(); if (tGLErr != GL_NO_ERROR) { std::cout << "OpenGL error " << tGLErr << " while " << task << "\n"; } }
struct OffscreenContext
{
GLXContext openGLContext;
Display *xDisplay;
GLXPixmap glx_pixmap;
Pixmap x11_pixmap;
int width;
int height;
fbo_t *fbo;
};
Bool glx_1_3_pixmap_dummy_context(OffscreenContext *ctx, Bool hybrid)
{
XVisualInfo *vInfo;
GLXFBConfig *fbConfigs;
int numReturned;
int result;
int dummyAttributes_1_3[] = {
GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
GLX_RENDER_TYPE, GLX_RGBA_BIT,
None
};
fbConfigs = glXChooseFBConfig( ctx->xDisplay,
DefaultScreen(ctx->xDisplay),
dummyAttributes_1_3, &numReturned );
if ( fbConfigs == NULL ) {
REPORTGLERROR("glXChooseFBConfig failed") ;
return False;
}
vInfo = glXGetVisualFromFBConfig( ctx->xDisplay, fbConfigs[0] );
if ( vInfo == NULL ) {
REPORTGLERROR("glXGetVisualFromFBConfig failed") ;
return False;
}
ctx->x11_pixmap = XCreatePixmap( ctx->xDisplay, DefaultRootWindow(ctx->xDisplay) , 10, 10, 8 );
if (hybrid) {
// GLX 1.2 - prevent Mesa warning
ctx->glx_pixmap = glXCreateGLXPixmap( ctx->xDisplay, vInfo, ctx->x11_pixmap );
} else {
// GLX 1.3
ctx->glx_pixmap = glXCreatePixmap( ctx->xDisplay, fbConfigs[0], ctx->x11_pixmap, NULL ); // GLX 1.3
}
ctx->openGLContext = glXCreateNewContext( ctx->xDisplay, fbConfigs[0], GLX_RGBA_TYPE, NULL, True );
if ( ctx->openGLContext == NULL ) {
REPORTGLERROR("glXCreateNewContext failed" );
return False;
}
result = glXMakeContextCurrent( ctx->xDisplay, ctx->glx_pixmap, ctx->glx_pixmap, ctx->openGLContext );
if ( result == False ) {
REPORTGLERROR("glXMakeContextCurrent failed" );
return False;
}
return True;
}
Bool make_glx_dummy_context(OffscreenContext *ctx)
{
/*
Before opening a framebuffer, an OpenGL context must be created.
For GLX, you can do this by creating a 'Pixmap' drawable then
creating the Context off of that. The Pixmap is then never used.
*/
int major;
int minor;
ctx->xDisplay = XOpenDisplay( NULL );
if ( ctx->xDisplay == NULL ) {
fprintf(stderr, "Unable to open a connection to the X server\n" );
return False;
}
/*
On some systems, the GLX library will report that it is version
1.2, but some 1.3 functions will be implemented, and, furthermore,
some 1.2 functions won't work right, while the 1.3 functions will,
but glXCreatePixmp will still generate a MESA GLX 1.3 runtime warning.
To workaround this, detect the situation and use 'hybrid' mix of
1.2 and 1.3 as needed.
*/
glXQueryVersion(ctx->xDisplay, &major, &minor);
if (major==1 && minor<=2) {
if (glXCreatePixmap!=NULL) { // 1.3 function exists, even though its 1.2
return glx_1_3_pixmap_dummy_context(ctx,True);
} else {
fprintf(stderr,"OpenGL error: GLX version 1.3 functions missing. Your GLX: %i.%i\n",major,minor);
return False;
}
} else if (major>=1 && minor>=3) {
return glx_1_3_pixmap_dummy_context(ctx,False);
}
}
void glewCheck() {
#ifdef DEBUG
cout << "GLEW version " << glewGetString(GLEW_VERSION) << "\n";
cout << (const char *)glGetString(GL_RENDERER) << "(" << (const char *)glGetString(GL_VENDOR) << ")\n"
<< "OpenGL version " << (const char *)glGetString(GL_VERSION) << "\n";
cout << "Extensions: " << (const char *)glGetString(GL_EXTENSIONS) << "\n";
if (GLEW_ARB_framebuffer_object) {
cout << "ARB_FBO supported\n";
}
if (GLEW_EXT_framebuffer_object) {
cout << "EXT_FBO supported\n";
}
if (GLEW_EXT_packed_depth_stencil) {
cout << "EXT_packed_depth_stencil\n";
}
#endif
}
OffscreenContext *create_offscreen_context(int w, int h)
{
OffscreenContext *ctx = new OffscreenContext;
ctx->width = w;
ctx->height = h;
ctx->openGLContext = NULL;
ctx->xDisplay = NULL;
ctx->glx_pixmap = NULL;
ctx->x11_pixmap = NULL;
ctx->fbo = NULL;
// fill ctx->xDisplay, ctx->openGLContext, x11_pixmap, glx_pixmap
if (!make_glx_dummy_context(ctx)) {
return NULL;
}
glewInit(); //must come after Context creation and before FBO calls.
glewCheck();
ctx->fbo = fbo_new();
if (!fbo_init(ctx->fbo, w, h)) {
return NULL;
}
return ctx;
}
bool teardown_offscreen_context(OffscreenContext *ctx)
{
fbo_unbind(ctx->fbo);
fbo_delete(ctx->fbo);
glXDestroyPixmap(ctx->xDisplay, ctx->glx_pixmap );
XFreePixmap(ctx->xDisplay, ctx->x11_pixmap );
glXDestroyContext( ctx->xDisplay, ctx->openGLContext );
return true;
}
/*!
Capture framebuffer from OpenGL and write it to the given filename as PNG.
*/
bool save_framebuffer(OffscreenContext *ctx, const char *filename)
{
int samplesPerPixel = 4; // R, G, B and A
GLubyte pixels[ctx->width * ctx->height * samplesPerPixel];
glReadPixels(0, 0, ctx->width, ctx->height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
// Flip it vertically - images read from OpenGL buffers are upside-down
int rowBytes = samplesPerPixel * ctx->width;
unsigned char *flippedBuffer = (unsigned char *)malloc(rowBytes * ctx->height);
if (!flippedBuffer) {
std::cout << "Unable to allocate flipped buffer for corrected image.";
return 1;
}
flip_image(pixels, flippedBuffer, samplesPerPixel, ctx->width, ctx->height);
bool writeok = write_png(filename, flippedBuffer, ctx->width, ctx->height);
free(flippedBuffer);
return writeok;
}
void bind_offscreen_context(OffscreenContext *ctx)
{
fbo_bind(ctx->fbo);
}
|