SFML community forums
Help => Graphics => Topic started by: Andy on April 19, 2012, 05:02:59 am
-
I'm probably going to get shot down for this (given my level of ignorance), but I'll ask away.
I'm trying to derive the drawable class with an object that makes calls to OpenGL directly.
And I'm trying to render a real basic sprite (for starters).
draw function definition
void BaseSprite::draw(sf::RenderTarget & target, sf::RenderStates states) const
{
glEnable(GL_TEXTURE_2D);
// Set the primitive color to white
glColor4f(1.f, 1.f, 1.f, 1.f);
// Bind the texture
glBindTexture(GL_TEXTURE_2D, myTexture->id());
glPushMatrix(); // Save modelview matrix
acl::PointF center = myBounds.center();
glTranslatef(center.x, center.y, 0.f);
glRotatef(0.f, 0.f, 0.f, 0.f);
glTranslatef(myBounds.x - center.x, myBounds.y - center.y, 0.f);
glBegin(GL_QUADS);
glTexCoord2f(0.f , myTextureBounds.h);
glVertex2f (0.f , 0.f );
glTexCoord2f(myTextureBounds.w, myTextureBounds.h);
glVertex2f (myBounds.w , 0.f );
glTexCoord2f(myTextureBounds.w, 0.f );
glVertex2f (myBounds.w , myBounds.h );
glTexCoord2f(0.f , 0.f );
glVertex2f (0.f , myBounds.h );
glEnd();
glPopMatrix();
}
Start up, which just sets up OpenGL
Graphics::instance().enable2d();
Main Loop code:
app.draw(txt); // a test sf::Text object
app.pushGLStates();
app.draw(bs); // which is a BaseSprite object
app.popGLStates();
I'm not sure what I'm missing (do I need setup for rendering something this? I'm not sure what to do). I do see it (the BaseSprite) flashing but then disappearing afterwords.
Is anyone willing to suggest anything?
Please don't angry with me, if this post is undeserving just tell me and I'll move on (eventually, hopefully).
Thanks for tips
-
pushGlStates and popGlStates must surround SFML draw calls, not your own calls that mix SFML and OpenGL.
This means that it is very hard to insert OpenGL stuff into a sf::Drawable. I don't even know if there's an easy way to do it with the new API -- why don't you use the new SFML 2 stuff, like sf::Vertex and sf::VertexArray?