Mac OS X header compatibility.
[grGL.git] / src / glancillary.c
1 #ifdef __APPLE__
2 #include <OpenGL/gl.h>
3 #include <OpenGL/glu.h>
4 #else
5 #include <GL/gl.h>
6 #include <GL/glu.h>
7 #endif
8
9 #include "glancillary.h"
10
11
12
13 /****************************
14  * GL init
15  ***************************/
16
17 void glaInit(void)
18 {
19   glClearColor(0.0, 0.0, 0.0, 0.0);
20   glShadeModel(GL_FLAT);
21   //glShadeModel(GL_SMOOTH);
22
23   glEnable(GL_BLEND);
24   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
25
26   // Init Model/view transformation
27   glMatrixMode(GL_MODELVIEW);
28
29   // Reset MV matrix
30   glLoadIdentity();
31 }
32
33
34
35
36 /****************************
37  * GL camera simulation helpers
38  ***************************/
39
40 void glaCameraRotatef(GLfloat *cmat, GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
41 {
42   glPushMatrix();
43     glLoadIdentity();
44     glRotatef(angle, x, y, z);
45     glMultMatrixf(cmat);
46     glGetFloatv(GL_MODELVIEW_MATRIX, cmat);
47   glPopMatrix();
48 }
49
50 void glaCameraTranslatef(GLfloat *cmat, GLfloat x, GLfloat y, GLfloat z)
51 {
52   glPushMatrix();
53     glLoadIdentity();
54     glTranslatef(x, y, z);
55     glMultMatrixf(cmat);
56     glGetFloatv(GL_MODELVIEW_MATRIX, cmat);
57   glPopMatrix();
58 }
59
60
61
62
63 /****************************
64  * GL box drawing example
65  ***************************/
66
67 void glaDrawExample(void)
68 {
69   // Draw the corner dots
70   glPointSize(5.0);
71   glColor3f(1.0, 1.0, 0.0);
72   glBegin(GL_POINTS);
73     glVertex3f(-1.0, -1.0, -1.0);
74     glVertex3f(1.0, -1.0, -1.0);
75     glVertex3f(1.0, 1.0, -1.0);
76     glVertex3f(-1.0, 1.0, -1.0);
77     glVertex3f(-1.0, -1.0, -1.0);
78     glVertex3f(-1.0, -1.0, 1.0);
79     glVertex3f(-1.0, 1.0, 1.0);
80   glEnd();
81
82   // Draw a line with fixed vertices
83   glColor3f(0.5, 1.0, 0.5);
84   glBegin(GL_LINE_STRIP);
85     glVertex3f(0.0, 0.0, 0.0);
86     glVertex3f(0.0, 0.5, 0.0);
87     glVertex3f(0.5, 0.5, 0.5);
88   glEnd();
89
90   // Draw the box boundaries
91   glColor3f(1.0, 0.0, 0.0);
92   glBegin(GL_LINE_STRIP);
93     glVertex3f(-1.0, -1.0, -1.0);
94     glVertex3f(1.0, -1.0, -1.0);
95     glVertex3f(1.0, 1.0, -1.0);
96     glVertex3f(-1.0, 1.0, -1.0);
97     glVertex3f(-1.0, -1.0, -1.0);
98     glVertex3f(-1.0, -1.0, 1.0);
99     glVertex3f(-1.0, 1.0, 1.0);
100     glVertex3f(-1.0, 1.0, -1.0);
101   glEnd();
102 }
103
104
105
106
107
108
109
110
111
112
113 /****************************
114  * GL selection (currently defunct)
115  ***************************/
116
117 // Selection helper, currently defunct.
118 void glaSelect(int x, int y)
119 {
120   int i;
121   GLuint namebuf[64] = {0};
122   GLint hits, view[4];
123   GLfloat projmat[16];
124
125   // Account for inverse Y coordinate
126   glGetIntegerv(GL_VIEWPORT, view);
127   y = view[3] - y;
128
129   glSelectBuffer(64, namebuf);
130   glRenderMode(GL_SELECT);
131
132   // Reset name stack
133   glInitNames();
134   glPushName(0);
135
136   // Restrict projection matrix to selection area
137   glGetFloatv(GL_PROJECTION_MATRIX, projmat);
138   glMatrixMode(GL_PROJECTION);
139   glPushMatrix();
140     glLoadIdentity();
141     gluPickMatrix(x, y, 1.0, 1.0, view);
142     glMultMatrixf(projmat);
143
144     // Redraw points to fill selection buffer
145     glMatrixMode(GL_MODELVIEW);
146     //glutSwapBuffers();
147     //drawPoints();
148
149     // Reset projection
150   glMatrixMode(GL_PROJECTION);
151   glPopMatrix();
152
153   hits = glRenderMode(GL_RENDER);
154
155   for (i = 0; i < hits; i++)
156     // Do something with the namebuf element here
157     break;
158
159   glMatrixMode(GL_MODELVIEW);
160 }