How to stop OpenGL from applying blending to certain content? (see pics)
Posted
by RexOnRoids
on Stack Overflow
See other posts from Stack Overflow
or by RexOnRoids
Published on 2010-05-25T22:29:29Z
Indexed on
2010/05/25
22:51 UTC
Read the original article
Hit count: 310
Supporting Info:
I use cocos2d to draw a sprite (graph background) on the screen (z:-1). I then use cocos2d to draw lines/points (z:0) on top of the background -- and make some calls to OpenGL blending functions before the drawing to SMOOTH out the lines.
Problem:
The problem is that: aside from producing smooth lines/points, calling these OpenGL blending functions seems to "degrade" the underlying sprite (graph background). As you can see from the images below, the "degraded" background seems to be made darker and less sharp in Case 2.
So there is a tradeoff: I can either have (Case 1) a nice background and choppy lines/points, or I can have (Case 2) nice smooth lines/points and a degraded background. But obviously I need both.
THE QUESTION:
How do I set OpenGL so as to only apply the blending to the layer with the Lines/Points in it and thus leave the background alone?
The Code:
I have included code of the draw() method of the CCLayer for both cases explained above. As you can see, the code producing the difference between Case 1 and Case 2 seems to be 1 or 2 lines involving OpenGL Blending.
Case 1 -- MainScene.h (CCLayer):
-(void)draw{
int lastPointX = 0;
int lastPointY = 0;
GLfloat colorMAX = 255.0f;
GLfloat valR;
GLfloat valG;
GLfloat valB;
if([self.myGraphManager ready]){
valR = (255.0f/colorMAX)*1.0f;
valG = (255.0f/colorMAX)*1.0f;
valB = (255.0f/colorMAX)*1.0f;
NSEnumerator *enumerator = [[self.myGraphManager.currentCanvas graphPoints] objectEnumerator];
GraphPoint* object;
while ((object = [enumerator nextObject])) {
if(object.filled){
/*Commenting out the following two lines induces a problem of making it impossible to have smooth lines/points, but has merit in that it does not degrade the background sprite.*/
//glEnable (GL_BLEND);
//glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
glEnable (GL_LINE_SMOOTH);
glLineWidth(1.5f);
glColor4f(valR, valG, valB, 1.0);
ccDrawLine(ccp(lastPointX, lastPointY), ccp(object.position.x, object.position.y));
lastPointX = object.position.x;
lastPointY = object.position.y;
glPointSize(3.0f);
glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
ccDrawPoint(ccp(lastPointX, lastPointY));
}
}
}
}
Case 2 -- MainScene.h (CCLayer):
-(void)draw{
int lastPointX = 0;
int lastPointY = 0;
GLfloat colorMAX = 255.0f;
GLfloat valR;
GLfloat valG;
GLfloat valB;
if([self.myGraphManager ready]){
valR = (255.0f/colorMAX)*1.0f;
valG = (255.0f/colorMAX)*1.0f;
valB = (255.0f/colorMAX)*1.0f;
NSEnumerator *enumerator = [[self.myGraphManager.currentCanvas graphPoints] objectEnumerator];
GraphPoint* object;
while ((object = [enumerator nextObject])) {
if(object.filled){
/*Enabling the following two lines gives nice smooth lines/points, but has a problem in that it degrades the background sprite.*/
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
glEnable (GL_LINE_SMOOTH);
glLineWidth(1.5f);
glColor4f(valR, valG, valB, 1.0);
ccDrawLine(ccp(lastPointX, lastPointY), ccp(object.position.x, object.position.y));
lastPointX = object.position.x;
lastPointY = object.position.y;
glPointSize(3.0f);
glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
ccDrawPoint(ccp(lastPointX, lastPointY));
}
}
}
}
© Stack Overflow or respective owner