C + + encapsulation of GLSL program objects

Source: Internet
Author: User
Tags fread rewind

In OpenGL shader programming, the most commonly used may be the program object, GLSL program object creation, loading shader and other steps are fixed, if each time write the same code, feel very waste of time, So now I am in the shader learning process of their own encapsulated Glslprogram class dedicated for your reference:


The header files are as follows:

/* * GLSLProgram.h * * Created * Author:zhouxuguang */#ifndef glslprogram_h_#define glslprogram_h_#include "Glpreco    Mpile.h "class Glslprogram {Public:glslprogram ();    Glslprogram (const char* pvertexsource, const char* pfragmentsource);        ~glslprogram ();        void Initwithshader (const char* pvertexsource, const char* pfragmentsource);  Compile and concatenate program void Linkprogram ();//load binary shader program void loadprogrambinary (const char * fileName, glenum format);//Use program object void        Use ();//using fixed pipeline void usefixedfunction ();//binding attribute variable void bindattriblocation (unsigned int index, const char *name); void bindfragdatalocation (unsigned int index, const char *name);//Get attribute Variable Glint getattributelocation (const char* Attrname );//Obtain the position of the specified uniform variable Glint getuniformlocation (const char* uniname); Glint Getuniformblockindex (const char* uniname);//Set the value of the property variable//void setattributevariable (char *, int); void setattributevariable (const char *, float); void setattributevariable (const char *, float, float, float); VOID setattributevariable (const char *, float[3]);//Set the value of the uniform variable void setuniformvariable (const char *, int); void setunif ormvariable (const char *, float), void setuniformvariable (const char *, float, float, float), void setuniformvariable (c Onst char *, float[3]); void setuniformmatrix2f (const char* name,int count, bool transpose, const float *value); void Setun iformmatrix3f (const char* name,int count, bool transpose, const float *value); void setuniformmatrix4f (const char* Name,in T count, bool transpose, const float *value), void Getactiveuniformblockiv (const char* uniname, Glenum pname, Glint *params ) void Getuniformindices (Glsizei uniformcount, const Glchar *const*uniformnames, Gluint *uniformindices); void        Getactiveuniformsiv (Glsizei uniformcount, const gluint *uniformindices, Glenum pname, Glint *params);        Load shader static Gluint Loadshader (Glenum shadertype, const char* psource); static bool Loadshaderfile (const char* pshaderfile,std::string &shaderstring);p rivate:gluint mprogram;//Program Object Gluint mvertexshader;     Vertex shader Gluint Mfragmentshader; Fragment Shaderstd::map<const char *, int>mattributelocs;//attribute attribute variable mapstd::map<const char *, int> Map} of the muniformlocs;//uniform variable; #endif/* Glslprogram_h_ */


The implementation file is as follows:

