Gl Graph

Gl Graph Source of glscene.cpp


#include <drawableset.h>
#include "glcamera.h"
#include "glscene.h"
#include "glaxes.h"
#include "glversion.h"
#include "glcanvas.h"
#include "glscenestat.h"
#include "mousekeyboardstate.h"
 
 
GlScene::GlScene(unsigned int width,unsigned int height)
{
 
    this->camera = new GlCamera(width,height);
    this->canvas = new GlCanvas(this->camera);
    this->axes = new GlAxes();
    this->version = new GlVersion();
    this->stat = new GlSceneStat();
 
    this->isKeepProportionXY = false;
    this->isRedrawNeeded=true;
    this->isTextInfo=true;
    this->isDepthBufferEnable=true;
    this->isInitDone= false;
 
    this->polygonMode = GL_FILL;
    this->view=FRONT_VIEW;
}
 
GlScene::~GlScene()
{
    delete this->camera ;
    delete this->canvas ;
    delete this->axes;
    delete this->version ;
    delete this->stat ;
}
 
void GlScene::setReDraw()
{
    isRedrawNeeded = true;
}
 
void GlScene::unsetReDraw()
{
    isRedrawNeeded = false;
}
 
bool GlScene::isReDraw()
{
    return isRedrawNeeded;
}
 
void GlScene::setKeepProportionXY(bool keepProportionXY)
{
    isRedrawNeeded = isRedrawNeeded | (isKeepProportionXY != keepProportionXY);
    isKeepProportionXY = keepProportionXY;
}
 
bool GlScene::getKeepProportionXY()
{
    return isKeepProportionXY;
}
 
void GlScene::swapKeepProportionXY()
{
    isKeepProportionXY = !isKeepProportionXY;
    camera->setKeepProportionXY(isKeepProportionXY);
    isRedrawNeeded = true;
}
 
 
void GlScene::setTextInfo(bool textInfo)
{
    isRedrawNeeded = isRedrawNeeded | (isTextInfo != textInfo);
    isTextInfo = textInfo;
}
 
bool GlScene::getTextInfo()
{
    return isTextInfo;
}
 
void GlScene::swapTextInfo()
{
    isTextInfo = !isTextInfo;
    isRedrawNeeded = true;
}
 
void GlScene::setDepthBuffer(bool depthBufferEnable)
{
    isRedrawNeeded = isRedrawNeeded | (isDepthBufferEnable != depthBufferEnable);
    isDepthBufferEnable = depthBufferEnable;
}
 
bool GlScene::getDepthBuffer()
{
    return isDepthBufferEnable;
}
 
void GlScene::swapDepthBuffer()
{
    isDepthBufferEnable = !isDepthBufferEnable;
    isRedrawNeeded = true;
}
 
bool GlScene::isSetChange()
{
    return set->isChanged();
}
 
void GlScene::swapPolygonMode()
{
 
    if (polygonMode == GL_POINT)
        polygonMode = GL_LINE;
    else if (polygonMode == GL_LINE)
        polygonMode = GL_FILL;
    else
        polygonMode = GL_POINT;
    isRedrawNeeded = true;
}
 
void GlScene::setPolygonMode(GLenum mode)
{
    polygonMode = mode;
}
 
GLenum GlScene::getPolygonMode()
{
    return polygonMode;
}
 
void GlScene::onResize(unsigned int width,unsigned int height)
{
    camera->setSize(width,height);
    isRedrawNeeded = true;
}
 
 
void GlScene::init(DrawableSet * set)
{
    this->version->init();
    this->set = set;
    this->set->init(this);
    this->isInitDone= true;
}
 
void GlScene::setFreeView()
{
    this->view=FREE_VIEW;
}
 
void GlScene::swapView()
{
    switch (this->view)
    {
    case FRONT_VIEW:
        this->view=UP_VIEW;
        break;
    case UP_VIEW:
        this->view=DOWN_VIEW;
        break;
    case DOWN_VIEW:
        this->view=LEFT_VIEW;
        break;
    case LEFT_VIEW:
        this->view=RIGHT_VIEW;
        break;
    case RIGHT_VIEW:
        this->view=BACK_VIEW;
        break;
    case BACK_VIEW:
        this->view=FRONT_VIEW;
        break;
    case FREE_VIEW:
        this->view=FRONT_VIEW;
        break;
    }
    Vec3f center=set->getCenter();
    camera->setBestView(this->view,center, set->getMaxSize());
    isRedrawNeeded = true;
}
 
const char * GlScene::getViewName()
{
    const char * name;
    switch (this->view)
    {
    case FRONT_VIEW:
        name="FRONT_VIEW";
        break;
    case UP_VIEW:
        name="UP_VIEW";
        break;
    case DOWN_VIEW:
        name="DOWN_VIEW";
        break;
    case LEFT_VIEW:
        name="LEFT_VIEW";
        break;
    case RIGHT_VIEW:
        name="RIGHT_VIEW";
        break;
    case BACK_VIEW:
        name="BACK_VIEW";
        break;
    case FREE_VIEW:
        name="FREE_VIEW";
        break;
    default:
        name="";
        break;
    }
    return name;
}
void GlScene::reset()
{
    camera->reset();
    isRedrawNeeded = true;
}
 
void GlScene::drawSelectionSquare(MouseKeybordState* state)
{
    float x1 = camera->XPixelToGl(state->xButtonPress);
    float y1 = camera->YPixelToGl(state->yButtonPress);
    float x2 = camera->XPixelToGl(state->xButtonMotion);
    float y2 = camera->YPixelToGl(state->yButtonMotion);
 
    glColor3f(0.5, 0.6, 0.3);
    glLineWidth(1);
    glBegin(GL_LINE_LOOP);
    glVertex2f(x1, y1);
    glVertex2f(x2, y1);
    glVertex2f(x2, y2);
    glVertex2f(x1, y2);
    glEnd();
}
 
 
void GlScene::draw()
{
 
    updateDepthBuffer();
    glColor3f(1, 1, 1);
    glLineWidth(1);
    stat->startScene();
    set->draw();
    stat->endScene();
    axes->draw(camera);
}
 
