SFML community forums

Help => Graphics => Topic started by: HawkDeath on December 07, 2016, 07:20:04 pm

Title: Drawing sf::Sprite together with OpenGL 4.x
Post by: HawkDeath 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:
(http://pokaz.im/di-P02Y.png)
Title: Drawing sf::Sprite together with OpenGL 4.x
Post by: eXpl0it3r 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. :)
Title: Re: Drawing sf::Sprite together with OpenGL 4.x
Post by: HawkDeath 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. :)
Title: Re: Drawing sf::Sprite together with OpenGL 4.x
Post by: dabbertorres 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.
Title: Re: Drawing sf::Sprite together with OpenGL 4.x
Post by: fallahn 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.
Title: Re: Drawing sf::Sprite together with OpenGL 4.x
Post by: HawkDeath 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.
Title: Re: Drawing sf::Sprite together with OpenGL 4.x
Post by: binary1248 on December 16, 2016, 06:59:30 pm
http://en.sfml-dev.org/forums/index.php?topic=20840.0