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

Author Topic: OpenGL and SFML 1.2  (Read 5113 times)

0 Members and 1 Guest are viewing this topic.

acrin1

  • Newbie
  • *
  • Posts: 45
    • View Profile
    • http://www.crpgdev.com
OpenGL and SFML 1.2
« on: January 18, 2008, 03:11:17 pm »
Hi,

I'm wanting to setup an OpenGL viewport within my game of say a quarter of the screen and use SFMLs graphics features to be able to draw on top of this area and within the rest of the game window

In SFML 1.1 this worked just the way I've described but I've noticed with 1.2 that any images or text I display are scaled down and drawn within the OpenGL viewport.

Is this by design?

Oh and the improvements to fonts look great!

Thanks

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
OpenGL and SFML 1.2
« Reply #1 on: January 18, 2008, 03:42:58 pm »
So, you mean that SFML is using the viewport that you setup instead of its own one ?

That's weird, version 1.2 has been improved a lot regarding internal state management. I'll check this.
Laurent Gomila - SFML developer

acrin1

  • Newbie
  • *
  • Posts: 45
    • View Profile
    • http://www.crpgdev.com
OpenGL and SFML 1.2
« Reply #2 on: January 18, 2008, 05:13:06 pm »
Yes. So if I set the pixel size of text to say 20 then this will be shrunk down to about 5 pixels on the game screen within the viewport (which is only 1/4 of the screen) instead of being drawn within the full game window at 20 pixels.

It's possible that my OpenGL code isn't correct as there are a lot of blanks in my knowledge but I suspect this isn't the case.

I can post a screenshot later if that would help or send you my example project / code.

Thanks.

acrin1

  • Newbie
  • *
  • Posts: 45
    • View Profile
    • http://www.crpgdev.com
OpenGL and SFML 1.2
« Reply #3 on: January 18, 2008, 11:32:35 pm »
There's an example here:

http://bp0.blogger.com/_nChu-_iqbBA/R5Em6ETV9kI/AAAAAAAAAFI/shHXXCfwsQY/s1600-h/Image1.png

Here is a slightly amended version of the OPenGL sample that produces the shot above:

Code: [Select]
#include <SFML/Graphics.hpp>
#include <fstream>
#include <iostream>

int main()
{
    // Create main window
    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML OpenGL");

    // Create a sprite for the background
    sf::Image BackgroundImage;
    if (!BackgroundImage.LoadFromFile("data/sky2.png"))
        return EXIT_FAILURE;
    sf::Sprite Background(BackgroundImage);

    // Load an OpenGL texture.
    // We could directly use a sf::Image as an OpenGL texture (with its Bind() member function),
    // but here we want more control on it (generate mipmaps, ...) so we create a new one
    GLuint Texture = 0;
    {
        sf::Image Image;
        if (!Image.LoadFromFile("data/crate.png"))
            return EXIT_FAILURE;
        glGenTextures(1, &Texture);
        glBindTexture(GL_TEXTURE_2D, Texture);
        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Image.GetWidth(), Image.GetHeight(), 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);
    }

    // Enable Z-buffer read and write
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);
    glClearDepth(1.f);

// Set The Viewport To The Top Left 1/4
glViewport (16, 284, 400, 300); // was 300
glMatrixMode (GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity (); // Reset The Projection Matrix
gluPerspective(60.f, 1.f, 0.1f, 500.f); // was 45 degrees

    // Bind our texture
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, Texture);
    glColor4f(1.f, 1.f, 1.f, 1.f);

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

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

            // Escape key : exit
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                Running = false;
        }

        // Draw background
        App.Draw(Background);

// App.BeginOpenGL();

        // Clear depth buffer
        glClear(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);

            glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f);
            glTexCoord2f(0, 1); glVertex3f(-50.f,  50.f, -50.f);
            glTexCoord2f(1, 1); glVertex3f( 50.f,  50.f, -50.f);
            glTexCoord2f(1, 0); glVertex3f( 50.f, -50.f, -50.f);

            glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, 50.f);
            glTexCoord2f(0, 1); glVertex3f(-50.f,  50.f, 50.f);
            glTexCoord2f(1, 1); glVertex3f( 50.f,  50.f, 50.f);
            glTexCoord2f(1, 0); glVertex3f( 50.f, -50.f, 50.f);

            glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f);
            glTexCoord2f(0, 1); glVertex3f(-50.f,  50.f, -50.f);
            glTexCoord2f(1, 1); glVertex3f(-50.f,  50.f,  50.f);
            glTexCoord2f(1, 0); glVertex3f(-50.f, -50.f,  50.f);

            glTexCoord2f(0, 0); glVertex3f(50.f, -50.f, -50.f);
            glTexCoord2f(0, 1); glVertex3f(50.f,  50.f, -50.f);
            glTexCoord2f(1, 1); glVertex3f(50.f,  50.f,  50.f);
            glTexCoord2f(1, 0); glVertex3f(50.f, -50.f,  50.f);

            glTexCoord2f(0, 1); glVertex3f(-50.f, -50.f,  50.f);
            glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f);
            glTexCoord2f(1, 0); glVertex3f( 50.f, -50.f, -50.f);
            glTexCoord2f(1, 1); glVertex3f( 50.f, -50.f,  50.f);

            glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f,  50.f);
            glTexCoord2f(0, 0); glVertex3f(-50.f, 50.f, -50.f);
            glTexCoord2f(1, 0); glVertex3f( 50.f, 50.f, -50.f);
            glTexCoord2f(1, 1); glVertex3f( 50.f, 50.f,  50.f);

        glEnd();

 // App.EndOpenGL();

        // Draw some text on top of our OpenGL object

        sf::String Text("This is a rotating cube");
        Text.SetSize(20.f);
Text.SetFont("data/Celtic Garamond.ttf");
Text.SetPosition(230.f, 300.f);
        Text.SetColor(sf::Color(255, 255, 255));
        App.Draw(Text);

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

    // Don't forget to destroy our texture
    glDeleteTextures(1, &Texture);

    return EXIT_SUCCESS;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
OpenGL and SFML 1.2
« Reply #4 on: January 19, 2008, 09:53:46 am »
I've just fixed it. Thank you very much for your feedback.
Laurent Gomila - SFML developer

acrin1

  • Newbie
  • *
  • Posts: 45
    • View Profile
    • http://www.crpgdev.com
OpenGL and SFML 1.2
« Reply #5 on: January 19, 2008, 03:03:38 pm »
Quote from: "Laurent"
I've just fixed it. Thank you very much for your feedback.


Thanks for the quick solution. I managed to download and recompile the grpahics library and everything seems to work fine.

Thanks.