aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llrender/llrendersphere.cpp
diff options
context:
space:
mode:
authorJacek Antonelli2008-09-06 18:24:57 -0500
committerJacek Antonelli2008-09-06 18:25:07 -0500
commit798d367d54a6c6379ad355bd8345fa40e31e7fe9 (patch)
tree1921f1708cd0240648c97bc02df2c2ab5f2fc41e /linden/indra/llrender/llrendersphere.cpp
parentSecond Life viewer sources 1.20.15 (diff)
downloadmeta-impy-798d367d54a6c6379ad355bd8345fa40e31e7fe9.zip
meta-impy-798d367d54a6c6379ad355bd8345fa40e31e7fe9.tar.gz
meta-impy-798d367d54a6c6379ad355bd8345fa40e31e7fe9.tar.bz2
meta-impy-798d367d54a6c6379ad355bd8345fa40e31e7fe9.tar.xz
Second Life viewer sources 1.21.0-RC
Diffstat (limited to 'linden/indra/llrender/llrendersphere.cpp')
-rw-r--r--linden/indra/llrender/llrendersphere.cpp182
1 files changed, 182 insertions, 0 deletions
diff --git a/linden/indra/llrender/llrendersphere.cpp b/linden/indra/llrender/llrendersphere.cpp
new file mode 100644
index 0000000..0c3cd19
--- /dev/null
+++ b/linden/indra/llrender/llrendersphere.cpp
@@ -0,0 +1,182 @@
1/**
2 * @file llrendersphere.cpp
3 * @brief implementation of the LLRenderSphere class.
4 *
5 * $LicenseInfo:firstyear=2001&license=viewergpl$
6 *
7 * Copyright (c) 2001-2008, Linden Research, Inc.
8 *
9 * Second Life Viewer Source Code
10 * The source code in this file ("Source Code") is provided by Linden Lab
11 * to you under the terms of the GNU General Public License, version 2.0
12 * ("GPL"), unless you have obtained a separate licensing agreement
13 * ("Other License"), formally executed by you and Linden Lab. Terms of
14 * the GPL can be found in doc/GPL-license.txt in this distribution, or
15 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
16 *
17 * There are special exceptions to the terms and conditions of the GPL as
18 * it is applied to this Source Code. View the full text of the exception
19 * in the file doc/FLOSS-exception.txt in this software distribution, or
20 * online at http://secondlifegrid.net/programs/open_source/licensing/flossexception
21 *
22 * By copying, modifying or distributing this software, you acknowledge
23 * that you have read and understood your obligations described above,
24 * and agree to abide by those obligations.
25 *
26 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
27 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
28 * COMPLETENESS OR PERFORMANCE.
29 * $/LicenseInfo$
30 */
31
32// Sphere creates a set of display lists that can then be called to create
33// a lit sphere at different LOD levels. You only need one instance of sphere
34// per viewer - then call the appropriate list.
35
36#include "linden_common.h"
37
38#include "llrendersphere.h"
39#include "llerror.h"
40
41#include "llglheaders.h"
42
43GLUquadricObj *gQuadObj2 = NULL;
44LLRenderSphere gSphere;
45
46void drawSolidSphere(GLdouble radius, GLint slices, GLint stacks);
47
48void drawSolidSphere(GLdouble radius, GLint slices, GLint stacks)
49{
50 if (!gQuadObj2)
51 {
52 gQuadObj2 = gluNewQuadric();
53 if (!gQuadObj2)
54 {
55 llwarns << "drawSolidSphere couldn't allocate quadric" << llendl;
56 return;
57 }
58 }
59
60 gluQuadricDrawStyle(gQuadObj2, GLU_FILL);
61 gluQuadricNormals(gQuadObj2, GLU_SMOOTH);
62 // If we ever changed/used the texture or orientation state
63 // of quadObj, we'd need to change it to the defaults here
64 // with gluQuadricTexture and/or gluQuadricOrientation.
65 gluQuadricTexture(gQuadObj2, GL_TRUE);
66 gluSphere(gQuadObj2, radius, slices, stacks);
67}
68
69
70// lat = 0 is Z-axis
71// lon = 0, lat = 90 at X-axis
72void lat2xyz(LLVector3 * result, F32 lat, F32 lon)
73{
74 // Convert a latitude and longitude to x,y,z on a normal sphere and return it in result
75 F32 r;
76 result->mV[VX] = (F32) (cos(lon * DEG_TO_RAD) * sin(lat * DEG_TO_RAD));
77 result->mV[VY] = (F32) (sin(lon * DEG_TO_RAD) * sin(lat * DEG_TO_RAD));
78 r = (F32) pow(result->mV[VX] * result->mV[VX] + result->mV[VY] * result->mV[VY], 0.5f);
79 if (r == 1.0f)
80 {
81 result->mV[VZ] = 0.0f;
82 }
83 else
84 {
85 result->mV[VZ] = (F32) pow(1 - r*r, 0.5f);
86 if (lat > 90.01)
87 {
88 result->mV[VZ] *= -1.0;
89 }
90 }
91}
92
93void lat2xyz_rad(LLVector3 * result, F32 lat, F32 lon)
94{
95 // Convert a latitude and longitude to x,y,z on a normal sphere and return it in result
96 F32 r;
97 result->mV[VX] = (F32) (cos(lon) * sin(lat));
98 result->mV[VY] = (F32) (sin(lon) * sin(lat));
99 r = (F32) pow(result->mV[VX] * result->mV[VX] + result->mV[VY] * result->mV[VY], 0.5f);
100 if (r == 1.0f)
101 result->mV[VZ] = 0.0f;
102 else
103 {
104 result->mV[VZ] = (F32) pow(1 - r*r, 0.5f);
105 if (lat > F_PI_BY_TWO) result->mV[VZ] *= -1.0;
106 }
107}
108
109// A couple thoughts on sphere drawing:
110// 1) You need more slices than stacks, but little less than 2:1
111// 2) At low LOD, setting stacks to an odd number avoids a "band" around the equator, making things look smoother
112void LLRenderSphere::prerender()
113{
114 // Create a series of display lists for different LODs
115 mDList[0] = glGenLists(1);
116 glNewList(mDList[0], GL_COMPILE);
117 drawSolidSphere(1.0, 30, 20);
118 glEndList();
119
120 mDList[1] = glGenLists(1);
121 glNewList(mDList[1], GL_COMPILE);
122 drawSolidSphere(1.0, 20, 15);
123 glEndList();
124
125 mDList[2] = glGenLists(1);
126 glNewList(mDList[2], GL_COMPILE);
127 drawSolidSphere(1.0, 12, 8);
128 glEndList();
129
130 mDList[3] = glGenLists(1);
131 glNewList(mDList[3], GL_COMPILE);
132 drawSolidSphere(1.0, 8, 5);
133 glEndList();
134}
135
136void LLRenderSphere::cleanupGL()
137{
138 for (S32 detail = 0; detail < 4; detail++)
139 {
140 glDeleteLists(mDList[detail], 1);
141 mDList[detail] = 0;
142 }
143
144 if (gQuadObj2)
145 {
146 gluDeleteQuadric(gQuadObj2);
147 gQuadObj2 = NULL;
148 }
149}
150
151// Constants here are empirically derived from my eyeballs, JNC
152//
153// The toughest adjustment is the cutoff for the lowest LOD
154// Maybe we should have more LODs at the low end?
155void LLRenderSphere::render(F32 pixel_area)
156{
157 S32 level_of_detail;
158
159 if (pixel_area > 10000.f)
160 {
161 level_of_detail = 0;
162 }
163 else if (pixel_area > 800.f)
164 {
165 level_of_detail = 1;
166 }
167 else if (pixel_area > 100.f)
168 {
169 level_of_detail = 2;
170 }
171 else
172 {
173 level_of_detail = 3;
174 }
175 glCallList(mDList[level_of_detail]);
176}
177
178
179void LLRenderSphere::render()
180{
181 glCallList(mDList[0]);
182}