1 /* glschool_gl.c, Copyright (c) 2005-2012 David C. Lambert <dcl@panix.com>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
13 #include "glschool_gl.h"
18 glschool_drawGoal(double *goal, GLuint goalList)
20 glColor3f(1.0, 0.0, 0.0);
23 glTranslatef(goal[0], goal[1], goal[2]);
24 glColor3f(1.0, 0.0, 0.0);
31 glschool_drawBoundingBox(BBox *bbox, Bool wire)
34 double xMin = BBOX_XMIN(bbox);
35 double yMin = BBOX_YMIN(bbox);
36 double zMin = BBOX_ZMIN(bbox);
38 double xMax = BBOX_XMAX(bbox);
39 double yMax = BBOX_YMAX(bbox);
40 double zMax = BBOX_ZMAX(bbox);
43 if (wire) glLineWidth(5.0);
46 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
47 glColor3f(0.0, 0.0, .15);
48 glVertex3f(xMin, yMin, zMin);
49 glVertex3f(xMax, yMin, zMin);
50 glVertex3f(xMax, yMax, zMin);
51 glVertex3f(xMin, yMax, zMin);
56 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
57 glColor3f(0.0, 0.0, .2);
58 glVertex3f(xMin, yMin, zMax);
59 glVertex3f(xMin, yMin, zMin);
60 glVertex3f(xMin, yMax, zMin);
61 glVertex3f(xMin, yMax, zMax);
66 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
67 glColor3f(0.0, 0.0, .2);
68 glVertex3f(xMax, yMin, zMin);
69 glVertex3f(xMax, yMin, zMax);
70 glVertex3f(xMax, yMax, zMax);
71 glVertex3f(xMax, yMax, zMin);
76 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
77 glColor3f(0.0, 0.0, .1);
78 glVertex3f(xMax, yMax, zMax);
79 glVertex3f(xMin, yMax, zMax);
80 glVertex3f(xMin, yMax, zMin);
81 glVertex3f(xMax, yMax, zMin);
86 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
87 glColor3f(0.0, 0.0, .3);
88 glVertex3f(xMin, yMin, zMax);
89 glVertex3f(xMax, yMin, zMax);
90 glVertex3f(xMax, yMin, zMin);
91 glVertex3f(xMin, yMin, zMin);
95 if (wire) glLineWidth(1.0);
101 glschool_createBBoxList(BBox *bbox, GLuint *bboxList, int wire)
104 *bboxList = glGenLists(1);
105 glNewList(*bboxList, GL_COMPILE);
106 polys = glschool_drawBoundingBox(bbox, wire);
112 glschool_createDrawLists(BBox *bbox, GLuint *bboxList, GLuint *goalList, GLuint *fishList, int *fish_polys, int *box_polys, int wire)
120 *box_polys += glschool_createBBoxList(bbox, bboxList, wire);
125 *goalList = glGenLists(1);
126 glNewList(*goalList, GL_COMPILE);
128 *box_polys += unit_sphere (10, 10, wire);
131 *fishList = glGenLists(1);
132 glNewList(*fishList, GL_COMPILE);
133 *fish_polys += cone (0, 0, 0,
136 faces, True, (faces <= 3), /* cap */
138 glTranslatef (0, 0, -0.3);
140 glRotatef (90, 1, 0, 0);
142 *fish_polys += unit_sphere (faces, faces, wire);
148 glschool_initLights(void)
150 GLfloat amb[4] = {0.1, 0.1, 0.1, 1.0};
151 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
152 GLfloat spc[4] = {1.0, 1.0, 1.0, 1.0};
153 GLfloat pos[4] = {0.0, 50.0, -50.0, 1.0};
155 glMatrixMode(GL_MODELVIEW);
158 glLightfv(GL_LIGHT0, GL_POSITION, pos);
159 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
160 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
161 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
164 glEnable(GL_LIGHTING);
168 glschool_initFog(void)
170 GLfloat fog[4] = {0.0, 0.0, 0.15, 1.0};
173 glFogi(GL_FOG_MODE, GL_EXP2);
174 glFogfv(GL_FOG_COLOR, fog);
175 glFogf(GL_FOG_DENSITY, .0025);
176 glFogf(GL_FOG_START, -100);
180 glschool_initGLEnv(Bool doFog)
182 GLfloat spc[4] = {1.0, 1.0, 1.0, 1.0};
185 glDepthFunc(GL_LESS);
187 glEnable(GL_COLOR_MATERIAL);
188 glMateriali(GL_FRONT, GL_SHININESS, 128);
189 glMaterialfv(GL_FRONT, GL_SPECULAR, spc);
190 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, spc);
192 glEnable(GL_NORMALIZE);
193 glEnable(GL_DEPTH_TEST);
194 glShadeModel(GL_SMOOTH);
195 glEnable(GL_CULL_FACE);
197 glschool_initLights();
198 if (doFog) glschool_initFog();
202 glschool_reshape(int width, int height)
204 GLfloat h = (GLfloat) width / (GLfloat) height;
206 glViewport (0, 0, (GLint) width, (GLint) height);
208 glMatrixMode(GL_PROJECTION);
210 gluPerspective(60.0, h, 0.1, 451.0);
212 glMatrixMode(GL_MODELVIEW);
217 glschool_getColorVect(XColor *colors, int index, double *colorVect)
219 colorVect[0] = colors[index].red / 65535.0;
220 colorVect[1] = colors[index].green / 65535.0;
221 colorVect[2] = colors[index].blue / 65535.0;
225 glschool_drawSchool(XColor *colors, School *s,
226 GLuint bboxList, GLuint goalList, GLuint fishList,
227 int rotCounter, Bool drawGoal_p, Bool drawBBox_p,
228 int fish_polys, int box_polys, unsigned long *polys)
233 double rotTheta = 0.0;
234 double colTheta = 0.0;
236 int nFish = SCHOOL_NFISH(s);
237 Fish *theFishes = SCHOOL_FISHES(s);
239 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
241 glMatrixMode(GL_MODELVIEW);
245 glDisable(GL_LIGHTING);
246 glCallList(bboxList);
247 glEnable(GL_LIGHTING);
251 if (drawGoal_p) glschool_drawGoal(SCHOOL_GOAL(s), goalList);
253 for(i = 0, f = theFishes; i < nFish; i++, f++) {
254 colTheta = glschool_computeNormalAndThetaToPlusZ(FISH_AVGVEL(f), xVect);
255 rotTheta = glschool_computeNormalAndThetaToPlusZ(FISH_VEL(f), xVect);
257 if (FISH_IAVGVEL(f,2) < 0.0) colTheta = 180.0 - colTheta;
258 if (FISH_VZ(f) < 0.0) rotTheta = 180.0 - rotTheta;
260 glschool_getColorVect(colors, (int)(colTheta+240)%360, colorVect);
261 glColor3f(colorVect[0], colorVect[1], colorVect[2]);
265 glTranslatef(FISH_X(f), FISH_Y(f), FISH_Z(f));
266 glRotatef(180.0+rotTheta, xVect[0], xVect[1], xVect[2]);
267 glCallList(fishList);
268 *polys += fish_polys;