Back to index

libsfml  1.6+dfsg2
OpenGL.cpp
Go to the documentation of this file.
00001 
00003 // Headers
00005 #include <SFML/Graphics.hpp>
00006 #include <iostream>
00007 
00008 
00015 int main()
00016 {
00017     // Create main window
00018     sf::RenderWindow App(sf::VideoMode(800, 600), "SFML OpenGL");
00019     App.PreserveOpenGLStates(true);
00020 
00021     // Create a sprite for the background
00022     sf::Image BackgroundImage;
00023     if (!BackgroundImage.LoadFromFile("datas/opengl/background.jpg"))
00024         return EXIT_FAILURE;
00025     sf::Sprite Background(BackgroundImage);
00026 
00027     // Load an OpenGL texture.
00028     // We could directly use a sf::Image as an OpenGL texture (with its Bind() member function),
00029     // but here we want more control on it (generate mipmaps, ...) so we create a new one
00030     GLuint Texture = 0;
00031     {
00032         sf::Image Image;
00033         if (!Image.LoadFromFile("datas/opengl/texture.jpg"))
00034             return EXIT_FAILURE;
00035         glGenTextures(1, &Texture);
00036         glBindTexture(GL_TEXTURE_2D, Texture);
00037         gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Image.GetWidth(), Image.GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, Image.GetPixelsPtr());
00038         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00039         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
00040     }
00041 
00042     // Enable Z-buffer read and write
00043     glEnable(GL_DEPTH_TEST);
00044     glDepthMask(GL_TRUE);
00045     glClearDepth(1.f);
00046 
00047     // Setup a perspective projection
00048     glMatrixMode(GL_PROJECTION);
00049     glLoadIdentity();
00050     gluPerspective(90.f, 1.f, 1.f, 500.f);
00051 
00052     // Bind our texture
00053     glEnable(GL_TEXTURE_2D);
00054     glBindTexture(GL_TEXTURE_2D, Texture);
00055     glColor4f(1.f, 1.f, 1.f, 1.f);
00056 
00057     // Create a clock for measuring the time elapsed
00058     sf::Clock Clock;
00059 
00060     // Start game loop
00061     while (App.IsOpened())
00062     {
00063         // Process events
00064         sf::Event Event;
00065         while (App.GetEvent(Event))
00066         {
00067             // Close window : exit
00068             if (Event.Type == sf::Event::Closed)
00069                 App.Close();
00070 
00071             // Escape key : exit
00072             if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
00073                 App.Close();
00074 
00075             // Adjust the viewport when the window is resized
00076             if (Event.Type == sf::Event::Resized)
00077                 glViewport(0, 0, Event.Size.Width, Event.Size.Height);
00078          }
00079 
00080         // Draw background
00081         App.Draw(Background);
00082 
00083         // Clear depth buffer
00084         glClear(GL_DEPTH_BUFFER_BIT);
00085 
00086         // Apply some transformations
00087         glMatrixMode(GL_MODELVIEW);
00088         glLoadIdentity();
00089         glTranslatef(0.f, 0.f, -200.f);
00090         glRotatef(Clock.GetElapsedTime() * 50, 1.f, 0.f, 0.f);
00091         glRotatef(Clock.GetElapsedTime() * 30, 0.f, 1.f, 0.f);
00092         glRotatef(Clock.GetElapsedTime() * 90, 0.f, 0.f, 1.f);
00093 
00094         // Draw a cube
00095         glBegin(GL_QUADS);
00096 
00097             glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f);
00098             glTexCoord2f(0, 1); glVertex3f(-50.f,  50.f, -50.f);
00099             glTexCoord2f(1, 1); glVertex3f( 50.f,  50.f, -50.f);
00100             glTexCoord2f(1, 0); glVertex3f( 50.f, -50.f, -50.f);
00101 
00102             glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, 50.f);
00103             glTexCoord2f(0, 1); glVertex3f(-50.f,  50.f, 50.f);
00104             glTexCoord2f(1, 1); glVertex3f( 50.f,  50.f, 50.f);
00105             glTexCoord2f(1, 0); glVertex3f( 50.f, -50.f, 50.f);
00106 
00107             glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f);
00108             glTexCoord2f(0, 1); glVertex3f(-50.f,  50.f, -50.f);
00109             glTexCoord2f(1, 1); glVertex3f(-50.f,  50.f,  50.f);
00110             glTexCoord2f(1, 0); glVertex3f(-50.f, -50.f,  50.f);
00111 
00112             glTexCoord2f(0, 0); glVertex3f(50.f, -50.f, -50.f);
00113             glTexCoord2f(0, 1); glVertex3f(50.f,  50.f, -50.f);
00114             glTexCoord2f(1, 1); glVertex3f(50.f,  50.f,  50.f);
00115             glTexCoord2f(1, 0); glVertex3f(50.f, -50.f,  50.f);
00116 
00117             glTexCoord2f(0, 1); glVertex3f(-50.f, -50.f,  50.f);
00118             glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f);
00119             glTexCoord2f(1, 0); glVertex3f( 50.f, -50.f, -50.f);
00120             glTexCoord2f(1, 1); glVertex3f( 50.f, -50.f,  50.f);
00121 
00122             glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f,  50.f);
00123             glTexCoord2f(0, 0); glVertex3f(-50.f, 50.f, -50.f);
00124             glTexCoord2f(1, 0); glVertex3f( 50.f, 50.f, -50.f);
00125             glTexCoord2f(1, 1); glVertex3f( 50.f, 50.f,  50.f);
00126 
00127         glEnd();
00128 
00129         // Draw some text on top of our OpenGL object
00130         sf::String Text("This is a rotating cube");
00131         Text.SetPosition(250.f, 300.f);
00132         Text.SetColor(sf::Color(128, 0, 128));
00133         App.Draw(Text);
00134 
00135         // Finally, display the rendered frame on screen
00136         App.Display();
00137     }
00138 
00139     // Don't forget to destroy our texture
00140     glDeleteTextures(1, &Texture);
00141 
00142     return EXIT_SUCCESS;
00143 }