Gl Graph

Gl Graph Source of glaxes.cpp


#include <glaxes.h>
#include <stdio.h>
#include <string.h>
#include "drawableset.h"
 
static void draw_str(const char *str, float x, float y, float z)
{
    glRasterPos3f(x, y, z);
    int size = strlen(str);
    glutBitmapWidth(GLUT_BITMAP_8_BY_13, 18);
 
    for (int i = 0; i < size; i++)
        glutBitmapCharacter(GLUT_BITMAP_8_BY_13, str[i]);
}
 
static void drawStr(float value, float x, float y, float z)
{
    const int strSize = 50;
    char buffer [strSize];
    snprintf(buffer, strSize, "%.2f", value);
    draw_str(buffer, x, y, z);
}
 
void GlAxes::set(DrawableSet * set)
{
 
 
 
    float setMinx = set->getMinX();
    float setMaxx = set->getMaxX();
    float setMiny = set->getMinY();
    float setMaxy = set->getMaxY();
    float setMinz = set->getMinZ();
    float setMaxz = set->getMaxZ();
 
    this->distx = setMaxx - setMinx;
    this->disty = setMaxy - setMiny;
    this->distz = setMaxz - setMinz;
 
    const float axemargin = 0.1;
 
    this->minx = setMinx - axemargin * this->distx;
    this->maxx = setMaxx + axemargin * this->distx;
    this->miny = setMiny - axemargin * this->disty;
    this->maxy = setMaxy + axemargin * this->disty;
    this->minz = setMinz - axemargin * this->distz;
    this->maxz = setMaxz + axemargin * this->distz;
 
    this->stepDistanceX = pow(10, trunc(log10(this->distx / 1.3)));
    this->minxAligned = (trunc(minx / this->stepDistanceX)) * this->stepDistanceX;
 
    this->stepDistanceY = pow(10, trunc(log10(this->disty / 1.3)));
    this->minyAligned = (trunc(miny / this->stepDistanceY)) * this->stepDistanceY;
 
    this->stepDistanceZ = pow(10, trunc(log10(this->distz / 1.3)));
    this->minzAligned = (trunc(minz / this->stepDistanceZ)) * this->stepDistanceZ;
}
 
void GlAxes::drawX(float YPixelSize)
{
 
    glBegin(GL_LINES);
    glColor3f(0.1, 0.1, 1);
    glVertex3f(this->minx - stepDistanceX, 0, 0);
    glVertex3f(this->maxx + stepDistanceX, 0, 0);
    if (stepDistanceX>0.0)
        for (int i = 0; true ; i++)
        {
            const float stepX = minxAligned + i*stepDistanceX;
            if (stepX > maxx)
                break;
 
            glVertex3f(stepX, 0, 0);
            glVertex3f(stepX, 8 * YPixelSize, 0);
        }
    glEnd();
}
 
void GlAxes::drawXValue()
{
    if (stepDistanceX>0.0)
        for (int i = 0; true; i++)
        {
            const float stepX = minxAligned + i*stepDistanceX;
            if (stepX > maxx)
                break;
 
            if (stepX != 0)
                drawStr(stepX, stepX, 0, 0);
        }
    draw_str("X", this->maxx + stepDistanceX, 0, 0);
}
 
void GlAxes::drawY(float XPixelSize)
{
 
    glBegin(GL_LINES);
    glVertex3f(0, this->miny - stepDistanceY, 0);
    glVertex3f(0, this->maxy + stepDistanceY, 0);
    if (stepDistanceY>0.0)
        for (int i = 0; true; i++)
        {
            const float stepY = minyAligned + i*stepDistanceY;
            if (stepY > maxy)
                break;
 
            glVertex3f(0, stepY, 0);
            glVertex3f(8 * XPixelSize, stepY, 0);
        }
    glEnd();
 
}
 
void GlAxes::drawYValue()
{
 
    if (stepDistanceY>0.0)
        for (int i = 0; true; i++)
        {
            const float stepY = minyAligned + i*stepDistanceY;
            if (stepY > maxy)
                break;
 
            if (stepY != 0)
                drawStr(stepY, 0, stepY, 0);
        }
    draw_str("Y", 0, this->maxy + stepDistanceY, 0);
}
 
void GlAxes::drawZ(float YPixelSize)
{
 
    glBegin(GL_LINES);
    glVertex3f(0, 0, this->minz - stepDistanceZ);
    glVertex3f(0, 0, this->maxz + stepDistanceZ);
    if (stepDistanceZ>0.0)
        for (int i = 0; true; i++)
        {
            const float stepZ = minzAligned + i*stepDistanceZ;
            if (stepZ > maxz)
                break;
 
            glVertex3f(0, 0, stepZ);
            glVertex3f(0, 8 * YPixelSize, stepZ);
        }
    glEnd();
 
}
 
void GlAxes::drawZValue()
{
    if (stepDistanceZ>0.0)
        for (int i = 0; true; i++)
        {
            const float stepZ = minzAligned + i*stepDistanceZ;
            if (stepZ > maxz)
                break;
 
            if (stepZ != 0)
                drawStr(stepZ, 0, 0, stepZ);
        }
    draw_str("Z", 0, 0, this->maxz + stepDistanceZ);
}
 
void GlAxes::draw(GlCamera * camera)
{
 
 
    glLineWidth(3);
 
    drawX(camera->getYPixelSize());
    drawY(camera->getXPixelSize());
    drawZ(camera->getYPixelSize());
 
    glClear(GL_DEPTH_BUFFER_BIT);
    glColor3f(0.7, 0.6, 0.7);
 
    Vec3f Z = camera->getSystem()->getZ();
    float dotproductZX = e1.dot(Z);
    float dotproductZY = e2.dot(Z);
    float dotproductZZ = e3.dot(Z);
 
    // to avoid polute sreen with unreadable values, check axe orientation
 
    if (fabs(dotproductZX) < 0.95)
        drawXValue();
    if (fabs(dotproductZY) < 0.95)
        drawYValue();
    if (fabs(dotproductZZ) < 0.95)
        drawZValue();
 
}