summaryrefslogtreecommitdiffstats
path: root/hacks/glx/glschool.c
blob: 6ad80a9e2ac4bd531c7a329465bb2382b1302dd1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* glschool.c, Copyright (c) 2005-2006 David C. Lambert <dcl@panix.com>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */
#include "xlockmore.h"
#include "glschool.h"

#define sws_opts			xlockmore_opts
#define DEFAULTS    "*delay:		20000       \n" \
                    "*showFPS:      False       \n" \
                    "*wireframe:    False       \n" \

#define release_glschool		(0)
#define glschool_handle_event	(xlockmore_no_events)

#undef countof
#define countof(x) (sizeof((x))/sizeof((*x)))

#define DEF_NFISH	"100"
#define DEF_FOG		"False"
#define DEF_DRAWBBOX	"True"
#define DEF_DRAWGOAL	"False"
#define DEF_GOALCHGF	"50"
#define DEF_MAXVEL	"7.0"
#define DEF_MINVEL	"1.0"
#define DEF_ACCLIMIT	"8.0"
#define DEF_DISTEXP	"2.2"
#define DEF_AVOIDFACT	"1.5"
#define DEF_MATCHFACT	"0.15"
#define DEF_CENTERFACT	"0.1"
#define DEF_TARGETFACT	"80"
#define DEF_MINRADIUS	"30.0"
#define DEF_MOMENTUM	"0.9"
#define DEF_DISTCOMP	"10.0"

static int			NFish;
static Bool			DoFog;
static Bool			DoDrawBBox;
static Bool			DoDrawGoal;
static int			GoalChgFreq;
static float		MinVel;
static float		MaxVel;
static float		DistExp;
static float		AccLimit;
static float		AvoidFact;
static float		MatchFact;
static float		TargetFact;
static float		CenterFact;
static float		MinRadius;
static float		Momentum;
static float		DistComp;

static XrmOptionDescRec opts[] = {
	{ "-nfish",		".nfish",		XrmoptionSepArg, 0 },
	{ "-fog",		".fog",			XrmoptionNoArg, "True" },
	{ "+fog",		".fog",			XrmoptionNoArg, "False" },
	{ "-drawgoal",	".drawgoal",	XrmoptionNoArg, "True" },
	{ "+drawgoal",	".drawgoal",	XrmoptionNoArg, "False" },
	{ "-drawbbox",	".drawbbox",	XrmoptionNoArg, "True" },
	{ "+drawbbox",	".drawbbox",	XrmoptionNoArg, "False" },
	{ "-goalchgf",	".goalchgf",	XrmoptionSepArg, 0 },
	{ "-maxvel",	".maxvel",		XrmoptionSepArg, 0 },
	{ "-minvel",	".minvel",		XrmoptionSepArg, 0 },
	{ "-acclimit",	".acclimit",	XrmoptionSepArg, 0 },
	{ "-distexp",	".distexp",		XrmoptionSepArg, 0 },
	{ "-avoidfact",	".avoidfact",	XrmoptionSepArg, 0 },
	{ "-matchfact",	".matchfact",	XrmoptionSepArg, 0 },
	{ "-centerfact",".centerfact",	XrmoptionSepArg, 0 },
	{ "-targetfact",".targetfact",	XrmoptionSepArg, 0 },
	{ "-minradius",	".minradius",	XrmoptionSepArg, 0 },
	{ "-distcomp",	".distcomp",	XrmoptionSepArg, 0 },
	{ "-momentum",	".momentum",	XrmoptionSepArg, 0 },
};

static argtype vars[] = {
	{&NFish,		"nfish",		"NFish",		DEF_NFISH, t_Int},
	{&DoFog,		"fog",			"DoFog",		DEF_FOG, t_Bool},
	{&DoDrawBBox,	"drawbbox",		"DoDrawBBox",	DEF_DRAWBBOX, t_Bool},
	{&DoDrawGoal,	"drawgoal",		"DoDrawGoal",	DEF_DRAWGOAL, t_Bool},
	{&GoalChgFreq,	"goalchgf",		"GoalChgFreq",	DEF_GOALCHGF,  t_Int},
	{&MaxVel,		"maxvel",		"MaxVel",		DEF_MAXVEL,  t_Float},
	{&MinVel,		"minvel",		"MinVel",		DEF_MINVEL,	t_Float},
	{&AccLimit,		"acclimit",		"AccLimit",		DEF_ACCLIMIT,  t_Float},
	{&DistExp,		"distexp",		"DistExp",		DEF_DISTEXP,  t_Float},
	{&AvoidFact,	"avoidfact",	"AvoidFact",	DEF_AVOIDFACT,  t_Float},
	{&MatchFact,	"matchfact",	"MatchFact",	DEF_MATCHFACT,  t_Float},
	{&CenterFact,	"centerfact",	"CenterFact",	DEF_CENTERFACT,  t_Float},
	{&TargetFact,	"targetfact",	"TargetFact",	DEF_TARGETFACT,  t_Float},
	{&MinRadius,	"minradius",	"MinRadius",	DEF_MINRADIUS,  t_Float},
	{&Momentum,		"momentum",		"Momentum",		DEF_MOMENTUM,  t_Float},
	{&DistComp,		"distcomp",		"DistComp",		DEF_DISTCOMP,  t_Float},
};

