Blending

Started by
4 comments, last by basy 16 years, 3 months ago
Hi... Can anybody try my code and tell me what i need to set. When hit LEFT or RIGHT, you can turn around the scene. My problem is: - red triangle is 100% transparent and blue is not transparent. - when turn to see blue is in front red, i can't see the red. - when turn to see red is in front blue, i can see blue without change of shade, and rest of red triangle around the blue one. What i need to set to see both triangles same transparent? Please.. #include <cstdlib> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> void MyReshapeFunc(GLsizei w, GLsizei h){ if(h == 0) h = 1; glViewport (0, 0, (GLsizei)w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective(60.0f, ((float)w)/((float)h), 1.0, 100.0); glMatrixMode (GL_MODELVIEW); } GLdouble MyCamX = 0.0; GLdouble MyCamZ = -10.0; void MyDisplayFunc(void){ glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity (); gluLookAt(MyCamX,0,MyCamZ, 0,0,0, 0,1,0); glBegin(GL_TRIANGLES); glEnable(GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glColor4f ( 0.5, 0.0, 0.0, 0.7); glVertex3f(-2.0,-2.0, 1.0); glVertex3f( 2.0,-2.0, 1.0); glVertex3f( 0.0, 2.0, 1.0); glColor4f ( 0.0, 0.0, 0.5, 0.7); glVertex3f(-2.0,-2.0,-1.0); glVertex3f( 2.0,-2.0,-1.0); glVertex3f( 0.0, 2.0,-1.0); glDisable(GL_BLEND); glEnd(); glFlush(); } void MyKeyboardFunc(int key, int x, int y){ switch (key){ case GLUT_KEY_F1: exit(0); case GLUT_KEY_LEFT: MyCamX -= MyCamZ/10; MyCamZ -= -MyCamX/10; MyDisplayFunc(); break; case GLUT_KEY_RIGHT: MyCamX += MyCamZ/10; MyCamZ += -MyCamX/10; MyDisplayFunc(); break; } } using namespace std; int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA); glutInitWindowSize (600, 400); glutInitWindowPosition (100, 100); glutCreateWindow ("Transparency Howto"); glutReshapeFunc(MyReshapeFunc); glutSpecialFunc(MyKeyboardFunc); glutDisplayFunc(MyDisplayFunc); glShadeModel (GL_FLAT); glClearColor(0.0, 0.0, 0.0, 0.0); glutMainLoop(); return 0; }
Advertisement
Without actually looking at the code I'm guessing it has to do with depth-testing. Either disable depth testing or render transparent objects by distance (farthest to closest).
In MyDisplayFunc:
You are enabling the blending and setting the blend mode inside the glbegin/glend block. I'm pretty sure you can't change these variables during a draw call.
Shift the glenable and glblendfunc to be before the glbegin. Also, try disabling depth testing (put gldisable(gl_depth_test) in before glbegin).

That's just what immediately stood out.
Thank you,
i do this:

void MyDisplayFunc(void){
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity ();
gluLookAt(MyCamX,0,MyCamZ, 0,0,0, 0,1,0);

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

glBegin(GL_TRIANGLES);

glColor4f ( 0.5, 0.0, 0.0, 0.7);
glVertex3f(-2.0,-2.0, 1.0);
glVertex3f( 2.0,-2.0, 1.0);
glVertex3f( 0.0, 2.0, 1.0);

glColor4f ( 0.0, 0.0, 0.5, 0.7);
glVertex3f(-2.0,-2.0,-1.0);
glVertex3f( 2.0,-2.0,-1.0);
glVertex3f( 0.0, 2.0,-1.0);

glEnd();

glDisable(GL_BLEND);

glFlush();
}
Hi.

I still have problem with correct funcionality of transparency.

Can somebody try this MyDisplayFunc function and tell me
what i need to do to see same color of back triangle through front one?
Left side is diffrent then right one:(
It depends on camera position and order of drawing triangle.
How to set up to sum colors of back and front triangle to see pink RGB(1,0,1) on the overlay place of back and front triangle?
What i need to setup blending, culling, alpha value?

void MyDisplayFunc(void){
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity ();
gluLookAt(MyCamX,0,MyCamZ, 0,0,0, 0,1,0);

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

glBegin(GL_TRIANGLES);

glColor4f ( 0.0, 0.0, 0.5, 0.5);
glVertex3f(-4.0,-2.0, 1.0);
glVertex3f( 0.0,-2.0, 1.0);
glVertex3f(-2.0, 2.0, 1.0);

glColor4f ( 0.5, 0.0, 0.0, 0.5);
glVertex3f(-4.0,-2.0,-1.0);
glVertex3f( 0.0,-2.0,-1.0);
glVertex3f(-2.0, 2.0,-1.0);

glColor4f ( 0.5, 0.0, 0.0, 0.5);
glVertex3f( 0.0,-2.0,-1.0);
glVertex3f( 4.0,-2.0,-1.0);
glVertex3f( 2.0, 2.0,-1.0);

glColor4f ( 0.0, 0.0, 0.5, 0.5);
glVertex3f( 0.0,-2.0, 1.0);
glVertex3f( 4.0,-2.0, 1.0);
glVertex3f( 2.0, 2.0, 1.0);

glEnd();

glDisable(GL_BLEND);

glFlush();
}



Thank you.
:)

I found my answer:

glBlendFunc (GL_ONE, GL_ONE);

This topic is closed to new replies.

Advertisement