offscreen rendering with GLXPixmaps
offscreen rendering with GLXPixmaps
- Subject: offscreen rendering with GLXPixmaps
- From: Glenn Tsang <email@hidden>
- Date: Thu, 13 Mar 2003 18:27:54 -0500
I am interested in the ability to do offscreen OpenGL rendering. Since
pbuffers are not yet available (when is GLX 1.3 going to be
supported?), I will have to settle with the software-based solution
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!
Cheers,
Glenn Tsang.
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, 0, GL_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 (window)!\n");
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)*w*h*3);
// 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\n%d %d\n 255\n", w, h);
fwrite(pixels, w*h*3*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);
}
_______________________________________________
x11-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/x11-users
X11 for Mac OS X FAQ: http://developer.apple.com/qa/qa2001/qa1232.html
Report issues, request features, feedback: http://developer.apple.com/bugreporter
Do not post admin requests to the list. They will be ignored.