35#import "MyOpenGLView.h"
44#define LOD_GRANULARITY ((float)(kOOPlanetDataLevels - 1))
45#define LOD_FACTOR (1.0 / 4.0)
48@interface OOPlanetDrawable (Private)
50- (void) recalculateTransform;
52- (void) debugDrawNormals;
54- (void) renderCommonParts;
61+ (instancetype) planetWithTextureName:(NSString *)textureName radius:(
float)radius
71+ (instancetype) atmosphereWithRadius:(
float)radius
82 if ((
self = [super init]))
85 [
self recalculateTransform];
86 [
self setLevelOfDetail:0.5f];
93- (id) initAsAtmosphere
95 if ((
self = [
self init]))
112- (id) copyWithZone:(NSZone *)zone
133 [_material autorelease];
134 _material = [material retain];
138- (NSString *) textureName
140 return [_material name];
144- (void) setTextureName:(NSString *)textureName
146 if (![textureName isEqual:[
self textureName]])
149 NSDictionary *spec = [@"{diffuse_map={repeat_s=yes;cube_map=yes};}" propertyList];
161- (void) setRadius:(
float)radius
163 _radius = fabsf(radius);
164 [
self recalculateTransform];
168- (float) levelOfDetail
170 return (
float)_lod / LOD_GRANULARITY;
174- (void) setLevelOfDetail:(
float)lod
176 _lod = roundf(OOClamp_0_1_f(lod) * LOD_GRANULARITY);
180- (void) calculateLevelOfDetailForViewDistance:(
float)distance
182 BOOL simple = [UNIVERSE reducedDetail];
183 float drawFactor = [[UNIVERSE gameView] viewSize].width / (simple ? 100.0 : 40.0);
184 float drawRatio2 = drawFactor * _radius / sqrtf(distance);
186 float lod = sqrtf(drawRatio2 * LOD_FACTOR);
189 lod -= 0.5f / LOD_GRANULARITY;
190 lod = OOClamp_0_max_f(lod, (LOD_GRANULARITY - 1) / LOD_GRANULARITY);
192 [
self setLevelOfDetail:lod];
196- (void) renderOpaqueParts
202 [
self renderCommonParts];
209- (void) renderTranslucentParts
216 [
self renderCommonParts];
223- (void) renderTranslucentPartsOnOpaquePass
232 OOGL(glDisable(GL_DEPTH_TEST));
233 [
self renderCommonParts];
234 OOGL(glEnable(GL_DEPTH_TEST));
241- (void) renderCommonParts
247 OOGL(glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT));
248 OOGL(glShadeModel(GL_SMOOTH));
252 OOGL(glEnable(GL_BLEND));
253 OOGL(glDepthMask(GL_FALSE));
257 OOGL(glDisable(GL_BLEND));
266 OOGL(glEnable(GL_LIGHTING));
267 OOGL(glEnable(GL_TEXTURE_2D));
269#if OO_TEXTURE_CUBE_MAP
270 if ([_material wantsNormalsAsTextureCoordinates])
272 OOGL(glDisable(GL_TEXTURE_2D));
273 OOGL(glEnable(GL_TEXTURE_CUBE_MAP));
277 OOGL(glDisableClientState(GL_COLOR_ARRAY));
279 OOGL(glEnableClientState(GL_TEXTURE_COORD_ARRAY));
282 if ([_material wantsNormalsAsTextureCoordinates])
292 OOGL(glEnable(GL_RESCALE_NORMAL));
304#if OO_TEXTURE_CUBE_MAP
305 if ([_material wantsNormalsAsTextureCoordinates])
307 OOGL(glEnable(GL_TEXTURE_2D));
308 OOGL(glDisable(GL_TEXTURE_CUBE_MAP));
321 OOGL(glDisableClientState(GL_TEXTURE_COORD_ARRAY));
326- (BOOL) hasOpaqueParts
328 return !_isAtmosphere;
332- (BOOL) hasTranslucentParts
334 return _isAtmosphere;
338- (GLfloat) collisionRadius
344- (GLfloat) maxDrawDistance
351- (BoundingBox) boundingBox
353 return (BoundingBox){{ -_radius, -_radius, -_radius }, { _radius, _radius, _radius }};
357- (void) setBindingTarget:(
id<OOWeakReferenceSupport>)target
359 [_material setBindingTarget:target];
363- (void) dumpSelfState
365 [
super dumpSelfState];
366 OOLog(
@"dumpState.planetDrawable",
@"radius: %g", [
self radius]);
367 OOLog(
@"dumpState.planetDrawable",
@"LOD: %g", [
self levelOfDetail]);
371- (void) recalculateTransform
373 _transform = OOMatrixForScaleUniform(_radius);
379- (void) debugDrawNormals
407 glColor3f(0.0f, 1.0f, 1.0f);
409 GLVertexOOVector(vector_add(v, vector_multiply_scalar(n, _radius * 0.05)));
412 Vector t = vector_flip(true_cross_product(n, b));
414 glColor3f(1.0f, 1.0f, 0.0f);
416 GLVertexOOVector(vector_add(v, vector_multiply_scalar(t, _radius * 0.03)));
418 glColor3f(0.0f, 1.0f, 0.0f);
420 GLVertexOOVector(vector_add(v, vector_multiply_scalar(b, _radius * 0.03)));
428- (NSSet *) allTextures
430 return [[
self material] allTextures];
OOINLINE void OODebugDrawBasisAtOrigin(GLfloat scale)
void OODebugEndWireframe(OODebugWFState state)
OODebugWFState OODebugBeginWireframe(BOOL ignoreZ)
#define OOLog(class, format,...)
#define OO_ENTER_OPENGL()
Vector OOVectorMultiplyMatrix(Vector v, OOMatrix m)
void OOGLPushModelView(void)
void OOGLMultModelView(OOMatrix matrix)
OOMatrix OOGLPopModelView(void)
#define OOVerifyOpenGLState()
#define OOSetOpenGLState(STATE)
const OOPlanetDataLevel kPlanetData[kOOPlanetDataLevels]
const GLfloat kOOPlanetVertices[kOOPlanetDataVertexCount *3]
const GLfloat kOOPlanetTexCoords[kOOPlanetDataVertexCount *2]
#define kOOPlanetDataLevels
const Vector kBasisYVector
void setTextureName:(NSString *textureName)
void setRadius:(float radius)
void setMaterial:(OOMaterial *material)