/* * GLSLProgram.cpp * * Created on: * Author:zhouxuguang */#include "GLSLProgram.h" Gluint Glslprogram::loadshader (Glenum shadertype, const char* Psource)    {Gluint shader = Glcreateshader (Shadertype);        if (shader) {Glshadersource (shader, 1, &psource, NULL);        Glcompileshader (shader);        Glint compiled = 0;        Glgetshaderiv (shader, Gl_compile_status, &compiled);            if (!compiled) {Glint Infolen = 0;            Glgetshaderiv (shader, Gl_info_log_length, &infolen);                if (Infolen) {char* buf = (char*) malloc (Infolen);                    if (buf! = null) {Glgetshaderinfolog (shader, Infolen, NULL, BUF);                    LOGE ("Could not compile shader%d:\n%s\n", Shadertype, BUF);                Free (BUF);                } gldeleteshader (shader);            Shader = 0; }}} return shader;} BOOL Glslprogram::loadshaderfile (const char *pshaderfiLe,std::string &shaderstring) {FILE *fpin = fopen (Pshaderfile, "RB");    if (Fpin = = NULL) {return false;    } fseek (Fpin, 0, seek_end);    Long length = (long) ftell (Fpin);    Char *buffer = new char[length];    Rewind (Fpin);    Fread (buffer, length, 1, fpin);        Fclose (Fpin);    shaderstring = buffer;    delete []buffer; return true;} Glslprogram::glslprogram (): Mvertexshader (0), Mfragmentshader (0), Mprogram (0) {}glslprogram::glslprogram (const char * Pvertexsource, const char* pfragmentsource) {initwithshader (pvertexsource,pfragmentsource);}        Glslprogram::~glslprogram () {if (Mprogram) {Gldetachshader (Mprogram, Mvertexshader);        Gldetachshader (Mprogram, Mfragmentshader);        Gldeleteshader (Mvertexshader);        Mvertexshader = 0;        Gldeleteshader (Mfragmentshader);                Mfragmentshader = 0;        Gldeleteprogram (Mprogram);    Mprogram = 0; }}void glslprogram::initwithshader (const char *pvertexsource, conSt Char *pfragmentsource) {Mvertexshader = Glslprogram::loadshader (Gl_vertex_shader, Pvertexsource);    if (!mvertexshader) {return;    } Mfragmentshader = Glslprogram::loadshader (Gl_fragment_shader, Pfragmentsource);    if (!mfragmentshader) {return; } Mprogram = Glcreateprogram ();}        void Glslprogram::linkprogram () {if (Mprogram) {Glattachshader (Mprogram, Mvertexshader);        Glattachshader (Mprogram, Mfragmentshader);        Gllinkprogram (Mprogram);        Glint linkstatus = Gl_false;        GLGETPROGRAMIV (Mprogram, Gl_link_status, &linkstatus);            if (linkstatus! = gl_true) {Glint buflength = 0;            GLGETPROGRAMIV (Mprogram, Gl_info_log_length, &buflength);                if (buflength) {char* buf = (char*) malloc (buflength);                    if (BUF) {Glgetprograminfolog (Mprogram, Buflength, NULL, buf);                Free (BUF); }} GldeLeteprogram (Mprogram);        Mprogram = 0; }}}void glslprogram::loadprogrambinary (const char * fileName, glenum format) {FILE *fpin = fopen (FileName, "RB"); Fpin = = NULL) {fprintf (stderr, "Cannot open input GLSL binary file '%s ' \ n", fileName); return;} Fseek (fpin, 0, seek_end); Glint length = (glint) ftell (Fpin); Glubyte *buffer = new glubyte[length];rewind (fpin); fread (buffer, length, 1, Fpin); fclose (Fpin); glprogrambinary (th Is->mprogram, format, buffer, length);d elete [] buffer;  Glint Success;glgetprogramiv (This->mprogram, Gl_link_status, &success); if (!success) {fprintf (stderr, "did not Successfully load the GLSL binary file '%s ' \ n ", fileName); return;}} void Glslprogram::use () {Gluseprogram (mprogram);} void Glslprogram::usefixedfunction () {gluseprogram (0);} void glslprogram::bindattriblocation (unsigned int index, const char *name) {glbindattriblocation (Mprogram, (Gluint) Index, (const glchar*) name);} void Glslprogram::bindfragdatalocation (unsignedint index, const char *name) {//Only desktop version supports}glint glslprogram::getattributelocation (const char* attrname) {Std::map<con St char *, int>::iterator iter = Mattributelocs.find (attrname); if (iter = = Mattributelocs.end ()) {mattributelocs[ Attrname] = glgetattriblocation (Mprogram, attrname);} return mattributelocs[attrname];} Glint glslprogram::getuniformlocation (const char* uniname) {std::map<const char*, int>::iterator iter = Muniformlocs.find (Uniname), if (iter = = Muniformlocs.end ()) {Muniformlocs[uniname] = glgetuniformlocation (MProgram, Uniname);} return muniformlocs[uniname];} Glint Glslprogram::getuniformblockindex (const char* uniname) {return Glgetuniformblockindex (Mprogram, uniName);}  void Glslprogram::setattributevariable (char* name, int val)//{//int loc;//if (loc = getattributelocation (name)) >= 0)//{//this->use ();//glvertexattrib1i (Loc, Val);//}//};void glslprogram::setattributevariable (const char* Name, float val) {int loc;if (loc = getattributelocation (name)) >= 0) {this->use (); glvertexattrib1f (Loc, val);}}; void glslprogram::setattributevariable (const char* name, float val0, float val1, float val2) {int loc;if (loc = GetAttr Ibutelocation (name)) >= 0) {this->use (); glvertexattrib3f (Loc, val0, Val1, Val2);}};  void glslprogram::setattributevariable (const char* name, float vals[3]) {int loc;if (loc = getattributelocation (name) ) >= 0) {this->use (); GLVERTEXATTRIB3FV (Loc, vals);}; void glslprogram::setuniformvariable (const char* name, int val) {int loc;if (loc = getuniformlocation (name)) >= 0) {this->use (); Gluniform1i (Loc, val);}}; void glslprogram::setuniformvariable (const char* name, float val) {int loc;if (loc = getuniformlocation (name)) > = 0) {this->use (); gluniform1f (Loc, val);}}; void glslprogram::setuniformvariable (const char* name, float val0, float val1, float val2) {int loc;if (loc = Getunifor Mlocation (name)) >= 0) {this->use (); gluniform3f (Loc, val0, Val1,VAL2);}};  void glslprogram::setuniformvariable (const char* name, float vals[3]) {int loc;if (loc = getuniformlocation (name)) >= 0) {this->use (); GLUNIFORM3FV (Loc, 3, vals);}; void glslprogram::setuniformmatrix2f (const char* name, int count, bool transpose, const float *value) {int loc;if (loc = Getuniformlocation (name)) >= 0) {this->use (); GLUNIFORMMATRIX2FV (Loc, Count, transpose, value);}} void glslprogram::setuniformmatrix3f (const char* name, int count, bool transpose, const float *value) {int loc;if (loc = Getuniformlocation (name)) >= 0) {this->use (); GLUNIFORMMATRIX3FV (Loc, Count, transpose, value);}} void glslprogram::setuniformmatrix4f (const char* name, int count, bool transpose, const float *value) {int loc;if (loc = Getuniformlocation (name)) >= 0) {this->use (); GLUNIFORMMATRIX4FV (Loc, Count, transpose, value);}} void Glslprogram::getactiveuniformblockiv (const char* uniname, Glenum pname, glint *params) {int loc;if (loc = GetuniforMblockindex (uniname)) >= 0) {this->use (); Glgetactiveuniformblockiv (Mprogram,loc,pname,params);}} void Glslprogram::getuniformindices (Glsizei uniformcount, const Glchar *const*uniformnames, Gluint *uniformIndices) { Glgetuniformindices (mprogram,uniformcount,uniformnames,uniformindices);} void Glslprogram::getactiveuniformsiv (Glsizei uniformcount, const gluint *uniformindices, Glenum pname, GLint *params) { Glgetactiveuniformsiv (mprogram,uniformcount,uniformindices,pname,params);}

Here also introduced a header file, in fact, the introduction of the corresponding header file on different platforms, the header file content is as follows:

  glprecompile.h//  glutilbox////  Created by Zhouxuguang.//  copyright©2016 year Zhouxuguang. All rights reserved.//#ifndef Glprecompile_hpp#define glprecompile_hpp#include <stdio.h> #include <stdlib.h > #include <string.h> #include <string> #include <vector> #include <map> #ifdef __android__# Include <GLES/gl.h> #include <GLES2/gl2.h> #include <GLES3/gl3.h> #elif defined (__ios__) #include <OpenGLES/ES1/gl.h> #include <OpenGLES/ES1/glext.h> #include <OpenGLES/ES2/gl.h> #include < opengles/es2/glext.h> #include <OpenGLES/ES3/gl.h> #include <OpenGLES/ES3/glext.h> #endif #endif/* GLPRECOMPILE_HPP * *

Hope to be useful to everyone, desktop version of the OpenGL header file is not processed, we need to also can change according to their own needs OK

C + + encapsulation of GLSL program objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.