Jump to content
  • Advertisement
Sign in to follow this  
Neurodoxx

OpenGL Can't draw polygons when GL_BLEND is enabled

This topic is 2333 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 everyone,

This might be a dumb question, but I just can't figure it out or find anything about it. I'm writing a simple C++/SDL/OpenGL application which can draw polygons on the screen and perform collision tests.

Some polygons wander around and others are static. Wandering polygons have a name that need to be displayed under them, for that I'm using a png image font and display lists to draw the characters quickly (I know this function works well as I'm using it everywhere in my projects). I must use blending in order to make the text drawing method work, but it turns out that my polygons cannot be displayed anymore under these circumstances. I'm using orthographic projection, and I disabled the depth test. I've just spent hours on it with no success and I'd be glad to get some help !

Here are the parts of my code I find more relevant for this issue :

OpenGL initialization :

#define SCR_WIDTH 800
#define SCR_HEIGHT 600

//in main() :
//...
//Zero matrix projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Set orthographic projection and viewport
gluOrtho2D(0,SCR_WIDTH,0,SCR_HEIGHT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,SCR_WIDTH,SCR_HEIGHT);
//OpenGL properties
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

//in the game loop, after the event handling :

glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
mymap.update(1.0/FPS);
mymap.draw();
SDL_GL_SwapBuffers();

mymap is a Map object containing all polygons.

The Polygon::draw() method :
void Polygon::draw()
{
glPushMatrix();
glTranslatef(position.get_x(),position.get_y(),0.0f);
//Draw filled polygon
glColor4f(color.r,color.g,color.b,color.a);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glBegin(GL_POLYGON);
for (unsigned int ii=0; ii<points.size(); ++ii)
{
glVertex2f(points[ii].x,points[ii].y);
}
glEnd();
glPopMatrix();
}

color being a simple struct containing RGBA information.

Here are some screenshots :
Without blending :
[attachment=7927:without_blending.jpg]
With blending the red text strings appear correctly, but no polygon :
[attachment=7926:with_blending.jpg]

Thanks a lot in advance for your precious help ! I can provide some more code if needed.

Share this post


Link to post
Share on other sites
Advertisement

What value has color.a?


1.0f set by default in the struct constructor, and no instance currently change this value.

Share this post


Link to post
Share on other sites
Looks fine to me, however you should try to play a bit with that alpha value and see how it goes. Put it 0.5, then 0.0 and so on.

Share this post


Link to post
Share on other sites

Looks fine to me, however you should try to play a bit with that alpha value and see how it goes. Put it 0.5, then 0.0 and so on.


That is actually what I did all alone in my despair, but it gave me no clue at all. Maybe this is hardware dependent after all, I'm currently working with a laptop borrowed from my gf which uses a graphic chipset instead of a true video card, it may simply not handle properly this type of blending operations. Tough luck !

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!