Compiling an OpenGL/Cg application

Setting flags in the Makefile

When you build your Makefile and want to make it work on Linux and MacOSX, you must take care of setting the right path for the linker:

ifeq ($(UNAME), Darwin)
  CLINKFLAGS = -framework OpenGL -framework GLUT -framework Cg
else
  CLINKFLAGS = -L/usr/X11R6/lib64 -L/usr/X11R6/lib
  CLINKFLAGS += -lCg -lCgGL -lglut -lGLU -lGL -lXi -lXmu -lX11 -lm -lpthread
endif

In this example, we are using the OpenGL GLUT and Cg librairies.

Including the right header

You also need to take care that MacOS and Linux have different ways to include OpenGL header. Here are some common headers you want to include:

#ifdef __APPLE__
    #include <OpenGL/glu.h>
    #include <OpenGL/glext.h>
#else
    #include <GL/glu.h>
    #include <GL/glext.h>
    #include <GL/glx.h>
    #include <GL/glxext.h>
#endif

Getting some function in order to use Cg

When you are on Linux, you need to take care to load some functions to use Cg, but in OSX they are already here. Here is the trick to make your C file for both platforms:

#ifndef __APPLE__
static void (*glGenProgramsARB)(GLuint, GLuint *) = NULL;
static void (*glBindProgramARB)(GLuint, GLuint) = NULL;
static void (*glProgramStringARB)(GLuint, GLuint, GLint, const GLbyte *) = NULL;
static void (*glActiveTextureARB)(GLenum) = NULL;
static void (*glMultiTexCoord3fARB)(GLenum, GLfloat, GLfloat, GLfloat) = NULL;
#endif
...
#ifdef __APPLE__
void set_function_pointers()
{
}
#elif
void set_function_pointers()
{
	glGenProgramsARB = (void (*)(GLuint, GLuint *))glXGetProcAddressARB("glGenProgramsARB");
	if(!glGenProgramsARB) {
		fprintf(stderr, "set_function_pointers(): glGenProgramsARB failed\n");
		exit(1);
	}
	glBindProgramARB = (void (*)(GLuint, GLuint))glXGetProcAddressARB("glBindProgramARB");
	if(!glBindProgramARB) {
		fprintf(stderr, "set_function_pointers(): glBindProgramARB failed\n");
		exit(1);
	}
	glProgramStringARB = (void (*)(GLuint, GLuint, GLint, const GLbyte *))glXGetProcAddressARB("glProgramStringARB");
	if(!my_glProgramStringARB) {
		fprintf(stderr, "set_function_pointers(): glProgramStringARB failed\n");
		exit(1);
	}
	glActiveTextureARB = (void (*)(GLuint))glXGetProcAddressARB("glActiveTexture");
	if(!glActiveTextureARB) {
		fprintf(stderr, "set_function_pointers(): glActiveTextureARB failed\n");
		exit(1);
	}
	glMultiTexCoord3fARB = (void (*)(GLuint, GLfloat, GLfloat, GLfloat))glXGetProcAddressARB("glMultiTexCoord3fARB");
	if(!glMultiTexCoord3fARB) {
		fprintf(stderr, "set_function_pointers(): glMultiTexCoord3fARB failed\n");
		exit(1);
	}
}
#endif /* __APPLE__ */

In the example, you have only a few methods. Some other methods might be handled the same way. For example, the same trick should be applied for the GLSL.

 
doc/best-practices.txt · Last modified: 2010/02/16 12:02 by 82.243.209.240
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki