//#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/Xfixes.h>
#include<GL/gl.h>
#include<GL/glx.h>
#include <GL/glext.h>
#include<GL/glu.h>
#include <GL/glut.h>
static PFNGLXBINDTEXIMAGEEXTPROC glXBindTexImageEXT = NULL;
static PFNGLXRELEASETEXIMAGEEXTPROC glXReleaseTexImageEXT = NULL;
Display *display;
Window window;
int value;
float top,bottom;
int nfbconfigs;
GLuint texture;
int i;
GLXFBConfig *fbconfigs;
Window getWindowIdFromName(Display *dpy, Window level, char *name)
{
Window *children;
Window dmy;
Window root;
Window id;
char *window_name;
int i;
unsigned int no_of_children;
if (0 == XQueryTree(dpy, level, &dmy, &dmy, &children, &no_of_children))
return 0;
for (i=0; i<no_of_children; i++)
{
if (XFetchName(dpy,children[i], &window_name))
{
if (strcmp (name
, window_name
)) XFree(window_name);
else
{
XFree(window_name);
return children[i];
}
}
else
{
if (id = getWindowIdFromName(dpy, children[i], name))
return id;
}
}
return 0;
}
void drawing(){
//sleep(1);
/* draw using pixmap as texture */
glClearColor(0.25, 0.25, 0.25, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glBegin (GL_QUADS);
glVertex3f(-1.0f,1.0f,0.0f);
glTexCoord2d(-1.0f,1.0f);
glVertex3f(1.0f,1.0f,0.0f);
glTexCoord2d(1.0f,1.0f);
glVertex3f(1.0f,-1.0f,0.0f);
glTexCoord2d(1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,0.0f);
glTexCoord2d(-1.0f,-1.0f);
glEnd ();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char **argv){
glutInit(&argc,argv);
//glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition(0,0);
glXBindTexImageEXT = (PFNGLXBINDTEXIMAGEEXTPROC)
glXGetProcAddress((GLubyte *) "glXBindTexImageEXT");
glXReleaseTexImageEXT = (PFNGLXRELEASETEXIMAGEEXTPROC)
glXGetProcAddress((GLubyte*) "glXReleaseTexImageEXT");
if(!glXBindTexImageEXT || !glXReleaseTexImageEXT)
{
printf("Some extension functions missing!\n"); //return 1;
}
display = XOpenDisplay(NULL);
window = XDefaultRootWindow(display);
XCompositeRedirectSubwindows (display, window, CompositeRedirectAutomatic);
XSelectInput (display, window, SubstructureNotifyMask);
XWindowAttributes attrib;
XGetWindowAttributes (display, window, &attrib);
VisualID visualid = XVisualIDFromVisual (attrib.visual);
fbconfigs = glXGetFBConfigs (display, XDefaultScreen(display), &nfbconfigs);
for (i = 0; i < nfbconfigs; i++)
{
XVisualInfo *visinfo = glXGetVisualFromFBConfig (display, fbconfigs[i]);
if (!visinfo || visinfo->visualid != visualid)
continue;
glXGetFBConfigAttrib (display, fbconfigs[i], GLX_DRAWABLE_TYPE, &value);
if (!(value & GLX_PIXMAP_BIT))
continue;
glXGetFBConfigAttrib (display, fbconfigs[i],
GLX_BIND_TO_TEXTURE_TARGETS_EXT,
&value);
if (!(value & GLX_TEXTURE_2D_BIT_EXT))
continue;
glXGetFBConfigAttrib (display, fbconfigs[i],
GLX_BIND_TO_TEXTURE_RGBA_EXT,
&value);
if (value == False)
{
glXGetFBConfigAttrib (display, fbconfigs[i],
GLX_BIND_TO_TEXTURE_RGB_EXT,
&value);
if (value == False)
continue;
}
glXGetFBConfigAttrib (display, fbconfigs[i],
GLX_Y_INVERTED_EXT,
&value);
if (value == True)
{
top = 0.0f;
bottom = 1.0f;
}
else
{
top = 1.0f;
bottom = 0.0f;
}
break;
}
if (i == nfbconfigs){
/* error 1 */
}
//Window wind = getWindowIdFromName(display,window,"Qt");
Pixmap pixmap = XCompositeNameWindowPixmap (display, window);
const int pixmapAttribs[] = { GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGBA_EXT,
GLX_MIPMAP_TEXTURE_EXT,
None };
GLXPixmap glxpixmap = glXCreatePixmap (display, fbconfigs[i], pixmap, pixmapAttribs);
gluOrtho2D(0,attrib.width,0,attrib.height);
glEnable(GL_TEXTURE_2D);
glGenTextures (1, &texture);
glBindTexture (GL_TEXTURE_2D, texture);
//XGrabServer(display);
glXBindTexImageEXT (display, glxpixmap, GLX_FRONT_LEFT_EXT, NULL);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//XUngrabServer(display);
//glXReleaseTexImageEXT (display,glxpixmap,GLX_FRONT_LEFT_EXT);
glutInitWindowSize(500,500);
glutCreateWindow("simplex");
glutDisplayFunc(drawing);
//glutIdleFunc(drawing);
glutMainLoop();
return 0;
}