1 /* glschool_gl.c, Copyright (c) 2005-2006 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"
16 static GLUquadricObj *Quadratic;
19 drawGoal(double *goal, GLuint goalList)
21 glColor3f(1.0, 0.0, 0.0);
24 glTranslatef(goal[0], goal[1], goal[2]);
25 glColor3f(1.0, 0.0, 0.0);
32 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);
55 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
56 glColor3f(0.0, 0.0, .2);
57 glVertex3f(xMin, yMin, zMax);
58 glVertex3f(xMin, yMin, zMin);
59 glVertex3f(xMin, yMax, zMin);
60 glVertex3f(xMin, yMax, zMax);
64 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
65 glColor3f(0.0, 0.0, .2);
66 glVertex3f(xMax, yMin, zMin);
67 glVertex3f(xMax, yMin, zMax);
68 glVertex3f(xMax, yMax, zMax);
69 glVertex3f(xMax, yMax, zMin);
73 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
74 glColor3f(0.0, 0.0, .1);
75 glVertex3f(xMax, yMax, zMax);
76 glVertex3f(xMin, yMax, zMax);
77 glVertex3f(xMin, yMax, zMin);
78 glVertex3f(xMax, yMax, zMin);
82 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
83 glColor3f(0.0, 0.0, .3);
84 glVertex3f(xMin, yMin, zMax);
85 glVertex3f(xMax, yMin, zMax);
86 glVertex3f(xMax, yMin, zMin);
87 glVertex3f(xMin, yMin, zMin);
90 if (wire) glLineWidth(1.0);
94 createBBoxList(BBox *bbox, GLuint *bboxList, int wire)
96 *bboxList = glGenLists(1);
97 glNewList(*bboxList, GL_COMPILE);
98 drawBoundingBox(bbox, wire);
104 createDrawLists(BBox *bbox, GLuint *bboxList, GLuint *goalList, GLuint *fishList, int wire)
106 if (Quadratic == (GLUquadricObj *)0) return;
108 gluQuadricDrawStyle(Quadratic, (wire ? GLU_LINE : GLU_FILL));
110 createBBoxList(bbox, bboxList, wire);
112 *goalList = glGenLists(1);
113 glNewList(*goalList, GL_COMPILE);
114 gluSphere(Quadratic, 5.0, 10, 10);
117 *fishList = glGenLists(1);
118 glNewList(*fishList, GL_COMPILE);
119 gluSphere(Quadratic, 2.0, 10, 5);
120 gluCylinder(Quadratic, 2.0, 0.0, 10.0, 10, 5);
126 createDrawLists(BBox *bbox, GLuint *bboxList, GLuint *goalList, GLuint *fishList, Bool wire)
128 createBBoxList(bbox, bboxList, wire);
130 *goalList = glGenLists(1);
131 glNewList(*goalList, GL_COMPILE);
132 glScalef(10.0, 10.0, 10.0);
133 unit_sphere(10, 10, wire);
136 *fishList = glGenLists(1);
137 glNewList(*fishList, GL_COMPILE);
138 glScalef(2.0, 2.0, 2.0);
139 unit_sphere(10, 10, wire);
152 GLfloat amb[4] = {0.1, 0.1, 0.1, 1.0};
153 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
154 GLfloat spc[4] = {1.0, 1.0, 1.0, 1.0};
155 GLfloat pos[4] = {0.0, 50.0, -50.0, 1.0};
157 glMatrixMode(GL_MODELVIEW);
160 glLightfv(GL_LIGHT0, GL_POSITION, pos);
161 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
162 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
163 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
166 glEnable(GL_LIGHTING);
172 GLfloat fog[4] = {0.0, 0.0, 0.15, 1.0};
175 glFogi(GL_FOG_MODE, GL_EXP2);
176 glFogfv(GL_FOG_COLOR, fog);
177 glFogf(GL_FOG_DENSITY, .0025);
178 glFogf(GL_FOG_START, -100);
182 initGLEnv(Bool doFog)
184 GLfloat spc[4] = {1.0, 1.0, 1.0, 1.0};
186 glClearColor(0.0, 0.0, 0.0, 0.0);
188 glDepthFunc(GL_LESS);
190 glEnable(GL_COLOR_MATERIAL);
191 glMateriali(GL_FRONT, GL_SHININESS, 128);
192 glMaterialfv(GL_FRONT, GL_SPECULAR, spc);
193 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, spc);
195 glEnable(GL_NORMALIZE);
196 glEnable(GL_DEPTH_TEST);
197 glShadeModel(GL_SMOOTH);
198 glEnable(GL_CULL_FACE);
201 if (doFog) initFog();
203 Quadratic = gluNewQuadric();
207 reshape(int width, int height)
209 GLfloat h = (GLfloat) width / (GLfloat) height;
211 glViewport (0, 0, (GLint) width, (GLint) height);
213 glMatrixMode(GL_PROJECTION);
215 gluPerspective(60.0, h, 0.1, 451.0);
217 glMatrixMode(GL_MODELVIEW);
222 getColorVect(XColor *colors, int index, double *colorVect)
224 colorVect[0] = colors[index].red / 65535.0;
225 colorVect[1] = colors[index].green / 65535.0;
226 colorVect[2] = colors[index].blue / 65535.0;
230 drawSchool(XColor *colors, School *s,
231 GLuint bboxList, GLuint goalList, GLuint fishList,
232 int rotCounter, Bool drawGoal_p, Bool drawBBox_p)
237 double rotTheta = 0.0;
238 double colTheta = 0.0;
240 int nFish = SCHOOL_NFISH(s);
241 Fish *theFishes = SCHOOL_FISHES(s);
243 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
245 glMatrixMode(GL_MODELVIEW);
249 glDisable(GL_LIGHTING);
250 glCallList(bboxList);
251 glEnable(GL_LIGHTING);
254 if (drawGoal_p) drawGoal(SCHOOL_GOAL(s), goalList);
256 for(i = 0, f = theFishes; i < nFish; i++, f++) {
257 colTheta = computeNormalAndThetaToPlusZ(FISH_AVGVEL(f), xVect);
258 rotTheta = computeNormalAndThetaToPlusZ(FISH_VEL(f), xVect);
260 if (FISH_IAVGVEL(f,2) < 0.0) colTheta = 180.0 - colTheta;
261 if (FISH_VZ(f) < 0.0) rotTheta = 180.0 - rotTheta;
263 getColorVect(colors, (int)(colTheta+240)%360, colorVect);
264 glColor3f(colorVect[0], colorVect[1], colorVect[2]);
268 glTranslatef(FISH_X(f), FISH_Y(f), FISH_Z(f));
269 glRotatef(180.0+rotTheta, xVect[0], xVect[1], xVect[2]);
270 glCallList(fishList);