Oolite
Loading...
Searching...
No Matches
OOPlanetDrawable.m
Go to the documentation of this file.
1/*
2
3 OOPlanetDrawable.m
4
5 Oolite
6 Copyright (C) 2004-2013 Giles C Williams and contributors
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 MA 02110-1301, USA.
22
23 */
24
25#import "OOStellarBody.h"
26#if NEW_PLANETS
27
28#import "MyOpenGLView.h"
29#import "OOMacroOpenGL.h"
30#import "OOOpenGL.h"
31#import "OOPlanetData.h"
32#import "OOPlanetDrawable.h"
34#import "Universe.h"
35
36#ifdef OO_DEBUG_MONITOR_ENABLED
37#import "Entity.h"
38#import "OODebugFlags.h"
39#import "OODebugGLDrawing.h"
40#endif
41
42#define LOD_GRANULARITY ((float)(kOOPlanetDataLevels - 1))
43#define LOD_FACTOR (1.0 / 4.0)
44
45@interface OOPlanetDrawable (Private)
46
47- (void)recalculateTransform;
48
49- (void)debugDrawNormals;
50
51- (void)renderCommonParts;
52
53@end
54
55@implementation OOPlanetDrawable
56
57+ (instancetype)planetWithTextureName:(NSString*)textureName radius:(float)radius
58{
59 OOPlanetDrawable* result = [[[self alloc] init] autorelease];
60 [result setTextureName:textureName];
61 [result setRadius:radius];
62
63 return result;
64}
65
66+ (instancetype)atmosphereWithRadius:(float)radius
67{
68 OOPlanetDrawable* result = [[[self alloc] initAsAtmosphere] autorelease];
69 [result setRadius:radius];
70
71 return result;
72}
73
74- (id)init
75{
76 if ((self = [super init])) {
77 _radius = 1.0f;
78 [self recalculateTransform];
79 [self setLevelOfDetail:0.5f];
80 }
81
82 return self;
83}
84
85- (id)initAsAtmosphere
86{
87 if ((self = [self init])) {
88 _isAtmosphere = YES;
89 }
90
91 return self;
92}
93
94- (void)dealloc
95{
96 DESTROY(_material);
97
98 [super dealloc];
99}
100
101- (id)copyWithZone:(NSZone*)zone
102{
103 OOPlanetDrawable* copy = [[[self class] allocWithZone:zone] init];
104 [copy setMaterial:[self material]];
105 copy->_isAtmosphere = _isAtmosphere;
106 copy->_radius = _radius;
107 copy->_transform = _transform;
108 copy->_lod = _lod;
109
110 return copy;
111}
112
113- (OOMaterial*)material
114{
115 return _material;
116}
117
118- (void)setMaterial:(OOMaterial*)material
119{
120 [_material autorelease];
121 _material = [material retain];
122}
123
124- (NSString*)textureName
125{
126 return [_material name];
127}
128
129- (void)setTextureName:(NSString*)textureName
130{
131 if (![textureName isEqual:[self textureName]]) {
132 [_material release];
133 NSDictionary* spec = [@"{diffuse_map={repeat_s=yes;cube_map=yes};}" propertyList];
134 _material = [[OOSingleTextureMaterial alloc] initWithName:textureName configuration:spec];
135 }
136}
137
138- (float)radius
139{
140 return _radius;
141}
142
143- (void)setRadius:(float)radius
144{
145 _radius = fabsf(radius);
146 [self recalculateTransform];
147}
148
149- (float)levelOfDetail
150{
151 return (float)_lod / LOD_GRANULARITY;
152}
153
154- (void)setLevelOfDetail:(float)lod
155{
156 _lod = roundf(OOClamp_0_1_f(lod) * LOD_GRANULARITY);
157}
158
159- (void)calculateLevelOfDetailForViewDistance:(float)distance
160{
161 BOOL simple = [UNIVERSE reducedDetail];
162 float drawFactor = [[UNIVERSE gameView] viewSize].width / (simple ? 100.0 : 40.0);
163 float drawRatio2 = drawFactor * _radius / sqrtf(distance); // proportional to size on screen in pixels
164
165 float lod = sqrtf(drawRatio2 * LOD_FACTOR);
166 if (simple) {
167 lod -= 0.5f / LOD_GRANULARITY; // Make LOD transitions earlier.
168 lod = OOClamp_0_max_f(lod, (LOD_GRANULARITY - 1) / LOD_GRANULARITY); // Don't use highest LOD.
169 }
170 [self setLevelOfDetail:lod];
171}
172
173- (void)renderOpaqueParts
174{
175 assert(_lod < kOOPlanetDataLevels);
176
178
179 [self renderCommonParts];
180
182}
183
184- (void)renderTranslucentParts
185{
186 assert(_lod < kOOPlanetDataLevels);
187
188 // yes, opaque - necessary changes made later
190
191 [self renderCommonParts];
192
194}
195
196- (void)renderTranslucentPartsOnOpaquePass
197{
198 assert(_lod < kOOPlanetDataLevels);
199
201
202 // yes, opaque - necessary changes made later
204
205 OOGL(glDisable(GL_DEPTH_TEST));
206 [self renderCommonParts];
207 OOGL(glEnable(GL_DEPTH_TEST));
208
210}
211
212- (void)renderCommonParts
213{
214 const OOPlanetDataLevel* data = &kPlanetData[_lod];
215
217
218 OOGL(glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT));
219 OOGL(glShadeModel(GL_SMOOTH));
220
221 if (_isAtmosphere) {
222 OOGL(glEnable(GL_BLEND));
223 OOGL(glDepthMask(GL_FALSE));
224 } else {
225 OOGL(glDisable(GL_BLEND));
226 }
227
228 // Scale the ball.
230 OOGLMultModelView(_transform);
231
232 [_material apply];
233
234 OOGL(glEnable(GL_LIGHTING));
235 OOGL(glEnable(GL_TEXTURE_2D));
236
237#if OO_TEXTURE_CUBE_MAP
238 if ([_material wantsNormalsAsTextureCoordinates]) {
239 OOGL(glDisable(GL_TEXTURE_2D));
240 OOGL(glEnable(GL_TEXTURE_CUBE_MAP));
241 }
242#endif
243
244 OOGL(glDisableClientState(GL_COLOR_ARRAY));
245
246 OOGL(glEnableClientState(GL_TEXTURE_COORD_ARRAY));
247
248 OOGL(glVertexPointer(3, GL_FLOAT, 0, kOOPlanetVertices));
249 if ([_material wantsNormalsAsTextureCoordinates]) {
250 OOGL(glTexCoordPointer(3, GL_FLOAT, 0, kOOPlanetVertices));
251 } else {
252 OOGL(glTexCoordPointer(2, GL_FLOAT, 0, kOOPlanetTexCoords));
253 }
254
255 // FIXME: instead of GL_RESCALE_NORMAL, consider copying and transforming the vertex array for each planet.
256 OOGL(glEnable(GL_RESCALE_NORMAL));
257 OOGL(glNormalPointer(GL_FLOAT, 0, kOOPlanetVertices));
258
259 OOGL(glDrawElements(GL_TRIANGLES, data->faceCount * 3, data->type, data->indices));
260
261#ifdef OO_DEBUG_MONITOR_ENABLED
262 if ([UNIVERSE wireframeGraphics]) {
264 }
265#endif
266
267#if OO_TEXTURE_CUBE_MAP
268 if ([_material wantsNormalsAsTextureCoordinates]) {
269 OOGL(glEnable(GL_TEXTURE_2D));
270 OOGL(glDisable(GL_TEXTURE_CUBE_MAP));
271 }
272#endif
273
275#ifdef OO_DEBUG_MONITOR_ENABLED
277 [self debugDrawNormals];
278#endif
279
281 OOGL(glPopAttrib());
282
283 OOGL(glDisableClientState(GL_TEXTURE_COORD_ARRAY));
284}
285
286- (BOOL)hasOpaqueParts
287{
288 return !_isAtmosphere;
289}
290
291- (BOOL)hasTranslucentParts
292{
293 return _isAtmosphere;
294}
295
296- (GLfloat)collisionRadius
297{
298 return _radius;
299}
300
301- (GLfloat)maxDrawDistance
302{
303 // FIXME
304 return INFINITY;
305}
306
307- (BoundingBox)boundingBox
308{
309 return (BoundingBox) { { -_radius, -_radius, -_radius }, { _radius, _radius, _radius } };
310}
311
312- (void)setBindingTarget:(id<OOWeakReferenceSupport>)target
313{
314 [_material setBindingTarget:target];
315}
316
317- (void)dumpSelfState
318{
319 [super dumpSelfState];
320 OOLog(@"dumpState.planetDrawable", @"radius: %g", [self radius]);
321 OOLog(@"dumpState.planetDrawable", @"LOD: %g", [self levelOfDetail]);
322}
323
324- (void)recalculateTransform
325{
326 _transform = OOMatrixForScaleUniform(_radius);
327}
328
329#ifdef OO_DEBUG_MONITOR_ENABLED
330
331- (void)debugDrawNormals
332{
333 OODebugWFState state;
334
336
337 state = OODebugBeginWireframe(NO);
338
339 const OOPlanetDataLevel* data = &kPlanetData[_lod];
340 unsigned i;
341
342 OOGLBEGIN(GL_LINES);
343 for (i = 0; i < data->vertexCount; i++) {
344 /* Fun sphere facts: the normalized coordinates of a point on a sphere at the origin
345 is equal to the object-space normal of the surface at that point.
346 Furthermore, we can construct the binormal (a vector pointing westward along the
347 surface) as the cross product of the normal with the Y axis. (This produces
348 singularities at the pole, but there have to be singularities according to the
349 Hairy Ball Theorem.) The tangent (a vector north along the surface) is then the
350 inverse of the cross product of the normal and binormal.
351
352 (This comment courtesy of the in-development planet shader.)
353 */
354 Vector v = make_vector(kOOPlanetVertices[i * 3], kOOPlanetVertices[i * 3 + 1], kOOPlanetVertices[i * 3 + 2]);
355 Vector n = v;
356 v = OOVectorMultiplyMatrix(v, _transform);
357
358 glColor3f(0.0f, 1.0f, 1.0f);
360 GLVertexOOVector(vector_add(v, vector_multiply_scalar(n, _radius * 0.05)));
361
364
365 glColor3f(1.0f, 1.0f, 0.0f);
367 GLVertexOOVector(vector_add(v, vector_multiply_scalar(t, _radius * 0.03)));
368
369 glColor3f(0.0f, 1.0f, 0.0f);
371 GLVertexOOVector(vector_add(v, vector_multiply_scalar(b, _radius * 0.03)));
372 }
373 OOGLEND();
374
375 OODebugEndWireframe(state);
376}
377#endif
378
379#ifdef OO_DEBUG_MONITOR_ENABLED
380- (NSSet*)allTextures
381{
382 return [[self material] allTextures];
383}
384
385#endif
386
387@end
388
389#endif /* NEW_PLANETS */
#define gDebugFlags
@ DEBUG_DRAW_NORMALS
#define OODebugDrawBasisAtOrigin(scale)
OOINLINE float OOClamp_0_max_f(float value, float max) INLINE_CONST_FUNC
OOINLINE float OOClamp_0_1_f(float value) INLINE_CONST_FUNC
#define DESTROY(x)
#define OOLog(class, format,...)
Definition OOLogging.h:84
#define OO_ENTER_OPENGL()
Vector OOVectorMultiplyMatrix(Vector v, OOMatrix m) CONST_FUNC
Definition OOMatrix.m:121
OOINLINE OOMatrix OOMatrixForScaleUniform(OOScalar s) INLINE_CONST_FUNC
Definition OOMatrix.h:273
void OOGLPushModelView(void)
void OOGLMultModelView(OOMatrix matrix)
OOMatrix OOGLPopModelView(void)
#define OOGLBEGIN
Definition OOOpenGL.h:269
@ OPENGL_STATE_OPAQUE
Definition OOOpenGL.h:121
#define OOVerifyOpenGLState()
Definition OOOpenGL.h:133
#define OOSetOpenGLState(STATE)
Definition OOOpenGL.h:132
#define OOGL(statement)
Definition OOOpenGL.h:262
#define OOGLEND
Definition OOOpenGL.h:270
const OOPlanetDataLevel kPlanetData[kOOPlanetDataLevels]
const GLfloat kOOPlanetVertices[kOOPlanetDataVertexCount *3]
const GLfloat kOOPlanetTexCoords[kOOPlanetDataVertexCount *2]
#define kOOPlanetDataLevels
OOINLINE Vector vector_flip(Vector v) INLINE_CONST_FUNC
Definition OOVector.h:227
OOINLINE Vector vector_add(Vector a, Vector b) INLINE_CONST_FUNC
Definition OOVector.h:195
OOINLINE Vector make_vector(OOScalar vx, OOScalar vy, OOScalar vz) INLINE_CONST_FUNC
Definition OOVector.h:149
struct Vector Vector
OOINLINE Vector true_cross_product(Vector first, Vector second) CONST_FUNC
Definition OOVector.h:285
const Vector kBasisYVector
Definition OOVector.h:46
OOINLINE Vector cross_product(Vector first, Vector second) INLINE_CONST_FUNC
Definition OOVector.h:294
#define GLVertexOOVector(v)
Definition OOVector.h:136
OOINLINE OOScalar distance(Vector v1, Vector v2) INLINE_CONST_FUNC
Definition OOVector.h:280
OOINLINE Vector vector_multiply_scalar(Vector v, OOScalar s) INLINE_CONST_FUNC
Definition OOVector.h:180
#define UNIVERSE
Definition Universe.h:827
void applyNone()
Definition OOMaterial.m:72
void setTextureName:(NSString *textureName)
void setRadius:(float radius)
void setMaterial:(OOMaterial *material)
const void * indices