View Full Version : another core graphics question -- textures

09-22-2006, 03:25 PM
So, I was using gworlds before in Carbon to import textures to opengl. Unfortunately, when I build a universal application the compiler says the calls I'm using are going to be deprecated. So, I saw the mac pdf reference from another post on the mac opengl board and it had an example of reading in a file with quartz. Anyhow, the example code works fine with one little issue -- the textures are all upside down! I had the same problem with my previous code which led to a transform solution:

//this works in the old code...
MatrixRecord matrix;
SetIdentityMatrix (&matrix);
TranslateMatrix ( &matrix, -X2Fix(0.5f * imageWidth), -X2Fix(0.5f * imageHeight));
ScaleMatrix (&matrix, X2Fix(1.0), X2Fix(-1.0), X2Fix (0.0), X2Fix (0.0));
TranslateMatrix ( &matrix, X2Fix(0.5f * imageWidth), X2Fix(0.5f * imageHeight));
err = GraphicsImportSetMatrix(giComp, &matrix);

Quartz has a similiar mechanism but I cant get it to work. I suspect its related to these calls...

CGContextRef myBitmapContext = CGBitmapContextCreate (myData,
width, height, 8,
width*4, space,
CGContextTranslateCTM(myBitmapContext, -width *0.5, -height *0.5 );
CGContextScaleCTM(myBitmapContext, 0, -1);
CGContextTranslateCTM(myBitmapContext, width *0.5, height*0.5 );
CGContextDrawImage(myBitmapContext, rect, myImageRef);

return myImageRef;

Some apple examples just have the 180 degree rotation...does not seem to work for me. Most of the examples are textures and I'm not sure if they realize they are upside down.(?)

Anyhow, the quartz stuff just produces a black texture.. :( Any ideas??

09-22-2006, 03:41 PM
As an aside, an upside down normal map import is going to create some really ugly results. ;p

09-22-2006, 04:43 PM
Figured it out. ( more like googled it out )

CGContextTranslateCTM(myBitmapContext, 0, height);
CGContextScaleCTM(myBitmapContext, 1.0, -1.0);

09-25-2006, 07:28 AM
Shoot, seem to be running into another problem.

If I use kCGImageAlphaPremultipliedFirst in

CGContextRef myBitmapContext = CGBitmapContextCreate (myData,
width, height, _bits,
width*4, space,

call then I get a nice texutre. Looks great with everything ( particularly leaves ) except for a few cases. For instance, if I have an explosion image. There is alot of aplha and its not constant. The explosion looks like a gray blob. The reds and oranges get premultipled to a gray like color.

OK, one would think just dont premultiply the alpha and use kCGImageAlphaFirst. But this always returns nothing in everycase. No more texture.


09-25-2006, 08:44 AM
Darn, looks like you have to have premultiplied alpha.


09-26-2006, 12:57 PM
well, sorry, i can't help you- i just came here because i like monologue threads :D if you decide to register here, i recommend your nickname should be 'hamlet'

10-03-2006, 07:26 AM
premultiplied alpha in this blog may help