12 May 2012

OpenGL Startup Framework - Part 2

part 2: utilities
What else do we need to quickly start OpenGL coding? I suggest: shaders and logger
GLUT by default creates console window that is visible during application work. This can be disabled... but on the other hand it gives us simple tool that we can use to debug and test application.
Code for logger using printf:
/** logging will be by using simple "printf" function */ 

#define utLOG(msg, ...) { printf("%s: ", __FUNCTION__); printf(msg, __VA_ARGS__); printf("\n"); } 
#define utLOG_ERROR(msg, ...) { printf("ERR in %s, %d: ", __FUNCTION__, __LINE__); printf(msg, __VA_ARGS__); printf("\n"); } 
// TODO: implement 
and usage:
// displays function name and the message itself 
utLOG("Hello world... param = %d", param); 
// displays function name, line... and the message itselt 
utLOG_ERROR("cannot init application..."); 
The framework was quite ok, but I moved on and created a bit better version.
Please see: OpenGL Startup V3.0 for more information.

Previously we enabled shaders by using GLEW - it loads needed extensions. But somehow we need to load shader code and create shader programs. For a long time I've used lighthouse3D sample code to load shaders: link Instead of several functions, shader managers, etc, etc... I created only two functions:
/** loads whole program composed of vertex shader and fragment shader 
 *  @param vsFile file name for vertex shader 
 *  @param fsFile file name for fragment shader 
 *  @param progID id of new program 
 *  @return false on error and errors are written by LOG macros. 
bool utLoadAndBuildShader(const char *vsFile, const char *fsFile, GLuint *progID); 

/** internally utLoadAndBuildShaderProgram uses array of shaders ids, so at the end of application one should delete them by calling that function */ 
void utDeleteAllUsedShaders(); 
the second one (ulDelete...) is used to destroy all internally used shaders. Each shader program is composed of vertex and fragment shader (geometry shaders and others are not supported in this framework...). At the end user uses only shader program ID, so ids of vertex and fragment shaders are not so important. To make one life easier I remember all used ids and destroy them at the end of the application. Of course user needs to call that function in proper place! usage:
// in Init(...) GLuint shaderLight; 
// id of program 
if (utLoadAndBuildShader("data\\light.vert", "data\\light.frag", &shaderLight) == false) 
    return false; 

// the CleanUP function: 
void CleanUp() 
    // destroy all... 
    utDeleteAllUsedShaders(); // ... 
whole source code will be added in part 3

Interested in new blog posts and occasional updates? Please sign up for my free newsletter.

© 2017, Bartlomiej Filipek, Blogger platform
Any opinions expressed herein are in no way representative of those of my employers.
This site contains ads or referral links, which provide me with a commission. Thank you for your understanding.