Gl Graph

Gl Graph Source of globjectdata.cpp


#include "globjectdata.h"
#include "iostream"
 
GlObjectData::GlObjectData()
{
    this->dataKind=GL_LINE_STRIP;
    this->args=NULL;
    reset();
}
 
 
GlObjectData::GlObjectData(GLenum dataKind,std::unordered_map<std::string, std::string> * args)
{
    this->dataKind=dataKind;
    this->args=args;
    reset();
}
 
void GlObjectData::reset()
{
    this->bufferDataSize=0;
    this->isNormalDataChanged=false;
    this->isColorDataChanged=false;
    this->isVectexDataChanged=false;
 
    if (this->args==NULL)
    {
        this->isVaoForced=false;
        this-> isVboForced=false;
        this->isVaForced=false;
    }
    else
    {
        this->isVaoForced=this->args->count("--VaoForced") == 1;
        this-> isVboForced=this->args->count("--VboForced") == 1;
        this->isVaForced=this->args->count("--VaForced") == 1;
    }
}
 
void GlObjectData::internalDraw()
{
    updateData();
    int s = vertexdata.size() / 3;
    glDrawArrays(dataKind, 0, s);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}
 
void GlObjectData::draw()
{
    if (!vertexdata.empty())
        internalDraw();
}
 
unsigned int GlObjectData::getNbVertex() const
{
    return vertexdata.size();
}
unsigned int GlObjectData::getNbColorVertex() const
{
    return colordata.size();
}
unsigned int GlObjectData::getNbNormalVertex() const
{
    return normaldata.size();
}
 
 
void GlObjectData::initGl3_3_0()
{
    // Use a Vertex Array Object
    glGenVertexArrays(1, &vao);
    glGenBuffers(1, &vbo);
}
void GlObjectData::initGl2()
{
    glGenBuffers(1, &vbo);
}
void GlObjectData::initGlOld()
{
}
 
void GlObjectData::init(GlVersion * glVersion)
{
    this->glVersion=glVersion;
 
    if (    this->isVaoForced|| this-> isVboForced|| this->isVaForced)
    {
        if (this->isVaoForced)
            initGl3_3_0();
        else if (this-> isVboForced)
            initGl2();
        else if (this->isVaForced)
            initGlOld();
    }
    else
    {
        if (glVersion->isOpenGl3_3())
            initGl3_3_0();
        else if (glVersion->isOpenGl2())
            initGl2();
        else
            initGlOld();
    }
}
 
void GlObjectData::updateData()
{
    if ( this->isVaoForced|| this-> isVboForced|| this->isVaForced)
    {
        if (this->isVaoForced)
            updateDataGl3_3_0();
        else if (this-> isVboForced)
            updateDataGl2();
        else if (this->isVaForced)
            updateDataGlOld();
    }
    else
    {
        if (this->glVersion->isOpenGl3_3())
            updateDataGl3_3_0();
        else if (this->glVersion->isOpenGl2())
            updateDataGl2();
        else
            updateDataGlOld();
    }
}
 
void GlObjectData::updateDataGl3_3_0()
{
 
    //std::cout << "updateDataGl3_3_0 vao "<< vao << " vbo "<< vbo  << std::endl;
    int vertexSize = vertexdata.size() * sizeof (float);
    int normalSize = normaldata.size() * sizeof (float);
    int colorSize = colordata.size() * sizeof (float);
    int bufferSize = vertexSize + normalSize + colorSize;
 
    bool globalSizeChanged =this->bufferDataSize != bufferSize;
    bool normalSizeChanged =this->normalDataSize != normalSize;
    bool colorSizeChanged  =this->colorDataSize != colorSize;
    bool vectexSizeChanged =this->vectexDataSize != vertexSize;
    bool sizeChanged =normalSizeChanged||colorSizeChanged||vectexSizeChanged;
 
    bool dataChanged =isNormalDataChanged || isColorDataChanged || isVectexDataChanged;
    bool updateNeeded = dataChanged || sizeChanged;
 
    glBindVertexArray(vao);
 
    if (updateNeeded)
    {
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
 
        glBufferData(GL_ARRAY_BUFFER, vertexSize, vertexdata.data(), GL_STATIC_DRAW);
//         glVertexAttribPointer(0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
//                               3, // size
//                               GL_FLOAT, // type
//                               GL_FALSE, // normalized?
//                               0, // stride
//                               (void*) 0 // array buffer offset
//                              );
        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(3, GL_FLOAT, 0, 0);
        //glEnableVertexAttribArray(0);
    }
 
}
 
