Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to draw background(sf::Image) and cube(OpenGL)?  (Read 4071 times)

0 Members and 1 Guest are viewing this topic.

langisser

  • Newbie
  • *
  • Posts: 9
    • View Profile
How to draw background(sf::Image) and cube(OpenGL)?
« on: January 12, 2010, 08:54:43 am »
I'm try to draw background(sf::Image) and cube(OpenGL) in same window.
And I have code from "Tutorial - Window - Using OpenGL". It's running ok.
But I can't draw background because sf::Window don't have Draw() function.
When I change
Code: [Select]
sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL"); to
Code: [Select]
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML OpenGL");
I can draw background but can't draw cube.

Code: [Select]

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
    // Create the main window
    sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL");

    // Create a clock for measuring time elapsed
    sf::Clock Clock;

    // Set color and depth clear value
    glClearDepth(1.f);
    glClearColor(0.f, 0.f, 0.f, 0.f);

    // Enable Z-buffer read and write
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);

    // Setup a perspective projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.f, 1.f, 1.f, 500.f);

    sf::Image BackgroundImage;
    if (!BackgroundImage.LoadFromFile("2uppaib.png"))
        return EXIT_FAILURE;
    sf::Sprite Background(BackgroundImage);
Background.Resize(850,625);
Background.SetPosition(Background.GetPosition().x-25,Background.GetPosition().y-12);

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();

            // Escape key : exit
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App.Close();

            // Resize event : adjust viewport
            if (Event.Type == sf::Event::Resized)
                glViewport(0, 0, Event.Size.Width, Event.Size.Height);
        }

        // Set the active window before using OpenGL commands
        // It's useless here because active window is always the same,
        // but don't forget it if you use multiple windows or controls
        App.SetActive();


        // Clear color and depth buffer
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Apply some transformations
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(0.f, 0.f, -200.f);
        glRotatef(Clock.GetElapsedTime() * 50, 1.f, 0.f, 0.f);
        glRotatef(Clock.GetElapsedTime() * 30, 0.f, 1.f, 0.f);
        glRotatef(Clock.GetElapsedTime() * 90, 0.f, 0.f, 1.f);

        // Draw a cube
        glBegin(GL_QUADS);

            glVertex3f(-50.f, -50.f, -50.f);
            glVertex3f(-50.f,  50.f, -50.f);
            glVertex3f( 50.f,  50.f, -50.f);
            glVertex3f( 50.f, -50.f, -50.f);

            glVertex3f(-50.f, -50.f, 50.f);
            glVertex3f(-50.f,  50.f, 50.f);
            glVertex3f( 50.f,  50.f, 50.f);
            glVertex3f( 50.f, -50.f, 50.f);

            glVertex3f(-50.f, -50.f, -50.f);
            glVertex3f(-50.f,  50.f, -50.f);
            glVertex3f(-50.f,  50.f,  50.f);
            glVertex3f(-50.f, -50.f,  50.f);

            glVertex3f(50.f, -50.f, -50.f);
            glVertex3f(50.f,  50.f, -50.f);
            glVertex3f(50.f,  50.f,  50.f);
            glVertex3f(50.f, -50.f,  50.f);

            glVertex3f(-50.f, -50.f,  50.f);
            glVertex3f(-50.f, -50.f, -50.f);
            glVertex3f( 50.f, -50.f, -50.f);
            glVertex3f( 50.f, -50.f,  50.f);

            glVertex3f(-50.f, 50.f,  50.f);
            glVertex3f(-50.f, 50.f, -50.f);
            glVertex3f( 50.f, 50.f, -50.f);
            glVertex3f( 50.f, 50.f,  50.f);

        glEnd();
        // Finally, display rendered frame on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}


How to draw background(sf::Image) and cube(OpenGL)?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
How to draw background(sf::Image) and cube(OpenGL)?
« Reply #1 on: January 12, 2010, 08:59:49 am »
Look at the OpenGL sample from the SDK, it shows how to do that.
Laurent Gomila - SFML developer

langisser

  • Newbie
  • *
  • Posts: 9
    • View Profile
How to draw background(sf::Image) and cube(OpenGL)?
« Reply #2 on: January 12, 2010, 10:22:37 am »
I have already fixed.
Because I forgot this function
Code: [Select]
App.PreserveOpenGLStates(true);

Geekette

  • Newbie
  • *
  • Posts: 1
    • View Profile
How to draw background(sf::Image) and cube(OpenGL)?
« Reply #3 on: June 14, 2011, 01:23:53 pm »
Hello langisser,
I know it's a lil bit late my post but plz can you send me the code or simply the link where did you find the code I urgently need it to carry on with my project thank you..
PS: I googled the tutorial you mentioned but couldn't find it
I was .. I am