Sign in to follow this  
3dnewbie

quick question regarding translucency and GL_BLEND

Recommended Posts

Hi, I setup blending and it all works fine. Translucency to. Really nice to see it work :-). At first i didn't but then i changed the order around of the objects being drawn and voila. anyway, my question is this: I have a blue quad like so glColor4f(0.0f,0.0f,0.1f,0.1f); It is nice blue with blending disabled. When blending is turned on however, i get an almost black square. At this point it is completly translucent. The more i turn up the alpha level however, the less translucent it gets (that's expected ofcourse) and the more it get's back to it's original level of opaqueness. However, the more i do so, the quad seems to be drawn as 2 triangles each with different colors. This is kind of unwanted :-). I think i understand why it does this, but i would like it to retain the quad and color i set which display without blending enabled, and then if i increase the alpha value, that it becomes more or less translucent.

Share this post


Link to post
Share on other sites
Quote:
Original post by 3dnewbie
However, the more i do so, the quad seems to be drawn as 2 triangles each with different colors.

This is kind of unwanted :-). I think i understand why it does this...
Hm, maybe you could post your theory. Offhand I can't think of why the colors of the triangles in the quad would differ.

You might also post the code where you render the quad.

Share this post


Link to post
Share on other sites
Well there's not much to post, i just draw a quad, set alpha channel to 0.5, and when i turn on blending i get a quad separated by 2 triangles each of different colour. It's transparent/translucent sure, but not in a particular nice way :-)

Share this post


Link to post
Share on other sites
Well, something must be happening to cause the effect you describe, and without seeing the code it's hard to say what that might be. So again, I'd go ahead and post the code where you render the quad.

Share this post


Link to post
Share on other sites
Ok, i'll post some pieces, i hope it's not to much. I've read about this phenomena and it seems to be normal, although i can't believe that. Maybe i should use ARB extension?

This is the code responsible for drawing a matrix:

GLvoid display_grid(GLvoid)
{

glBegin(GL_QUADS);
for(x=-10.0;x<GRID_SIZE;x+=SECTOR_SIZE) {
for(y=-10.0;y<GRID_SIZE;y+=SECTOR_SIZE) {
/* Set color for QUADS */
glColor4f(0.0f,0.0f,0.1,0.1f);
/* Draw QUADS */
glVertex3f(x,y,0); /* left */
glVertex3f(x,y+SECTOR_SIZE,0); /* left */
glVertex3f(x,y,0); /* right up*/
glVertex3f(x+SECTOR_SIZE,y,0); /* right down */
glVertex3f(x+SECTOR_SIZE,y+SECTOR_SIZE,0); /* leftbottom */
glVertex3f(x,y+SECTOR_SIZE,0); /* rightbottom */
glVertex3f(x+SECTOR_SIZE,y,0); /* leftupper*/
glVertex3f(x+SECTOR_SIZE,y+SECTOR_SIZE,0);
}
}
glEnd();

glBegin(GL_LINES);
for(x=-10.0;x<GRID_SIZE;x+=SECTOR_SIZE) {
for(y=-10.0;y<GRID_SIZE;y+=SECTOR_SIZE) {
/* Set color for LINES */
glColor4f(0.0f,0.2f,0.0,1.0f);
/* Draw lines */
glVertex3f(x,y,0); /* left */
glVertex3f(x,y+SECTOR_SIZE,0); /* left */
glVertex3f(x,y,0); /* right up*/
glVertex3f(x+SECTOR_SIZE,y,0); /* right down */
glVertex3f(x+SECTOR_SIZE,y+SECTOR_SIZE,0); /* leftbottom */
glVertex3f(x,y+SECTOR_SIZE,0); /* rightbottom */
glVertex3f(x+SECTOR_SIZE,y,0); /* leftupper*/
glVertex3f(x+SECTOR_SIZE,y+SECTOR_SIZE,0);
}
}
glEnd();

}



This is at the users request to toggle options:


