Gl Graph

Gl Graph Source of glcanvas.cpp


#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glx.h>
#include <GL/glxext.h>
#include <GL/glut.h>
#include <string>
#include <string.h>
#include "glcanvas.h"
 
 
 
GlCanvas::GlCanvas()
{
}
 
GlCanvas::GlCanvas(GlCamera *c)
{
    this->camera=c;
    this->width=c->getWidth();
    this->height=c->getHeight();
    this->textcursorx = 0;
    this->textcursory = 0;
}
 
void GlCanvas::setColor(float r,float g, float b)
{
    glColor3f(r, g, b);
    setRasterPos();
}
 
void GlCanvas::drawString(const char *str)
{
    unsigned int size = strlen(str);
    glutBitmapWidth(GLUT_BITMAP_8_BY_13, 12);
 
    for (unsigned int i = 0; i < size; i++)
    {
        char c=str[i];
        if (c == '\n')
            newLine();
        else
        {
            this->textcursorx++;
            glutBitmapCharacter(GLUT_BITMAP_8_BY_13, c);
        }
    }
}
void GlCanvas::setRasterPos()
{
    const float XPixelSize = camera->getGlWidth()/camera->getWidth();
    const float YPixelSize = camera->getGlHeight()/camera->getHeight();
 
    const float x = -0.9 * camera->getGlWidth()/2 + 8 * XPixelSize * this->textcursorx;
    const float y =  0.9 * camera->getGlHeight()/2 - 10 * YPixelSize * this->textcursory;
    glRasterPos2f(x, y);
}
 
void GlCanvas::newLine()
{
    this->textcursorx=0;
    this->textcursory++;
    setRasterPos();
}
 
void GlCanvas::resetLine()
{
    this->textcursorx = 0;
    this->textcursory = 0;
    newLine();
}
 
GlCanvas& operator<<(GlCanvas& c, int value)
{
 
    const int strSize = 50;
    char buffer [strSize];
    snprintf(buffer, strSize, "%i", value);
    c.drawString(buffer);
 
    return c;
}
 
GlCanvas& operator<<(GlCanvas& c, float value)
{
    const int strSize = 50;
    char buffer [strSize];
    snprintf(buffer, strSize, "%f", value);
    c.drawString(buffer);
 
    return c;
}
 
GlCanvas& operator<<(GlCanvas& c, double value)
{
    const int strSize = 50;
    char buffer [strSize];
    snprintf(buffer, strSize, "%fl", value);
    c.drawString(buffer);
 
    return c;
}
 
GlCanvas& operator<<(GlCanvas& c, const char * value)
{
    c.drawString(value);
    return c;
}
 
GlCanvas& operator<<(GlCanvas& c, std::string value)
{
    c.drawString(value.c_str());
    return c;
}
 
GlCanvas& operator<<(GlCanvas& c, GlVersion & version)
{
 
    c << "GL_VERSION   :" << version.getGlVERSION() << "\n";
    c << "GL_VENDOR    :" << version.getGlVENDOR() << "\n";
    c << "GL_RENDERER  :" << version.getGlRENDERER() << "\n";
    c << "GL_EXTENSIONS:\n";
    int nb = version.getGlEXTENSIONSNB();
    unsigned int alternate=0;
    for (int i = 0; i < nb; i++)
    {
        std::string ext = version.getGlEXTENSION(i);
        std::string ending;
 
        if (i % 3 == 2)
        {
            ending = "\n";
            alternate++;
        }
        else
        {
            ending = "";
            for (unsigned int i = 0; i < 50 - ext.size(); i++)
                ending.append(" ");
        }
 
        float color;
        if (alternate%2==0)
            color=0.5;
        else
            color=0.9;
        c.setColor(0.5, 0.5, color);
        c << ext << ending;
    }
    return c;
}