void GlScene::drawAll(MouseKeybordState* state)
{
 
    updateView();
    setOpenglPojectionMatrix();
 
    clearBuffer();
    loadIdentity();
 
    if (this->isTextInfo)
        drawText(state->xButtonMotion,state->yButtonMotion);
 
    pushMatrix();
    setOpenglModelViewMatrix();
    draw();
    popMatrix();
 
 
    if (state->isSelectionSquare)
    {
        glDisable(GL_DEPTH_TEST);
        drawSelectionSquare(state);
    }
}
 
void GlScene::drawTextOpengl()
{
    *canvas << "Opengl     : " << version->getGlVersionMain() << "." << version->getGlMinorVersionMain()<< "\n";
}
 
void GlScene::drawTextMinMax()
{
    *canvas << "min x      : " << set->getMinX() << "\n";
    *canvas << "max x      : " << set->getMaxX() << "\n";
    *canvas << "min y      : " << set->getMinY() << "\n";
    *canvas << "max y      : " << set->getMaxY() << "\n";
    *canvas << "min z      : " << set->getMinZ() << "\n";
    *canvas << "max z      : " << set->getMaxZ() << "\n";
}
 
void GlScene::drawTextZoom()
{
    Vec3f scale = camera->getScale();
    *canvas << "zoom x      : " << scale.getX() << "\n";
    *canvas << "zoom y      : " << scale.getY() << "\n";
    *canvas << "zoom z      : " << scale.getZ() << "\n";
}
 
void GlScene::drawTextCameraPosition()
{
    Vec3f p = camera->getPosition();
    *canvas << "Camera X   : " << p.getX() << "\n";
    *canvas << "Camera Y   : " << p.getY() << "\n";
    *canvas << "Camera Z   : " << p.getZ() << "\n";
}
 
void GlScene::drawTextMousePosition(int xButtonMotion,int yButtonMotion)
{
    float mouseX = camera->getWorldX(xButtonMotion);
    float mouseY = camera->getWorldY(yButtonMotion);
    *canvas << "x          : " << mouseX << "\n";
    *canvas << "y          : " << mouseY << "\n";
    *canvas << "r          : " << sqrt(mouseX * mouseX + mouseY * mouseY) << "\n";
}
 
void GlScene::drawTextPolygonMode()
{
    *canvas << "Polygon Mode :";
    int mode=getPolygonMode();
    if (mode==GL_POINT)
        *canvas << "Point\n";
    else if (mode==GL_LINE)
        *canvas << "Line\n";
    else
        *canvas << "Fill\n";
}
 
void GlScene::drawTextViewName()
{
    *canvas << "View :"  << getViewName() << "\n";
}
 
void GlScene::drawTextFps()
{
    *canvas << "Max FPS: " << 1.0/stat->getMeanTime() << "\n";
}
 
void GlScene::drawTextNbElement()
{
    *canvas << "Nb elements: " << set->getTotalElementNb() << "\n";
}
 
 
void GlScene::drawTextDepthBuffer()
{
    if (this->isDepthBufferEnable)
        *canvas << "Depth Buffer Enable\n";
    else
        *canvas << "Depth Buffer Disable\n";
}
 
 
void GlScene::drawText(int xButtonMotion,int yButtonMotion)
{
    canvas->setColor(0.5, 0.5, 0.7);
    canvas->resetLine();
    drawTextOpengl();
    drawTextMinMax();
    drawTextZoom();
    drawTextCameraPosition();
    drawTextMousePosition( xButtonMotion, yButtonMotion);
    drawTextViewName();
    drawTextPolygonMode();
    drawTextFps();
    drawTextNbElement();
    drawTextDepthBuffer();
}
 
void GlScene::drawText()
{
    canvas->setColor(0.5, 0.5, 0.7);
    canvas->resetLine();
    drawTextOpengl();
    drawTextMinMax();
    drawTextZoom();
    drawTextCameraPosition();
    drawTextPolygonMode();
    drawTextNbElement();
    drawTextDepthBuffer();
}
 
 
//Manage Draw operation
void GlScene::clearBuffer()
{
    glPolygonMode(GL_FRONT_AND_BACK, polygonMode);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
 
void GlScene::loadIdentity()
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
 
void  GlScene::updateView()
{
    if (set->isChanged())
    {
        set->consummeChanged();
        if (set->getMaxSize()>0)
        {
            axes->set(set);
            Vec3f center=set->getCenter();
            camera->setBestView(center, set->getMaxSize());
            camera->setInitial();
            isRedrawNeeded = true;
        }
    }
}
 
void  GlScene::updateDepthBuffer()
{
    if (this->isDepthBufferEnable)
    {
        glDepthFunc( GL_LESS);
        glEnable(GL_DEPTH_TEST);
    } else
        glDisable(GL_DEPTH_TEST);
}
 
void GlScene::setOpenglPojectionMatrix()
{
    camera->setOpenglPojectionMatrix();
}
 
void GlScene::setOpenglModelViewMatrix()
{
    camera->setOpenglModelViewMatrix();
}
 
void GlScene::pushMatrix() {
    glPushMatrix();
}
 
void GlScene::popMatrix() {
    glPopMatrix();
}
 
 
float GlScene::getXPixelSize()
{
    return camera->getXPixelSize();
}
 
float GlScene::getYPixelSize()
{
    return camera->getYPixelSize();
}