PageRenderTime 65ms CodeModel.GetById 11ms app.highlight 50ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/screensavers/rsxs-0.9/src/euphoria/wisp.cc

http://github.com/xbmc/xbmc
C++ | 277 lines | 229 code | 17 blank | 31 comment | 29 complexity | 4a7f81f8564894817ed5ef58739dacf5 MD5 | raw file
  1/*
  2 * Really Slick XScreenSavers
  3 * Copyright (C) 2002-2006  Michael Chapman
  4 *
  5 * This program is free software; you can redistribute it and/or modify
  6 * it under the terms of the GNU General Public License version 2 as
  7 * published by the Free Software Foundation.
  8 *
  9 * This program is distributed in the hope that it will be useful,
 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 * GNU General Public License for more details.
 13 *
 14 * You should have received a copy of the GNU General Public License
 15 * along with this program; if not, write to the Free Software
 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 17 *
 18 *****************************************************************************
 19 *
 20 * This is a Linux port of the Really Slick Screensavers,
 21 * Copyright (C) 2002 Terence M. Welsh, available from www.reallyslick.com
 22 */
 23#include <common.hh>
 24
 25#include <euphoria.hh>
 26#include <wisp.hh>
 27
 28Wisp::Wisp() {
 29	float recHalfDens = 1.0f / (float(Hack::density) * 0.5f);
 30
 31	_vertex.resize(   Hack::density + 1, Hack::density + 1);
 32	_intensity.resize(Hack::density + 1, Hack::density + 1);
 33	_gridPos.resize(  Hack::density + 1, Hack::density + 1);
 34
 35	for (unsigned int i = 0; i <= Hack::density; ++i)
 36		for (unsigned int j = 0; j <= Hack::density; ++j) {
 37			Vector v(
 38				float(i) * recHalfDens - 1.0f,
 39				float(j) * recHalfDens - 1.0f,
 40				0.0f
 41			);
 42			v.z() = v.lengthSquared();
 43			_gridPos(i, j) = v;
 44		}
 45
 46	// initialize constants
 47	for (unsigned int i = 0; i < NUMCONSTS; ++i) {
 48		_c[i] = Common::randomFloat(2.0f) - 1.0f;
 49		_cr[i] = Common::randomFloat(M_PI * 2.0f);
 50		_cv[i] = Common::randomFloat(Hack::speed * 0.03f) +
 51			(Hack::speed * 0.001f);
 52	}
 53
 54	// pick color
 55	_HSL.set(
 56		Common::randomFloat(1.0f),
 57		0.1f + Common::randomFloat(0.9f),
 58		1.0f
 59	);
 60	_hueSpeed = Common::randomFloat(0.1f) - 0.05f;
 61	_saturationSpeed = Common::randomFloat(0.04f) + 0.001f;
 62}
 63
 64void Wisp::update() {
 65	// visibility constants
 66	static float viscon1 = Hack::visibility * 0.01f;
 67	static float viscon2 = 1.0f / viscon1;
 68
 69	// update constants
 70	for (unsigned int i = 0; i < NUMCONSTS; ++i) {
 71		_cr[i] += _cv[i] * Common::elapsedSecs;
 72		if (_cr[i] > M_PI * 2.0f)
 73			_cr[i] -= M_PI * 2.0f;
 74		_c[i] = std::cos(_cr[i]);
 75	}
 76
 77	// update vertex positions
 78	for (unsigned int i = 0; i <= Hack::density; ++i)
 79		for (unsigned int j = 0; j <= Hack::density; ++j)
 80			_vertex(i, j).set(
 81				_gridPos(i, j).x() * _gridPos(i, j).x() *
 82					_gridPos(i, j).y() * _c[0] +
 83					_gridPos(i, j).z() * _c[1] + 0.5f * _c[2],
 84				_gridPos(i, j).y() * _gridPos(i, j).y() *
 85					_gridPos(i, j).z() * _c[3] +
 86					_gridPos(i, j).x() * _c[4] + 0.5f * _c[5],
 87				_gridPos(i, j).z() * _gridPos(i, j).z() *
 88					_gridPos(i, j).x() * _c[6] +
 89					_gridPos(i, j).y() * _c[7] + _c[8]
 90			);
 91
 92	// update vertex normals for most of mesh
 93	for (unsigned int i = 1; i < Hack::density; ++i)
 94		for (unsigned int j = 1; j < Hack::density; ++j) {
 95			Vector up(_vertex(i, j + 1) - _vertex(i, j - 1));
 96			Vector right(_vertex(i + 1, j) - _vertex(i - 1, j));
 97			up.normalize();
 98			right.normalize();
 99			Vector crossVec(Vector::cross(right, up));
100			// Use depth component of normal to compute intensity
101			// This way only edges of wisp are bright
102			_intensity(i, j) = Common::clamp(
103				viscon2 * (viscon1 - std::abs(crossVec.z())),
104				0.0f, 1.0f
105			);
106		}
107
108	// update color
109	float h = _HSL.h() + _hueSpeed * Common::elapsedSecs;
110	if (h < 0.0f) h += 1.0f;
111	if (h > 1.0f) h -= 1.0f;
112	float s = _HSL.s() + _saturationSpeed * Common::elapsedSecs;
113	if (s <= 0.1f) {
114		s = 0.1f;
115		_saturationSpeed = -_saturationSpeed;
116	}
117	if (s >= 1.0f) {
118		s = 1.0f;
119		_saturationSpeed = -_saturationSpeed;
120	}
121	_HSL.h() = h;
122	_HSL.s() = s;
123	_RGB = _HSL;
124}
125
126void Wisp::draw() const {
127	glPushMatrix();
128
129	if (Hack::wireframe) {
130		for (unsigned int i = 1; i < Hack::density; ++i) {
131			glBegin(GL_LINE_STRIP);
132			for (unsigned int j = 0; j <= Hack::density; ++j) {
133				glColor3f(
134					_RGB.r() + _intensity(i, j) - 1.0f,
135					_RGB.g() + _intensity(i, j) - 1.0f,
136					_RGB.b() + _intensity(i, j) - 1.0f
137				);
138				glTexCoord2d(
139					_gridPos(i, j).x() - _vertex(i, j).x(),
140					_gridPos(i, j).y() - _vertex(i, j).y()
141				);
142				glVertex3fv(_vertex(i, j).get());
143			}
144			glEnd();
145		}
146		for (unsigned int j = 1; j < Hack::density; ++j) {
147			glBegin(GL_LINE_STRIP);
148			for (unsigned int i = 0; i <= Hack::density; ++i) {
149				glColor3f(
150					_RGB.r() + _intensity(i, j) - 1.0f,
151					_RGB.g() + _intensity(i, j) - 1.0f,
152					_RGB.b() + _intensity(i, j) - 1.0f
153				);
154				glTexCoord2d(
155					_gridPos(i, j).x() - _vertex(i, j).x(),
156					_gridPos(i, j).y() - _vertex(i, j).y()
157				);
158				glVertex3fv(_vertex(i, j).get());
159			}
160			glEnd();
161		}
162	} else {
163		for (unsigned int i = 0; i < Hack::density; ++i) {
164			glBegin(GL_TRIANGLE_STRIP);
165			for (unsigned int j = 0; j <= Hack::density; ++j) {
166				glColor3f(
167					_RGB.r() + _intensity(i + 1, j) - 1.0f,
168					_RGB.g() + _intensity(i + 1, j) - 1.0f,
169					_RGB.b() + _intensity(i + 1, j) - 1.0f
170				);
171				glTexCoord2d(
172					_gridPos(i + 1, j).x() - _vertex(i + 1, j).x(),
173					_gridPos(i + 1, j).y() - _vertex(i + 1, j).y()
174				);
175				glVertex3fv(_vertex(i + 1, j).get());
176				glColor3f(
177					_RGB.r() + _intensity(i, j) - 1.0f,
178					_RGB.g() + _intensity(i, j) - 1.0f,
179					_RGB.b() + _intensity(i, j) - 1.0f
180				);
181				glTexCoord2d(
182					_gridPos(i, j).x() - _vertex(i, j).x(),
183					_gridPos(i, j).y() - _vertex(i, j).y()
184				);
185				glVertex3fv(_vertex(i, j).get());
186			}
187			glEnd();
188		}
189	}
190
191	glPopMatrix();
192}
193
194void Wisp::drawAsBackground() const {
195	glPushMatrix();
196	glTranslatef(_c[0] * 0.2f, _c[1] * 0.2f, 1.6f);
197
198	if (Hack::wireframe) {
199		for (unsigned int i = 1; i < Hack::density; ++i) {
200			glBegin(GL_LINE_STRIP);
201			for (unsigned int j = 0; j <= Hack::density; ++j) {
202				glColor3f(
203					_RGB.r() + _intensity(i, j) - 1.0f,
204					_RGB.g() + _intensity(i, j) - 1.0f,
205					_RGB.b() + _intensity(i, j) - 1.0f
206				);
207				glTexCoord2d(
208					_gridPos(i, j).x() - _vertex(i, j).x(),
209					_gridPos(i, j).y() - _vertex(i, j).y()
210				);
211				glVertex3f(
212					_gridPos(i, j).x(),
213					_gridPos(i, j).y(),
214					_intensity(i, j)
215				);
216			}
217			glEnd();
218		}
219		for (unsigned int j = 1; j < Hack::density; ++j) {
220			glBegin(GL_LINE_STRIP);
221			for (unsigned int i = 0; i <= Hack::density; ++i) {
222				glColor3f(
223					_RGB.r() + _intensity(i, j) - 1.0f,
224					_RGB.g() + _intensity(i, j) - 1.0f,
225					_RGB.b() + _intensity(i, j) - 1.0f
226				);
227				glTexCoord2d(
228					_gridPos(i, j).x() - _vertex(i, j).x(),
229					_gridPos(i, j).y() - _vertex(i, j).y()
230				);
231				glVertex3f(
232					_gridPos(i, j).x(),
233					_gridPos(i, j).y(),
234					_intensity(i, j)
235				);
236			}
237			glEnd();
238		}
239	} else {
240		for (unsigned int i = 0; i < Hack::density; ++i) {
241			glBegin(GL_TRIANGLE_STRIP);
242			for (unsigned int j = 0; j <= Hack::density; ++j) {
243				glColor3f(
244					_RGB.r() + _intensity(i + 1, j) - 1.0f,
245					_RGB.g() + _intensity(i + 1, j) - 1.0f,
246					_RGB.b() + _intensity(i + 1, j) - 1.0f
247				);
248				glTexCoord2d(
249					_gridPos(i + 1, j).x() - _vertex(i + 1, j).x(),
250					_gridPos(i + 1, j).y() - _vertex(i + 1, j).y()
251				);
252				glVertex3f(
253					_gridPos(i + 1, j).x(),
254					_gridPos(i + 1, j).y(),
255					_intensity(i + 1, j)
256				);
257				glColor3f(
258					_RGB.r() + _intensity(i, j) - 1.0f,
259					_RGB.g() + _intensity(i, j) - 1.0f,
260					_RGB.b() + _intensity(i, j) - 1.0f
261				);
262				glTexCoord2d(
263					_gridPos(i, j).x() - _vertex(i, j).x(),
264					_gridPos(i, j).y() - _vertex(i, j).y()
265				);
266				glVertex3f(
267					_gridPos(i, j).x(),
268					_gridPos(i, j).y(),
269					_intensity(i, j)
270				);
271			}
272			glEnd();
273		}
274	}
275
276	glPopMatrix();
277}