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();
}
}