Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / 3D Theory & Graphics / Render to texture with openGL Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
lefranck

May 24, 2005, 06:47 AM

Hi people,

here is my problem. I want to do a render to texture, then map the texture to an object and render it, nothing stunning, but I never played with that before. So I create a pbuffer and a matching context, switch to it, render, switch back to window context and try to copy the content of the pbuffer to my texture. I switchj between contexts with glXMakeContextCurrent, and when I switch back to window context I pass the pbuffer as readable. The problem is that if I use glReadPixels and glTexImage2D it works fine, but it's of course really slow, and with glCopyTexImage2D my texture stays unaltered. Any idea what I could be doing wrong ?

 
Rui Martins

May 24, 2005, 08:51 AM

Without seeing any code the only things I can come up with are:
- You are not using binary Powers for the width and/or size of the rendered texture.
- Check your Color Mask
- The Previous texture is cached in a Display List (?)
- Driver Problems

 
kiddygames

May 24, 2005, 09:14 AM

Hello,

using a pbuffer, you don't have to copy the pbuffer content to a texture, but bind your pbuffer to your active texture with the BindTexImageARB extension.

for windows it's something like :

  1.  
  2.   glBindTexture( GL_TEXTURE_2D, myCurrentTextureID );
  3.   wglBindTexImageARB(myPBufferHandle, WGL_FRONT_LEFT_ARB );
  4.  
  5.    // render here
  6.  
  7.   glBindTexture( GL_TEXTURE_2D, myCurrentTextureID );
  8.   wglReleaseTexImageARB( myPBuffer, WGL_FRONT_LEFT_ARB );
  9.  


no pixel blit here

Stéphane

 
Rui Martins

May 24, 2005, 11:23 AM

Are you sure you have a LEFT and RIGHT buffer ?
i.e. does your graphics board support these buffer for stereoscopy ?

Did your application confirm (queried) the existence of these buffers ?

NOTE: It is usual to draw on the BACK buffer, when double buffering!

Maybe you should be using one of:
WGL_BACK_LEFT_ARB
WGL_BACK_RIGHT_ARB
OR:
WGL_AUX0_ARB
WGL_AUX1_ARB
WGL_AUX2_ARB
WGL_AUX3_ARB
WGL_AUX4_ARB
WGL_AUX5_ARB
WGL_AUX6_ARB
WGL_AUX7_ARB
WGL_AUX8_ARB
WGL_AUX9_ARB

NOTE:You should also check the return boolean on each of those operations.

Check the Spec too:
http://oss.sgi.com/projects/ogl-sample/registry/ARB/wgl_render_texture.txt

 
lefranck

May 24, 2005, 12:06 PM

Thanks for your reply.

First of all, i can't use wgl cos I use X-window and not Windows. I have to stick to glx and wglBindTexImageARB is not supported yet. I'm planning on porting that to windows later, in which case I'll use those functions of course.

Then, here is some code...

That's how I create my target texture:

  1.  
  2.  // Create target texture
  3.   for (int i = 0; i < 1024*1024; i++) {
  4.     texData[i*3] = 0;
  5.     texData[(i*3) + 1] = 255;
  6.     texData[(i*3) + 2] = 0;
  7.   }
  8.   glGenTextures(1, &targetTexture);
  9.   glBindTexture(GL_TEXTURE_2D, targetTexture);
  10.   glTexImage2D(GL_TEXTURE_2D, 0, 3, 1024, 1024, 0, GL_RGB, GL_UNSIGNED_BYTE, texData);
  11.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  12.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  13.   free(texData);
  14.  


