| #include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
int main()
{
sf::ContextSettings contextSettings;
contextSettings.depthBits = 32;
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML graphics with OpenGL" , sf::Style::Default, contextSettings);
window.setVerticalSyncEnabled( true );
window.setActive();
sf::Texture backgroundTexture;
if (!backgroundTexture.loadFromFile( "resources/background.jpg" ))
return EXIT_FAILURE;
sf::Sprite background(backgroundTexture);
sf::Font font;
if (!font.loadFromFile( "resources/sansation.ttf" ))
return EXIT_FAILURE;
sf::Text text( "SFML / OpenGL demo" , font);
text.setColor(sf::Color(255, 255, 255, 170));
text.setPosition(250.f, 450.f);
GLuint texture = 0;
{
sf::Image image;
if (!image.loadFromFile( "resources/texture.jpg" ))
return EXIT_FAILURE;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image.getSize().x, image.getSize().y, GL_RGBA, GL_UNSIGNED_BYTE, image.getPixelsPtr());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClearDepth(1.f);
glDisable(GL_LIGHTING);
glViewport(0, 0, window.getSize().x, window.getSize().y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat ratio = static_cast < float >(window.getSize().x) / window.getSize().y;
glFrustum(-ratio, ratio, -1.f, 1.f, 1.f, 500.f);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
GLfloat cube[] =
{
-20, -20, -20, 0, 0,
-20, 20, -20, 1, 0,
-20, -20, 20, 0, 1,
-20, -20, 20, 0, 1,
-20, 20, -20, 1, 0,
-20, 20, 20, 1, 1,
20, -20, -20, 0, 0,
20, 20, -20, 1, 0,
20, -20, 20, 0, 1,
20, -20, 20, 0, 1,
20, 20, -20, 1, 0,
20, 20, 20, 1, 1,
-20, -20, -20, 0, 0,
20, -20, -20, 1, 0,
-20, -20, 20, 0, 1,
-20, -20, 20, 0, 1,
20, -20, -20, 1, 0,
20, -20, 20, 1, 1,
-20, 20, -20, 0, 0,
20, 20, -20, 1, 0,
-20, 20, 20, 0, 1,
-20, 20, 20, 0, 1,
20, 20, -20, 1, 0,
20, 20, 20, 1, 1,
-20, -20, -20, 0, 0,
20, -20, -20, 1, 0,
-20, 20, -20, 0, 1,
-20, 20, -20, 0, 1,
20, -20, -20, 1, 0,
20, 20, -20, 1, 1,
-20, -20, 20, 0, 0,
20, -20, 20, 1, 0,
-20, 20, 20, 0, 1,
-20, 20, 20, 0, 1,
20, -20, 20, 1, 0,
20, 20, 20, 1, 1
};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, 5 * sizeof (GLfloat), cube);
glTexCoordPointer(2, GL_FLOAT, 5 * sizeof (GLfloat), cube + 3);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
sf::Clock clock ;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
window.close();
if (event.type == sf::Event::Resized)
glViewport(0, 0, event.size.width, event.size.height);
}
window.pushGLStates();
window.draw(background);
window.popGLStates();
glClear(GL_DEPTH_BUFFER_BIT);
float x = sf::Mouse::getPosition(window).x * 200.f / window.getSize().x - 100.f;
float y = -sf::Mouse::getPosition(window).y * 200.f / window.getSize().y + 100.f;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(x, y, -100.f);
glRotatef( clock .getElapsedTime().asSeconds() * 50.f, 1.f, 0.f, 0.f);
glRotatef( clock .getElapsedTime().asSeconds() * 30.f, 0.f, 1.f, 0.f);
glRotatef( clock .getElapsedTime().asSeconds() * 90.f, 0.f, 0.f, 1.f);
glDrawArrays(GL_TRIANGLES, 0, 36);
window.pushGLStates();
window.draw(text);
window.popGLStates();
window.display();
}
glDeleteTextures(1, &texture);
return EXIT_SUCCESS;
}
|