GLvoid engine_options(GLvoid)
{

/* Statistics and engine options */
if(STATISTICS)
display_stats();

if(DEPTH_TEST)
glEnable(GL_DEPTH_TEST);
else glDisable(GL_DEPTH_TEST);

if(BLENDING)
glEnable(GL_BLEND);
else glDisable(GL_BLEND);

if(ANTIALIASING) {
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glHint(GL_POINT_SMOOTH,GL_NICEST);
glHint(GL_LINE_SMOOTH,GL_NICEST);
glHint(GL_POLYGON_SMOOTH,GL_NICEST);}
else {
glDisable(GL_POINT_SMOOTH);
glDisable(GL_LINE_SMOOTH);
glDisable(GL_POLYGON_SMOOTH);
}

if(TEXTURE_2D)
glEnable(GL_TEXTURE_2D);
else glDisable(GL_TEXTURE_2D);

if(COORDINATE) {
for(x=-10.0;x<GRID_SIZE;x+=SECTOR_SIZE) {
for(y=-10.0;y<GRID_SIZE;y+=SECTOR_SIZE) {
/* Set color for font because bitmap function is buggy */
glColor4f(1.0,1.0,1.0,1.0);
renderbitmapfont(x,y,0,"x%1.1f y%1.1f",x,y);
}
}
}

}


This is called upon initialization:


GLint dl_display_grid;
GLfloat x,y,z;

GLvoid init(GLvoid)
{

/* Add texture loading routine here */

/* Enable default 2D texturing */
glEnable(GL_TEXTURE_2D);
TEXTURE_2D=!TEXTURE_2D;

glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);

/* Enable default Depth testing */
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
DEPTH_TEST=!DEPTH_TEST;

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

/* Enable default Blending */
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
//glBlendFunc(GL_DST_COLOR,GL_ZERO);
glEnable(GL_BLEND);
BLENDING=!BLENDING;

/* Enable default anti-aliasing */
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glHint(GL_POINT_SMOOTH,GL_NICEST);
glHint(GL_LINE_SMOOTH,GL_NICEST);
glHint(GL_POLYGON_SMOOTH,GL_NICEST);
ANTIALIASING=!ANTIALIASING;

/* Setup display lists */
//dl_display_grid=glGenLists(1);
//glNewList(dl_display_grid,GL_COMPILE);
// display_grid();
//glEnd();

//glEndList();

}


I init GLUT with:
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_ALPHA);


I also have an if statement in my main rendering loop that keeps my HUD on top of the screen even when blending is disabled by drawing it last, but this shouldn't really matter.

Share this post


Link to post
Share on other sites
Quote:

/* Draw QUADS */
glVertex3f(x,y,0); /* left */
glVertex3f(x,y+SECTOR_SIZE,0); /* left */
glVertex3f(x,y,0); /* right up*/
glVertex3f(x+SECTOR_SIZE,y,0); /* right down */

glVertex3f(x+SECTOR_SIZE,y+SECTOR_SIZE,0); /* leftbottom */
glVertex3f(x,y+SECTOR_SIZE,0); /* rightbottom */
glVertex3f(x+SECTOR_SIZE,y,0); /* leftupper*/
glVertex3f(x+SECTOR_SIZE,y+SECTOR_SIZE,0);


Unless I've totally forgotten my OpenGL knowledge, that draws 2 quads, and neither have 4 valid coordinate sets. I'm surprised it looks correct in any circumstances.

Your comments are inconsistent too.

Share this post


Link to post
Share on other sites
uhh hmm. well if they would be all be considered vertices it would be correct. as GL_LINES always needs 2 vertice points, that is correct. For GL_QUADS i think you're right (one vertice defines one line?). In that case i would have to fix it. Argh.

Could this be the reason for the triangle artifact appearing ?

don't mind the comments, i know they have to be corrected.

Share this post