void GlObjectData::updateDataGl2()
{
    int vertexSize = vertexdata.size() * sizeof (float);
    int normalSize = normaldata.size() * sizeof (float);
    int colorSize = colordata.size() * sizeof (float);
    int bufferSize = vertexSize + normalSize + colorSize;
 
    bool globalSizeChanged =this->bufferDataSize != bufferSize;
    bool normalSizeChanged =this->normalDataSize != normalSize;
    bool colorSizeChanged  =this->colorDataSize != colorSize;
    bool vectexSizeChanged =this->vectexDataSize != vertexSize;
    bool sizeChanged =normalSizeChanged||colorSizeChanged||vectexSizeChanged;
 
    bool dataChanged =isNormalDataChanged || isColorDataChanged || isVectexDataChanged;
    bool updateNeeded = dataChanged || sizeChanged;
 
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    printGlError("glBindBuffer");
 
 
 
    if (updateNeeded)
    {
        if (globalSizeChanged)
        {
            glBufferData(GL_ARRAY_BUFFER, bufferSize, NULL, GL_STATIC_DRAW);
            printGlError("glBufferData");
            this->bufferDataSize = bufferSize;
        }
 
        if (vectexSizeChanged||isVectexDataChanged)
        {
            //std::cout << "glBufferSubData vertexdata"   << std::endl;
            glBufferSubData(GL_ARRAY_BUFFER, 0, vertexSize, vertexdata.data());
            printGlError("glBufferSubData1");
            isVectexDataChanged=false;
            this->vectexDataSize=vertexSize;
        }
 
        if (normalSize>0)
        {
            if( normalSizeChanged|| isNormalDataChanged)
            {
                //std::cout << "glBufferSubData normaldata"   << std::endl;
                glBufferSubData(GL_ARRAY_BUFFER, vertexSize, normalSize, normaldata.data());
                printGlError("glBufferSubData2");
                isNormalDataChanged=false;
                this->normalDataSize=normalSize;
            }
        }
 
        if (colorSize>0)
        {
            if( colorSizeChanged|| isColorDataChanged)
            {
                //std::cout << "glBufferSubData colordata"   << std::endl;
                glBufferSubData(GL_ARRAY_BUFFER, vertexSize+normalSize, colorSize, colordata.data());
                printGlError("glBufferSubData3");
                isColorDataChanged=false;
                this->colorDataSize=colorSize;
            }
        }
    }
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, 0);
    if (normalSize>0)
    {
        glNormalPointer(GL_FLOAT, 0, (void*) vertexSize);
        glEnableClientState(GL_NORMAL_ARRAY);
    }
    else
        glDisableClientState(GL_NORMAL_ARRAY);
 
    if (colorSize>0)
    {
        glColorPointer(3,GL_FLOAT, 0, (void*) (vertexSize+normalSize));
        glEnableClientState(GL_COLOR_ARRAY);
    }
    else
        glDisableClientState(GL_COLOR_ARRAY);
 
 
}
 
void GlObjectData::updateDataGlOld()
{
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, vertexdata.data());
    if (!normaldata.empty())
    {
        glNormalPointer(GL_FLOAT, 0, normaldata.data());
        glEnableClientState(GL_NORMAL_ARRAY);
    }
    else
        glDisableClientState(GL_NORMAL_ARRAY);
    if (!colordata.empty())
    {
        glColorPointer(3, GL_FLOAT, 0, colordata.data());
        glEnableClientState(GL_COLOR_ARRAY);
    }
    else
        glDisableClientState(GL_COLOR_ARRAY);
}
 
 
void GlObjectData::addNormalData(float val)
{
    normaldata.push_back(val);
}
void GlObjectData::addColorData(float val)
{
    colordata.push_back(val);
}
void GlObjectData::addVertexData(float val)
{
    vertexdata.push_back(val);
}
 
void GlObjectData::addNormalData(float val1,float val2,float val3)
{
    normaldata.push_back(val1);
    normaldata.push_back(val2);
    normaldata.push_back(val3);
}
void GlObjectData::addColorData(float val1,float val2,float val3)
{
    colordata.push_back(val1);
    colordata.push_back(val2);
    colordata.push_back(val3);
}
void GlObjectData::addVertexData(float val1,float val2,float val3)
{
    vertexdata.push_back(val1);
    vertexdata.push_back(val2);
    vertexdata.push_back(val3);
}
 
void GlObjectData::addNormalData(Vec3f &vec)
{
    addNormalData(vec.getX(),vec.getY(),vec.getZ());
}
 
void GlObjectData::addColorData(Vec3f &vec)
{
    addColorData(vec.getX(),vec.getY(),vec.getZ());
}
 
void GlObjectData::addVertexData(Vec3f &vec)
{
    addVertexData(vec.getX(),vec.getY(),vec.getZ());
}
 
void GlObjectData::setNormalData(int index,Vec3f &vec)
{
    normaldata[index]=vec.getX();
    normaldata[index+1]=vec.getY();
    normaldata[index+2]=vec.getZ();
    isNormalDataChanged=true;
}
 
void GlObjectData::setColorData(int index,Vec3f &vec)
{
    colordata[index]=vec.getX();
    colordata[index+1]=vec.getY();
    colordata[index+2]=vec.getZ();
    isColorDataChanged=true;
}
 
void GlObjectData::setVertexData(int index,Vec3f &vec)
{
    vertexdata[index]=vec.getX();
    vertexdata[index+1]=vec.getY();
    vertexdata[index+2]=vec.getZ();
    isVectexDataChanged=true;
}
 
 
 
 
void GlObjectData::printGlError(const char *msg)
{
    GLenum err=glGetError();
    if (err==GL_NO_ERROR) return;
    else if (err==GL_INVALID_ENUM) printf("%s :GL_INVALID_ENUM\n",msg);
    else if (err==GL_INVALID_VALUE) printf("%s :GL_INVALID_VALUE\n",msg);
    else if (err==GL_INVALID_OPERATION) printf("%s :GL_INVALID_OPERATION\n",msg);
    else if (err==GL_INVALID_FRAMEBUFFER_OPERATION) printf("%s :GL_INVALID_FRAMEBUFFER_OPERATION\n",msg);
    else if (err==GL_OUT_OF_MEMORY) printf("%s :GL_OUT_OF_MEMORY\n",msg);
    else if (err==GL_STACK_UNDERFLOW) printf("%s :GL_STACK_UNDERFLOW\n",msg);
    else if (err==GL_STACK_OVERFLOW) printf("%s :GL_STACK_OVERFLOW\n",msg);
    else printf("%s :ERR %i\n",msg,err);
}
 
GlLineData::GlLineData(std::unordered_map<std::string, std::string> * args)  : GlObjectData(GL_LINES,args)
{
}
 
void GlLineData::addLineNormalData(GlNormalLine &normalLine)
{
    addNormalData(normalLine.normalPts1);
    addNormalData(normalLine.normalPts2);
}
void GlLineData::addLineColorData(GlColorLine &colorLine)
{
    addColorData(colorLine.colorPts1);
    addColorData(colorLine.colorPts2);
}
void GlLineData::addLineVertexData(GlLine &line)
{
    addVertexData(line.pts1);
    addVertexData(line.pts2);
}
 
 
GlLineStripData::GlLineStripData(std::unordered_map<std::string, std::string> * args)  : GlObjectData(GL_LINE_STRIP,args)
{
}
void GlLineStripData::addLineNormalData(Vec3f &vec1)
{
    addNormalData(vec1);
}
void GlLineStripData::addLineColorData(Vec3f &vec1)
{
    addColorData(vec1);
}
void GlLineStripData::addLineVertexData(Vec3f &vec1)
{
    addVertexData(vec1);
}
 
 
 
 
GlTriangleData::GlTriangleData(std::unordered_map<std::string, std::string> * args)  : GlObjectData(GL_TRIANGLES,args)
{
}
void GlTriangleData::addTriangleNormalData(GlNormalTriangle &normalTriangle)
{
    addNormalData(normalTriangle.normalPts1);
    addNormalData(normalTriangle.normalPts2);
    addNormalData(normalTriangle.normalPts3);
}
void GlTriangleData::addTriangleColorData(GlColorTriangle &colorTriangle)
{
    addColorData(colorTriangle.colorPts1);
    addColorData(colorTriangle.colorPts2);
    addColorData(colorTriangle.colorPts3);
}
void GlTriangleData::addTriangleVertexData(GlTriangle &triangle)
{
    addVertexData(triangle.pts1);
    addVertexData(triangle.pts2);
    addVertexData(triangle.pts3);
}
 
 
 
GlQuadData::GlQuadData(std::unordered_map<std::string, std::string> * args)  : GlObjectData(GL_QUADS,args)
{
}
void GlQuadData::addQuadNormalData(GlNormalQuad &normalQuad)
{
    addNormalData(normalQuad.normalPts1);
    addNormalData(normalQuad.normalPts2);
    addNormalData(normalQuad.normalPts3);
    addNormalData(normalQuad.normalPts4);
}
void GlQuadData::addQuadColorData(GlColorQuad &colorQuad)
{
    addColorData(colorQuad.colorPts1);
    addColorData(colorQuad.colorPts2);
    addColorData(colorQuad.colorPts3);
    addColorData(colorQuad.colorPts4);
}
void GlQuadData::addQuadVertexData(GlQuad &quad)
{
    indexdata.push_back(vertexdata.size());
    addVertexData(quad.pts1);
    addVertexData(quad.pts2);
    addVertexData(quad.pts3);
    addVertexData(quad.pts4);
}
 
 
 
void GlQuadData::setQuadNormalData(int index,GlNormalQuad &normalQuad)
{
    int vertexIndex=indexdata[index];
    setNormalData(vertexIndex,normalQuad.normalPts1);
    setNormalData(vertexIndex+3,normalQuad.normalPts2);
    setNormalData(vertexIndex+6,normalQuad.normalPts3);
    setNormalData(vertexIndex+9,normalQuad.normalPts4);
}
void GlQuadData::setQuadColorData (int index,GlColorQuad &colorQuad)
{
    int vertexIndex=indexdata[index];
    setColorData(vertexIndex,colorQuad.colorPts1);
    setColorData(vertexIndex+3,colorQuad.colorPts2);
    setColorData(vertexIndex+6,colorQuad.colorPts3);
    setColorData(vertexIndex+9,colorQuad.colorPts4);
}
void GlQuadData::setQuadVertexData(int index,GlQuad &quad)
{
    int vertexIndex=indexdata[index];
    setVertexData(vertexIndex,quad.pts1);
    setVertexData(vertexIndex+3,quad.pts2);
    setVertexData(vertexIndex+6,quad.pts3);
    setVertexData(vertexIndex+9,quad.pts4);
}