mast4as
unread,Mar 22, 2012, 10:37:38 AM3/22/12You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to
Hi everyone,
I have something strange happening in my code that I can't explain.
Even though it's related to GLX I hope someone can help me with
this ??? So I "copied" some simple code that creates X window and GLX
context to make some GL calls to. When I create a 3.1 GLX context then
my drawing code works fine. But when I create a 3.2 nothing is drawn
to the screen ??? Even though the context is properly created ... when
I print out the version of the context, it returns "3.2.0 NVIDIA
xxx.xxxx". Do you have any idea why this difference between 3.1 and
3.2. I am not using the right flags to glXCreateContextAttribsARB ??
Thank you very much ... Hope someone can help
-Coralie
// create a window render to it save image quit
void createX11Window(const Scene *world)
{
Display *display = XOpenDisplay(0);
if (!display) throw("Failed to open X display");
// Get a matching FB config
static int visual_attribs[] =
{
GLX_X_RENDERABLE , True,
GLX_DRAWABLE_TYPE , GLX_WINDOW_BIT,
GLX_RENDER_TYPE , GLX_RGBA_BIT,
GLX_X_VISUAL_TYPE , GLX_TRUE_COLOR,
GLX_RED_SIZE , 8,
GLX_GREEN_SIZE , 8,
GLX_BLUE_SIZE , 8,
GLX_ALPHA_SIZE , 8,
GLX_DEPTH_SIZE , 24,
GLX_STENCIL_SIZE , 8,
GLX_DOUBLEBUFFER , True,
//GLX_SAMPLE_BUFFERS , 1,
//GLX_SAMPLES , 4,
None
};
int glx_major, glx_minor;
// FBConfigs were added in GLX version 1.3.
if (!glXQueryVersion(display, &glx_major, &glx_minor) ||
(( glx_major == 1 ) && ( glx_minor < 3 )) || (glx_major < 1))
throw("invalid GLX version");
printf("Getting matching framebuffer configs\n");
int fbcount;
GLXFBConfig *fbc = glXChooseFBConfig(display,
DefaultScreen( display ), visual_attribs, &fbcount);
if (!fbc) throw("Failed to retrieve a framebuffer config\n");
printf( "Found %d matching FB configs.\n", fbcount );
// Pick the FB config/visual with the most samples per pixel
printf( "Getting XVisualInfos\n" );
int best_fbc = -1, worst_fbc = -1, best_num_samp = -1, worst_num_samp
= 999;
int i;
for (i = 0; i < fbcount; i++) {
XVisualInfo *vi = glXGetVisualFromFBConfig(display, fbc[i]);
if (vi)
{
int samp_buf, samples;
glXGetFBConfigAttrib(display, fbc[i], GLX_SAMPLE_BUFFERS,
&samp_buf);
glXGetFBConfigAttrib(display, fbc[i], GLX_SAMPLES, &samples);
printf( "Matching fbconfig %d, visual ID 0x%2x: SAMPLE_BUFFERS =
%d, SAMPLES = %d\n",
i, vi->visualid, samp_buf, samples);
if (best_fbc < 0 || samp_buf && samples > best_num_samp)
best_fbc = i, best_num_samp = samples;
if (worst_fbc < 0 || !samp_buf || samples < worst_num_samp)
worst_fbc = i, worst_num_samp = samples;
}
XFree( vi );
}
GLXFBConfig bestFbc = fbc[best_fbc];
// Be sure to free the FBConfig list allocated by glXChooseFBConfig()
XFree( fbc );
// Get a visual
XVisualInfo *vi = glXGetVisualFromFBConfig( display, bestFbc );
printf( "Chosen visual ID = 0x%x\n", vi->visualid );
printf( "Creating colormap\n" );
XSetWindowAttributes swa;
Colormap cmap;
swa.colormap = cmap = XCreateColormap(display, RootWindow(display, vi-
>screen), vi->visual, AllocNone);
swa.background_pixmap = None ;
swa.border_pixel = 0;
swa.event_mask = StructureNotifyMask;
printf("Creating window\n");
Window win = XCreateWindow(display, RootWindow(display, vi->screen),
0, 0, world->width, world->height, 0, vi->depth, InputOutput,
vi->visual,
CWBorderPixel|CWColormap|CWEventMask, &swa);
if (!win) throw("Failed to create window.\n");
// Done with the visual info data
XFree(vi);
XStoreName(display, win, "GL 3.2 Window");
printf("Mapping window\n");
XMapWindow( display, win );
// Get the default screen's GLX extension list
const char *glxExts = glXQueryExtensionsString( display,
DefaultScreen( display ) );
// NOTE: It is not necessary to create or make current to a context
before
// calling glXGetProcAddressARB
glXCreateContextAttribsARBProc glXCreateContextAttribsARB = 0;
glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc)
glXGetProcAddressARB( (const GLubyte *)
"glXCreateContextAttribsARB" );
GLXContext ctx = 0;
// Install an X error handler so the application won't exit if GL 3.0
// context allocation fails.
//
// Note this error handler is global. All display connections in all
threads
// of a process use the same error handler, so be sure to guard
against other
// threads issuing X commands while this code is running.
ctxErrorOccurred = false;
int (*oldHandler)(Display*, XErrorEvent*) =
XSetErrorHandler(&ctxErrorHandler);
// Check for the GLX_ARB_create_context extension string and the
function.
// If either is not present, use GLX 1.3 context creation method.
if (!isExtensionSupported(glxExts, "GLX_ARB_create_context") || !
glXCreateContextAttribsARB) {
printf( "glXCreateContextAttribsARB() not found ... using old-style
GLX context\n" );
ctx = glXCreateNewContext( display, bestFbc, GLX_RGBA_TYPE, 0,
True );
}
// If it does, try to get a GL 3.0 context!
else
{
int context_attribs[] =
{
GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
GLX_CONTEXT_MINOR_VERSION_ARB, 2,
GLX_CONTEXT_FLAGS_ARB ,
GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
//GLX_CONTEXT_PROFILE_MASK_ARB ,
GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
None
};
printf( "Creating context\n" );
ctx = glXCreateContextAttribsARB( display, bestFbc, 0, True,
context_attribs );
//char tmp[512];
//glGetString(GL_VERSION, tmp);
// Sync to ensure any errors generated are processed.
XSync( display, False );
if ( !ctxErrorOccurred && ctx )
printf( "Created GL 3.2 context\n" );
else {
// Couldn't create GL 3.0 context. Fall back to old-style 2.x
context.
// When a context version below 3.0 is requested, implementations
will
// return the newest context version compatible with OpenGL
versions less
// than version 3.0.
// GLX_CONTEXT_MAJOR_VERSION_ARB = 1
context_attribs[1] = 1;
// GLX_CONTEXT_MINOR_VERSION_ARB = 0
context_attribs[3] = 0;
ctxErrorOccurred = false;
printf( "Failed to create GL 3.0 context ... using old-style GLX
context\n" );
ctx = glXCreateContextAttribsARB( display, bestFbc, 0, True,
context_attribs );
}
}
// Sync to ensure any errors generated are processed.
XSync( display, False );
// Restore the original error handler
XSetErrorHandler( oldHandler );
if ( ctxErrorOccurred || !ctx )
{
printf( "Failed to create an OpenGL context\n" );
exit(1);
}
// Verifying that context is a direct context
if ( ! glXIsDirect ( display, ctx ) )
{
printf( "Indirect GLX rendering context obtained\n" );
}
else
{
printf( "Direct GLX rendering context obtained\n" );
}
printf( "Making context current\n" );
glXMakeCurrent( display, win, ctx );
fprintf(stderr, ">>>>>>>>>> %s\n", glGetString(GL_VERSION));
//glEnable(GL_DEPTH_TEST);
glClearColor(1,0,0,1);
glViewport(0, 0, world->width, world->height);
GLint dim[4];
glGetIntegerv(GL_VIEWPORT, dim); std::cerr << dim[0] << " " << dim[1]
<< " " << dim[2] << " " << dim[3] << std::endl;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float fov = 30;
float angle = tan(M_PI / 180.f * fov * 0.5);
float nearClipping = 0.1;
float frameAspectRatio = world->width / (float)world->height;
float top = angle * nearClipping;
float bottom = -top;
float right = frameAspectRatio * top;
float left = -right;
fprintf(stderr, "%f %f %f %f\n", left, right, top, bottom);
glFrustum(left, right, bottom, top, 0.1, 1000);
//gluPerspective(30,(float)512/512,0.1,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3f(1,1,1);
glTranslatef(0, 0, -15);
glRectf(-1, -1, 1, 1);
glFlush();
glXSwapBuffers ( display, win );
sleep(1);
//drawAndCapture(display, win, world);
glXMakeCurrent( display, 0, 0 );
glXDestroyContext( display, ctx );
XDestroyWindow( display, win );
XFreeColormap( display, cmap );
XCloseDisplay( display );
}