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

Author Topic: Can't put text on my OpenGL drawing.  (Read 3042 times)

0 Members and 1 Guest are viewing this topic.

manasij7479

  • Newbie
  • *
  • Posts: 7
    • View Profile
Can't put text on my OpenGL drawing.
« on: November 20, 2012, 08:49:47 am »
Here is what I'm doing:
(The OpenGl stuff isn't encapsulated yet, as I'm just learning it now)
What seems to be missing ?
Here is the main loop:
        while(win.isOpen())
        {              
                draw();        
                win.display();
               
                std::string input;
                std::getline(std::cin,input);
                std::cout<<shell(input);
               
                win.pushGLStates();
                        sf::Font font;
                        font.getDefaultFont();
                        sf::Text text;
                        text.setFont(font);
                        text.setCharacterSize(20);
                        text.setStyle(sf::Text::Bold);
                        text.setColor(sf::Color::Black);
                        text.setPosition(0,0);
                        text.setString(input);
                        win.draw(text);
                win.popGLStates();
               
               
                sf::Event eve;
                while(win.pollEvent(eve))
                {
                        if(eve.type==sf::Event::Closed)
                                win.close();
                }
        }
 

And the rest of the main.cpp file, (though everything other than the text gets drawn fine).
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Text.hpp>
#include <GL/glew.h>
#include <iostream>
#include "shader.h"
#include "program.h"
#include "logostate.h"
#include "shell.h"
void setup();
void draw();

namespace global
{
        GLuint trail_vao,turtle_vao,trail_vbo,turtle_vbo;
        mm::Program* turtle_prog,*trail_prog;
        GLint col_ul,theta_ul;
        mm::LogoState turtle;
}
using namespace global;
void GlewInit()
{
        GLenum err = glewInit();
        if (GLEW_OK != err)
                std::cerr<<"Error "<<glewGetErrorString(err);
}

int main()
{
        sf::RenderWindow win(sf::VideoMode(600,600,32),"Logo");
        GlewInit();
        setup();
        mm::Shell shell(&turtle);
       
        while(win.isOpen())
        {              
                draw();        
                win.display();
               
                std::string input;
                std::getline(std::cin,input);
                std::cout<<shell(input);
               
                win.pushGLStates();
                        sf::Font font;
                        font.getDefaultFont();
                        sf::Text text;
                        text.setFont(font);
                        text.setCharacterSize(20);
                        text.setStyle(sf::Text::Bold);
                        text.setColor(sf::Color::Black);
                        text.setPosition(0,0);
                        text.setString(input);
                        win.draw(text);
                win.popGLStates();
               
               
                sf::Event eve;
                while(win.pollEvent(eve))
                {
                        if(eve.type==sf::Event::Closed)
                                win.close();
                }
        }
        return 0;
}

void setup()
{

        turtle_prog = new mm::Program
        (
                {
                        mm::Shader(GL_VERTEX_SHADER,"turtle.vert"),
                        mm::Shader(GL_FRAGMENT_SHADER,"turtle.frag")
                }
        );
        trail_prog = new mm::Program
        (
                {
                        mm::Shader(GL_VERTEX_SHADER,"trail.vert"),
                        mm::Shader(GL_FRAGMENT_SHADER,"trail.frag")
                }
        );
       
        glGenVertexArrays(1,&trail_vao);
        glBindVertexArray(trail_vao);
        glGenBuffers(1,&trail_vbo);
        glBindBuffer(GL_ARRAY_BUFFER,trail_vbo);
        glBufferData(GL_ARRAY_BUFFER,turtle.vdata.size()*sizeof(float),turtle.vdata.data(),GL_DYNAMIC_DRAW);
        glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,0);
        glEnableVertexAttribArray(0);
       
        glGenVertexArrays(1,&turtle_vao);
        glBindVertexArray(turtle_vao);
        glGenBuffers(1,&turtle_vbo);
        glBindBuffer(GL_ARRAY_BUFFER,turtle_vbo);
        glBufferData(GL_ARRAY_BUFFER,6*sizeof(float),turtle.turtle(),GL_DYNAMIC_DRAW);
        glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,0);
        glEnableVertexAttribArray(0);
}
void draw()
{
       
        float* x=turtle.bgColor();
        glClearColor(x[0],x[1],x[2],x[3]);
        glClear(GL_COLOR_BUFFER_BIT);
       
        glUseProgram(trail_prog->getHandle());
        float *y = turtle.penColor();
        col_ul = glGetUniformLocation(trail_prog->getHandle(),"col");
        glUniform4fv(col_ul,1,y);
       
        glBindVertexArray(trail_vao);
        glBindBuffer(GL_ARRAY_BUFFER,trail_vbo);
        glBufferData(GL_ARRAY_BUFFER,turtle.vdata.size()*sizeof(float),turtle.vdata.data(),GL_DYNAMIC_DRAW);
        glDrawArrays(GL_LINE_STRIP,0,turtle.vdata.size()/2);
       
        glUseProgram(0);
       
        glUseProgram(turtle_prog->getHandle());
        glBindVertexArray(turtle_vao);
        glBindBuffer(GL_ARRAY_BUFFER,turtle_vbo);
        glBufferData(GL_ARRAY_BUFFER,6*sizeof(float),turtle.turtle(),GL_DYNAMIC_DRAW);
        glDrawArrays(GL_TRIANGLES,0,3);
       
        glUseProgram(0);
       
}
 
The ultimate goal here is to get rid of iostream totally and get the input using sf::Event and mirrorring the input to the window in realtime. So, if there is a shortcut to that, please point me to that instead.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Can't put text on my OpenGL drawing.
« Reply #1 on: November 20, 2012, 08:52:13 am »
Quote
sf::Font font;
font.getDefaultFont();
You should read the doc/examples/tutorials ;)
Hint: getDefaultFont() is a static function and it returns something.
Laurent Gomila - SFML developer

manasij7479

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Can't put text on my OpenGL drawing.
« Reply #2 on: November 20, 2012, 09:01:21 am »
You should read the doc/examples/tutorials ;)
Hint: getDefaultFont() is a static function and it returns something.
Changed that part to:
sf::Font font(sf::Font::getDefaultFont());
 
Since, sf::Font has a copy constructor that should be alright, I guess.
And sf::Text default constructs itself with the default font anyway .
(and the docs are great, btw !)

And it doesn't seem to change anything visible, unfortunately.

 

anything