That's how I create my pbuffer:
  1.  
  2. // Create a pbuffer
  3. GLXFBConfig *config = NULL;
  4. int FB_attribs[] = { GLX_RED_SIZE, 8,
  5.                      GLX_GREEN_SIZE, 8,
  6.                      GLX_BLUE_SIZE, 8,
  7.                      GLX_X_RENDERABLE, True,
  8.                      GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
  9.                      None};
  10. int n_configs = 0;
  11.  
  12. config = glXChooseFBConfig(GLWin.dpy, 0, FB_attribs, &n_configs);
  13. if (n_configs == 0) {
  14.   cerr << "Could not get matching frame buffer configuration" << endl;
  15. } else {
  16.   cout << "Found " << n_configs << " matching frame buffer configurations" << endl;
  17.  
  18.   int PB_attribs[] = {GLX_PBUFFER_WIDTH, 1024,
  19.                       GLX_PBUFFER_HEIGHT, 1024};
  20.   pbufferCTX = glXCreateNewContext(GLWin.dpy, config[0], GLX_RGBA_TYPE, NULL, true);
  21.   pbuffer = glXCreatePbuffer(GLWin.dpy, config[0], PB_attribs);
  22.  


And when I render I first do:
  1.  
  2. if (!glXMakeContextCurrent(GLWin.dpy, pbuffer, pbuffer, pbufferCTX) ) {
  3.   cerr << "Could not make pbuffer the current drawable" << endl;
  4. }
  5.  
  6. ... render stuff in the pbuffer (hopefully) ...
  7.  


Then I change context and render the textured quad:
  1.  
  2. if (!glXMakeContextCurrent(GLWin.dpy, GLWin.win, pbuffer, GLWin.ctx) ) {
  3.   cerr << "Could not make frame buffer the current drawable" << endl;
  4. }
  5. ...
  6. glCopyTexImage2D(targetTexture, 0, GL_RGB, 0, 0, 1024, 1024, 0);
  7. glBindTexture(GL_TEXTURE_2D, targetTexture);
  8. //glReadPixels(0, 0, 1024, 1024, GL_RGB, GL_UNSIGNED_BYTE, tmpData);
  9. //glTexImage2D(GL_TEXTURE_2D, 0, 3, 1024, 1024, 0, GL_RGB, GL_UNSIGNED_BYTE, tmpData);
  10.  
  11. glXSwapBuffers(GLWin.dpy, GLWin.win);
  12.  


I do use powers of 2 (1024*1024). About the caching, I tried to call glFlush (right technique?) but that didn't help.

 
Rui Martins

May 24, 2005, 12:15 PM

Just a wild guess, swap these lines:

  1. glBindTexture(GL_TEXTURE_2D, targetTexture);
  2. glCopyTexImage2D(targetTexture, 0, GL_RGB, 0, 0, 1024, 1024, 0);

 
lefranck

May 24, 2005, 01:28 PM

Already tried that, but it doesn't make a difference.

Thanks,
Franck.

 
kiddygames

May 24, 2005, 01:57 PM

the wglXXX was just a sample, but I suppose that if pbuffer is supported on x-windows, there's a way to bind the pbuffer to a texture without copying pixels ? It's just the main purpose of the pbuffer, and the BindTexImageARB is an ARB extension so are you sure it's not supported by x-windows ?

Stéphane

 
kiddygames

May 24, 2005, 02:01 PM

Well, it's just a sample, but in most case you don't have to double buffer your pbuffer (so no back buffer) and I think you have to choose

WGL_FRONT_LEFT_ARB
WGL_FRONT_RIGHT_ARB

as WGL_FRONT_ARB or WGL_BACK_ARB don't exist if I remenber.

But without stereo I guess WGL_FRONT_LEFT_ARB and WGL_FRONT_RIGHT_ARB give the same result.

Stéphane

 
lefranck

May 24, 2005, 02:15 PM

Unfortunately you definitely can't. I found that on NVidia's website. As far as I understand, wgl and glx are extensions an are not part of the OpenGL specifications. Under Linux, the fastest way is to copy the content of the pbuffer to a texture using glCopyTex(Sub)Image2D. And I have an NVidia card (FX5900 XT).

Franck.

 
Axel

May 24, 2005, 02:36 PM

Take a look at EXT_framebuffer_object. nVIDIA already supports it AFAIR.

 
Chris

May 24, 2005, 04:42 PM

ATI not yet, regrettably.

 
d00sint2

May 24, 2005, 04:58 PM

Do nVidia support it though? I have a GeForce FX 5200 and the latest linux drivers and can not see the ext_framebuffer extension.

 
Reedbeta

May 25, 2005, 01:24 AM

I guess you need to call wglShareLists or rather the GLX equivalent to share texture objects between the two contexts - if not, you won't be able to copy the pbuffer to the texture because the pbuffer can't see the texture from its context.

 
dreddi

May 25, 2005, 02:35 AM


Hi,

If you can't use the pbuffer because it does not work, then why
you just stop using it:> First render to color buffer, copy it
to a texture using glCopyTexImage2D and then render your scene using
this texture.

Always works:)

Regards,
dReddi

 
lefranck

May 25, 2005, 04:17 AM

Just updated to the latest drivers (1.0.7174), still no support for FBO. Plus my GPU fan isnow very noisym so I think I'll switch back :) I'm not specifying anything to share, so that might be the reason, I'll have a look at that as soon as I have time again... And if that doesn't work either I'll have a look at color buffers.

Franck.

 
lefranck

May 30, 2005, 11:19 AM

Ok, mea culpa, I decided to use glGetError() and found out wy it wasn't working. The correct syntax is:

  1.  
  2. glBindTexture(GL_TEXTURE_2D, targetTexture);
  3. glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 1024, 1024, 0);
  4.  


This works fine with both pbuffer and color buffer. Only my code runs twice as fast if I simply use the color buffer. So I'm not sure why I'm using a pbuffer then, I think I got sidetracked by that nvidia pdf... Anyway, thanks for your help !!

Franck.

 
This thread contains 17 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.