Hi Niels,
this is a simple example program. It just copies a sub-image from the pbuffer into a texture. Every time the window is resized, the pbuffer will be destroyed and re-created with the same size as the window.
Hope it helps!
#include <GL/glut.h>
#include <GL/glx.h>
int sbAttrib = { GLX_DOUBLEBUFFER, 0,
GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
None };
int pbAttrib = { GLX_PBUFFER_WIDTH, 100,
GLX_PBUFFER_HEIGHT, 100,
GLX_PRESERVED_CONTENTS, True,
None };
Display *dpy;
GLXFBConfig *fbc;
GLXPbuffer pbuf;
GLXDrawable currdraw;
GLXContext currctx, pbufctx;
GLuint texture;
void init()
{
int i, j, c, n;
GLubyte texImage[64][64][4];
// Create a texture
glClearColor(0.5, 0.5, 0.5, 0.0);
glEnable(GL_TEXTURE_2D);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
for (i=0; i<64; i++) {
for (j=0; j<64; j++) {
c = ((((i&0x8)==0)^((j&0x8))==0))*255;
texImage[i][j][0] = (GLubyte) c;
texImage[i][j][1] = (GLubyte) c;
texImage[i][j][2] = (GLubyte) c;
texImage[i][j][3] = (GLubyte) 255;
}
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
// Create the pbuffer
// (Texture objects are shared between the two contexts)
dpy = glXGetCurrentDisplay();
currctx = glXGetCurrentContext();
currdraw = glXGetCurrentDrawable();
fbc = glXChooseFBConfig(dpy, DefaultScreen(dpy), sbAttrib, &n);
pbuf = glXCreatePbuffer(dpy, fbc[0], pbAttrib);
pbufctx = glXCreateNewContext(dpy, fbc[0], GLX_RGBA_TYPE, currctx, True);
}
void paint()
{
glXMakeCurrent(dpy, pbuf, pbufctx);
glClearColor(1.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, texture);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 20, 20, 0, 0, 30, 30);
glXMakeCurrent(dpy, currdraw, currctx);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(-5.0, -5.0);
glTexCoord2f(1.0, 0.0); glVertex2f(5.0, -5.0);
glTexCoord2f(1.0, 1.0); glVertex2f(5.0, 5.0);
glTexCoord2f(0.0, 1.0); glVertex2f(-5.0, 5.0);
glEnd();
glDisable(GL_TEXTURE_2D);
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-10.0, 10.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Now destroy the pbuffer and create a new one
pbAttrib[1] = w;
pbAttrib[3] = h;
glXDestroyPbuffer(dpy, pbuf);
pbuf = glXCreatePbuffer(dpy, fbc[0], pbAttrib);
}
int main( int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(“PBuffer Test”);
init();
glutDisplayFunc(paint);
glutReshapeFunc(reshape);
glutMainLoop();
return 1;
}
[This message has been edited by andy1 (edited 06-04-2002).]