Offscreen rendering with pbuffers

I am having real problems with trying to use pbuffers for offscreen rendering (requires GLX 1.3). My code compiles without bugs but I am getting the usual BadAlloc error messages. None of the available configurations that are returned by glXChooseFBConfig remove the BadAlloc error. Does anyone have working code for Linux? Or any ideas what I may be doing wrong?

My code:

#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

// Compile with: ‘cc -I/usr/X11R6/include pbuffers.c -o pbuffers -L/usr/X11R6/lib -lGLU -lGL -lX11’

main() {
Display *dpy;
GLXFBConfig *fbconfig;
int screen;
GLXContext cx;
int fbconfigCount;
GLXPbuffer pbuffer;
int w=512, h=512;
int attribList[]={GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_MAX_PBUFFER_WIDTH, w,
GLX_MAX_PBUFFER_HEIGHT, h,
GLX_RED_SIZE, 4,
GLX_GREEN_SIZE, 4,
GLX_BLUE_SIZE, 4,
GLX_DRAWABLE_TYPE,GLX_PBUFFER_BIT,
GLX_DEPTH_SIZE, 24,
None};
FILE *file;
unsigned char *pixels;

// Create display
dpy = XOpenDisplay(0);

// Set screen
screen = DefaultScreen(dpy);

// Configure frame buffer
fbconfig = glXChooseFBConfig(dpy,
screen,
attribList,
&fbconfigCount);
if (fbconfig == NULL | | fbconfigCount <= 0) {
fprintf(stderr, "P-Buffers not supported.
");
exit(0);
}

// Create P-Buffer
pbuffer = glXCreatePbuffer(dpy,
fbconfig[0],
NULL);
if (pbuffer == None) {
fprintf(stderr, "Failed to create P-Buffer.
");
exit(0);
}

// Create graphics context
cx = glXCreateNewContext(dpy,
fbconfig[0],
GLX_RGBA_TYPE,
NULL,
GL_TRUE);
if (!cx) {
fprintf(stderr, "Failed to create graphics context.
");
exit(0);
}

// Activate graphics context
if (!glXMakeContextCurrent(dpy, pbuffer, pbuffer, cx)) {
fprintf(stderr, "Failed to activate graphics context.
");
exit(0);
}

// If all the preceding commands execute OK, then all the OpenGL instructions
// will be executed in the offscreen buffer

Howdy,

you need to request the size of your pixel buffer when you create it (the size is ignored when you choose the pixelformat!)

 // Create P-Buffer
pbuffer = glXCreatePbuffer(dpy,
fbconfig[0],
NULL);

should look like

{
  int attrib_2[]={
      GLX_PBUFFER_WIDTH, w_,           
      GLX_PBUFFER_HEIGHT, h_,
      GLX_NONE
    };
  pbuffer=glXCreatePbuffer(display, config, attrib_2);

c.f. http://molt.zdv.uni-mainz.de/doc_link/en_US/a_doc_lib/libs/openglrf/glXCreatePbuffer

cheers,
John

Thanks for the quick reply. Your suggestion did the trick.

Thanks to both! I had problem with Pbuffer, but your sample, with the correction, works fine! :slight_smile:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.