PageRenderTime 52ms CodeModel.GetById 16ms app.highlight 28ms RepoModel.GetById 2ms app.codeStats 0ms

/xbmc/screensavers/rsxs-0.9/src/implicit.hh

http://github.com/xbmc/xbmc
C++ Header | 128 lines | 89 code | 17 blank | 22 comment | 2 complexity | c5ae245b8011e42e187a66caba55dca1 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#ifndef _IMPLICIT_HH
 24#define _IMPLICIT_HH
 25
 26#include <common.hh>
 27
 28#include <color.hh>
 29#include <memory>
 30#include <vector.hh>
 31
 32typedef float (*ImplicitField)(const Vector&);
 33typedef std::list<Vector> CrawlPointVector;
 34
 35template <typename T> class LazyVector {
 36private:
 37	T* _data;
 38	unsigned int _used, _capacity;
 39public:
 40	LazyVector() : _data(new T[1000]), _used(0), _capacity(1000) {}
 41	LazyVector(const LazyVector& lv) : _data(new T[lv._capacity]), 
 42		_used(lv._used), _capacity(lv._capacity) {
 43		std::copy(lv._data, lv._data + _used, _data);
 44	}
 45	~LazyVector() { delete[] _data; }
 46
 47	void reset() { _used = 0; }
 48	unsigned int size() const { return _used; }
 49
 50	typedef const T* const_iterator;
 51	const T* begin() const { return _data; }
 52	const T* end() const { return _data + _used; }
 53
 54	void push_back(const T& v) {
 55		if (_used == _capacity) {
 56			_capacity += 1000;
 57			T* temp = new T[_capacity];
 58			std::uninitialized_copy(_data, _data + _used, temp);
 59			delete[] _data;
 60			_data = temp;
 61		}
 62		_data[_used++] = v;
 63	}
 64};
 65
 66enum Axis {
 67	X_AXIS,
 68	Y_AXIS,
 69	Z_AXIS
 70};
 71
 72class Implicit : public ResourceManager::Resource<void> {
 73private:
 74	static unsigned int _width, _height, _length;
 75	static unsigned int _width1, _height1, _length1;
 76	static Vector _lbf;
 77	static float _cw;
 78
 79	static unsigned int _cubeTable[256][17];
 80	static bool _crawlTable[256][6];
 81public:
 82	static void init(unsigned int, unsigned int, unsigned int, float);
 83	void operator()() const {}
 84private:
 85	struct Info {
 86		struct Cube {
 87			unsigned int serial;
 88			unsigned char mask;
 89		} cube;
 90		struct Corner {
 91			unsigned int serial;
 92			float value;
 93			Vector XYZ;
 94		} corner;
 95		struct Edge {
 96			unsigned int serial;
 97			unsigned int index;
 98		} edge[3];
 99	};
100	std::vector<Info> _info;
101
102	struct VertexData {
103		float nx, ny, nz;
104		float x, y, z;
105	};
106
107	unsigned int _serial;
108
109	ImplicitField _field;
110	float _threshold;
111
112	LazyVector<VertexData> _vertices;
113	LazyVector<unsigned int> _indices;
114	LazyVector<unsigned int> _lengths;
115
116	inline unsigned char calculateCube(unsigned int);
117	inline void crawl(unsigned int, unsigned int, unsigned int);
118	inline void polygonize(unsigned int);
119	inline void addVertex(Axis, unsigned int);
120public:
121	Implicit(ImplicitField);
122
123	void update(float, const CrawlPointVector&);
124	void update(float);
125	void draw(GLenum = GL_TRIANGLE_STRIP) const;
126};
127
128#endif // _IMPLICIT_HH