Forum de la communauté SFML

Aide => Graphique => Discussion démarrée par: lanquemar le Octobre 03, 2012, 10:04:59 pm

Titre: [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: lanquemar le Octobre 03, 2012, 10:04:59 pm
Bonsoir,
Alors voilà, cela fait une heure que je cherche, mais je ne trouve rien, alors qu'il me semble avoir déjà lu quelque chose quelque part... Je m'excuse donc d'avance pour un probable double post  :-\
En fait, j'ai un contexte OpenGL 3.3 grâce à un sf::Window, et je voudrais tout simplement écrire du texte (2D) (sf::Text) sur ma fenêtre... Apparemment, il faudrait absolument passer par un sf::RenderWindow pour avoir la méthode draw, mais il n'y aurait vraiment pas moyen de garder un sf:Window ?
Désolé du dérangement,
Merci beaucoup !  :)
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: Laurent le Octobre 04, 2012, 08:03:08 am
Citer
il faudrait absolument passer par un sf::RenderWindow pour avoir la méthode draw
Voilà, t'as tout compris. Et quel est le problème à utiliser sf::RenderWindow ?
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: lanquemar le Octobre 04, 2012, 07:24:48 pm
Merci de la réponse !

Ben en fait, je n'arrive pas à modifier mon code pour passer de sf::Window à sf::RenderWindow.
Ce que je veux dire c'est que si je change seulement mes sf::Window en sf::RenderWindow, j'ai des milliers d'erreurs (dans glew apparemment :/).
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: Laurent le Octobre 04, 2012, 08:33:50 pm
Les erreurs sont faites pour être résolues, pas contournées. Et si tu veux de l'aide il va falloir être plus précis quant aux messages d'erreur ;)
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: lanquemar le Octobre 04, 2012, 08:41:22 pm
D'accord, pas de problème ! Merci de m'aider ! :)

Alors voilà, dans mon code, j'ai seulement et uniquement remplacé :
sf::Window *m_window;
par
sf::RenderWindow *m_window;
Et
m_window = new sf::Window(sf::VideoMode(m_xWin, m_yWin), m_titleWin.c_str(), sf::Style::Default, m_setSettings);
par
m_window = new sf::RenderWindow(sf::VideoMode(m_xWin, m_yWin), m_titleWin.c_str(), sf::Style::Default, m_setSettings);

Lorsque j'essaye de compiler, voilà ce que j'ai :
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xd78): multiple definition of `__glewWindowPos2dARB'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3155: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xd7c): multiple definition of `__glewWindowPos2dvARB'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3155: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xd80): multiple definition of `__glewWindowPos2fARB'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3155: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xd84): multiple definition of `__glewWindowPos2fvARB'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3155: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xd88): multiple definition of `__glewWindowPos2iARB'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3155: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xd8c): multiple definition of `__glewWindowPos2ivARB'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3155: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xd90): multiple definition of `__glewWindowPos2sARB'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3155: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xd94): multiple definition of `__glewWindowPos2svARB'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3155: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xd98): multiple definition of `__glewWindowPos3dARB'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3156: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xd9c): multiple definition of `__glewWindowPos3dvARB'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3156: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xda0): multiple definition of `__glewWindowPos3fARB'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3156: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xda4): multiple definition of `__glewWindowPos3fvARB'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3156: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xda8): multiple definition of `__glewWindowPos3iARB'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3156: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xdac): multiple definition of `__glewWindowPos3ivARB'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3156: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xdb0): multiple definition of `__glewWindowPos3sARB'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3156: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xdb4): multiple definition of `__glewWindowPos3svARB'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3156: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xdd8): multiple definition of `__glewAlphaFragmentOp1ATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3157: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xddc): multiple definition of `__glewAlphaFragmentOp2ATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3157: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xde0): multiple definition of `__glewAlphaFragmentOp3ATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3157: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xde4): multiple definition of `__glewBeginFragmentShaderATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3157: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xde8): multiple definition of `__glewBindFragmentShaderATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3157: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xdec): multiple definition of `__glewColorFragmentOp1ATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3157: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xdf0): multiple definition of `__glewColorFragmentOp2ATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3157: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xdf4): multiple definition of `__glewColorFragmentOp3ATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3157: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xdf8): multiple definition of `__glewDeleteFragmentShaderATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3157: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xdfc): multiple definition of `__glewEndFragmentShaderATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3158: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe00): multiple definition of `__glewGenFragmentShadersATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3158: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe04): multiple definition of `__glewPassTexCoordATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3158: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe08): multiple definition of `__glewSampleMapATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3158: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe0c): multiple definition of `__glewSetFragmentShaderConstantATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3158: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe28): multiple definition of `__glewArrayObjectATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3158: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe2c): multiple definition of `__glewFreeObjectBufferATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3159: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe30): multiple definition of `__glewGetArrayObjectfvATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3159: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe34): multiple definition of `__glewGetArrayObjectivATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3159: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe38): multiple definition of `__glewGetObjectBufferfvATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3159: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe3c): multiple definition of `__glewGetObjectBufferivATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3159: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe40): multiple definition of `__glewGetVariantArrayObjectfvATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3159: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe44): multiple definition of `__glewGetVariantArrayObjectivATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3159: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe48): multiple definition of `__glewIsObjectBufferATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3159: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe4c): multiple definition of `__glewNewObjectBufferATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3159: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe50): multiple definition of `__glewUpdateObjectBufferATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3159: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe54): multiple definition of `__glewVariantArrayObjectATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3159: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe64): multiple definition of `__glewClientActiveVertexStreamATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3160: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe68): multiple definition of `__glewNormalStream3bATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3160: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe6c): multiple definition of `__glewNormalStream3bvATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3160: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe70): multiple definition of `__glewNormalStream3dATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3160: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe74): multiple definition of `__glewNormalStream3dvATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3160: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe78): multiple definition of `__glewNormalStream3fATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3160: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe7c): multiple definition of `__glewNormalStream3fvATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3160: first defined here
C:\Program Files (x86)\CodeBlocks\SFML-2.0-rc\lib/libsfml-graphics-s-d.a(glew.c.o):glew.c:(.bss+0xe80): multiple definition of `__glewNormalStream3iATI'
obj\Debug\glew.o:D:/Programmation/Robot/glew.c:3160: first defined here

(Je suis désolé du long texte que ça fait  :-\ )

Sachant que j'inclue bien <GL/glew.h> avant les includes SFML :)

Merci !
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: Laurent le Octobre 04, 2012, 08:46:32 pm
GLEW est déjà inclus à SFML, du coup quand tu lies celle-ci statiquement, et que de ton côté tu lies GLEW également statiquement (ou l'inclue dans ton code, c'est pareil), il se retrouve en double et l'éditeur de liens n'aime pas ça. Retirer glew.c de ton code devrait résoudre le problème.
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: lanquemar le Octobre 04, 2012, 08:48:53 pm
Et donc je ne peux pas lier statiquement glew ?
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: lanquemar le Octobre 04, 2012, 09:22:32 pm
J'ai lié dynamiquement GLEW, et j'ai donc ajouté glew32.dll à côté de mon exécutable...
Plus de problème de compilation, mais quand je lance le programme, il s'ouvre, et "cesse de fonctionner"...
J'ai dû faire une erreur quelque part, mais je ne sais pas où :/
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: Laurent le Octobre 04, 2012, 09:58:46 pm
Non, je te disais de ne pas du tout lier GLEW. Il est déjà dans SFML.
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: lanquemar le Octobre 04, 2012, 10:07:00 pm
Bon, j'ai réussis à faire quelque chose qui fonctionne ! Merci !

Par contre, j'ai encore un problème...
Mon but à la base était de pouvoir écrire du texte.
Maintenant que GLEW/SFML cohabitent correctement, j'ai fait ceci :
 // Declare and load a font
     sf::Font font;
     font.loadFromFile("arial.ttf");

     // Create a text
     sf::Text text("hello");
     text.setFont(font);
     text.setCharacterSize(30);
     text.setStyle(sf::Text::Bold);
     text.setColor(sf::Color::Red);

Mais quand je lance mon programme, pas de texte du tout, et un message dans la console :
An internal OpenGL call failed in Texture.cpp (95) : unknown error, no description

Qu'est-ce que je peux faire ?  :-\
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: Laurent le Octobre 04, 2012, 10:11:33 pm
Peux-tu extraire un code minimal et complet qui reproduit le problème ?
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: lanquemar le Octobre 04, 2012, 10:17:39 pm
Alors, j'ai essayé de faire un condensé de mes fonctions :

     m_window = new sf::RenderWindow(sf::VideoMode(m_xWin, m_yWin), m_titleWin.c_str(), sf::Style::Default, m_setSettings);

     GLenum glew_status = glewInit();
     if (glew_status != GLEW_OK)
     {
          std::cout << "[ERROR] Cannot init GLEW : " << glewGetErrorString(glew_status) << std::endl;
          return false;
     }

     if (!GLEW_VERSION_2_0)
     {
          std::cout << "[ERROR] Your graphic card does not support OpenGL 2.0." << std::endl;
          return false;
     }

     // Enable alpha
     glEnable(GL_BLEND);
     glEnable(GL_DEPTH_TEST);
     //glDepthFunc(GL_LESS);
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    bool running = true;

     // Declare and load a font
     sf::Font font;
     font.loadFromFile("arial.ttf");

     // Create a text
     sf::Text text("Hello");
     text.setFont(font);
     text.setCharacterSize(10);
     text.setStyle(sf::Text::Bold);
     text.setColor(sf::Color::Red);
     text.setPosition(100,100);

     sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    Cube cubeTest;
    Plan planTest;

    glm::mat4 modelview = glm::lookAt(glm::vec3(3.0, 2.0, 5.0), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));
    glm::mat4 projection = glm::perspective(60.0f, 1.0f * m_xWin/m_yWin, 0.0001f, 1000.0f);

    while (running)
    {
        // gestion des évènements
        sf::Event event;
        while (m_window->pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                // on stoppe le programme
                running = false;
            }
            else if (event.type == sf::Event::Resized)
            {
                // on ajuste le viewport lorsque la fenêtre est redimensionnée
                glViewport(0, 0, event.size.width, event.size.height);
                m_xWin = event.size.width;
                m_yWin = event.size.height;
            }
        }

        // effacement les tampons de couleur/profondeur
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        m_window->pushGLStates();
        m_window->draw(shape);
        m_window->draw(text);
        m_window->popGLStates();

        // dessin...
        cubeTest.draw(modelview, projection);
        planTest.draw(modelview, projection);

        // termine la trame courante (en interne, échange les deux tampons de rendu)
        m_window->display();
    }
 

Voilà, j'espère que c'est assez clair et concis  :-\
Il y a peut être quelques incohérences, mais le tout est placé dans plusieurs méthodes d'une classe, donc bon...

EDIT : Et avec ce code, je n'ai ni rond vert, ni texte...
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: Laurent le Octobre 04, 2012, 10:20:56 pm
Je n'ai pas besoin de tout ton code, juste du morceau minimal qui produit l'erreur. Et ce serait bien si c'était complet, le but c'est que je puisse le copier et le tester rapidos ;)
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: lanquemar le Octobre 04, 2012, 10:22:57 pm
Ben en gros, j'ai un contexte OpenGL avec GLEW et SFML dans une RenderWindow, j'affiche un plan et un cube, j'essaye d'afficher du texte par dessus, mais ça ne fonctionne pas...
Est-ce que je dois faire un autre morceau de code plus concentré ?
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: Laurent le Octobre 04, 2012, 10:28:51 pm
Oui ! C'est vraiment pas compliqué, tu enlèves petit à petit les trucs qui ont l'air inutiles, jusqu'à ce que le bug disparaisse. Là tu as un code vraiment minimal. C'est pas pour t'embêter que je te fais faire ça, c'est juste le moyen le plus rapide de cerner un bug. Scruter un gros code pendant des heures, j'ai pas vraiment le temps de le faire ; par contre copier quelques lignes bien ciblées, tester et debugger dans la foulée, ça je peux ;)
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: lanquemar le Octobre 04, 2012, 10:32:48 pm
Oui oui je me doute, et je te remercie !  :)

En fait, j'ai fait mieux, j'ai fait un nouveau projet, j'ai minimaliser mon code pour ne garder que ceci (j'ai essayé de garder les mêmes libs) :
#include <GL/glew.h>

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

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(1024, 768), "SFML works!");
    window.setVerticalSyncEnabled(false);
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

         // Declare and load a font
     sf::Font font;
     font.loadFromFile("arial.ttf");

     // Create a text
     sf::Text text("Hello");
     text.setFont(font);
     text.setCharacterSize(30);
     text.setStyle(sf::Text::Bold);
     text.setColor(sf::Color::Red);
     text.setPosition(100,100);

    GLenum code;

    /* initialisation de GLEW */
    code = glewInit();
    if(code != GLEW_OK)
    {
        fprintf(stderr, "impossible d'initialiser GLEW : %s\n",
                        glewGetErrorString(code));
    }

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

        window.clear();

        window.pushGLStates();
        window.draw(shape);
        window.draw(text);
        window.popGLStates();
        window.display();
    }

    return 0;
}

 

Et là tout fonctionne correctement, alors que sur mon projet principal, non...  :(
Ça commence à m'énerver xD !

Je vais continuer de chercher, merci encore :)
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: Laurent le Octobre 04, 2012, 10:36:59 pm
Voilà qui me fait gagner encore plus de temps :P

Et qui t'en fera gagner à toi aussi, vu que maintenant tu as une version qui marche et que tu peux comparer au code qui foire.
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: lanquemar le Octobre 04, 2012, 10:55:44 pm
Apparemment, si je fais de la 2D avec l'OpenGL, ça fonctionne...
Dès que je passe à la 3D, plus de texte ni de rond vert...
C'est bizarre, je regarderai ça plus tard :)

Merci encore :)
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: Laurent le Octobre 05, 2012, 07:41:55 am
Qu'est-ce que "3D" inclue exactement ? C'est quand tu ajoutes quelle(s) fonction(s) que ça foire ?
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: lanquemar le Octobre 05, 2012, 07:31:21 pm
En fait, ça foire quand cette fonction est exécutée :
int init_resources()
{
  GLfloat cube_vertices[] = {
    // front
    -1.0, -1.0,  1.0,
     1.0, -1.0,  1.0,
     1.0,  1.0,  1.0,
    -1.0,  1.0,  1.0,
    // back
    -1.0, -1.0, -1.0,
     1.0, -1.0, -1.0,
     1.0,  1.0, -1.0,
    -1.0,  1.0, -1.0,
  };

  glGenBuffers(1, &vbo_cube_vertices);
  glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_vertices);
  glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW);

  GLfloat cube_colors[] = {
    // front colors
    1.0, 0.0, 0.0,
    0.0, 1.0, 0.0,
    0.0, 0.0, 1.0,
    1.0, 1.0, 1.0,
    // back colors
    1.0, 0.0, 0.0,
    0.0, 1.0, 0.0,
    0.0, 0.0, 1.0,
    1.0, 1.0, 1.0,
  };

  glGenBuffers(1, &vbo_cube_colors);
  glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_colors);
  glBufferData(GL_ARRAY_BUFFER, sizeof(cube_colors), cube_colors, GL_STATIC_DRAW);

  GLushort cube_elements[] = {
    // front
    0, 1, 2,
    2, 3, 0,
    // top
    1, 5, 6,
    6, 2, 1,
    // back
    7, 6, 5,
    5, 4, 7,
    // bottom
    4, 0, 3,
    3, 7, 4,
    // left
    4, 5, 1,
    1, 0, 4,
    // right
    3, 2, 6,
    6, 7, 3,
  };

  glGenBuffers(1, &ibo_cube_elements);

  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_cube_elements);
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_elements), cube_elements, GL_STATIC_DRAW);

  GLint link_ok = GL_FALSE;
  GLuint vs, fs;

  if ((vs = create_shader("cube.v.glsl", GL_VERTEX_SHADER))   == 0) return 0;
  if ((fs = create_shader("cube.f.glsl", GL_FRAGMENT_SHADER)) == 0) return 0;

  program = glCreateProgram();
  glAttachShader(program, vs);
  glAttachShader(program, fs);
  glLinkProgram(program);
  glGetProgramiv(program, GL_LINK_STATUS, &link_ok);

  if (!link_ok) {
    fprintf(stderr, "glLinkProgram:");
    print_log(program);
    return 0;
  }

  const char* attribute_name;
  attribute_name = "coord3d";
  attribute_coord3d = glGetAttribLocation(program, attribute_name);

  if (attribute_coord3d == -1) {
    fprintf(stderr, "Could not bind attribute %s\n", attribute_name);
    return 0;
  }

  attribute_name = "v_color";
  attribute_v_color = glGetAttribLocation(program, attribute_name);

  if (attribute_v_color == -1) {
    fprintf(stderr, "Could not bind attribute %s\n", attribute_name);
    return 0;
  }

  const char* uniform_name;
  uniform_name = "mvp";
  uniform_mvp = glGetUniformLocation(program, uniform_name);

  if (uniform_mvp == -1) {
    fprintf(stderr, "Could not bind uniform %s\n", uniform_name);
    return 0;
  }
  return 1;
}
 

Donc si je résume ;
 - sans cette fonction, j'ai mon texte ;
 - avec cette fonction, j'ai une fenêtre avec mes dessins OpenGL ;
 - pas d'erreur de compilation ;
 - pas de message particulier dans la console ;

En fait, quand je disais que quand je fais de la 2D ça fonctionne, c'est simplement qu'en fait, je n'utilisais pas les Shaders pour la 2D. Par contre, j'utilise les Shaders pour la 3D, et là ça foire...
Donc ça serait les Shaders ?  :-\

EDIT : En fait, je me trompe totalement : quand je faisais de la 2D (et que ça foirait pas), j'utilisais aussi deux shaders, mais que je rentrais moi même dans un tableau de char (sans passer par une ouverture de fichier), donc ça ne peut pas être ça non plus... :( Grr
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: lanquemar le Octobre 05, 2012, 08:03:29 pm
J'ai continué d'affiner ma recherche dans la fonction d'init, et voilà ce que j'ai trouvé :
glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_vertices);

Avec cette ligne (sans la suite de la fonction), pas de texte.
Sans cette ligne (ni la suite de la fonction), texte.

Je comprends de moins en moins bien le problème... xD  :o
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: Laurent le Octobre 05, 2012, 08:45:26 pm
Tu utilises la 2.0 RC ou bien les dernières sources ?

Sinon, un petit
glBindBuffer(GL_ARRAY_BUFFER, 0);
avant les fonctions SFML devrait faire l'affaire.
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: lanquemar le Octobre 05, 2012, 08:50:52 pm
J'utilise la version RC  :)

Mais par contre, le bind à 0 fonctionne parfaitement !  :D  :D

Merci beaucoup !!!!

Et désolé pour le dérangement  :-\
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: Laurent le Octobre 05, 2012, 11:16:42 pm
Y a pas de mal, tu pouvais pas savoir ;)
Titre: Re : [SFML 2.0 | OpenGL] Du texte dans un sf::Window ?
Posté par: G. le Octobre 06, 2012, 02:02:34 am
Merci ! J'avais le même problème avec le texte.  :)