Jump to content
  • Advertisement
Sign in to follow this  
As-12

OpenGL Blending problem.

This topic is 4422 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

I have a problem with blending in opengl.. This is my code. THe problem is the object blend nothing and result with 1 rectangle and another rectangle behind but it cant see both of it. I do enable blend and put blendfunc(); and stuff. But Still not working. Anyideas to fix it? My code void glRect(GLfloat width,GLfloat height) { glBegin(GL_QUADS); glVertex3f((width) / 2,height / 2,0); glVertex3f(-(width) / 2,(height / 2),0); glVertex3f(-(width) / 2,-(height / 2),0); glVertex3f((width) / 2,-(height / 2),0); glEnd(); } void Display() { glClearColor (0.0f, 0.0f, 0.0f, 0.0f); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearDepth(1.0); glEnable(GL_DEPTH_TEST); glShadeModel(GL_FLAT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(52.0f,256/256,1,1000); glViewport(0,0,256,256); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0,0,-5); glPushMatrix (); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glColor4f(0,0,1,0.5); glTranslatef(0,0,0.3); glRect(2,2); glTranslatef(0,0,-0.3); glColor4f(1,0,0,1); glRect(1,3); glPopMatrix (); }

Share this post


Link to post
Share on other sites
Advertisement
When you post that much code, try to use tags

.

But as to your problem, I think you want to disable depth testing before you use blending.
Levi

Share this post


Link to post
Share on other sites
Quote:
Original post by levjs
...
I think you want to disable depth testing before you use blending.
...
In this case that should work. In general you would want to render all opaque objects, disable depth writing only and leave depth testing enabled (so that transparent objects are still occluded by opaque objects that are in front of them), and then render transparent objects from back to front with blending enabled. Use glDepthMask to enable/disable depth writing.

There are a couple other things I notice that are slightly odd to me.
Quote:
glColor4f(0,0,1,0.5);
glTranslatef(0,0,0.3);
glRect(2,2);
glTranslatef(0,0,-0.3);

glColor4f(1,0,0,1);
glRect(1,3);
That looks like you are rendering one quad and then another quad behind it. This will not blend correctly; you need to render objects further away first. You will still get blending, but it will be the further object blending with the closer object instead of the other way around. For some blend modes this will have the same result, but not for regular alpha blending (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA).

And there is already a glRect* function. You shouldn't be making your own. You shouldn't use the gl prefix on your own functions either because it makes it confusing for other people when they read your code, or for yourself if you're reading some of your old code.

Hope that helped.

Share this post


Link to post
Share on other sites
Thx alot for the advice. Well from the book it didn't say about the order. So I thought that I have to draw transparent object 1st then draw the solid object after. But I was wrong. And Sry I shouldn't use gl prefix for my function. So I will improved . Thanks a lot ..

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!