Link to post
Share on other sites
Polygons have as many vertices as they have edges (quads being the special case of polygons with 4 of each). They don't "define" each other because each edge connects two adjacent vertices, and each vertex is at the common point of two adjacent edges. However, you're normally supposed to "visit" each vertex in order, either clockwise or counter-clockwise around the polygon. That should look like:


glVertex3f(x, y, 0);
glVertex3f(x, y + SECTOR_SIZE, 0);
glVertex3f(x + SECTOR_SIZE, y + SECTOR_SIZE, 0);
glVertex3f(x + SECTOR_SIZE, y, 0);


I hope I don't need too much explanation where I got those values from :)

Share this post


Link to post
Share on other sites
Well yes but am i correct in that although 4 glVertex calls are enough to define a quad as opposed to needing 8 glVertex calls for lines, GL_QUAD does also accepts 8 vertex calls (a bit redundant maybe, but it still seems to work.

Share this post


Link to post
Share on other sites
You can specify any number of vertices within a glBegin(GL_QUADS); glEnd(); block. The first four vertices will define the first quad. The next four vertices will define a second quad. And so on (if the number of vertices is not divisible by four the remainder will be ignored). So your call draws two quads, not one, both of which are degenerate. If you want one quad then specify exactly four distinct co-planar vertex coordinates.

Σnigma

Share this post


Link to post
Share on other sites
Ok, thanks i will fix that then. Back to the original question, why do i get a quad that is divided into two triangles each with a different colour when turning blending on?

Share this post


Link to post
Share on other sites
It's probably the way your two degenerate quads are being interpreted. If your gl just splits each quad into two triangles, one of which uses the first three vertices and the other the last three, then your two degenerate quads will generate four triangles, one of which will have zero size. Of the remaining three triangles, two will overlap, blending together to generate a different shade.

Σnigma

Share this post


Link to post
Share on other sites
That makes sense. Thanks enigma. I seem to have fixed it thanks to your suggestions. However, now i have another weird problem which i can't seem to pinpoint: The grid lines i'm overlaying on top of the quad seem to be exact one row off!

For example, if the grid is facing me then the top row is simply blue without any grid and the bottom row has a row extra with a grid but now blue quad...

This is my new code. I'm not sure the quad is correct now, but when i display one it did correctly. I'm not sure it's done clockwise though.


GLvoid display_grid(GLvoid)
{
// |
// |
// _____x_____x_______
// |
// y | y

glBegin(GL_QUADS);
for(x=-10.0;x<GRID_SIZE;x+=SECTOR_SIZE) {
for(y=-10.0;y<GRID_SIZE;y+=SECTOR_SIZE) {
/* Set color for QUADS */
glColor4f(0.0f,0.0f,0.1,0.9f);
/* Draw QUADS */
glVertex3f(x,y,0); /* left side */
glVertex3f(x+SECTOR_SIZE,y,0); /* rightside */
glVertex3f(x+SECTOR_SIZE,y-SECTOR_SIZE,0); /* upperside*/
glVertex3f(x,y-SECTOR_SIZE,0); /*lowerside */
}
}
glEnd();

glBegin(GL_LINES);
for(x=-10.0;x<GRID_SIZE;x+=SECTOR_SIZE) {
for(y=-10.0;y<GRID_SIZE;y+=SECTOR_SIZE) {
/* Set color for LINES */
glColor4f(0.0f,0.2f,0.0,1.0f);
/* Draw lines */
glVertex3f(x,y,0); /* Lower left */
glVertex3f(x,y+SECTOR_SIZE,0); /* To upper left */
glVertex3f(x,y+SECTOR_SIZE,0); /* Upper left */
glVertex3f(x+SECTOR_SIZE,y+SECTOR_SIZE,0); /* To upper right */
glVertex3f(x+SECTOR_SIZE,y+SECTOR_SIZE,0); /* Upper right */
glVertex3f(x+SECTOR_SIZE,y,0); /* To bottom right */
glVertex3f(x+SECTOR_SIZE,y,0); /* Bottom right */
glVertex3f(x,y,0); /* To Lower left */
}
}
glEnd();

}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this