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

Author Topic: SFML with OpenGL  (Read 9578 times)

0 Members and 1 Guest are viewing this topic.

Paci4416

  • Newbie
  • *
  • Posts: 6
    • View Profile
SFML with OpenGL
« on: September 02, 2009, 07:45:23 pm »
Hello ,
I'm new for SFML and my goal is to build a small game with 3D models.
I'm also new to OpenGL and never used it.

So I found some tut's about OpenGL but i dont know if i can learn from them because i'm using also the sfml.

For example I want to learn from this site:
http://nehe.gamedev.net/lesson.asp?index=01

But I dont know if I need to write everything written there or SFML does somthing for me , and how to add this code with the SFML main loop and other.

Hope you understand me, Thank you in advance. :)

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
SFML with OpenGL
« Reply #1 on: September 02, 2009, 09:20:55 pm »
SFML / OS X developer

Paci4416

  • Newbie
  • *
  • Posts: 6
    • View Profile
SFML with OpenGL
« Reply #2 on: September 03, 2009, 01:09:07 am »
I read this tut but still that wasnt what I meant.
Look at this code:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=01

it's alott of code just to create a blank window with some error handling.
and on the sfml tut with Ogl it took something like 6 commands to create this window.

So i want to know what SFML already does for me in OpenGL so i know what i need to really learn about OpenGL

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML with OpenGL
« Reply #3 on: September 03, 2009, 07:52:13 am »
All SFML does is creating a valid OpenGL context. In other words, all the OS specific stuff (wgl**) is done internally, so that you can focus on the useful/funny part (gl***).
Laurent Gomila - SFML developer

kryton9

  • Newbie
  • *
  • Posts: 11
    • View Profile
SFML OpenGL, 3D and 2D help needed
« Reply #4 on: September 21, 2009, 10:28:30 pm »
I am trying to run 3D and also 2D OpenGL with SFML.
I tried modifying some of the tutorials, and got close. But I can't get the previous 3D screen to clear. As you see in the code, I commented out what I thought would work, and as is-- at least you can see what the problem is.

Thanks for any help!

Code: [Select]

#include <SFML/Graphics.hpp>
#include <iostream>


int main()
{
    // Create main window
    sf::RenderWindow App(sf::VideoMode(1024, 768, 32), "SFML OpenGL", sf::Style::Fullscreen);
    App.PreserveOpenGLStates(true);

    // Create a sprite for the background
    sf::Image BackgroundImage;
    if (!BackgroundImage.LoadFromFile("data/ks9CubeArt.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/ks9CubeArt.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);

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

    // 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;

    // Draw some text on top of our OpenGL object
    sf::String Text("ks9.us - Let's play!");
    Text.SetColor(sf::Color(0,255,0));

    // 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();

            // Adjust the viewport when the window is resized
            if (Event.Type == sf::Event::Resized)
                glViewport(0, 0, Event.Size.Width, Event.Size.Height);
         }



        /* When I use (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)************
         * the cube does not show up as I would think it should.             *
         *                                                                   *
         * How can I get rid of the ghosting from the previous               *
         * cube renders, thanks! *********************************************
         */
        //glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
        glClear(GL_DEPTH_BUFFER_BIT);

                // Draw background
        App.Draw(Background);

        // Apply some transformations
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        glTranslatef(0.f, 0.f, -150.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.Draw(Text);

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

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

    return EXIT_SUCCESS;
}


Code::Blocks Project download of source and running exe file in 7 zip format. CBProject

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML with OpenGL
« Reply #5 on: September 21, 2009, 11:10:59 pm »
What happens if you uncomment this line?
Laurent Gomila - SFML developer

kryton9

  • Newbie
  • *
  • Posts: 11
    • View Profile
SFML with OpenGL
« Reply #6 on: September 22, 2009, 12:36:51 am »
I do not see the cube at all. Only the sprite and text.

With the comment, I see the cube, but it doesn't erase the previous draws of the cube, so it leaves a trail of previous cube renders.

Thanks for replying, sorting this out will really help.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML with OpenGL
« Reply #7 on: September 22, 2009, 08:49:59 am »
So, the OpenGL sample from the SDK doesn't work with no modification??
Laurent Gomila - SFML developer

kryton9

  • Newbie
  • *
  • Posts: 11
    • View Profile
SFML with OpenGL
« Reply #8 on: September 23, 2009, 05:08:20 am »
I'll figure it out, thanks anyways.

 

anything