SFML pushGLStates/popGLStates causing opengl texture to disappear

Started by
1 comment, last by sun1 9 years, 3 months ago

Hi,

I'm trying to draw some opengl textures after some SFML draw calls. from what I understand I need to call pushGLStates before and draw calls and then popGLStates after any draw calls. Then I should be safe to call my opengl code. However, in doing so, opengl stops drawing textures on the screen. I'm pretty confused about what might be causing the problems. I've attached an image showing the problem (the texutre is displayed on the left when I comment out the calls to pushGLStates/popGLStates). Any ideas on what be causing the problem? Even if I comment out all sfml drawing code, I still see the same problem.

Here's my games init code which basically runs a bunch of opengl code.


void Game::init()
{
	glDisable(GL_LIGHTING);

	// Configure the viewport (the same size as the window)
	glViewport(0, 0, m_mainRenderWindow->getSize().x,
			m_mainRenderWindow->getSize().y);

	glMatrixMode(GL_PROJECTION);
	glOrtho(0, 1280, 800, 0, -1, 1);
	glMatrixMode(GL_MODELVIEW);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);

	glEnable(GL_TEXTURE_2D);
	glShadeModel(GL_SMOOTH);
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
	glClearDepth(1.0f);                  // Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);             // Enables Depth Testing
	glDepthFunc(GL_LEQUAL);

}

Here's my render function:


void Game::render() {
	m_mainRenderWindow->setActive(true);
	//TODO sfml stuff here
	m_mainRenderWindow->clear(sf::Color(50, 50, 50));
	m_mainRenderWindow->pushGLStates();
	m_mainRenderWindow->draw(m_cameraSystem);
	m_mainRenderWindow->draw(m_mapLoader);
	m_mainRenderWindow->draw(m_mouseSplitterSystem);
	m_fixedTimeStepSystem.drawDebug();
	m_mainRenderWindow->popGLStates();

	//TODO draw opengl stuff here
	glClear(GL_DEPTH_BUFFER_BIT);
	m_openglTextureRenderer.render();

	m_mainRenderWindow->display();

}

m_openglTextureRenderer is an instance of OpenGLTextureRenderer, which draws a bunch of textures to the screen, here's the render code for that:


void OpenGLTextureRenderer::OpenGLTextureRendererImpl::render(std::vector<anax::Entity>& entities) {
	const float M2P = 30.0f;
	for (auto entity : entities) {
		if(!entity.isActivated() || !entity.isValid()) continue;

		auto& texCoordsComp = entity.getComponent<Texcoords>();
		auto& physicsComp = entity.getComponent<PhysicsComponent>();
		auto& texCoordsVec = texCoordsComp.textCoords;

		if(texCoordsVec.size() <= 0)
			continue;

		b2Body* body = physicsComp.physicsBody;

		glBindTexture(GL_TEXTURE_2D, texCoordsComp.texture);
		b2PolygonShape* shape =
				((b2PolygonShape*) body->GetFixtureList()->GetShape());
		std::vector<b2Vec2> points(shape->GetVertexCount());
		for (int i = 0; i < shape->GetVertexCount(); i++) {
			points[i] = (shape)->GetVertex(i);
		}

		glPushMatrix();

	       b2Vec2 center = (body->GetPosition());

		float angle = body->GetAngle();
		glTranslatef(static_cast<float>(floor(center.x * M2P)), static_cast<float>(floor(center.y * M2P)), 0.0f);
		glRotatef(angle * 180.0 / M_PI, 0, 0, 1);

		glBegin(GL_POLYGON); //begin drawing of polygon
		for (int i = 0; i < shape->GetVertexCount(); i++) {
			glTexCoord2d(texCoordsVec[i].x, texCoordsVec[i].y);
			glVertex2f(floor(points[i].x * M2P), floor(points[i].y * M2P));
		}
		glEnd(); //end drawing of polygon
		glPopMatrix();
	}
}
Advertisement

hi,

I'm rather new to SFML, but as I'm not sure of what the RenderWindow::clear() function does, I would rather use glClear(COLOR_BUFFER_BIT) in your Game::render() function.

Or maybe put the RenderWindow::pushGLStates() before the RenderWindow::clear() ?

Hope it helps. wink.png

hi,

I'm rather new to SFML, but as I'm not sure of what the RenderWindow::clear() function does, I would rather use glClear(COLOR_BUFFER_BIT) in your Game::render() function.

Or maybe put the RenderWindow::pushGLStates() before the RenderWindow::clear() ?

Hope it helps. wink.png

I got rid of the RenderWindow::clear(), but that didn't do anything. Infact, I got rid of the pushGLStates/popGLStates and I'm trying to handle all the states my self. Though I'm not really confident with that. Here's what I have:


void Game::render() {

	//TODO draw opengl stuff here
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	m_openglTextureRenderer.render();

	//TODO sfml stuff here
	glPushAttrib(GL_CURRENT_BIT | GL_TRANSFORM_BIT | GL_VIEWPORT_BIT);
	TextureLoader::checkError("glPushAttrib");
   	glPushMatrix();

	m_mainRenderWindow->resetGLStates();
	m_mainRenderWindow->draw(m_cameraSystem);
	m_mainRenderWindow->draw(m_mapLoader);
//	m_mainRenderWindow->draw(m_animationSystem);
	m_mainRenderWindow->draw(m_mouseSplitterSystem);
	m_fixedTimeStepSystem.drawDebug();

	glPopAttrib();
	glPopMatrix();

	m_mainRenderWindow->display();

}

Infact all my code can be found here in this branch: https://github.com/SundeepK/Clone/blob/wip/refactor_level_loading_code/src/Game.cpp . It's not really in a shareable state atm, but all the code is pretty much there. I don't see any opengl errors in my console, but I'll have to do some reading to understand how to save states, any help appreciated. With the code above, the texture is showing correctly, but I'm not really convinced since I'm not too familiar with opengl.

This topic is closed to new replies.

Advertisement