Weird OpenGL Issues (Mach-O)

Apprentice
Posts: 17
Joined: 2005.05
Post: #1
Hi,

I'm developing a program with REALbasic 5.5.4 which requires the use of OpenGL, and must be cross-platform. Everything works, drawing, coloring, blending, etc, except for texturing... All my textures appear as white. They show up in the profiler, and everything. They just arn't being drawn.

At first I thought it was a bug in my texturing code. Nope. Its about 10 lines and I must have checked it at least 20 times. Then I thought it might be a problem in the way the context is setup. Bingo.

But its not how I create the context, its what library I use. I was originally compiling my program for Mach-O and Win32, and just out of curiosity made it compile for PEF instead. Wow. Suddenly it works. Its rather strange...

Here is the code I'm using to setup the context. As you can see, there is no difference in how I'm actually doing it, just which library I use...

Code:
dim attributes as memoryBlock
  dim context, pixelFormat as integer
  
  #IF TARGETMACHO
    ' the context is created fine. but texturing dosn't work...
    declare function aglChoosePixelFormat lib "AGL" ( gdevs as integer, ndev as integer, attribs as ptr ) as integer
    declare sub aglDestroyPixelFormat lib "AGL" ( pix as integer )
    declare function aglCreateContext lib "AGL" ( pix as integer, share as integer ) as integer
    declare function aglSetDrawable lib "AGL" ( ctx as integer, draw as integer ) as boolean
    
    declare function GetWindowPort lib "Carbon" ( window as integer ) as integer
    
    const AGL_NONE = 0
    const AGL_RGBA = 4
    const AGL_DOUBLEBUFFER = 5
    const AGL_PIXEL_SIZE = 50
    const AGL_NO_RECOVERY = 72
    const AGL_ACCELERATED = 73
    
    attributes = new memoryBlock(28)
    
    attributes.long(0) = AGL_RGBA
    attributes.long(4) = AGL_DOUBLEBUFFER
    attributes.long(8) = AGL_NO_RECOVERY
    attributes.long(12) = AGL_ACCELERATED
    attributes.long(16) = AGL_PIXEL_SIZE
    attributes.long(20) = 32
    attributes.long(24) = AGL_NONE
    
    pixelFormat = aglChoosePixelFormat( 0, 0, attributes )
    if pixelFormat = 0 then raise new runtimeException
    
    context = aglCreateContext( pixelFormat, 0 )
    if context = 0 then raise new runtimeException
    
    if not aglSetDrawable( context, GetWindowPort( me.handle ) ) then _
        raise new runtimeException
    
    aglDestroyPixelFormat( pixelFormat )
    
  #ELSEIF TARGETCARBON
    ' texturing works :/ kinda odd
    declare function aglChoosePixelFormat lib "OpenGLLibrary" ( gdevs as integer, ndev as integer, attribs as ptr ) as integer
    declare sub aglDestroyPixelFormat lib "OpenGLLibrary" ( pix as integer )
    declare function aglCreateContext lib "OpenGLLibrary" ( pix as integer, share as integer ) as integer
    declare function aglSetDrawable lib "OpenGLLibrary" ( ctx as integer, draw as integer ) as boolean
    
    declare function GetWindowPort lib "CarbonLib" ( window as integer ) as integer
    
    const AGL_NONE = 0
    const AGL_RGBA = 4
    const AGL_DOUBLEBUFFER = 5
    const AGL_PIXEL_SIZE = 50
    const AGL_NO_RECOVERY = 72
    const AGL_ACCELERATED = 73
    
    attributes = new memoryBlock(28)
    
    attributes.long(0) = AGL_RGBA
    attributes.long(4) = AGL_DOUBLEBUFFER
    attributes.long(8) = AGL_NO_RECOVERY
    attributes.long(12) = AGL_ACCELERATED
    attributes.long(16) = AGL_PIXEL_SIZE
    attributes.long(20) = 32
    attributes.long(24) = AGL_NONE
    
    pixelFormat = aglChoosePixelFormat( 0, 0, attributes )
    if pixelFormat = 0 then raise new runtimeException
    
    context = aglCreateContext( pixelFormat, 0 )
    if context = 0 then raise new runtimeException
    
    if not aglSetDrawable( context, GetWindowPort( me.handle ) ) then _
        raise new runtimeException
    
    aglDestroyPixelFormat( pixelFormat )
    
  #ELSEIF TARGETWIN32
  #ENDIF
  
  me.theContext = context

To make sure it wasn't my problem (I sure hope its not :/ ) I simplified my drawing code... Here is it:

Code:
#IF TARGETMACHO
    declare function aglSetCurrentContext lib "AGL" ( ctx as integer ) as boolean
    if not aglSetCurrentContext( me.theContext ) then _
        raise new runtimeException
  #ELSEIF TARGETCARBON
    declare function aglSetCurrentContext lib "OpenGLLibrary" ( ctx as integer ) as boolean
    if not aglSetCurrentContext( me.theContext ) then _
        raise new runtimeException
  #ENDIF
  
  dim id, data as memoryBlock
  dim p as picture
  
  if me.textureID = 0 then
    id = new memoryBlock(4)
    glGenTextures( 1, id )
    glBindTexture( GL_TEXTURE_2D, id.long(0) )
    
    p = new picture( 128, 128, 32 )
    p.graphics.drawPicture nehe, 0, 0
    
    ' xGLGetPictureBytes... A plugin function that converts an rb picture into RGBA data...
    data = xGLGetPictureBytes( p, true, false, nil )
    
    glTexImage2d( GL_TEXTURE_2D, 0, GL_RGBA, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, data )
    
    me.textureID = id.long(0)
  end
  
  glEnable( GL_TEXTURE_2D )
  
  glClear( GL_COLOR_BUFFER_BIT + GL_DEPTH_BUFFER_BIT )
  
  glViewport( 0, 0, me.width, me.height )
  
  glMatrixMode( GL_PROJECTION )
  glLoadIdentity
  
  glOrtho( 0, me.height, me.width, 0, 0, 1 )
  
  glMatrixMode( GL_MODELVIEW )
  glLoadIdentity
  
  glBegin( GL_QUADS )
  glTexCoord2f( 0, 0 )
  glVertex2i( 0, 0 )
  glTexCoord2f( 1, 0 )
  glVertex2i( 128, 0 )
  glTexCoord2f( 1, 1 )
  glVertex2i( 128, 128 )
  glTexCoord2f( 0, 1 )
  glVertex2i( 0, 128 )
  glEnd
  
  
  #IF TARGETMACHO
    declare sub aglSwapBuffers lib "AGL" ( ctx as integer )
    aglSwapBuffers( me.theContext )
  #ELSEIF TARGETCARBON
    declare sub aglSwapBuffers lib "OpenGLLibrary" ( ctx as integer )
    aglSwapBuffers( me.theContext )
  #ENDIF

As I said, both PEF and Mach-O versions compile and run, and both display geometry, but the Mach-O version doesn't draw the texture... It shows up in Apple's OpenGL Profiler fine (its not corrupted or anything), so the texture is actually being bound and such, just not being drawn...

I know people might be wondering why I want to use Mach-O, since PEF still works with MacOS X... I have to use other Mach-O only technologies such as CoreAudio, so I need the project to be Mach-O. (And it also happens to be the native executable format for MacOS X)

Quite puzzling....

Thanks,
Adam
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #2
Add:
Code:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
If you read the GL spec, the default minification filter is GL_NEAREST_MIPMAP_LINEAR, so using glTexImage2d for only the top mipmap level results in an incomplete texture; getting white objects is the correct behavior. If you don't use mipmaps you must set the minification filter.

10.2 and earlier had the wrong default filter and let you get away with not setting it. Presumably PEF apps under 10.3 keep that behavior.
Quote this message in a reply
Apprentice
Posts: 17
Joined: 2005.05
Post: #3
Thanks arekkusu that fixed it... Its funny how something like that is the solution.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Weird OpenGL thing... jfFlash 9 3,733 Sep 13, 2006 08:11 PM
Last Post: akb825
  Weird OpenGL texture problem DoG 27 9,536 May 2, 2003 07:01 PM
Last Post: henryj
  Carbon Opengl issues David 4 3,646 Dec 26, 2002 01:56 AM
Last Post: David