X Server & OpenGL Render

How to be capable to grab the display of an application that is running in the background (no focus) and convert it into a texture for OpenGL?

What do you mean ? Be very more precise. Is it a GL program running in background ? Or is it a simple X program ? Do you want to make ‘real-time’ texture creation ?

What I’m doing is… I got a display (:1) that is running with Xvfb, and on my display (:0) I got an OpenGL application that is running… Now in example I start xterm on the display :1, what I want to do is to grab the display of xterm and load it as a texture in my OpenGL application. I guess is pretty much as you said, ‘real-time’ texture creation.

I just don’t get it how to grab the display (pixmap or x window dump) of (ie) xterm running on my display :1 and load it as a texture in my OpenGL program running on my display :0, except by passing by xwd and convert it into jpeg, but its really slow, what are my other options since I need to do this operation in memory to speed up the whole thing?

Tks in advance…

Unfortunately I can’t help you. You will effectively need to take a screenshot from one program and translate it into your another program. But you might use networking for that (just a guess since I don’t know how you do that translation) and that you use 2 different displays (so 2 distinct X clients). glCopyTexSubImage (or so) should help you.

Also, I can remember VNV, do you know it, maybe you should look at how it is done, that might help you a bit.

Do you have the address of the website? Can’t find it…

Ok well, I’ve done some research and come out with this sample code, now my problem is how to be capable to convert the Ximage/ZPixmap to a format that OpenGL understand, and load it as a texture…

#include <stdlib.h>
#include <stdio.h>

#include <GL/glu.h>
#include <X11/Xlib.h>
#include <X11/Xmu/WinUtil.h>

int main (int argc, char **argv)
{
	Display *dpy;
	dpy = XOpenDisplay(":1.0");
	
	if (dpy == NULL) 
	{ printf("Cannot connect to X server ':1.0'
");
      exit(1); }
	
	Window target_win;
	target_win = 0x400016; // In example...
  
	  
    XWindowAttributes win_info;

    int absx, absy, x, y;
	XImage *image;
	unsigned width, height;
    Window dummywin;
		
	XGetWindowAttributes(dpy, target_win, &win_info);
	XTranslateCoordinates(dpy, target_win, RootWindow(dpy, 0), 0, 0,  &absx, &absy, &dummywin);

	win_info.x = absx;
    win_info.y = absy;
    width = win_info.width;
    height = win_info.height;	
    x = absx - win_info.x;
    y = absy - win_info.y;		
	
	XMapRaised(dpy, target_win);
	XSync(dpy, 0);
	XSetInputFocus(dpy, target_win, RevertToPointerRoot, CurrentTime);

	image = XGetImage(dpy, target_win, x, y, width, height, AllPlanes, ZPixmap);
	if(!image) printf("Unable to create image...
");		
		
	printf("%d %d", image->width, image->height);
	
	
	GLuint texture;
    glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,  GL_LINEAR);
	
    gluBuild2DMipmaps (GL_TEXTURE_2D, GL_RGB,
                       image->width, image->height,
                       GL_RGB, GL_UNSIGNED_BYTE,
                       image->data); // Exit with segmentation fault...
	
	XDestroyImage(image);
	XCloseDisplay(dpy);
	exit(0);	
}

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.