Jump to content

  • Log In with Google      Sign In   
  • Create Account


Neurodoxx

Member Since 08 Mar 2012
Offline Last Active Apr 06 2012 01:15 PM
-----

Topics I've Started

Can't draw polygons when GL_BLEND is enabled

27 March 2012 - 11:58 PM

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 :
Attached File  without_blending.jpg   20.24KB   26 downloads
With blending the red text strings appear correctly, but no polygon :
Attached File  with_blending.jpg   11.1KB   26 downloads

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

Drawable entities and fast isometric depth sorting

08 March 2012 - 07:16 AM

Hello everyone,

Being new here, I'd like to introduce myself. Neurodoxx, french student in robotics, currently developing a C++/OpenGL isometric game engine.

My request is two-fold.

The engine I'm programming relies on a component based entity system to make sure a given entity only contains what it needs to carry out its duty. For instance, trigger entities won't need to display any sprite so they don't include the render component. It follows that some entities are not renderable. My engine is currently running a back to front painter's algorithm to display tiles on a map (hastily implemented as a multi-dimensional array to run tests quickly) and is unable to integrate displayable entities in the world. I came up with the idea (and I don't doubt that's a common way to do it) that my engine should deal with tiles and renderable entities the same way, which in OOP is easily done by deriving tiles and entities from the same -let's call it Drawable- interface. However, with my component-oriented design for the entity system I'm basically hitting a snag.
It appears to me that tiles should be implemented as entities at the cost of a relatively small overhead, yet I can't find an elegant way to iterate only over drawable entities in my render function. Should I split drawable and non-drawable entities into two different lists ?
I've been travelling all over the web to find a smart solution to address this issue but came up with no clue which might indicate I'm not following a good path, I fear, and as a result I can't sleep.

This problem apart, I know that some day I will need to perform depth-sorting over my list of displayable objects so as to draw them in the right order. This, in theory, I can deal with as I've seen many tutorials on how to do it efficiently. But can I, and if yes, how should I sort it in order to render exclusively objects located under the viewport ?

Thanks in advance for your feedback, and if you need code to make this less abstract, I can provide !
Have a good day !

PARTNERS