Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: "Transparent" glClearColor in NSOpenGLView?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "Transparent" glClearColor in NSOpenGLView?



Thanks for the tip.

Clearly, this is what I'm looking for, but I have a problem.

My NSOpenGLView subclass calls some routines in a C++ library I've made.

What happens there is some init code that does the following:
- Load a picture with ImageMagick's C++ library.
- Set glClearColor to all 0s.
- Setup the picture as a texture.

And some drawing code that does this:
- Set the viewport to the entire NSOpenGLView (bounds).
- Clear the color buffer.
- Calculate where in the viewport the picture will be drawn (to maintain aspect ratio and stay in the middle of the viewport).
- Set the viewport to the newly calculated size.
- Draw a quad with the previously loaded texture, filling the entire (newly set) viewport.

In my NSOpenGLView subclass, the drawing routine is called on reshape.

The first time I draw the picture, everything looks perfect. It's drawn in the middle of the viewport, while the rest of the view is transparent, letting the window's background show trough.

On subsequent reshapes, however, the background gets distorted. Sometimes cleared to black (as suggested by the clear color), sometimes filling with garbage that looks like distorted pieces of the window's background (white lines or gray/black line if the window is textured) and sometimes a combination of the two.

I've included the code below, except the routine for calculating a new viewport size.

Here's the init code:
prepareGL() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_TEXTURE_2D);
glPixelStorei(GL_UNPACK_ALIGNMENT, 8);
glGenTextures(numberOfTextures, &d_textureId[0]);

glBindTexture(GL_TEXTURE_2D, d_textureId[TEX_ID_PICTURE]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, // Target
GL_RGB, // internalFormat
d_pictureWidth, // width
d_pictureHeight, // height
GL_RGBA, // format
GL_UNSIGNED_SHORT, // type
d_picture->setPixels( // pixels
0, 0,
d_pictureWidth,
d_pictureHeight));

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

And here's the drawing code:

drawPicture(const int& canvasWidth, const int& canvasHeight) {
glViewport(0, 0,
static_cast<GLsizei>(canvasWidth),
static_cast<GLsizei>(canvasHeight));
glLoadIdentity();
glTranslatef(0.0, 0.0, -1.0);
glClear(GL_COLOR_BUFFER_BIT);

/* Calculating new viewport size, considering
the picture's size and aspect ratio.
The result is four integers, indicating a
starting position relative to the current
viewport and a width and height. */

glViewport(static_cast<GLsizei>(viewportWidthStart),
static_cast<GLsizei>(viewportHeightStart),
static_cast<GLsizei>(viewportWidth),
static_cast<GLsizei>(viewportHeight));
glLoadIdentity();
glTranslatef(0.0, 0.0, -1.0);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, d_textureId[TEX_ID_PICTURE]);

glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(-1.0, -1.0);
glTexCoord2f(0.0, 1.0); glVertex2f(-1.0, 1.0);
glTexCoord2f(1.0, 1.0); glVertex2f(1.0, 1.0);
glTexCoord2f(1.0, 0.0); glVertex2f(1.0, -1.0);
glEnd();

glFlush();
}


- Idar

Josh Flowers wrote:
You need to do something like this:

long opaque = 0;
[openGLContext setValues:&opaque forParameter:NSOpenGLCPSurfaceOpacity];

There's a brief description of NSOpenGLCPSurfaceOpacity here: http://developer.apple.com/documentation/Cocoa/Reference/ ApplicationKit/ObjC_classic/Classes/NSOpenGLContext.html


Hello,

Is it possible to have an NSOpenGLView clear to a "transparent" color, so that it's parent window's color/texture in effect becomes the glClearColor?


- Idar
_______________________________________________
mac-opengl mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/mac-opengl
Do not post admin requests to the list. They will be ignored.

_______________________________________________
mac-opengl mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/mac-opengl
Do not post admin requests to the list. They will be ignored.
_______________________________________________
mac-opengl mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/mac-opengl
Do not post admin requests to the list. They will be ignored.

References: 
 >"Transparent" glClearColor in NSOpenGLView? (From: Idar Tollefsen <email@hidden>)
 >Re: "Transparent" glClearColor in NSOpenGLView? (From: Josh Flowers <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.