ENTRYPOINT ModeSpecOpt glschool_opts = {countof(opts), opts, countof(vars), vars, NULL};

typedef struct {
	int			nColors;
	int			rotCounter;
	int			goalCounter;
	Bool		drawGoal;
	Bool		drawBBox;			
	GLuint		bboxList;
	GLuint		goalList;
	GLuint		fishList;
        int		fish_polys, box_polys;
	XColor		*colors;
	School		*school;
	GLXContext	*context;
} glschool_configuration;

static glschool_configuration	*scs = NULL;

ENTRYPOINT void
reshape_glschool(ModeInfo *mi, int width, int height)
{
	Bool					wire = MI_IS_WIREFRAME(mi);
	double					aspect = (double)width/(double)height;
	glschool_configuration	*sc = &scs[MI_SCREEN(mi)];

	glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *sc->context);
	if (sc->school != (School *)0) {
		glschool_setBBox(sc->school, -aspect*160, aspect*160, -130, 130, -450, -50.0);
		glDeleteLists(sc->bboxList, 1);
                glschool_createBBoxList(&SCHOOL_BBOX(sc->school),
                                        &sc->bboxList, wire);
	}
	glschool_reshape(width, height);
}

ENTRYPOINT void
init_glschool(ModeInfo *mi)
{
	int						width = MI_WIDTH(mi);
	int						height = MI_HEIGHT(mi);
	Bool					wire = MI_IS_WIREFRAME(mi);
	glschool_configuration	*sc;

	MI_INIT (mi, scs);
	sc = &scs[MI_SCREEN(mi)];

	sc->drawGoal = DoDrawGoal;
	sc->drawBBox = DoDrawBBox;

	sc->nColors = 360;
	sc->context = init_GL(mi);
	sc->colors = (XColor *)calloc(sc->nColors, sizeof(XColor));
	make_color_ramp(0, 0, 0,
					0.0, 1.0, 1.0,
					359.0, 1.0, 1.0,
					sc->colors, &sc->nColors,
					False, 0, False);

	sc->school = glschool_initSchool(NFish, AccLimit, MaxVel, MinVel, DistExp, Momentum,
							MinRadius, AvoidFact, MatchFact, CenterFact, TargetFact,
							DistComp);
	if (sc->school == (School *)0) {
		fprintf(stderr, "couldn't initialize TheSchool, exiting\n");
		exit(1);
	}

	reshape_glschool(mi, width, height);

	glschool_initGLEnv(DoFog);
	glschool_initFishes(sc->school);
	glschool_createDrawLists(&SCHOOL_BBOX(sc->school), 
                                 &sc->bboxList, &sc->goalList, &sc->fishList,
                                 &sc->fish_polys, &sc->box_polys, wire);
	glschool_computeAccelerations(sc->school);
}

ENTRYPOINT void
draw_glschool(ModeInfo *mi)
{
	Window					window = MI_WINDOW(mi);
	Display					*dpy = MI_DISPLAY(mi);
	glschool_configuration	*sc = &scs[MI_SCREEN(mi)];

	if (!sc->context) {
		fprintf(stderr, "no context\n");
		return;
	}

	glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *sc->context);

        mi->polygon_count = 0;

	if ((sc->goalCounter % GoalChgFreq) == 0)
		glschool_newGoal(sc->school);
	sc->goalCounter++;

	sc->rotCounter++;
	sc->rotCounter = (sc->rotCounter%360);

	glschool_applyMovements(sc->school);
	glschool_drawSchool(sc->colors, sc->school, sc->bboxList, 
                            sc->goalList, sc->fishList, sc->rotCounter, 
                              sc->drawGoal, sc->drawBBox, 
                            sc->fish_polys, sc->box_polys,
                            &mi->polygon_count);
	glschool_computeAccelerations(sc->school);

	if (mi->fps_p)
		do_fps(mi);

	glFinish();
	glXSwapBuffers(dpy, window);
}


ENTRYPOINT void
free_glschool(ModeInfo *mi)
{
	glschool_configuration	*sc = &scs[MI_SCREEN(mi)];

        if (!sc->context) return;
	glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *sc->context);

        if (sc->school) glschool_freeSchool (sc->school);
        if (sc->colors) free (sc->colors);
        if (glIsList(sc->bboxList)) glDeleteLists(sc->bboxList, 1);
        if (glIsList(sc->goalList)) glDeleteLists(sc->goalList, 1);
        if (glIsList(sc->fishList)) glDeleteLists(sc->fishList, 1);
}

XSCREENSAVER_MODULE("GLSchool", glschool)