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

Author Topic: Drawing sf::Sprite together with OpenGL 4.x  (Read 4058 times)

0 Members and 1 Guest are viewing this topic.

HawkDeath

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Drawing sf::Sprite together with OpenGL 4.x
« on: December 07, 2016, 07:20:04 pm »
Hi,
I try draw simple triangle (OpenGL) with simple sprite and font.
#include <SFML\Graphics.hpp>
#include <GL\glew.h>

GLuint program = 0, vao = 0;
namespace OpenGL
{
   void Initialize()
   {
      glewExperimental = true;
      glewInit();
      static const char * vs_source[] =
      {
         "#version 420 core                                                 \n"
         "                                                                  \n"
         "void main(void)                                                   \n"
         "{                                                                 \n"
         "    const vec4 vertices[] = vec4[](vec4( 1, -1, 0.5, 1.0),  \n"
         "                                   vec4(-1, -1, 0.5, 1.0),  \n"
         "                                   vec4( 0.0,  1.0, 0.5, 1.0)); \n"
         "                                                                  \n"
         "    gl_Position = vertices[gl_VertexID];                          \n"
         "}                                                                 \n"
      };

      static const char * fs_source[] =
      {
         "#version 420 core                                                 \n"
         "                                                                  \n"
         "out vec4 color;                                                   \n"
         "                                                                  \n"
         "void main(void)                                                   \n"
         "{                                                                 \n"
         "    color = vec4(0.0, 0.8, 1.0, 1.0);                             \n"
         "}                                                                 \n"
      };
      program = glCreateProgram();
      GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
      glShaderSource(fs, 1, fs_source, nullptr);
      glCompileShader(fs);

      GLuint vs = glCreateShader(GL_VERTEX_SHADER);
      glShaderSource(vs, 1, vs_source, nullptr);
      glCompileShader(vs);

      glAttachShader(program, vs);
      glAttachShader(program, fs);

      glLinkProgram(program);

      glGenVertexArrays(1, &vao);
      glBindVertexArray(vao);

   }

   void glRender()
   {
      static const GLfloat green[] = { 0.0f, 0.25f, 0.0f, 1.0f };
      //glClearBufferfv(GL_COLOR, 0, green);
      glClearColor(0.0f, 0.25f, 0.0f, 1.0f);
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glUseProgram(program);
      glDrawArrays(GL_TRIANGLES, 0, 3);

   }

   void Shutdown()
   {
      glDeleteVertexArrays(1, &vao);
      glDeleteProgram(program);
   }
};
int main()
{
   sf::ContextSettings glContextSettings;
   glContextSettings.majorVersion = 4;
   glContextSettings.minorVersion = 2;
   glContextSettings.antialiasingLevel = 8;
   glContextSettings.depthBits = 24;
   sf::RenderWindow window(sf::VideoMode(800, 600), "SFML and OpenGL 4.x or 3.2+ test",
      sf::Style::Default,glContextSettings);

   sf::Texture tex;
   tex.loadFromFile("CHAPTER1.TGA");
   sf::Sprite sprite;
   sprite.setTexture(tex);

   sf::Font font;
   font.loadFromFile("Ubuntu-R.ttf");
   sf::Text text;
   text.setFont(font);
   text.setString("test string");

   OpenGL::Initialize();

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

      window.pushGLStates();
      window.draw(sprite);
      window.draw(text);
      window.popGLStates();
     
      window.display();
   }
   OpenGL::Shutdown();
   return 0;
}
 
The result I get this error:
« Last Edit: December 09, 2016, 06:33:51 pm by HawkDeath »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Drawing sf::Sprite together with OpenGL 4.x
« Reply #1 on: December 08, 2016, 06:40:31 pm »
The image doesn't load for us. Also maybe explain a bit more what you expect and what already tried to get it to work prior to posting here. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

HawkDeath

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Drawing sf::Sprite together with OpenGL 4.x
« Reply #2 on: December 09, 2016, 06:34:33 pm »
The image doesn't load for us. Also maybe explain a bit more what you expect and what already tried to get it to work prior to posting here. :)
Fixed. :)

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Drawing sf::Sprite together with OpenGL 4.x
« Reply #3 on: December 09, 2016, 08:48:50 pm »
SFML requires compatibility mode to be on in OpenGL.
You need to change "core" to "compatibility" in your shaders.

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Drawing sf::Sprite together with OpenGL 4.x
« Reply #4 on: December 09, 2016, 10:39:55 pm »
SFML requires compatibility mode to be on in OpenGL.
You need to change "core" to "compatibility" in your shaders.

To clarify: SFML requires compatibility mode when mixing with SFML drawing - you can still get core contexts when using just the window module. Also it means no OS X support, as OS X only supports core profiles.

HawkDeath

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Drawing sf::Sprite together with OpenGL 4.x
« Reply #5 on: December 16, 2016, 02:49:12 pm »
Still not work. :( I try this way:
#version 420 compatibility
and this way:
#version 420
both not work.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

 

anything