Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Alpha-masked .tiffs in OpenGL

Alpha-masked .tiffs in OpenGL
Thread Tools
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Aug 21, 2002, 02:31 AM
 
Hello ...


I'm trying to port some graphics aspects of an app to OpenGL for (MUCH) greater performance, but I'm getting hung up on the methodology of drawing alpha-masked .tiffs (to take the place of using NSImage "compositeToPoint" commands in Cocoa).

Thanks to the Cocoa ports of sample code from the NeHe tutorials, I can load a .tiff properly; and with these settings

glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glEnable( GL_ALPHA_TEST );
glAlphaFunc( GL_GREATER, 0.0 );

I can render the images _almost_ the way I want: the effects of the alpha mask are apparent, creating black or darkened areas within the image; but I can't see _through_ those areas to what's "under" the image.

NeHe has a tutorial that covers OpenGL masking via separate black-and-white bitmaps, but I need levels of transparency. Another demo discusses loading TGA files, which support an alpha channel, but I don't see what it's doing that I'm not.

Searching the OpenGL.org discussion groups hasn't helped much, since I'm still new to a lot of the jargon and command options.

Can someone explain what I'm missing, or point me to some simple sample Cocoa/Obj-C code?


Thanks and regards,

DayLateDon
     
Mac Enthusiast
Join Date: Jan 2001
Status: Offline
Reply With Quote
Aug 21, 2002, 10:26 AM
 
try JUST this:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE);

enabling alpha-test i don't think does what you want... blending alone should work.
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Aug 22, 2002, 02:04 AM
 
Hello ...

Well, now I seem to have the opposite problem: I can see through the holes and semi-transparent areas of the .tiff texture just fine, but the should-be-opaque areas are combining with the background color.

Perhaps relatedly, my scene also involves a figure created as a collection of white lines. No matter where I draw the .tiff-textured quad in relation to the line figure ("above" or "below", z-axis-wise), the quad never obscures the line figure. (Using glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ), I get obscuration ... but then I can't see through the "holes".)


Maybe it's not just what I'm doing wrong, but _where_ I'm doing it wrong. Here's the essence of my code (in an NSOpenGLView subclass), excluding only the details of the figure drawing and texture loading:

initGL
{
( load .tiff texture --works fine)

glClearColor( .5f, .5f, .5f, 0.50f );
glClearDepth( 1.0f );
glEnable( GL_BLEND );
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
}
end initGL

drawRect
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

( draw white line figure )

glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, ( my texture ) );

( draw .tiff-textured quad --works fine, apart from the alpha problems )

glDisable( GL_TEXTURE_2D );

[ [ self openGLContext ] flushBuffer ];
}
end drawRect


Any more suggestions?

Thanks and regards,

DayLateDon
     
Mac Enthusiast
Join Date: Jan 2001
Status: Offline
Reply With Quote
Aug 25, 2002, 08:22 PM
 
another shot in the dark:

did you:
glEnable(GL_DEPTH_TEST); ??
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Aug 25, 2002, 09:55 PM
 
Hello ...


glEnable( GL_DEPTH_TEST ) ?

I did at one point, but it wasn't in my current build. Doesn't seem to help: With it enabled, only the top-most item (the textured quad or my line figure) appears.

If I enable it before drawing the textured quad (which is on top) then disable it after, I get the same thing as if I hadn't had it at all. (Enabling/disabling around the line drawing doesn't do anything.)

How does Apple draw the icons in the zooming icons screen saver?


I appreciate the help,

DayLateDon
     
Mac Enthusiast
Join Date: Jan 2001
Status: Offline
Reply With Quote
Aug 26, 2002, 11:02 PM
 
in that case, i have no idea -- sorry... perhaps it's something wrong with your tiff?
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Sep 1, 2002, 01:10 AM
 
Hello ...


Well, it turns out (I think) that something was wrong with the .tiff ... not the file itself, but the way it got loaded. The texture-loading routine's call to glTexImage2d() had a "color components" value of only 3 (R, G, B); changing that to 4 (R, G, B, A) eliminated my problem with holes and semi-transparent areas of the rendered texture. (The sample code from which I took the texture-loading routine distinguishes between alphad and non-alphad images when setting some properties, and I thought it had covered all of the bases.) Note to self: The problem is always the part you _know_ isn't the problem.

I don't have alpha-testing or depth-testing enabled. Just blending, with glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ).


Now to go figure out how to make the NSOpenGLView and the OpenGL scene's background transparent ...


Thanks for the help!

DayLateDon
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:11 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2