⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 splines.h

📁 3D 游戏界的大牛人 John Carmack 终于放出了 Q3 的源代码
💻 H
📖 第 1 页 / 共 2 页
字号:
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.

This file is part of Quake III Arena source code.

Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/
#ifndef __SPLINES_H
#define __SPLINES_H

extern "C" {
#ifdef Q3RADIANT
#include "../qgl.h"
#else
//#include "../renderer/qgl.h"
#endif
}
#include "util_list.h"
#include "util_str.h"
#include "math_vector.h"

typedef int fileHandle_t;

//extern void glBox(idVec3_t &color, idVec3_t &point, float size);
//extern void glLabeledPoint(idVec3_t &color, idVec3_t &point, float size, const char *label);

static vec4_t blue(0, 0, 1, 1);
static vec4_t red(1, 0, 0, 1);

class idPointListInterface {
public:
	idPointListInterface() {
		selectedPoints.Clear();
	}
	virtual ~idPointListInterface() {}
	
	virtual int numPoints() {
		return 0;
	}
	
	virtual void addPoint(const float x, const float y, const float z) {}
	virtual void addPoint(const idVec3_t &v) {}
	virtual void removePoint(int index) {}
	virtual idVec3_t *getPoint(int index) { return NULL; }
	
	int	selectPointByRay(float ox, float oy, float oz, float dx, float dy, float dz, bool single) {
		idVec3_t origin(ox, oy, oz);
		idVec3_t dir(dx, dy, dz);
		return selectPointByRay(origin, dir, single);
	}

	int	selectPointByRay(const idVec3_t origin, const idVec3_t direction, bool single) {
		int		i, besti, count;
		float	d, bestd;
		idVec3_t	temp, temp2;

		// find the point closest to the ray
		besti = -1;
		bestd = 8;
		count = numPoints();

		for (i=0; i < count; i++) {
			temp = *getPoint(i);
			temp2 = temp;
			temp -= origin;
			d = DotProduct(temp, direction);
			__VectorMA (origin, d, direction, temp);
			temp2 -= temp;
			d = temp2.Length();
			if (d <= bestd) {
				bestd = d;
				besti = i;
			}
		}

		if (besti >= 0) {
			selectPoint(besti, single);
		}

		return besti;
	}

	int isPointSelected(int index) {
		int count = selectedPoints.Num();
		for (int i = 0; i < count; i++) {
			if (selectedPoints[i] == index) {
				return i;
			}
		}
		return -1;
	}
	
	int selectPoint(int index, bool single) {
		if (index >= 0 && index < numPoints()) {
			if (single) {
				deselectAll();
			} else {
				if (isPointSelected(index) >= 0) {
					selectedPoints.Remove(index);
				}
			}
			return selectedPoints.Append(index);
		}
		return -1;
	}
	
	void selectAll() {
		selectedPoints.Clear();
		for (int i = 0; i < numPoints(); i++) {
			selectedPoints.Append(i);
		}
	}

	void deselectAll() {
		selectedPoints.Clear();
	}

	virtual void updateSelection(float x, float y, float z) {
		idVec3_t move(x, y, z);
		updateSelection(move);
	}

	virtual void updateSelection(const idVec3_t &move) {
		int count = selectedPoints.Num();
		for (int i = 0; i < count; i++) {
			*getPoint(selectedPoints[i]) += move;
		}
	}
/*
	void drawSelection() {
		int count = selectedPoints.Num();
		for (int i = 0; i < count; i++) {
			glBox(red, *getPoint(selectedPoints[i]), 4);
		}
	}
*/
protected:
	idList<int> selectedPoints;

};


class idSplineList {

public:

	idSplineList() {
		clear();
	}

	idSplineList(const char *p) {
		clear();
		name = p;
	};

	~idSplineList() {
		clear();
	};

	void clearControl() {
		for (int i = 0; i < controlPoints.Num(); i++) {
			delete controlPoints[i];
		}
		controlPoints.Clear();
	}

	void clearSpline() {
		for (int i = 0; i < splinePoints.Num(); i++) {
			delete splinePoints[i];
		}
		splinePoints.Clear();
	}

	void parse(const char *(*text));
	void write(fileHandle_t file, const char *name);

	void clear() {
		clearControl();
		clearSpline();
		splineTime.Clear();
		selected = NULL;
		dirty = true;
		activeSegment = 0;
		granularity = 0.025;
		pathColor.set(1.0, 0.5, 0.0);
		controlColor.set(0.7, 0.0, 1.0);
		segmentColor.set(0.0, 0.0, 1.0);
		activeColor.set(1.0, 0.0, 0.0);
	}

	void initPosition(long startTime, long totalTime);
	const idVec3_t *getPosition(long time);


//	void draw(bool editMode);
	void addToRenderer();

	void setSelectedPoint(idVec3_t *p);
	idVec3_t *getSelectedPoint() {
	  return selected;
	}

	void addPoint(const idVec3_t &v) {
		controlPoints.Append(new idVec3_t(v));
		dirty = true;
	}

	void addPoint(float x, float y, float z) {
		controlPoints.Append(new idVec3_t(x, y, z));
		dirty = true;
	}

	void updateSelection(const idVec3_t &move);

	void startEdit() {
		editMode = true;
	}
		
	void stopEdit() {
		editMode = false;
	}

	void buildSpline();

	void setGranularity(float f) {
		granularity = f;
	}

	float getGranularity() {
		return granularity;
	}

	int numPoints() {
		return controlPoints.Num();
	}

	idVec3_t *getPoint(int index) {
		assert(index >= 0 && index < controlPoints.Num());
		return controlPoints[index];
	}

	idVec3_t *getSegmentPoint(int index) {
		assert(index >= 0 && index < splinePoints.Num());
		return splinePoints[index];
	}


	void setSegmentTime(int index, int time) {
		assert(index >= 0 && index < splinePoints.Num());
		splineTime[index] = time;
	}

	double getSegmentTime(int index) {
		assert(index >= 0 && index < splinePoints.Num());
		return splineTime[index];
	}
	void addSegmentTime(int index, int time) {
		assert(index >= 0 && index < splinePoints.Num());
		splineTime[index] += time;
	}

	float totalDistance();

	static idVec3_t zero;

	int getActiveSegment() {
		return activeSegment;
	}

	void setActiveSegment(int i) {
		//assert(i >= 0 && (splinePoints.Num() > 0 && i < splinePoints.Num()));
		activeSegment = i;
	}

	int numSegments() {
		return splinePoints.Num();
	}

	void setColors(idVec3_t &path, idVec3_t &segment, idVec3_t &control, idVec3_t &active) {
		pathColor = path;
		segmentColor = segment;
		controlColor = control;
		activeColor = active;
	}

	const char *getName() {
		return name.c_str();
	}

	void setName(const char *p) {
		name = p;
	}

	bool validTime() {
		if (dirty) {
			buildSpline();
		}
		// gcc doesn't allow static casting away from bools
		// why?  I've no idea...
		return (bool)(splineTime.Num() > 0 && splineTime.Num() == splinePoints.Num());
	}

	void setTime(long t) {
		time = t;
	}

	void setBaseTime(long t) {
		baseTime = t;
	}

protected:
	idStr name;
	float calcSpline(int step, float tension);
	idList<idVec3_t*> controlPoints;
	idList<idVec3_t*> splinePoints;
	idList<double> splineTime;
	idVec3_t *selected;
	idVec3_t pathColor, segmentColor, controlColor, activeColor;
	float granularity;
	bool editMode;
	bool dirty;
	int activeSegment;
	long baseTime;
	long time;
	friend class idCamera;
};

// time in milliseconds 
// velocity where 1.0 equal rough walking speed
struct idVelocity {
	idVelocity(long start, long duration, float s) {
		startTime = start;
		time = duration;
		speed = s;
	}
	long	startTime;
	long	time;
	float	speed;
};

// can either be a look at or origin position for a camera
// 
class idCameraPosition : public idPointListInterface {
public:
	
	virtual void clear() {
		editMode = false;
		for (int i = 0; i < velocities.Num(); i++) {
			delete velocities[i];
			velocities[i] = NULL;
		}
		velocities.Clear();
	}

	idCameraPosition(const char *p) {
		name = p;
	}

	idCameraPosition() {
		time = 0;
		name = "position";
	}

	idCameraPosition(long t) {
		time = t;
	}

	virtual ~idCameraPosition() {
		clear();
	}

	
	// this can be done with RTTI syntax but i like the derived classes setting a type
	// makes serialization a bit easier to see
	//
	enum positionType {
		FIXED = 0x00,
		INTERPOLATED,
		SPLINE,
		POSITION_COUNT
	};


	virtual void start(long t) {
		startTime = t;
	}

	long getTime() {
		return time;
	}

	virtual void setTime(long t) {
		time = t;
	}

	float getVelocity(long t) {
		long check = t - startTime;
		for (int i = 0; i < velocities.Num(); i++) {
			if (check >= velocities[i]->startTime && check <= velocities[i]->startTime + velocities[i]->time) {
				return velocities[i]->speed;
			}
		}
		return baseVelocity;
	}

	void addVelocity(long start, long duration, float speed) {
		velocities.Append(new idVelocity(start, duration, speed));
	}

	virtual const idVec3_t *getPosition(long t) { 
		assert(true);
		return NULL;
	}

//	virtual void draw(bool editMode) {};

	virtual void parse(const char *(*text)) {};
	virtual void write(fileHandle_t file, const char *name);
	virtual bool parseToken(const char *key, const char *(*text));

	const char *getName() {
		return name.c_str();
	}

	void setName(const char *p) {
		name = p;
	}

	virtual void startEdit() {
		editMode = true;
	}

	virtual void stopEdit() {
		editMode = false;
	}

//	virtual void draw() {};

	const char *typeStr() {
		return positionStr[static_cast<int>(type)];
	}

	void calcVelocity(float distance) {
		float secs = (float)time / 1000;
		baseVelocity = distance / secs;
	}

protected:
	static const char* positionStr[POSITION_COUNT];
	long		startTime;
	long		time;
	idCameraPosition::positionType type;
	idStr		name;
	bool	editMode;
	idList<idVelocity*> velocities;
	float		baseVelocity;
};

class idFixedPosition : public idCameraPosition {
public:

	void init() {
		pos.Zero();
		type = idCameraPosition::FIXED;
	}
	
	idFixedPosition() : idCameraPosition() {
		init();
	}
	
	idFixedPosition(idVec3_t p) : idCameraPosition() {
		init();
		pos = p;
	}

	virtual void addPoint(const idVec3_t &v) {
		pos = v;
	}
	
	virtual void addPoint(const float x, const float y, const float z) {
		pos.set(x, y, z);
	}


	~idFixedPosition() {
	}

	virtual const idVec3_t *getPosition(long t) { 
		return &pos;
	}

	void parse(const char *(*text));
	void write(fileHandle_t file, const char *name);

	virtual int numPoints() {
		return 1;
	}

	virtual idVec3_t *getPoint(int index) {
		if (index != 0) {
			assert(true);
		};
		return &pos;
	}
/*
	virtual void draw(bool editMode) {
		glLabeledPoint(blue, pos, (editMode) ? 5 : 3, "Fixed point");
	}
*/
protected:
	idVec3_t pos;
};

class idInterpolatedPosition : public idCameraPosition {
public:

	void init() {
		type = idCameraPosition::INTERPOLATED;
		first = true;
		startPos.Zero();
		endPos.Zero();
	}
	
	idInterpolatedPosition() : idCameraPosition() {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -