Gl Graph

Gl Graph Source of glrenderer.cpp


#include <stdio.h>
#define GLX_GLXEXT_PROTOTYPES
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glx.h>
#include <GL/glxext.h>
#include <GL/glut.h>
 
#include <stdio.h>
#include <iostream>
#include <strings.h>
#include <png.h>
 
#include "glrenderer.h"
GlRenderer::GlRenderer(unsigned int width, unsigned int height)
{
    this->width=width;
    this->height=height;
    this->debug=false;
}
GlRenderer::GlRenderer(unsigned int width, unsigned int height, bool debug)
{
    this->width=width;
    this->height=height;
    this->debug=debug;
}
 
 
 
void GlRenderer::printGlError(const char *msg)
{
    if (debug)
    {
        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);
    }
}
 
 
 
 
#ifndef  NO_PNG
/**
 * error function handling
 */
 
static void user_error_ptr()
{
    printf("user_error_ptr\n");
}
static void user_error_fn(png_struct_def*, const char* s)
{
    printf("user_error_fn :%s\n",s);
}
static void user_warning_fn(png_struct_def*, const char* s)
{
    printf("user_warning_fn:%s\n",s);
}
#endif
 
 
void GlRenderer::writePng()
{
#ifdef  NO_PNG
    printf("PNG not activated");
#else
    unsigned int imageSize=this->width*this->height*3;
    unsigned char * data= new unsigned char [ imageSize];
 
    bzero(data,imageSize);
 
    glReadPixels(0, 0,  this->width , this->height, GL_RGB,  GL_UNSIGNED_BYTE, ( void *) data );
 
    std::string filename=getPngFileName();
    FILE *fp = fopen(filename.c_str(), "wb");
    if (!fp)
    {
        printf(" fopen failed");
        return;
    }
 
    png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,user_error_fn, user_warning_fn);
    if (!png_ptr)
    {
        printf(" png_ptr failed");
        return;
    }
 
    png_infop info_ptr = png_create_info_struct(png_ptr);
    if (!info_ptr)
    {
        printf(" info_ptr failed");
        png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
        return;
    }
    png_init_io(png_ptr, fp);
    png_set_IHDR(png_ptr, info_ptr, width, height,8, PNG_COLOR_TYPE_RGB,  PNG_INTERLACE_NONE ,PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
 
    png_byte ** row_pointers=new png_byte* [height];
 
    for (unsigned int i=0; i<height; i++)
        row_pointers[i]=&data[3*width*(height-1-i)];
 
    png_set_rows(png_ptr, info_ptr,row_pointers);
    png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
    delete data;
    fclose(fp);
#endif
}