PDA

View Full Version : [OpenGL/MFC] problem of bit setting of render texture (ripple error)?


NDark
03-10-2008, 09:00 PM
hi,

I have a problem of ripple error from rendering on OpenGL / MFC with texture . render result is like below :

http://vision.csie.ncku.edu.tw/~ndark/blogPortal/download/ScreenShotRipple.jpg

It seems that the bit for each channel is too small to describe smooth color from 0 to 255 , but I don't know which part of setting goes wrong ?

I render one projective texture on a mesh , the loaded image ( 24 bit ) is smooth from Left , White ( 255 255 255 ) to Right , Black ( 0 0 0 ) .

。The Image loading is : RGB , unsigned byte for each channel . I add 4th channel with filling 255 to each pixel to it after loading .

。The texture loading is linear of filter and RGBA 4 channel , like this

    glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR ) ;
    glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR ) ;
    glTexImage2D( GL_TEXTURE_2D , 0 , 4 , GetWidth() , GetHeight() , 0 , GL_RGBA , GL_UNSIGNED_BYTE , CaptureImgBuff_RGBA ) ;

。Projective texture setting is linear of filter and GL_REPLACE for glTexEnvi( ... )

。When draw mesh , I use glVertex4d( x , y , z , 1.0 ) , for each vertex . Of course , disable lighting and blending .

。Read out back frame buffer , use glReadPixels ( 0 , 0, w , h , GL_RGBA , GL_UNSIGNED_BYTE , pdata ) , and store pdata to TGA file .

the result is shown at top link , it seems that there are only 16 steps(4bit??) to describe each channel from 0 to 255 .



So I guess the question is about setting rendering device of Windows application( MFC in here ) ?


。At setting device part , PIXELFORMATDESCRIPTOR of SetPixelFormat( ... ) is

    static PIXELFORMATDESCRIPTOR pfd = {
      sizeof(PIXELFORMATDESCRIPTOR), // Size of this structure
      1, // Version of this structure
      PFD_DRAW_TO_WINDOW |// Draw to Window (not to bitmap)
      PFD_SUPPORT_OPENGL | // Support OpenGL calls in window
      PFD_DOUBLEBUFFER, // Double buffered mode
      PFD_TYPE_RGBA, // RGBA Color mode
      24, // Want 24bit color
      0,0,0,0,0,0, // Not used to select mode
      8, // cAlphaBits : Specifies the number of alpha bitplanes in each RGBA color buffer.
      0, // Not used to select mode
      0,0,0,0,0, // Not used to select mode
      32, // Size of depth buffer
      8, // cStencilBits
      0,
      PFD_MAIN_PLANE, // Draw in main plane
      0,
      0,0,0 };

    nPixelFormat = ChoosePixelFormat(m_hDC_3DModel, &pfd) , nPixelFormat return is 51 with cAlphaBits specified , and 7 when cAlphaBits is not specified .
    SetPixelFormat(m_hDC_3DModel, nPixelFormat, &pfd)) ;

However , nPixelFormat will fail in render texture , surprisingly , 24 or 32 will ok in rendering texture ( but still have ripple error ) .




Do you have any idea about the solution?

Thanks in advance,

NDark

Reedbeta
03-10-2008, 09:06 PM
I wonder if the texture is not being stored with enough bits internally. Try setting the third parameter of glTexImage2D to GL_RGBA8 instead of 4. (That parameter was changed to 'internal format' insted of 'number of components' in OpenGL 1.1.)

Also, please use the ... tags to post code.

NDark
03-11-2008, 12:39 AM
thank you for reply ,

I set the Internal Texture Formats of glTexImage2D to GL_RGBA8 , and the result go smooth . Excellent !!



However , it doesn't explain why the function ,

  nPixelFormat = ChoosePixelFormat(m_hDC_3DModel, &pfd) ,

return 51 and when 51 are used in

  SetPixelFormat(m_hDC_3DModel, nPixelFormat, &pfd) ,

the texture is not availble ( I draw each vertex of glColor4ub( 255 , 255 , 255 , alphaValue ) , and only color and alphaValue seem work a little ) , only when i set nPixelFormat to 24 or 32 , the texture is correct , though alpha is all white again .


thanks in advance ,


NDark

Reedbeta
03-11-2008, 09:55 AM
I see you're asking for a 32-bit depth buffer. Does it work better if you only ask for 24 depth bits?

The reason is that most graphics cards have a pixel format with 24 bits of color, 8 bits alpha, 24 bits depth, and 8 bits stencil, so it fits very nicely into 64 bits per pixel. If you want 32 bits depth and also a stencil buffer, you're asking for 72 bits per pixel, which the graphics card probably won't do because that's not a nice power of two. So ChoosePixelFormat will compromise on something when you ask for this pixel format.

Also, could you explain more clearly what you're trying to do with the alpha channel? I'm having a hard time understanding you.

NDark
03-12-2008, 01:48 AM
I've try your suggestion and other possible Pixel format descriptor of "PIXELFORMATDESCRIPTOR pfd" , and still won't work .( even though i set depth buffer to 8 , stencil buffer to 0 ).

  I try to save frame buffer data ( screenshot so called ) including alpha channel in TGA file , before that I only save RGB in it , using :

  glReadPixels( 0 , 0 , w , h , GL_RGBA , GL_UNSIGNED_BYTE , pBuff ) ;
  WriteTga( filename , w , h , 4 , pBuff ) ;

  I've read this thread
"http://www.devmaster.net/forums/showthread.php?t=3414&highlight=alpha+pixel" , and I think we are doing the same thing ?

  When I call for 8 in cAlphaBits among PIXELFORMATDESCRIPTOR , the function ChoosePixelFormat return 51 and the texture will not work .

  If cAlphaBits is 0 , the ChoosePixelFormat return 7 ( no matter how much the depth buffer bit , color bit , and stencil buff bit are ) , however , texture work well .

  Please let me know if anyplace I should explain more , or source code I should post .




NDark

Reedbeta
03-12-2008, 08:19 AM
It doesn't help me to hear what ChoosePixelFormat returned; those numbers are all hardware specific. But you can call DescribePixelFormat and give it one of those numbers and it will tell you what actual pixel format that represents.

Anyway, make sure your video driver is up to date.