PageRenderTime 53ms CodeModel.GetById 2ms app.highlight 48ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/screensavers/rsxs-0.9/src/hyperspace/tunnel.cc

http://github.com/xbmc/xbmc
C++ | 174 lines | 123 code | 25 blank | 26 comment | 20 complexity | 9ed7e7f9a4ba4dcb3e55f2e550ff58ba 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) 2005 Terence M. Welsh, available from www.reallyslick.com
 22 */
 23#include <common.hh>
 24
 25#include <caustic.hh>
 26#include <color.hh>
 27#include <spline.hh>
 28#include <tunnel.hh>
 29#include <vector.hh>
 30
 31#define TUNNEL_RESOLUTION 20
 32
 33namespace Tunnel {
 34	unsigned int _numSections;
 35	unsigned int _section;
 36
 37	float _radius;
 38	float _widthOffset;
 39	float _texSpin;
 40
 41	stdx::dim3<Vector, TUNNEL_RESOLUTION + 1, TUNNEL_RESOLUTION + 1> _v;
 42	stdx::dim3<Vector, TUNNEL_RESOLUTION + 1, TUNNEL_RESOLUTION + 1> _t;
 43	stdx::dim3<RGBColor, TUNNEL_RESOLUTION + 1, TUNNEL_RESOLUTION + 1> _c;
 44
 45	float _loH, _loS, _loL;
 46	float _hiH, _hiS, _hiL;
 47
 48	void make();
 49};
 50
 51void Tunnel::init() {
 52	CausticTextures::init();
 53
 54	_radius = 0.1f;
 55	_widthOffset = 0.0f;
 56	_texSpin = 0.0f;
 57
 58	_numSections = Spline::points - 5;
 59	_section = 0;
 60
 61	_v.resize(_numSections);
 62	_t.resize(_numSections);
 63	_c.resize(_numSections);
 64
 65	_loH = _loS = _hiH = _hiS = 0.0f;
 66	_loL = _hiL = M_PI;
 67}
 68
 69void Tunnel::make() {
 70	_widthOffset += Common::elapsedTime * 1.5f;
 71	while (_widthOffset >= M_PI * 2.0f)
 72		_widthOffset -= M_PI * 2.0f;
 73	_texSpin += Common::elapsedTime * 0.1f;
 74	while (_texSpin >= M_PI * 2.0f)
 75		_texSpin -= M_PI * 2.0f;
 76
 77	_loH += Common::elapsedTime * 0.04f;
 78	_hiH += Common::elapsedTime * 0.15f;
 79	_loS += Common::elapsedTime * 0.04f;
 80	_hiS += Common::elapsedTime;
 81	_loL += Common::elapsedTime * 0.04f;
 82	_hiL += Common::elapsedTime * 0.5f;
 83
 84	while (_loH > M_PI * 2.0f) _loH -= M_PI * 2.0f;
 85	while (_hiH > M_PI * 2.0f) _hiH -= M_PI * 2.0f;
 86	while (_loS > M_PI * 2.0f) _loS -= M_PI * 2.0f;
 87	while (_hiS > M_PI * 2.0f) _hiS -= M_PI * 2.0f;
 88	while (_loL > M_PI * 2.0f) _loL -= M_PI * 2.0f;
 89	while (_hiL > M_PI * 2.0f) _hiL -= M_PI * 2.0f;
 90
 91	unsigned int n = _numSections;
 92	for (unsigned int k = 0; k < n; ++k) {
 93		// create new vertex data for this section
 94		for (unsigned int i = 0; i <= TUNNEL_RESOLUTION; ++i) {
 95			float where = float(i) / float(TUNNEL_RESOLUTION);
 96
 97			Vector pos(Spline::at(k + 2, where));
 98			Vector dir(Spline::direction(k + 2, where));
 99			RotationMatrix rot(RotationMatrix::lookAt(Vector(), dir, Vector(0.0f, 1.0f, 0.0f)));
100
101			for (unsigned int j = 0; j <= TUNNEL_RESOLUTION; ++j) {
102				float angle = float(j) * M_PI * 2.0f / float(TUNNEL_RESOLUTION);
103				Vector vertex(rot.transform(Vector(
104					(_radius + _radius * 0.5f * std::cos(2.0f * pos.x() + _widthOffset)) * std::cos(angle),
105					(_radius + _radius * 0.5f * std::cos(pos.z() + _widthOffset)) * std::sin(angle),
106					0.0f
107				)));
108
109				// set vertex coordinates
110				_v(k, i, j) = pos + vertex;
111
112				// set texture coordinates
113				_t(k, i, j).x() = 4.0f * float(i) / float(TUNNEL_RESOLUTION);
114				_t(k, i, j).y() = float(j) / float(TUNNEL_RESOLUTION) + std::cos(_texSpin);
115
116				// set colors
117				HSLColor HSL(
118					2.0f * std::cos(0.1f * _v(k, i, j).x() + _loH) - 1.0f,
119					0.25f * (std::cos(0.013f * _v(k, i, j).y() - _loS)
120						+ std::cos(_v(k, i, j).z() + _hiS) + 2.0f),
121					2.0f * std::cos(0.01f * _v(k, i, j).z() + _loL)
122						+ std::cos(0.4f * _v(k, i, j).x() - _hiL)
123						+ 0.3f * std::cos(4.0f * (_v(k, i, j).x() + _v(k, i, j).y() + _v(k, i, j).z()))
124				);
125				HSL.clamp();
126				if (HSL.s() > 0.7f) HSL.s() = 0.7f;
127				_c(k, i, j) = RGBColor(HSL);
128			}
129		}
130	}
131}
132
133void Tunnel::draw() {
134	Tunnel::make();
135
136	glPushAttrib(GL_ENABLE_BIT);
137		glEnable(GL_TEXTURE_2D);
138		CausticTextures::use();
139
140		unsigned int n = _numSections;
141		if (Hack::shaders) {
142			for (unsigned int k = 0; k < n; ++k) {
143				for (unsigned int i = 0; i < TUNNEL_RESOLUTION; ++i) {
144					glBegin(GL_TRIANGLE_STRIP);
145					for (unsigned int j = 0; j <= TUNNEL_RESOLUTION; ++j) {
146						glColor4f(_c(k, i + 1, j).r(), _c(k, i + 1, j).g(), _c(k, i + 1, j).b(), Hack::lerp);
147						glTexCoord2fv(_t(k, i + 1, j).get());
148						glVertex3fv(_v(k, i + 1, j).get());
149						glColor4f(_c(k, i, j).r(), _c(k, i, j).g(), _c(k, i, j).b(), Hack::lerp);
150						glTexCoord2fv(_t(k, i, j).get());
151						glVertex3fv(_v(k, i, j).get());
152					}
153					glEnd();
154				}
155			}
156		} else {
157			for (unsigned int k = 0; k < n; ++k) {
158				for (unsigned int i = 0; i < TUNNEL_RESOLUTION; ++i) {
159					glBegin(GL_TRIANGLE_STRIP);
160					for (unsigned int j = 0; j <= TUNNEL_RESOLUTION; ++j) {
161						glColor3fv(_c(k, i + 1, j).get());
162						glTexCoord2fv(_t(k, i + 1, j).get());
163						glVertex3fv(_v(k, i + 1, j).get());
164						glColor3fv(_c(k, i, j).get());
165						glTexCoord2fv(_t(k, i, j).get());
166						glVertex3fv(_v(k, i, j).get());
167					}
168					glEnd();
169				}
170			}
171		}
172
173	glPopAttrib();
174}