This is the mail archive of the
cygwin-xfree@cygwin.com
mailing list for the Cygwin XFree86 project.
Offscreen rendering
- From: "Kei Okada" <k-okada at jsk dot t dot u-tokyo dot ac dot jp>
- To: <cygwin-xfree at cygwin dot com>
- Cc: <k-okada at jsk dot t dot u-tokyo dot ac dot jp>
- Date: Fri, 27 Aug 2004 15:30:05 +0900
- Subject: Offscreen rendering
- Reply-to: cygwin-xfree at cygwin dot com
Hi,
I have a Cygwin/X installed on a XP and try to use a offscreen rendering
function.
Attached sample program works well on a Linux X server (Xfree 4.3.0),
but it does not works on a Cygwin/X both XWin and XWin_GL.
When I use XWin_GL, the program crashes at the first
glXMakeCurrent(dp, GPix, Ctxt) with the message like
change current
X Error of failed request: GLXBadContext
Major opcode of failed request: 146 (GLX)
Minor opcode of failed request: 5 (X_GLXMakeCurrent)
Serial number of failed request: 1143
Current serial number in output stream: 114
and When I use XWin, the X server crashes at the second
glXMakeCurrent(dpy, glwin, cx).
Messages on the shell invoking the sample program are
save buffer...
done
X connection to localhost:0.0 broken (explicit kill or server shutdown).
and the messages on the x server shell are
Signal 11
login: fatal IO error 32 (Broken pipe) or KillClient on X server ":0.0"
X connection to :0.0 broken (explicit kill or server shutdown).
giving up.
xinit: Connection refused (errno 111): unable to connect to X server
xinit: No such process (errno 3): unexpected signal 2.
What I am doing wrong? Does someone know the solution?
Thanks
Kei Okada
//http://www.nakadai.com/~nakadai/opengl/FAQ/openglfaq.html#faq-doc-4-18
/*
* Example of an X Window System OpenGL program.
* OpenGL code is taken from auxdemo.c in the Platform SDK
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <GL/glx.h>
#include <GL/gl.h>
#include <GL/glu.h>
/* X globals, defines, and prototypes */
Display *dpy;
Window glwin;
//static int attributes[] = {GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};
static int attributes[] = {GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 1, 0};
#define SWAPBUFFERS glXSwapBuffers(dpy, glwin)
#define BLACK_INDEX 0
#define RED_INDEX 1
#define GREEN_INDEX 2
#define BLUE_INDEX 4
#define WIDTH 300
#define HEIGHT 200
/* OpenGL globals, defines, and prototypes */
GLfloat latitude, longitude, latinc, longinc;
GLdouble radius;
#define GLOBE 1
#define CYLINDER 2
#define CONE 3
GLvoid resize(GLsizei, GLsizei);
GLvoid initializeGL(GLsizei, GLsizei);
GLvoid drawScene(GLvoid);
void polarView( GLdouble, GLdouble, GLdouble, GLdouble);
void saveScene();
static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
{
if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
return GL_TRUE;
}
return GL_FALSE;
}
int
main(int argc, char **argv)
{
XVisualInfo *vi;
Colormap cmap;
XSetWindowAttributes swa;
GLXContext cx;
XEvent event;
GLboolean needRedraw = GL_FALSE, recalcModelView = GL_TRUE;
int dummy;
_Xdebug = 1;
dpy = XOpenDisplay(NULL);
XSynchronize(dpy, 0);
if (dpy == NULL){
fprintf(stderr, "could not open display\n");
exit(1);
}
if(!glXQueryExtension(dpy, &dummy, &dummy)){
fprintf(stderr, "could not open display");
exit(1);
}
/* find an OpenGL-capable Color Index visual with depth buffer */
vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributes);
printf("vi = %p, depth = %d\n", vi, vi->depth);
if (vi == NULL) {
fprintf(stderr, "could not get visual\n");
exit(1);
}
XVisualInfo *Visp;
int attrList[10], n=0;
attrList[n++] = GLX_RGBA;
attrList[n++] = GLX_RED_SIZE; attrList[n++] = 8;
attrList[n++] = GLX_GREEN_SIZE; attrList[n++] = 8;
attrList[n++] = GLX_BLUE_SIZE; attrList[n++] = 8;
attrList[n++] = GLX_DEPTH_SIZE; attrList[n++] = 1;
attrList[n++] = None;
Visp = glXChooseVisual(dpy,
DefaultScreen(dpy), attrList);
printf("Visp = %p, depth = %d\n", Visp, Visp->depth);
static Pixmap XPix = 0;
static GLXPixmap GPix = 0;
XPix = XCreatePixmap(dpy, RootWindow(dpy, Visp->screen), WIDTH, HEIGHT,
Visp->depth);
printf("XPix = %p\n", XPix);
GPix = glXCreateGLXPixmap(dpy, Visp, XPix);
printf("GPix = %p\n", GPix);
/* create an OpenGL rendering context */
cx = glXCreateContext(dpy, vi, None, GL_TRUE); // direct
//cx = glXCreateContext(dpy, vi, None, GL_FALSE);
printf("cx = %p\n", cx);
GLXContext Ctxt;
Ctxt = glXCreateContext(dpy, Visp, None, GL_FALSE); // through X server
//Ctxt = glXCreateContext(dpy, Visp, None, GL_TRUE);
printf("Ctxt = %p\n", Ctxt);
if (cx == NULL || Ctxt == NULL) {
fprintf(stderr, "could not create rendering context\n");
exit(1);
}
/* create an X colormap since probably not using default visual */
cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
vi->visual, AllocNone);
swa.colormap = cmap;
swa.border_pixel = 0;
swa.event_mask = ExposureMask | KeyPressMask | StructureNotifyMask;
glwin = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, WIDTH,
HEIGHT, 0, vi->depth, InputOutput, vi->visual,
CWBorderPixel | CWColormap | CWEventMask, &swa);
XSetStandardProperties(dpy, glwin, "xogl", "xogl", None, argv,
argc, NULL);
glXMakeCurrent(dpy, glwin, cx);
XMapWindow(dpy, glwin);
XIfEvent(dpy, &event, WaitForMapNotify, (char *)glwin);
initializeGL(WIDTH, HEIGHT);
resize(WIDTH, HEIGHT);
/* Animation loop */
while (1) {
KeySym key;
while (XPending(dpy)) {
XNextEvent(dpy, &event);
switch (event.type) {
case KeyPress:
XLookupString((XKeyEvent *)&event, NULL, 0, &key, NULL);
switch (key) {
case XK_Left:
longinc += 0.5;
break;
case XK_Right:
longinc -= 0.5;
break;
case XK_Up:
latinc += 0.5;
break;
case XK_Down:
latinc -= 0.5;
break;
case 's':
printf("change current\n");
glXMakeCurrent(dpy, GPix, Ctxt);
printf ("save buffer...\n");
{
float a = latitude, o = longitude;
initializeGL(WIDTH, HEIGHT);
resize(WIDTH, HEIGHT);
latitude = a; longitude = o;
drawScene();
saveScene();
}
printf("done\n");
glXMakeCurrent(dpy, glwin, cx);
printf("change current\n");
break;
case 'e':
exit(1);
break;
}
break;
case ConfigureNotify:
resize(event.xconfigure.width, event.xconfigure.height);
break;
}
}
drawScene();
}
return 0;
}
/* OpenGL code */
GLvoid resize( GLsizei width, GLsizei height )
{
GLfloat aspect;
glViewport( 0, 0, width, height );
aspect = (GLfloat) width / height;
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, aspect, 3.0, 7.0 );
glMatrixMode( GL_MODELVIEW );
}
GLvoid createObjects()
{
GLUquadricObj *quadObj;
glNewList(GLOBE, GL_COMPILE);
quadObj = gluNewQuadric ();
gluQuadricDrawStyle (quadObj, GLU_LINE);
gluSphere (quadObj, 1.5, 16, 16);
glEndList();
glNewList(CONE, GL_COMPILE);
quadObj = gluNewQuadric ();
gluQuadricDrawStyle (quadObj, GLU_FILL);
gluQuadricNormals (quadObj, GLU_SMOOTH);
gluCylinder(quadObj, 0.3, 0.0, 0.6, 15, 10);
glEndList();
glNewList(CYLINDER, GL_COMPILE);
glPushMatrix ();
glRotatef ((GLfloat)90.0, (GLfloat)1.0, (GLfloat)0.0, (GLfloat)0.0);
glTranslatef ((GLfloat)0.0, (GLfloat)0.0, (GLfloat)-1.0);
quadObj = gluNewQuadric ();
gluQuadricDrawStyle (quadObj, GLU_FILL);
gluQuadricNormals (quadObj, GLU_SMOOTH);
gluCylinder (quadObj, 0.3, 0.3, 0.6, 12, 2);
glPopMatrix ();
glEndList();
}
GLvoid initializeGL(GLsizei width, GLsizei height)
{
GLfloat maxObjectSize, aspect;
GLdouble near_plane, far_plane;
glClearIndex( (GLfloat)BLACK_INDEX);
glClearDepth( 1.0 );
glEnable(GL_DEPTH_TEST);
glMatrixMode( GL_PROJECTION );
aspect = (GLfloat) width / height;
gluPerspective( 45.0, aspect, 3.0, 7.0 );
glMatrixMode( GL_MODELVIEW );
near_plane = 3.0;
far_plane = 7.0;
maxObjectSize = 3.0F;
radius = near_plane + maxObjectSize/2.0;
latitude = 0.0F;
longitude = 0.0F;
latinc = 6.0F;
longinc = 2.5F;
createObjects();
}
void polarView(GLdouble radius, GLdouble twist, GLdouble latitude,
GLdouble longitude)
{
glTranslated(0.0, 0.0, -radius);
glRotated(-twist, 0.0, 0.0, 1.0);
glRotated(-latitude, 1.0, 0.0, 0.0);
glRotated(longitude, 0.0, 0.0, 1.0);
}
GLvoid drawScene(GLvoid)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
latitude += latinc/50;
longitude += longinc/50;
polarView( radius, 0, latitude, longitude );
glIndexi(RED_INDEX);
glCallList(CONE);
glIndexi(BLUE_INDEX);
glCallList(GLOBE);
glIndexi(GREEN_INDEX);
glPushMatrix();
glTranslatef(0.8F, -0.65F, 0.0F);
glRotatef(30.0F, 1.0F, 0.5F, 1.0F);
glCallList(CYLINDER);
glPopMatrix();
glPopMatrix();
SWAPBUFFERS;
}
void saveScene()
{
char imgbuf[WIDTH*HEIGHT*3];
glReadBuffer(GL_FRONT);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, imgbuf);
FILE *fp = fopen("test.ppm", "w+");
fprintf(fp, "P6\n");
fprintf(fp, "#\n");
fprintf(fp, "%d %d 255\n", WIDTH, HEIGHT);
fwrite(imgbuf, 1, WIDTH*HEIGHT*3, fp);
fclose(fp);
}