Flat shading in GLSL 140 and color picking

Published May 17, 2010
Advertisement
So I worked back in color picking, finally! What I do, is to send a unique color to each vertex in the terrain through attribs (I only pick over the terrain, but could extend to objects too), I then pass the color to the fragment shader with flat interpolation specified (or no interpolation), whic is then stored in a second color attachment. This creates a grid of unique colors over the terrain. I identify each such cell in the grid with a node on the terrain that holds a position and some other data, which is a node in the scenegraph also. So I can easily place towers, select towers for upgrade, etc.

Some code:
	glBindFramebuffer(GL_FRAMEBUFFER, sceneFBO);	{		glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		glEnable(GL_DEPTH_TEST);		glDepthMask(GL_TRUE);		glCullFace(GL_BACK);		if(coreManager->getStateManager()->getCurrentState()->getPerspectiveCamera())			coreManager->setActiveCamera(coreManager->getStateManager()->getCurrentState()->getPerspectiveCamera());		displayScene(true, false, lightView, lightDir);		doPicking();	} glBindFramebuffer(GL_FRAMEBUFFER, 0);


void GenericManager::doPicking(){	int x = 0;	int y = 0;	if(this->coreManager->getStateManager()->getCurrentState())		this->coreManager->getStateManager()->getCurrentState()->getMousePos(x,y);	GLint viewport[4];	glGetIntegerv(GL_VIEWPORT, viewport);	y = viewport[3] - y; //port to opengl coords	unsigned char pixel[3];	glReadBuffer(GL_COLOR_ATTACHMENT1);	glReadPixels((GLint)x, (GLint)y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);}


Next Entry Picking
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement