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