Michael,
I don’t know if you have a German version, but in my book on pp36-40 there is a chapter on singletons. I use the method titled “An Even Better Way” and just use that to create subclasses. For example, I have a class called glStateMgr defined like this:
#ifndef _GL_STATE_MGR_H
#define _GL_STATE_MGR_H
#ifdef _WIN32
#include <windows.h>
#endif
// Surround OpenGL C stuff with an extern declaration.
#ifdef _cplusplus
extern “C” {
#endif
#include <GL/gl.h>
#ifdef _cplusplus
};
#endif
// Include the singleton base class header.
#include “singleton.h”
// OpenGL state manager class. It is much faster to manually keep track of
// state variables than to use glGet* functions (very slow) or to redundantly
// update state variables with their current value (slow).
class glStateMgr : Singleton {
public:
// Constructor/destructor.
glStateMgr();
~glStateMgr();
// Turn on/off a capability.
void glDisable(GLenum cap);
void glEnable(GLenum cap);
// Set the matrix to be manipulated.
void glMatrixMode(GLenum mode);
// Controls how color values in the fragment being processed (the source)
// are combined with those already stored in the framebuffer (the destination).
void glBlendFunc(GLenum sfactor, GLenum dfactor);
// Sets the comparison function for the depth test.
void glDepthFunc(GLenum func);
// Sets the shading model.
void glShadeModel(GLenum mode);
private:
// Forward nested class declaration means that
// I don’t have to #include
// until the .cpp file.
class capsMap;
// Capabilities on/off state for the glEnable/glDisable calls.
capsMap& _capsMap;
// Current depth comparison function.
GLenum _gl_depth_func;
// Current modifiable matrix.
GLenum _gl_matrix_mode;
// Current shade model.
GLenum _gl_shade_model;
// Current source blending factor.
GLenum _gl_src_blend_func;
// Current destination blending factor.
GLenum _gl_dst_blend_func;
};
#endif // _GL_STATE_MGR_H not defined.
This is an early prototype. It’s not nearly as optimised for space as LK’s but it’s easy to understand.
I use it to keep track of the states you can see, as well as a bunch of glEnable() states too. In this project I only need these states so far, but I will extend the class to include as much as possible eventually.
Hope that helps ,
Toby
PS I think the only way to know about optimising for the CPU is through experience. Some excellent books that I have on low level optimisation are Inner Loops by Rick Booth and Michael Abrash’s Graphics Programming Black Book. Both are a little old but good reading. The best optimisation comes from choosing good algorithms first though.