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);
120 gluSphere(Quadratic, 2.0, 10, 5);
121 gluCylinder(Quadratic, 2.0, 0.0, 10.0, 10, 5);
123 gluSphere(Quadratic, 2.0, 3, 2);
124 gluCylinder(Quadratic, 2.0, 0.0, 10.0, 3, 2);
131 createDrawLists(BBox *bbox, GLuint *bboxList, GLuint *goalList, GLuint *fishList, Bool wire)
133 createBBoxList(bbox, bboxList, wire);
135 *goalList = glGenLists(1);
136 glNewList(*goalList, GL_COMPILE);
137 glScalef(10.0, 10.0, 10.0);
138 unit_sphere(10, 10, wire);
141 *fishList = glGenLists(1);
142 glNewList(*fishList, GL_COMPILE);
143 glScalef(2.0, 2.0, 2.0);
144 unit_sphere(10, 10, wire);
157 GLfloat amb[4] = {0.1, 0.1, 0.1, 1.0};
158 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
159 GLfloat spc[4] = {1.0, 1.0, 1.0, 1.0};
160 GLfloat pos[4] = {0.0, 50.0, -50.0, 1.0};
162 glMatrixMode(GL_MODELVIEW);
165 glLightfv(GL_LIGHT0, GL_POSITION, pos);
166 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
167 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
168 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
171 glEnable(GL_LIGHTING);
177 GLfloat fog[4] = {0.0, 0.0, 0.15, 1.0};
180 glFogi(GL_FOG_MODE, GL_EXP2);
181 glFogfv(GL_FOG_COLOR, fog);
182 glFogf(GL_FOG_DENSITY, .0025);
183 glFogf(GL_FOG_START, -100);
187 initGLEnv(Bool doFog)
189 GLfloat spc[4] = {1.0, 1.0, 1.0, 1.0};
192 glDepthFunc(GL_LESS);
194 glEnable(GL_COLOR_MATERIAL);
195 glMateriali(GL_FRONT, GL_SHININESS, 128);
196 glMaterialfv(GL_FRONT, GL_SPECULAR, spc);
197 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, spc);
199 glEnable(GL_NORMALIZE);
200 glEnable(GL_DEPTH_TEST);
201 glShadeModel(GL_SMOOTH);
202 glEnable(GL_CULL_FACE);
205 if (doFog) initFog();
207 Quadratic = gluNewQuadric();
211 reshape(int width, int height)
213 GLfloat h = (GLfloat) width / (GLfloat) height;
215 glViewport (0, 0, (GLint) width, (GLint) height);
217 glMatrixMode(GL_PROJECTION);
219 gluPerspective(60.0, h, 0.1, 451.0);
221 glMatrixMode(GL_MODELVIEW);
226 getColorVect(XColor *colors, int index, double *colorVect)
228 colorVect[0] = colors[index].red / 65535.0;
229 colorVect[1] = colors[index].green / 65535.0;
230 colorVect[2] = colors[index].blue / 65535.0;
234 drawSchool(XColor *colors, School *s,
235 GLuint bboxList, GLuint goalList, GLuint fishList,
236 int rotCounter, Bool drawGoal_p, Bool drawBBox_p)
241 double rotTheta = 0.0;
242 double colTheta = 0.0;
244 int nFish = SCHOOL_NFISH(s);
245 Fish *theFishes = SCHOOL_FISHES(s);
247 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
249 glMatrixMode(GL_MODELVIEW);
253 glDisable(GL_LIGHTING);
254 glCallList(bboxList);
255 glEnable(GL_LIGHTING);
258 if (drawGoal_p) drawGoal(SCHOOL_GOAL(s), goalList);
260 for(i = 0, f = theFishes; i < nFish; i++, f++) {
261 colTheta = computeNormalAndThetaToPlusZ(FISH_AVGVEL(f), xVect);
262 rotTheta = computeNormalAndThetaToPlusZ(FISH_VEL(f), xVect);
264 if (FISH_IAVGVEL(f,2) < 0.0) colTheta = 180.0 - colTheta;
265 if (FISH_VZ(f) < 0.0) rotTheta = 180.0 - rotTheta;
267 getColorVect(colors, (int)(colTheta+240)%360, colorVect);
268 glColor3f(colorVect[0], colorVect[1], colorVect[2]);
272 glTranslatef(FISH_X(f), FISH_Y(f), FISH_Z(f));
273 glRotatef(180.0+rotTheta, xVect[0], xVect[1], xVect[2]);
274 glCallList(fishList);