00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if !defined(_XRB_ENGINE2_OBJECT_HPP_)
00012 #define _XRB_ENGINE2_OBJECT_HPP_
00013
00014 #include "xrb.hpp"
00015
00016 #include "xrb_color.hpp"
00017 #include "xrb_engine2_enums.hpp"
00018 #include "xrb_engine2_types.hpp"
00019 #include "xrb_transform2.hpp"
00020 #include "xrb_vector.hpp"
00021
00022 namespace Xrb
00023 {
00024
00025 class RenderContext;
00026 class Serializer;
00027
00028 namespace Engine2
00029 {
00030
00031 class Entity;
00032 class ObjectLayer;
00033 class QuadTree;
00034 class World;
00035
00036
00037
00038
00039
00040 class Object : public FloatTransform2
00041 {
00042 public:
00043
00044
00045
00046
00047
00048
00049 class DrawData
00050 {
00051 public:
00052
00053 DrawData (
00054 RenderContext const &render_context,
00055 FloatMatrix2 const &transformation)
00056 :
00057 m_render_context(render_context),
00058 m_transformation(transformation)
00059 { }
00060 ~DrawData () { }
00061
00062 inline RenderContext const &GetRenderContext () const { return m_render_context; }
00063 inline FloatMatrix2 const &Transformation () const { return m_transformation; }
00064
00065 inline void SetTransformation (FloatMatrix2 const &transformation) { m_transformation = transformation; }
00066
00067 private:
00068
00069 RenderContext const &m_render_context;
00070 FloatMatrix2 m_transformation;
00071 };
00072
00073 struct TransparentObjectOrder
00074 {
00075 bool operator () (Object const *t0, Object const *t1)
00076 {
00077 return t0->ZDepth() > t1->ZDepth()
00078 ||
00079 (t0->ZDepth() == t1->ZDepth() && t0 < t1);
00080 }
00081 };
00082
00083
00084
00085
00086
00087
00088
00089
00090 class DrawLoopFunctor
00091 {
00092 public:
00093
00094
00095
00096 static Float const ms_radius_limit_upper;
00097 static Float const ms_radius_limit_lower;
00098 static Float const ms_distance_fade_slope;
00099 static Float const ms_distance_fade_intercept;
00100
00101 DrawLoopFunctor (
00102 RenderContext const &render_context,
00103 FloatMatrix2 const &world_to_screen,
00104 Float pixels_in_view_radius,
00105 FloatVector2 const &view_center,
00106 Float view_radius,
00107 bool is_collect_transparent_object_pass,
00108 TransparentObjectVector *transparent_object_vector,
00109 QuadTreeType quad_tree_type);
00110 ~DrawLoopFunctor () { }
00111
00112 inline Object::DrawData const &ObjectDrawData () const { return m_object_draw_data; }
00113 inline FloatMatrix2 const &WorldToScreen () const { return m_object_draw_data.Transformation(); }
00114 inline Float PixelsInViewRadius () const { return m_pixels_in_view_radius; }
00115 inline FloatVector2 const &ViewCenter () const { return m_view_center; }
00116 inline Float ViewRadius () const { return m_view_radius; }
00117 inline bool IsCollectTransparentObjectPass () const { return m_is_collect_transparent_object_pass; }
00118 inline TransparentObjectVector *GetTransparentObjectVector () const { return m_transparent_object_vector; }
00119 inline Uint32 DrawnOpaqueObjectCount () const { return m_drawn_opaque_object_count; }
00120 inline Uint32 DrawnTransparentObjectCount () const { return m_drawn_transparent_object_count; }
00121
00122 inline void SetWorldToScreen (FloatMatrix2 const &world_to_screen) { m_object_draw_data.SetTransformation(world_to_screen); }
00123 inline void SetViewCenter (FloatVector2 view_center) { m_view_center = view_center; }
00124 inline void SetIsCollectTransparentObjectPass (bool is_collect_transparent_object_pass) { m_is_collect_transparent_object_pass = is_collect_transparent_object_pass; }
00125
00126
00127 void operator () (Engine2::Object const *object);
00128
00129 private:
00130
00131 Float CalculateDistanceFade (Float object_radius);
00132
00133 Object::DrawData m_object_draw_data;
00134 Float m_pixels_in_view_radius;
00135 FloatVector2 m_view_center;
00136 Float m_view_radius;
00137 bool m_is_collect_transparent_object_pass;
00138 TransparentObjectVector *const m_transparent_object_vector;
00139 QuadTreeType m_quad_tree_type;
00140 mutable Uint32 m_drawn_opaque_object_count;
00141 mutable Uint32 m_drawn_transparent_object_count;
00142 };
00143
00144 virtual ~Object ();
00145
00146
00147
00148
00149
00150
00151
00152
00153 static Object *Create (
00154 Serializer &serializer,
00155 CreateEntityFunction CreateEntity);
00156
00157 virtual void Write (Serializer &serializer) const;
00158
00159
00160
00161
00162
00163
00164 virtual void Draw (DrawData const &draw_data, Float alpha_mask) const { }
00165
00166
00167
00168
00169
00170 inline ObjectType GetObjectType () const { return m_object_type; }
00171 inline Float ZDepth () const { return m_z_depth; }
00172 inline bool IsDynamic () const { return m_entity != NULL; }
00173 inline Entity *GetEntity () const { return m_entity; }
00174 inline Color const &ColorBias () const { return m_color_bias; }
00175 inline Color const &ColorMask () const { return m_color_mask; }
00176
00177 inline Color &ColorBias () { return m_color_bias; }
00178
00179 inline Color &ColorMask () { return m_color_mask; }
00180 inline bool IsTransparent () const { return m_is_transparent; }
00181 inline Float Radius (QuadTreeType quad_tree_type) const { ASSERT1(quad_tree_type < QTT_COUNT); CalculateTransform(); return m_radius[quad_tree_type]; }
00182 inline Float RadiusSquared (QuadTreeType quad_tree_type) const { ASSERT1(quad_tree_type < QTT_COUNT); CalculateTransform(); return m_radius[quad_tree_type]*m_radius[quad_tree_type]; }
00183 inline Float VisibleRadius () const { CalculateTransform(); return m_radius[QTT_VISIBILITY]; }
00184 inline Float VisibleRadiusSquared () const { CalculateTransform(); return m_radius[QTT_VISIBILITY]*m_radius[QTT_VISIBILITY]; }
00185 inline Float PhysicalRadius () const { CalculateTransform(); return m_radius[QTT_PHYSICS_HANDLER]; }
00186 inline Float PhysicalRadiusSquared () const { CalculateTransform(); return m_radius[QTT_PHYSICS_HANDLER]*m_radius[QTT_PHYSICS_HANDLER]; }
00187
00188 inline ObjectLayer *GetObjectLayer () const { return m_object_layer; }
00189
00190 World *GetWorld () const;
00191
00192 inline bool HasOwnerQuadTree (QuadTreeType const quad_tree_type) const
00193 {
00194 ASSERT3(quad_tree_type <= QTT_COUNT);
00195 return m_owner_quad_tree[quad_tree_type] != NULL;
00196 }
00197
00198 inline QuadTree *OwnerQuadTree (QuadTreeType const quad_tree_type) const
00199 {
00200 ASSERT3(quad_tree_type <= QTT_COUNT);
00201 return m_owner_quad_tree[quad_tree_type];
00202 }
00203
00204
00205
00206
00207 inline void SetZDepth (Float z_depth)
00208 {
00209 ASSERT_NAN_SANITY_CHECK(Math::IsFinite(z_depth));
00210 m_z_depth = z_depth;
00211 }
00212
00213 void SetEntity (Entity *entity);
00214
00215 inline void SetIsTransparent (bool is_transparent) { m_is_transparent = is_transparent; }
00216
00217 inline void SetObjectLayer (ObjectLayer *object_layer)
00218 {
00219 m_object_layer = object_layer;
00220 }
00221
00222 inline void SetOwnerQuadTree (
00223 QuadTreeType const quad_tree_type,
00224 QuadTree *const owner_quad_tree)
00225 {
00226 ASSERT3(quad_tree_type <= QTT_COUNT);
00227 m_owner_quad_tree[quad_tree_type] = owner_quad_tree;
00228 }
00229
00230 protected:
00231
00232
00233 Object (ObjectType object_type);
00234
00235
00236
00237
00238
00239
00240
00241 static ObjectType ReadObjectType (Serializer &serializer);
00242
00243
00244 void WriteObjectType (Serializer &serializer) const;
00245
00246
00247 void ReadClassSpecific (Serializer &serializer);
00248
00249
00250 void WriteClassSpecific (Serializer &serializer) const;
00251
00252
00253
00254
00255
00256
00257
00258
00259 virtual void CalculateRadius (QuadTreeType quad_tree_type) const { m_radius[quad_tree_type] = ScaleFactor(); }
00260
00261
00262
00263
00264 void CloneProperties (Object const *object);
00265
00266
00267 void CalculateTransform () const;
00268
00269 inline void IndicateRadiiNeedToBeRecalculated () { m_radii_need_to_be_recalculated = true; }
00270
00271
00272 mutable Float m_radius[QTT_COUNT];
00273
00274 ObjectLayer *m_object_layer;
00275
00276
00277
00278
00279 QuadTree *m_owner_quad_tree[QTT_COUNT];
00280
00281 private:
00282
00283
00284 ObjectType const m_object_type;
00285
00286
00287
00288 Float m_z_depth;
00289
00290
00291 Entity *m_entity;
00292
00293 Color m_color_bias;
00294
00295 Color m_color_mask;
00296
00297
00298 mutable bool m_radii_need_to_be_recalculated;
00299
00300
00301 bool m_is_transparent;
00302 };
00303
00304 }
00305
00306 }
00307
00308 #endif // !defined(_XRB_ENGINE2_OBJECT_HPP_)
00309
00310
00312
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374