the following snippet creates a window, clears the buffer, reads a pixel and displays its rgb- and alpha-values.
note the “GLX_ALPHA_SIZE, 8” in the attribute array- it’s not enough to add “GLX_RGBA” to the attributes list. the visual id is displayed; type ‘glxinfo’ in the shell to see this visual’s capabilities (depth size, alpha size etc.)
(if you are using glut, i think you have to use “GLUT_WINDOW_ALPHA_SIZE”)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<GL/glx.h>
#include<GL/glu.h>
Display *dpy = XOpenDisplay(NULL);
Window root = DefaultRootWindow(dpy);
GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, GLX_ALPHA_SIZE, 8, None };
XVisualInfo *vi = glXChooseVisual(dpy, 0, att);
GLXContext glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
Colormap cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
int dep = DefaultDepth(dpy, 0);
int cmask = CWColormap | CWBorderPixel | CWEventMask;
XWindowAttributes gwa;
Window win;
int main(int argc, char *argv[]){
XSetWindowAttributes swa;
XEvent xev;
swa.colormap = cmap;
swa.border_pixel = 0;
swa.event_mask = ExposureMask | KeyPressMask;
win = XCreateWindow(dpy, root, 0, 0, 100, 100, 0, dep, InputOutput, vi->visual, cmask, &swa);
XMapWindow(dpy, win);
glXMakeCurrent(dpy, win, glc);
glClearColor(0.00, 0.00, 0.60, 0.30);
printf("
VisualID = %p
", vi->visualid);
while(true) {
XNextEvent(dpy, &xev);
if(xev.type == KeyPress) {
glXMakeCurrent(dpy, None, NULL);
glXDestroyContext(dpy, glc);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
exit(0); }
if(xev.type == Expose) {
//
// RESIZE VIEWPORT, CLEAR BUFFER
//
XGetWindowAttributes(dpy, win, &gwa);
glViewport(0, 0, gwa.width, gwa.height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glXSwapBuffers(dpy, win);
//
// READ PIXEL VALUES
//
GLfloat pixels[4];
glReadBuffer(GL_FRONT);
glReadPixels(10, 10, 1, 1, GL_RGBA, GL_FLOAT, pixels);
printf("
R= %7.3f, G= %7.3f, B= %7.3f, A= %7.3f
", pixels[0], pixels[1], pixels[2], pixels[3]); } } }
//
// gcc -o gl-alpha gl-alpha.cc -lX11 -lGL -lGLU
//