summaryrefslogtreecommitdiffstats
path: root/lib/gltools.h
blob: e5975da32c3b6c2e7a34ef229adf1cd84be99202 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// GLTools.h
// OpenGL SuperBible
// Copyright 1998 - 2003 Richard S. Wright Jr.
// Code by Richard S. Wright Jr.
// All Macros prefixed with GLT_, all functions prefixed with glt... This
// should avoid most namespace problems
// Some of these functions allocate memory. Use CRT functions to free
// Report bugs to rwright@starstonesoftware.com

#ifndef __GLTOOLS__LIBRARY
#define __GLTOOLS__LIBRARY

#define FREEGLUT_STATIC

// Bring in OpenGL 
// Windows
#ifdef WIN32
#include <windows.h>        // Must have for Windows platform builds
#include "glee.h"            // OpenGL Extension "autoloader"
#include <gl\gl.h>            // Microsoft OpenGL headers (version 1.1 by themselves)
#include <gl\glu.h>            // OpenGL Utilities
#include "glut.h"            // Glut (Free-Glut on Windows)
#endif

// Mac OS X
#ifdef __APPLE__
#include <Carbon/Carbon.h>    // Brings in most Apple specific stuff
#include "glee.h"        // OpenGL Extension "autoloader"
#include <OpenGL/gl.h>        // Apple OpenGL haders (version depends on OS X SDK version)
#include <OpenGL/glu.h>        // OpenGL Utilities
#include <Glut/glut.h>        // Apples Implementation of GLUT

// Just ignore sleep on Apple
#define Sleep(x)

#endif

#ifdef linux
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <GL/glx.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Just ignore sleep in linux too
#define Sleep(x)
#endif

#ifdef linux
typedef GLvoid (*CallBack)();
#else

#ifndef WIN32
typedef GLvoid (*CallBack)(...);            //  XCode (GNU) style
#else
typedef GLvoid (_stdcall *CallBack)();      // Visual C++ style
#endif

#endif

// Needed for NURBS callbacks... VC++ vs. GNU
/*#ifndef WIN32
#define CALLBACK (GLvoid (*)(...))
#else
#define CALLBACK (GLvoid (__stdcall*)())
#endif
*/

// Universal includes
#include <math.h>

// There is a static block allocated for loading shaders to prevent heap fragmentation
#define MAX_SHADER_LENGTH   8192
    
///////////////////////////////////////////////////////
// Macros for big/little endian happiness
// These are intentionally written to be easy to understand what they 
// are doing... no flames please on the inefficiency of these.
#ifdef __BIG_ENDIAN__
///////////////////////////////////////////////////////////
// This function says, "this pointer is a little endian value"
// If the value must be changed it is... otherwise, this
// function is defined away below (on Intel systems for example)
inline void LITTLE_ENDIAN_WORD(void *pWord)
{
    unsigned char *pBytes = (unsigned char *)pWord;
    unsigned char temp;
    
    temp = pBytes[0];
    pBytes[0] = pBytes[1];
    pBytes[1] = temp;
}

///////////////////////////////////////////////////////////
// This function says, "this pointer is a little endian value"
// If the value must be changed it is... otherwise, this
// function is defined away below (on Intel systems for example)
inline void LITTLE_ENDIAN_DWORD(void *pWord)
{
    unsigned char *pBytes = (unsigned char *)pWord;
    unsigned char temp;
    
    // Swap outer bytes
    temp = pBytes[3];
    pBytes[3] = pBytes[0];
    pBytes[0] = temp;
    
    // Swap inner bytes
    temp = pBytes[1];
    pBytes[1] = pBytes[2];
    pBytes[2] = temp;
}
#else

// Define them away on little endian systems
#define LITTLE_ENDIAN_WORD 
#define LITTLE_ENDIAN_DWORD 
#endif

///////////////////////////////////////////////////////////////////////////////
//         THE LIBRARY....
///////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////
// Load a .TGA file
GLbyte *gltLoadTGA(const char *szFileName, GLint *iWidth, GLint *iHeight, GLint *iComponents, GLenum *eFormat);

// Capute the frame buffer and write it as a .tga
GLint gltWriteTGA(const char *szFileName);

// Draw a Torus
void gltDrawTorus(GLfloat majorRadius, GLfloat minorRadius, GLint numMajor, GLint numMinor);

// Just draw a simple sphere with normals and texture coordinates
void gltDrawSphere(GLfloat fRadius, GLint iSlices, GLint iStacks);

// Draw a 3D unit Axis set
void gltDrawUnitAxes(void);

// Shader loading support
bool bLoadShaderFile(const char *szFile, GLhandleARB shader);
GLhandleARB gltLoadShaderPair(const char *szVertexProg, const char *szFragmentProg);

// Get the OpenGL version, returns fals on error
bool gltGetOpenGLVersion(int &nMajor, int &nMinor);

// Check to see if an exension is supported
int gltIsExtSupported(const char *szExtension);

// Get the function pointer for an extension
void *gltGetExtensionPointer(const char *szFunctionName);

///////////////////////////////////////////////////////////////////////////////
// Win32 Only
#ifdef WIN32
int gltIsWGLExtSupported(HDC hDC, const char *szExtension);
#endif


#endif