PDA

View Full Version : GL States vs. SFML 2.0



immapoint
09-23-2012, 07:17 AM
Hi there,

I'm rather new to OpenGL, just saying..
Anyways, I have this piece of code:


sf::Text fps("60", arial); // arial = Arial Font
fps.setColor(sf::Color::Green);
fps.setPosition(10, 10);

// FPS Clock
sf::Clock clock;
float dt = 1.f;
float next = 1.f;

std::stringstream ss;

bool running = true;
while(running){

// some calculations for the FPS-Text
dt = clock.restart().asSeconds();
next += dt;

// do some event handling (resize + close)
sf::Event event;
while ( window.pollEvent(event) ){

switch(event.type){

case sf::Event::Closed:
running = false;
break;

case sf::Event::Resized:
glViewport(0, 0, event.size.width, event.size.height);
gluPerspective(45, (event.size.width / event.size.height), 1.f, 500.f);
break;

};

}

glClear(GL_COLOR_BUFFER_BIT);

// draw a triangle
glBegin(GL_TRIANGLES);

glColor3ub(255, 42, 212);
glVertex3f(.0f, 2.f, -10.f);

glColor3ub(255, 0, 127);
glVertex3f(-10.f, -2.f, -10.f);

glColor3ub(255, 0, 42);
glVertex3f(2.f, -2.f, -10.f);

glEnd();

// some more calculations for the FPS-Text
if ( next > .7f ){
ss.str("");
ss << (int) (1.f / dt);
fps.setString(ss.str());
next -= 1.f;
}

window.resetGLStates();
window.draw(fps);

// "reset" GL states as I had them
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, (800 / 600), 1.f, 500.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

window.display();
}

It's supposed to draw a triangle through OpenGL and a text using SFML. Well, it does but this way it is pretty slow.

First off, I initialized OpenGL like this:


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, (800 / 600), 1.f, 500.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


To draw things with SFML, it's necessary to reset the GL states. sf::RenderWindow::pushGLStates() and sf::RenderWindow::popGLStates() does well here, but as mentioned in this tutorial (http://www.sfml-dev.org/tutorials/2.0/window-opengl.php), it's slow. Alternatively, it is possible to push the needed states by oneself, reset them, draw then and pop the states in the end.
The only problem for me: I'm not sure which states are to be saved.
I don't have any other states set except those I set when initializing, so I figured out it could look something like that:


glPushAttrib(GL_TRANSFORM_BIT | GL_VIEWPORT_BIT);
glPushMatrix();
window.resetGLStates();
window.draw(fps);
glPopMatrix();
glPopAttrib();

This didn't work, so I desperately put in all bitfields I could find here (http://www.opengl.org/sdk/docs/man/xhtml/glPushAttrib.xml):


glPushAttrib(GL_ACCUM_BUFFER_BIT | GL_COLOR_BUFFER_BIT | [..]);
// rest like above

And this didn't work as well.
So I'm asking myself how this could work :S

Also, another question regarding gluPerspective:
The parameters are sth like this:


gluPerspective([angle], [aspect ratio], [znear], [zfar]);

Ok, the first and the two last parameters are clear to me, but what does the aspect ratio mean? Of course, it somehow affects the way you see things in 3D space. But how?
I'm not sure how the positioning works also. Say you have the following call:


gluPerspective(45, (800 / 600), 1.f, 100.f);

It's possible to see the section between -1 and -100 on the Z axis. But what about the X and Y axis? How do i know whether a point, say (10, 100, -3) is visible or not? For a little contrast, take some 2D space. You are able to set a width and a height. Having a certain point, you instantly know if that point is visible. Say your width and height are both 500. The point (50, 400) is visible, whereas the point (4005, 20) is not because it's X-value is way above 500.
Is there a way to "calculate" wheter you can see or can't see a point?

Thanks in advance.