Page 1 of 1

[PB 5.31] DrawingBufferPixelFormat-#PB_PixelFormat_ReversedY

Posted: Wed Dec 03, 2014 2:59 am
by juankprada
I'm using the following code with a Windows and a Linux machine both 64bits to generate OpenGL textures out of a loaded image.

Code: Select all

Procedure LoadGlTexture(*tex.GLTexture)
        UsePNGImageDecoder()
	Protected image = LoadImage(#PB_Any, "data/gonghead.png")
	Protected textureId.i
	glEnable(#GL_BLEND)
	glGenTextures(1, @textureId)
	glBindTexture(#GL_TEXTURE_2D, textureId)
	
	; grab the pixel data into the buffer
	Protected *buffer
	Protected dbpixelFormat.b
	Protected widthInBytes	
	
	StartDrawing(ImageOutput(image))
 		*buffer = DrawingBuffer()
 		widthInBytes = DrawingBufferPitch()
 		dbpixelFormat.b = DrawingBufferPixelFormat()
 	StopDrawing()

        Protected invertedY.b = dbpixelFormat & #PB_PixelFormat_ReversedY   
 	Protected depth.b = dbpixelFormat ! #PB_PixelFormat_ReversedY
        ....
The textured is loaded in both machines and I am able to display them using glBegin()/glEnd() OpenGL instructions (the same vertex and texture coordinates for both machines).

The thing is in Linux the image is upside down. i.e Y inverted and the real issue is that using DrawingBufferPixelFormat() didn't reported the flag #PB_PixelFormat_ReversedY which I could use in order to flip the texture.

Re: [PB 5.31] DrawingBufferPixelFormat-#PB_PixelFormat_Rever

Posted: Tue Jun 02, 2015 5:37 pm
by juankprada
This is kind of old thread, But I havent receive any feedback on this. Is this normal operation? is this a bug? is somebody looking at this issue?

I would like to know more about the status of this. or if there is anything else needed from me to get it reviewd and fixed if possible


Thanks

Re: [PB 5.31] DrawingBufferPixelFormat-#PB_PixelFormat_Rever

Posted: Tue Jun 02, 2015 6:27 pm
by Julian
Any change of putting a zip file up with an example piece of code and the image you're using?

It makes things a lot easier to test if you provide a working example showing the problem :)

Re: [PB 5.31] DrawingBufferPixelFormat-#PB_PixelFormat_Rever

Posted: Thu Sep 10, 2015 9:33 am
by Fred
Yes, would be cool to have a full working snippet. The #PB_PixelFormat_ReversedY flag should be only set on Windows, as it's the way it handle bitmap. On linux this flag shouldn't be set.

Re: [PB 5.31] DrawingBufferPixelFormat-#PB_PixelFormat_Rever

Posted: Mon Dec 14, 2015 6:28 pm
by Fred
Any chance to get a short code showing the issue ?

Re: [PB 5.31] DrawingBufferPixelFormat-#PB_PixelFormat_Rever

Posted: Mon Dec 14, 2015 6:56 pm
by juankprada
Sure, This is what I am using to load a PNG file and create an OpenGL texture with it:

Code: Select all

;---------------------------
; Structures
;---------------------------
Structure GLTexture
  id.i
  width.i
  height.i
  depth.i
  inverted.b
  *bytes ;data
EndStructure	


; Procedure used to load OpenGL Textures
Declare LoadGLTexture(*tex.GLTexture)



Procedure LoadGlTexture(*tex.GLTexture)
  
  UsePNGImageDecoder()
  
  Protected image = LoadImage(#PB_Any, "data/gonghead.png")
  Protected textureId.i
  
  glEnable(#GL_BLEND)
  glGenTextures(1, @textureId)
  glBindTexture(#GL_TEXTURE_2D, textureId)
  
  ; grab the pixel data into the buffer 
  StartDrawing(ImageOutput(image))
  Protected *buffer = DrawingBuffer()
  Protected widthInBytes = DrawingBufferPitch()
  Protected dbpixelFormat.b = DrawingBufferPixelFormat()
  StopDrawing()
  
  Protected invertedY.b = dbpixelFormat & #PB_PixelFormat_ReversedY   
  Protected depth.b = dbpixelFormat ! #PB_PixelFormat_ReversedY
  Protected imgWidth.i = ImageWidth(image)
  Protected imgHeight.i = ImageHeight(image)
  
  Protected openGLDepth
  Select depth
    Case #PB_PixelFormat_8Bits   
      openGLDepth = #GL_RED
    Case #PB_PixelFormat_16Bits      
      openGLDepth = #GL_RG
    Case #PB_PixelFormat_24Bits_RGB  
      openGLDepth = #GL_RGB
    Case #PB_PixelFormat_24Bits_BGR 
      openGLDepth = #GL_BGR
    Case #PB_PixelFormat_32Bits_RGB  
      openGLDepth = #GL_RGBA
    Case #PB_PixelFormat_32Bits_BGR 
      
      openGLDepth = #GL_BGRA
  EndSelect
  
  glTexParameteri (#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR);
  glTexParameteri (#GL_TEXTURE_2D, #GL_TEXTURE_MAG_FILTER, #GL_NEAREST);
  glTexParameteri (#GL_TEXTURE_2D, #GL_TEXTURE_WRAP_S, #GL_CLAMP_TO_EDGE);
  glTexParameteri (#GL_TEXTURE_2D, #GL_TEXTURE_WRAP_T, #GL_CLAMP_TO_EDGE);
  
  glTexImage2D (#GL_TEXTURE_2D, 0, #GL_RGBA, imgWidth, imgHeight, 0, openGLDepth, #GL_UNSIGNED_BYTE, *buffer);
  
  glDisable(#GL_BLEND)
  
  *tex\id = textureId
  *tex\bytes = *buffer
  *tex\width = imgWidth
  *tex\height = imgHeight
  
  If  invertedY.b = #PB_PixelFormat_ReversedY  
    *tex\inverted = #True
  Else
    *tex\inverted = #False
  EndIf	
  FreeImage(image)
  
  
EndProcedure
I am not sure it if its going to work out of the box for you as I am using a GLEW wrapper to have access to all OpenGL commands available (maybe there might be conflicts with the name convention used by default in PB for openGL commands)

Re: [PB 5.31] DrawingBufferPixelFormat-#PB_PixelFormat_Rever

Posted: Mon Dec 14, 2015 9:38 pm
by Fred
Unfortunately, we need full working snippet.

Re: [PB 5.31] DrawingBufferPixelFormat-#PB_PixelFormat_Rever

Posted: Mon Dec 14, 2015 10:01 pm
by juankprada
Ok, here is a sample using OpenGL Gadget.

Code: Select all


Global RollAxisX.f
Global RollAxisY.f
Global RollAxisZ.f

Global RotateSpeedX.f = 1.0
Global RotateSpeedY.f
Global RotateSpeedZ.f = 1.0

Global ZoomFactor.f = 1.0 ; Distance of the camera. Negative value = zoom back

#GL_BGR=$80E0
#GL_BGRA=$80E1
#GL_RGB=$1907
#GL_RGBA=$1908

Structure GLTexture
  id.i
  width.i
  height.i
  depth.i
  inverted.b
  *bytes ;data
EndStructure	


; Procedure used to load OpenGL Textures
Declare LoadGLTexture(*tex.GLTexture)



Procedure LoadGlTexture(*tex.GLTexture)
  
  UsePNGImageDecoder()
  
  Protected image = LoadImage(#PB_Any, "data/gonghead.png")
  Protected textureId.i
  
  glEnable_(#GL_BLEND)
  glGenTextures_(1, @textureId)
  glBindTexture_(#GL_TEXTURE_2D, textureId)
  
  ; grab the pixel data into the buffer 
  StartDrawing(ImageOutput(image))
  Protected *buffer = DrawingBuffer()
  Protected widthInBytes = DrawingBufferPitch()
  Protected dbpixelFormat.b = DrawingBufferPixelFormat()
  StopDrawing()
  
  Protected invertedY.b = dbpixelFormat & #PB_PixelFormat_ReversedY   
  Protected depth.b = dbpixelFormat ! #PB_PixelFormat_ReversedY
  Protected imgWidth.i = ImageWidth(image)
  Protected imgHeight.i = ImageHeight(image)
  
  Protected openGLDepth
  Select depth
    Case #PB_PixelFormat_8Bits   
      openGLDepth = #GL_RED
   
    Case #PB_PixelFormat_24Bits_RGB  
      openGLDepth = #GL_RGB
    Case #PB_PixelFormat_24Bits_BGR 
      openGLDepth = #GL_BGR
    Case #PB_PixelFormat_32Bits_RGB  
      openGLDepth = #GL_RGBA
    Case #PB_PixelFormat_32Bits_BGR 
      
      openGLDepth = #GL_BGRA
  EndSelect
  
  glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR);
  glTexParameteri_ (#GL_TEXTURE_2D, #GL_TEXTURE_MAG_FILTER, #GL_NEAREST);
 
  glTexImage2D_ (#GL_TEXTURE_2D, 0, #GL_RGBA, imgWidth, imgHeight, 0, openGLDepth, #GL_UNSIGNED_BYTE, *buffer);
  
  glDisable_(#GL_BLEND)
  
  *tex\id = textureId
  *tex\bytes = *buffer
  *tex\width = imgWidth
  *tex\height = imgHeight
  
  If  invertedY.b = #PB_PixelFormat_ReversedY  
    *tex\inverted = #True
  Else
    *tex\inverted = #False
  EndIf	
  FreeImage(image)
  
  
EndProcedure






OpenWindow(0, 0, 0, 530, 320, "OpenGL Gadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)


OpenGLGadget(1, 0, 0, 530, 320)
glClearColor_(0.4, 0.6, 0.9, 1)

AddWindowTimer(0, 1, 16) ; about 60 fps
Global tex.GLTexture 
      
LoadGLTexture(@tex)
Repeat
  Event = WaitWindowEvent()
  
  Select Event
    Case #PB_Event_Timer
        If EventTimer() = 1
         SetGadgetAttribute(1, #PB_OpenGL_SetContext, #True)
         glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT);
        
        
        
        glMatrixMode_(#GL_PROJECTION);
        glPushMatrix_()              ;
        glLoadIdentity_()            ;
        glOrtho_(0.0, 800, 0.0, 600, -1.0, 1.0);
        glMatrixMode_(#GL_MODELVIEW)           ;
        glPushMatrix_()                        ;
        
        
        glLoadIdentity_();
        glDisable_(#GL_LIGHTING);
        
        glBindTexture_(#GL_TEXTURE_2D, tex\id)
        glEnable_(#GL_TEXTURE_2D)
        glEnable_(#GL_BLEND)
        glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA);
        glBegin_(#GL_QUADS)
        
        glTexCoord2f_(0.0, 0.0); 
        glVertex3f_( 0.0, 0.0,  0.0);
        
        
        glTexCoord2f_(0.0, 1.0); 
        glVertex3f_( 0.0,  120,  0.0);  
        
        
        
        glTexCoord2f_(1.0, 1.0); 
        glVertex3f_( 60.2,  120.0,  0.0);
        
        
        
        glTexCoord2f_(1.0, 0.0); 
        glVertex3f_( 60.2,  0.0,  0.0);
        
        
        glEnd_()
        glFinish_()
        SetGadgetAttribute(1, #PB_OpenGL_FlipBuffers, #True)
      EndIf
  EndSelect
  
Until Event = #PB_Event_CloseWindow

__________________________________________________
Quote tags>Code tags
14.12.2015
RSBasic

Re: [PB 5.31] DrawingBufferPixelFormat-#PB_PixelFormat_Rever

Posted: Tue Dec 15, 2015 9:39 am
by Fred
After investigation, it's an error in your code: you affect DrawingBufferPixelFormat() to a '.b' variable which is wrong, as #PB_PixelFormat_ReversedY value is 32768. In a general rule you should not use .b/.w/.a/.u/.l outside of structure or array, as it brings no advantage, but you can truncate results very easily. Also the test to remove the flag should be "dbpixelFormat & ~#PB_PixelFormat_ReversedY" now. It correctly return "inversed" on Windows, and "not inversed" on Linux/OSX. Another note: to use the *buffer directly from DrawingBuffer(), StopDrawing() should not be called before the end of the use, as it does release it ! So it can work if you are lucky, but will fail or gives strange results/crash in other case.

Code: Select all

Global RollAxisX.f
Global RollAxisY.f
Global RollAxisZ.f

Global RotateSpeedX.f = 1.0
Global RotateSpeedY.f
Global RotateSpeedZ.f = 1.0

Global ZoomFactor.f = 1.0 ; Distance of the camera. Negative value = zoom back

#GL_BGR=$80E0
#GL_BGRA=$80E1
#GL_RGB=$1907
#GL_RGBA=$1908

Structure GLTexture
id.i
width.i
height.i
depth.i
inverted.b
*bytes ;data
EndStructure


; Procedure used to load OpenGL Textures
Declare LoadGLTexture(*tex.GLTexture)



Procedure LoadGlTexture(*tex.GLTexture)

UsePNGImageDecoder()

Protected image = LoadImage(#PB_Any, "shark.png")
Protected textureId.i

glEnable_(#GL_BLEND)
glGenTextures_(1, @textureId)
glBindTexture_(#GL_TEXTURE_2D, textureId)

; grab the pixel data into the buffer
StartDrawing(ImageOutput(image))
Protected *buffer = DrawingBuffer()
Protected widthInBytes = DrawingBufferPitch()
Protected dbpixelFormat = DrawingBufferPixelFormat()

Protected invertedY = dbpixelFormat & #PB_PixelFormat_ReversedY
Protected depth = dbpixelFormat & ~#PB_PixelFormat_ReversedY
Protected imgWidth.i = ImageWidth(image)
Protected imgHeight.i = ImageHeight(image)

Protected openGLDepth
Select depth
Case #PB_PixelFormat_8Bits
openGLDepth = #GL_RED

Case #PB_PixelFormat_24Bits_RGB
openGLDepth = #GL_RGB
Case #PB_PixelFormat_24Bits_BGR
openGLDepth = #GL_BGR
Case #PB_PixelFormat_32Bits_RGB
openGLDepth = #GL_RGBA
Case #PB_PixelFormat_32Bits_BGR

openGLDepth = #GL_BGRA
EndSelect

glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR);
glTexParameteri_ (#GL_TEXTURE_2D, #GL_TEXTURE_MAG_FILTER, #GL_NEAREST);

glTexImage2D_ (#GL_TEXTURE_2D, 0, #GL_RGBA, imgWidth, imgHeight, 0, openGLDepth, #GL_UNSIGNED_BYTE, *buffer);

glDisable_(#GL_BLEND)

*tex\id = textureId
*tex\bytes = *buffer
*tex\width = imgWidth
*tex\height = imgHeight

StopDrawing()


If invertedY = #PB_PixelFormat_ReversedY
*tex\inverted = #True
Else
*tex\inverted = #False
EndIf

Debug *tex\inverted

FreeImage(image)


EndProcedure






OpenWindow(0, 0, 0, 530, 320, "OpenGL Gadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)


OpenGLGadget(1, 0, 0, 530, 320)
glClearColor_(0.4, 0.6, 0.9, 1)

AddWindowTimer(0, 1, 16) ; about 60 fps
Global tex.GLTexture

LoadGLTexture(@tex)
Repeat
Event = WaitWindowEvent()

Select Event
Case #PB_Event_Timer
If EventTimer() = 1
SetGadgetAttribute(1, #PB_OpenGL_SetContext, #True)
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT);



glMatrixMode_(#GL_PROJECTION);
glPushMatrix_() ;
glLoadIdentity_() ;
glOrtho_(0.0, 800, 0.0, 600, -1.0, 1.0);
glMatrixMode_(#GL_MODELVIEW) ;
glPushMatrix_() ;


glLoadIdentity_();
glDisable_(#GL_LIGHTING);

glBindTexture_(#GL_TEXTURE_2D, tex\id)
glEnable_(#GL_TEXTURE_2D)
glEnable_(#GL_BLEND)
glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA);
glBegin_(#GL_QUADS)

glTexCoord2f_(0.0, 0.0);
glVertex3f_( 0.0, 0.0, 0.0);


glTexCoord2f_(0.0, 1.0);
glVertex3f_( 0.0, 120, 0.0);



glTexCoord2f_(1.0, 1.0);
glVertex3f_( 60.2, 120.0, 0.0);



glTexCoord2f_(1.0, 0.0);
glVertex3f_( 60.2, 0.0, 0.0);


glEnd_()
glFinish_()
SetGadgetAttribute(1, #PB_OpenGL_FlipBuffers, #True)
EndIf
EndSelect

Until Event = #PB_Event_CloseWindow

Re: [PB 5.31] DrawingBufferPixelFormat-#PB_PixelFormat_Rever

Posted: Tue Dec 15, 2015 2:28 pm
by juankprada
i feel so ashamed for posting a bug report with errors in the code. Sorry for making you waste your time and thanks for figuring out the errors.

Re: [PB 5.31] DrawingBufferPixelFormat-#PB_PixelFormat_Rever

Posted: Tue Dec 15, 2015 4:18 pm
by Fred
No problem, really.