Jump to content
  • Advertisement
Sign in to follow this  
basy

Blending

This topic is 3939 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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();
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!