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

Author Topic: Can't change position of sf::Sprite  (Read 1314 times)

0 Members and 1 Guest are viewing this topic.

glorifiedelbow

  • Newbie
  • *
  • Posts: 2
    • View Profile
Can't change position of sf::Sprite
« on: March 28, 2020, 10:03:28 pm »
Hello! I'm trying to set up SFML with GLEW so I can directly use OpenGL calls instead of being limited to the SFML graphics package. As a simple test, I tried to render a single triangle to a sf::RenderTexture, and then render that texture onto a sf::RenderWindow by attaching it to a sf::Sprite. My triangle rendered correctly at first, but then I tried adding in some code to make the sprite move around, but the code seemed to have no effect and my triangle still rendered to stretch across the whole screen. This makes me wonder if I'm really rendering to the RenderTexture or if I'm somehow rendering my triangle directly to the window without realizing it. Anyway, here's my code:

#include <gl/glew.h>

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

#include "Shader.h"

int main()
{

        sf::RenderWindow window(sf::VideoMode(800, 600), "OpenGL 4.3", sf::Style::Default);
        window.setVerticalSyncEnabled(true);

        sf::ContextSettings settings;
        settings.majorVersion = 4;
        settings.minorVersion = 3;
       
        sf::RenderTexture gl_renderTarget;
        gl_renderTarget.create(800, 600, settings);
        gl_renderTarget.setActive(true);
       
        sf::Sprite gl_rendering(gl_renderTarget.getTexture());

        if (glewInit() != GLEW_OK)
        {

                std::cout << "Failed to initialize GLEW.\n";

                int qqq; std::cin >> qqq;
                return -1;

        }

        // Just a simple class that I wrote for handling shaders
        ShaderProgram shader("shader.vs", "shader.fs");

        GLfloat verts[] = {
                -1.0f, -1.0f,
                1.0f, -1.0f,
                1.0f, 1.0f
        };
       
        GLuint vao;
        glGenVertexArrays(1, &vao);
        glBindVertexArray(vao);

        GLuint vbo;
        glGenBuffers(1, &vbo);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);

        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
        glEnableVertexAttribArray(0);

        glBindVertexArray(0);

        while (window.isOpen())
        {

                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                gl_renderTarget.setActive(true);

                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                glBindVertexArray(vao);
                shader.use();
                glDrawArrays(GL_TRIANGLES, 0, 3);
               
                // As far as I understand, this should be making the triangle rotate every iteration, but instead it is stationary
                gl_rendering.rotate(10.0f);
                window.draw(gl_rendering);

                window.display();

        }

        return 0;
}
 

Any help would be greatly appreciated  :D I don't usually ask for help on forums like this, but I've been stuck on this for hours and I don't think I'm gonna figure it out anytime soon. This is my first time using SFML, so I'm sorry if it's something super obvious that I'm just overlooking.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Can't change position of sf::Sprite
« Reply #1 on: March 30, 2020, 04:20:09 pm »
The Graphics Module* in SFML also uses OpenGL. It needs its own state so you need to switch OpenGL states between using the graphics module and manual OpenGL.

There's more information about it here:
https://www.sfml-dev.org/tutorials/2.5/window-opengl.php#using-opengl-together-with-the-graphics-module

*sf::Sprite is a part of the graphics module.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

glorifiedelbow

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Can't change position of sf::Sprite
« Reply #2 on: March 30, 2020, 08:23:27 pm »
Thanks for your reply! I actually saw that page, but I guess I just have a misunderstanding of how OpenGL works and what "states" are. I've only ever used OpenGL by itself and not in combination with a second graphics library. I'll do a little more research on OpenGL and then come back to this project. :)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Can't change position of sf::Sprite
« Reply #3 on: March 31, 2020, 05:48:06 pm »
"The easiest solution is to let SFML do it for you"

glDraw...

window.pushGLStates();

window.draw(...);

window.popGLStates();

glDraw...

 8)

Can't say I fully understand the underlying mechanics of the sharing of OpenGL with SFML but this is the official 'easy' solution, although you can do more manual switching if optimisation is needed. It shows how in tha same tutorial :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything