PageRenderTime 36ms CodeModel.GetById 1ms app.highlight 30ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/llcamera.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 217 lines | 132 code | 39 blank | 46 comment | 0 complexity | aa5931b5ffaf5fb111e80958e5f203d4 MD5 | raw file
  1/** 
  2 * @file llcamera.h
  3 * @brief Header file for the LLCamera class.
  4 *
  5 * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#ifndef LL_CAMERA_H
 28#define LL_CAMERA_H
 29
 30
 31#include "llmath.h"
 32#include "llcoordframe.h"
 33#include "llplane.h"
 34#include "llvector4a.h"
 35
 36const F32 DEFAULT_FIELD_OF_VIEW 	= 60.f * DEG_TO_RAD;
 37const F32 DEFAULT_ASPECT_RATIO 		= 640.f / 480.f;
 38const F32 DEFAULT_NEAR_PLANE 		= 0.25f;
 39const F32 DEFAULT_FAR_PLANE 		= 64.f;	// far reaches across two horizontal, not diagonal, regions
 40
 41const F32 MAX_ASPECT_RATIO 	= 50.0f;
 42const F32 MAX_NEAR_PLANE 	= 10.f;
 43const F32 MAX_FAR_PLANE 	= 100000.0f; //1000000.0f; // Max allowed. Not good Z precision though.
 44const F32 MAX_FAR_CLIP		= 512.0f;
 45
 46const F32 MIN_ASPECT_RATIO 	= 0.02f;
 47const F32 MIN_NEAR_PLANE 	= 0.1f;
 48const F32 MIN_FAR_PLANE 	= 0.2f;
 49
 50// Min/Max FOV values for square views. Call getMin/MaxView to get extremes based on current aspect ratio.
 51static const F32 MIN_FIELD_OF_VIEW = 5.0f * DEG_TO_RAD;
 52static const F32 MAX_FIELD_OF_VIEW = 175.f * DEG_TO_RAD;
 53
 54// An LLCamera is an LLCoorFrame with a view frustum.
 55// This means that it has several methods for moving it around 
 56// that are inherited from the LLCoordFrame() class :
 57//
 58// setOrigin(), setAxes()
 59// translate(), rotate()
 60// roll(), pitch(), yaw()
 61// etc...
 62
 63
 64class LLCamera
 65: 	public LLCoordFrame
 66{
 67public:
 68	
 69	LLCamera(const LLCamera& rhs)
 70	{
 71		*this = rhs;
 72	}
 73	
 74	enum {
 75		PLANE_LEFT = 0,
 76		PLANE_RIGHT = 1,
 77		PLANE_BOTTOM = 2,
 78		PLANE_TOP = 3,
 79		PLANE_NUM = 4
 80	};
 81	enum {
 82		PLANE_LEFT_MASK = (1<<PLANE_LEFT),
 83		PLANE_RIGHT_MASK = (1<<PLANE_RIGHT),
 84		PLANE_BOTTOM_MASK = (1<<PLANE_BOTTOM),
 85		PLANE_TOP_MASK = (1<<PLANE_TOP),
 86		PLANE_ALL_MASK = 0xf
 87	};
 88
 89	enum
 90	{
 91		AGENT_PLANE_LEFT = 0,
 92		AGENT_PLANE_RIGHT,
 93		AGENT_PLANE_NEAR,
 94		AGENT_PLANE_BOTTOM,
 95		AGENT_PLANE_TOP,
 96		AGENT_PLANE_FAR,
 97	};
 98
 99	enum {
100		HORIZ_PLANE_LEFT = 0,
101		HORIZ_PLANE_RIGHT = 1,
102		HORIZ_PLANE_NUM = 2
103	};
104	enum {
105		HORIZ_PLANE_LEFT_MASK = (1<<HORIZ_PLANE_LEFT),
106		HORIZ_PLANE_RIGHT_MASK = (1<<HORIZ_PLANE_RIGHT),
107		HORIZ_PLANE_ALL_MASK = 0x3
108	};
109
110private:
111	LLPlane mAgentPlanes[7];  //frustum planes in agent space a la gluUnproject (I'm a bastard, I know) - DaveP
112	U8 mPlaneMask[8];         // 8 for alignment	
113	
114	F32 mView;					// angle between top and bottom frustum planes in radians.
115	F32 mAspect;				// width/height
116	S32 mViewHeightInPixels;	// for ViewHeightInPixels() only
117	F32 mNearPlane;
118	F32 mFarPlane;
119	LLPlane mLocalPlanes[4];
120	F32 mFixedDistance;			// Always return this distance, unless < 0
121	LLVector3 mFrustCenter;		// center of frustum and radius squared for ultra-quick exclusion test
122	F32 mFrustRadiusSquared;
123	
124	LLPlane mWorldPlanes[PLANE_NUM];
125	LLPlane mHorizPlanes[HORIZ_PLANE_NUM];
126
127	U32 mPlaneCount;  //defaults to 6, if setUserClipPlane is called, uses user supplied clip plane in
128
129	LLVector3 mWorldPlanePos;		// Position of World Planes (may be offset from camera)
130public:
131	LLVector3 mAgentFrustum[8];  //8 corners of 6-plane frustum
132	F32	mFrustumCornerDist;		//distance to corner of frustum against far clip plane
133	LLPlane& getAgentPlane(U32 idx) { return mAgentPlanes[idx]; }
134
135public:
136	LLCamera();
137	LLCamera(F32 vertical_fov_rads, F32 aspect_ratio, S32 view_height_in_pixels, F32 near_plane, F32 far_plane);
138	virtual ~LLCamera();
139	
140
141	void setUserClipPlane(LLPlane& plane);
142	void disableUserClipPlane();
143	virtual void setView(F32 vertical_fov_rads);
144	void setViewHeightInPixels(S32 height);
145	void setAspect(F32 new_aspect);
146	void setNear(F32 new_near);
147	void setFar(F32 new_far);
148
149	F32 getView() const							{ return mView; }				// vertical FOV in radians
150	S32 getViewHeightInPixels() const			{ return mViewHeightInPixels; }
151	F32 getAspect() const						{ return mAspect; }				// width / height
152	F32 getNear() const							{ return mNearPlane; }			// meters
153	F32 getFar() const							{ return mFarPlane; }			// meters
154
155	// The values returned by the min/max view getters depend upon the aspect ratio
156	// at the time they are called and therefore should not be cached.
157	F32 getMinView() const;
158	F32 getMaxView() const;
159	
160	F32 getYaw() const
161	{
162		return atan2f(mXAxis[VY], mXAxis[VX]);
163	}
164	F32 getPitch() const
165	{
166		F32 xylen = sqrtf(mXAxis[VX]*mXAxis[VX] + mXAxis[VY]*mXAxis[VY]);
167		return atan2f(mXAxis[VZ], xylen);
168	}
169
170	const LLPlane& getWorldPlane(S32 index) const	{ return mWorldPlanes[index]; }
171	const LLVector3& getWorldPlanePos() const		{ return mWorldPlanePos; }
172	
173	// Copy mView, mAspect, mNearPlane, and mFarPlane to buffer.
174	// Return number of bytes copied.
175	size_t writeFrustumToBuffer(char *buffer) const;
176
177	// Copy mView, mAspect, mNearPlane, and mFarPlane from buffer.
178	// Return number of bytes copied.
179	size_t readFrustumFromBuffer(const char *buffer);
180	void calcAgentFrustumPlanes(LLVector3* frust);
181	void ignoreAgentFrustumPlane(S32 idx);
182
183	// Returns 1 if partly in, 2 if fully in.
184	// NOTE: 'center' is in absolute frame.
185	S32 sphereInFrustumOld(const LLVector3 &center, const F32 radius) const;
186	S32 sphereInFrustum(const LLVector3 &center, const F32 radius) const;
187	S32 pointInFrustum(const LLVector3 &point) const { return sphereInFrustum(point, 0.0f); }
188	S32 sphereInFrustumFull(const LLVector3 &center, const F32 radius) const { return sphereInFrustum(center, radius); }
189	S32 AABBInFrustum(const LLVector4a& center, const LLVector4a& radius);
190	S32 AABBInFrustumNoFarClip(const LLVector4a& center, const LLVector4a& radius);
191
192	//does a quick 'n dirty sphere-sphere check
193	S32 sphereInFrustumQuick(const LLVector3 &sphere_center, const F32 radius); 
194
195	// Returns height of object in pixels (must be height because field of view
196	// is based on window height).
197	F32 heightInPixels(const LLVector3 &center, F32 radius ) const;
198
199	// return the distance from pos to camera if visible (-distance if not visible)
200	F32 visibleDistance(const LLVector3 &pos, F32 rad, F32 fudgescale = 1.0f, U32 planemask = PLANE_ALL_MASK) const;
201	F32 visibleHorizDistance(const LLVector3 &pos, F32 rad, F32 fudgescale = 1.0f, U32 planemask = HORIZ_PLANE_ALL_MASK) const;
202	void setFixedDistance(F32 distance) { mFixedDistance = distance; }
203	
204	friend std::ostream& operator<<(std::ostream &s, const LLCamera &C);
205
206protected:
207	void calculateFrustumPlanes();
208	void calculateFrustumPlanes(F32 left, F32 right, F32 top, F32 bottom);
209	void calculateFrustumPlanesFromWindow(F32 x1, F32 y1, F32 x2, F32 y2);
210	void calculateWorldFrustumPlanes();
211};
212
213
214#endif
215
216
217