Gl Graph

Gl Graph Source of glshader.cpp


#include "glshader.h"
#include "iostream"
#include "stdio.h"
#include <string.h>
 
static void 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);
}
 
 
GLuint GlShader::initFragmentShaders()
{
    GLuint ProgramFragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    printGlError("glCreateShader GL_FRAGMENT_SHADER");
 
 
    static const GLchar* source[] =
    {
        "varying float attenuation;\n"
        "varying vec4 position;\n"
        "varying float skipProcessing;\n"
        "uniform float squaresize;\n"
        "uniform float minz;\n"
        "uniform float maxz;\n"
        "\n"
        "\n"
        "\n"
        "void main(void)\n"
        "{\n"
        " float a=fract (position.x*2.0*squaresize);\n"
        " float b=fract (position.y*2.0*squaresize);\n"
        " float blue=(position.z-minz)/(maxz-minz);\n"
        " vec4  c=(1.0-skipProcessing)*vec4(0.6,1,blue,1.0)+skipProcessing*vec4(0.1,0.4,blue,1.0);\n"
        " vec4 color;\n"
        " if ( a>0.5 && b>0.5)\n"
        "    color   = attenuation*c;\n"
        " else if ( a<0.5 && b>0.5)\n"
        "    color   = attenuation*vec4(1.0,1.0,1.0,1.0);\n"
        " else if ( a>0.5 && b<0.5)\n"
        "    color   = attenuation*vec4(1.0,1.0,1.0,1.0);\n"
        " else\n"
        "    color   = attenuation*c;\n"
        "gl_FragColor   = color;\n"
 
        "}\n"
    };
 
 
    glShaderSource(ProgramFragmentShader, 1, (const GLcharARB **) &source,NULL);
    glCompileShader(ProgramFragmentShader);
    int compiled = GL_FALSE;
    glGetShaderiv(ProgramFragmentShader, GL_COMPILE_STATUS, &compiled);
    if (compiled==GL_FALSE)
    {
        std::cout << "FragmentShader compilation error! Check compiler log!" << std::endl;
        printCompilerLog(ProgramFragmentShader);
        printSource(source[0]);
    }
    return ProgramFragmentShader;
}
 
GLuint GlShader::initVertexShaders()
{
    GLuint ProgramVertexShader = glCreateShader(GL_VERTEX_SHADER);
    printGlError("glCreateShader GL_VERTEX_SHADER");
 
    static const GLchar* source[] =
    {
        "varying float attenuation;\n"
        "varying vec4 position;\n"
        "varying float skipProcessing;\n"
        "uniform float minx;\n"
        "uniform float maxx;\n"
        "uniform float miny;\n"
        "uniform float maxy;\n"
        "uniform float minz;\n"
        "uniform float maxz;\n"
 
        "uniform float size;\n"
        "\n"
        "\n"
        "void main(void)\n"
        "{\n"
        "   if (length(gl_Color.rgb)>0.01)\n"
        "   {\n"
        "   skipProcessing=1.0f;\n"
        "   }\n"
        "   else\n"
        "   {\n"
        "   skipProcessing=0.0f;\n"
        "   }\n"
        "   gl_FrontColor=gl_Color;\n"
        "   vec4 pos =   gl_ModelViewMatrix* gl_Vertex;\n"
        "   vec4 boxvectmin =  gl_ModelViewMatrix*vec4(minx,miny,minz,1.0);\n"
        "   vec4 boxvectmax =  gl_ModelViewMatrix*vec4(maxx,maxy,maxz,1.0);\n"
        "   float truesize =  abs(boxvectmin.z)+abs(boxvectmax.z);\n"
        "   vec4 posLight =  vec4(0.0,0.0,truesize,1.0);\n"
        "   float distance = length(vec3(pos)-vec3(posLight));\n"
        "   float k1= (truesize)/(distance+0.1);\n"
        "   vec3 normal = normalize(gl_NormalMatrix * gl_Normal);\n"
        "   float k2= abs(dot(normalize(vec3(posLight)),normal));\n"
        "   attenuation= (k1+0.15)*(k2+0.15);\n"
        "   position=gl_Vertex;\n"
        "   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
        "}\n"
    };
 
    glShaderSource(ProgramVertexShader, 1, (const GLcharARB **) &source, NULL);
    glCompileShader(ProgramVertexShader);
    int compiled = GL_FALSE;
    glGetShaderiv(ProgramVertexShader, GL_COMPILE_STATUS, &compiled);
    if (compiled==GL_FALSE)
    {
        std::cout << "VertexShader compilation error! Check compiler log!" << std::endl;
        printCompilerLog(ProgramVertexShader);
        printSource(source[0]);
 
    }
    return ProgramVertexShader;
 
}
 
 
void GlShader::printSource(const GLchar* source)
{
    unsigned int lenght= strlen(source);
    unsigned int lineNb= 1;
    std::cout  << lineNb << ":";
    for (unsigned int i=0; i<lenght; i++)
        if (source[i]=='\n')
        {
            lineNb++;
            std::cout  << std::endl;
            std::cout  << lineNb << ":";
        } else
            std::cout <<  source[i];
}
 
void GlShader::printCompilerLog(GLuint programObject)
{
    if (programObject!=0)
    {
        int blen = 0;
        if (this->glVersion->isOpenGl3_3())
            glGetProgramiv(programObject,GL_INFO_LOG_LENGTH  , &blen);
        else
            glGetObjectParameterivARB(programObject,GL_OBJECT_INFO_LOG_LENGTH_ARB  , &blen);
 
        if (blen > 1)
        {
            GLchar *compiler_log= (GLchar*)malloc(blen);
            if (compiler_log!=NULL)
            {
                int slen = 0;
                if (this->glVersion->isOpenGl3_3())
                    glGetProgramInfoLog(programObject, blen, &slen, compiler_log);
                else
                    glGetInfoLogARB(programObject, blen, &slen, compiler_log);
                std::cout << "compiler_log: " << compiler_log;
                free(compiler_log);
            }
        }
        else
            std::cout << "No log available"  << std::endl;
    }
}
 
 
void GlShader::init(GlVersion * glVersion)
{
    this->glVersion=glVersion;
    if (this->glVersion->isOpenGl2())
    {
        GLuint fragmentShader = initFragmentShaders();
        GLuint vertexShader = initVertexShaders();
 
        this->shaderObject = glCreateProgram();
        glAttachShader(this->shaderObject, fragmentShader);
        glAttachShader(this->shaderObject, vertexShader);
 
        glLinkProgram(this->shaderObject);
        int linked=GL_FALSE;
        glGetProgramiv(this->shaderObject, GL_LINK_STATUS, &linked);
        if (linked==GL_FALSE)
        {
            std::cout << "Linker error" << std::endl;
        }
        initDone=true;
    }
    else
        std::cout << "Need OpenGl 2.0 for shader" << std::endl;
}
 
 
void GlShader::setUniform(float size,float squaresize)
{
    glUniform1f(glGetUniformLocation(this->shaderObject, "size"), size);
    glUniform1f(glGetUniformLocation(this->shaderObject, "squaresize"), squaresize);
}
 
void GlShader::setMinMaxz(float minz,float maxz)
{
    glUniform1f(glGetUniformLocation(this->shaderObject, "minz"), minz);
    glUniform1f(glGetUniformLocation(this->shaderObject, "maxz"), maxz);
}
void GlShader::setMinMaxx(float minx,float maxx)
{
    glUniform1f(glGetUniformLocation(this->shaderObject, "minx"), minx);
    glUniform1f(glGetUniformLocation(this->shaderObject, "maxx"), maxx);
}
void GlShader::setMinMaxy(float miny,float maxy)
{
    glUniform1f(glGetUniformLocation(this->shaderObject, "miny"), miny);
    glUniform1f(glGetUniformLocation(this->shaderObject, "maxy"), maxy);
}
 
void GlShader::enable()
{
    glUseProgram(this->shaderObject);
}
 
void GlShader::disable() {
    glUseProgram(0);
}