Offscreen rendering with GLXPixmaps

I am interested in offscreen OpenGL rendering using pixmaps. To this end I have been trying to port code over to Mac OS X without success. The code compiles cleanly but all I get is a black image. The code does work on other UNIXes. Any idea what is going
wrong? Any help will be greatly appreciated!

Here’s the 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 pixmaps.c -o pixmaps -L/usr/X11R6/lib -lGLU -lGL -lX11’

main() {
Display *dpy;
XVisualInfo *vis;
Pixmap pix;
GLXContext cx;
GLXPixmap px;
int attribList[]={GLX_RGBA,
GLX_RED_SIZE, 1,
GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1,
GLX_DEPTH_SIZE, 24,
None};

FILE *file;
unsigned char *pixels;
int w=512, h=512;

// Creation of the display
dpy = XOpenDisplay(0);

// Creation of the visual
vis = glXChooseVisual(dpy, DefaultScreen(dpy), attribList);

// Creation of the graphical context
cx = glXCreateContext(dpy, vis, NULL, false);

// Create the pixmap, where one designs the scene
pix = XCreatePixmap(dpy, RootWindow(dpy, vis->screen), w, h, vis->depth);
px = glXCreateGLXPixmap(dpy, vis, pix);

// Activate the current context
if (!(glXMakeCurrent(dpy, px, cx))) {
fprintf(stderr, "glXMakeCurrent failed!
");
exit(0);
}

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

// Initialise OpenGL
glClearColor(255, 255, 255, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);

// Perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 1.0, 2000.0);

// Modelview
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// Camera position
glPushMatrix();
gluLookAt(10, 8, 5, 0, 0, 0, 0, 1, 0);

// Draw cube
glColor3f(1, 1, 0);
glBegin(GL_POLYGON);
glVertex3f(-2, 2, -2);
glVertex3f( 2, 2, -2);
glVertex3f( 2, -2, -2);
glVertex3f(-2, -2, -2);
glEnd();
glColor3f(0, 1, 0);
glBegin(GL_POLYGON);
glVertex3f(-2, -2, 2);
glVertex3f( 2, -2, 2);
glVertex3f( 2, 2, 2);
glVertex3f(-2, 2, 2);
glEnd();
glColor3f(0, 0, 1);
glBegin(GL_POLYGON);
glVertex3f(-2, -2, -2);
glVertex3f(-2, -2, 2);
glVertex3f(-2, 2, 2);
glVertex3f(-2, 2, -2);
glEnd();
glColor3f(1, 0, 0);
glBegin(GL_POLYGON);
glVertex3f( 2, 2, -2);
glVertex3f( 2, 2, 2);
glVertex3f( 2, -2, 2);
glVertex3f( 2, -2, -2);
glEnd();
glColor3f(0, 1, 1);
glBegin(GL_POLYGON);
glVertex3f(-2, 2, -2);
glVertex3f(-2, 2, 2);
glVertex3f( 2, 2, 2);
glVertex3f( 2, 2, -2);
glEnd();
glColor3f(1, 0, 1);
glBegin(GL_POLYGON);
glVertex3f( 2, -2, -2);
glVertex3f( 2, -2, 2);
glVertex3f(-2, -2, 2);
glVertex3f(-2, -2, -2);
glEnd();
glFlush();

// Allocate the buffer for extracting the offscreen result
pixels = (unsigned char )malloc(sizeof(unsigned char)wh3);

// Read the buffer result
glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, pixels);

// Write out to a PPM file
file = fopen(“sw.ppm”, “wb”);
fprintf(file, "P6
%d %d
255
", w, h);
fwrite(pixels, wh3*sizeof(unsigned char), 1, file);
fclose(file);

// Destroy the GLX pixmap
glXDestroyGLXPixmap(dpy, px);
// Destroy the pixmap
XFreePixmap(dpy, pix);
// Destroy the graphical context
glXDestroyContext(dpy, cx);
// Close the display
XCloseDisplay(dpy);
}

Have you upgraded to Apple’s X11 beta 3?

Yes, I have updated to X11 beta 3, and I’ve downloaded the corresponding X11 SDK. Does the code actually work for you?

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