os/graphics/m3g/m3gcore11/inc/m3g_node.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: Node interface
    15 *
    16 */
    17 
    18 #ifndef __M3G_NODE_H__
    19 #define __M3G_NODE_H__
    20 
    21 /*!
    22  * \internal
    23  * \file
    24  * \brief Node interface
    25  */
    26 
    27 #include "m3g_transformable.h"
    28 
    29 #include "m3g_gl.h"
    30 #include "m3g_math.h"
    31 #include "m3g_lightmanager.h"
    32 #include "m3g_renderqueue.h"
    33 
    34 #define NODE_ALPHA_FACTOR_BITS  16
    35 
    36 /* NOTE: Implementation depends on these values -- do not change! */
    37 #define CULLMASK_OUTSIDE        0x0
    38 #define CULLMASK_INSIDE         0x1
    39 #define CULLMASK_INTERSECTS     0x2
    40 #define CULLMASK_ALL            0xAAA
    41 M3G_CT_ASSERT(CULLMASK_INSIDE == (CULLMASK_INTERSECTS >> 1));
    42 
    43 /*
    44  * Estimated load metrics for VF culling
    45  *
    46  */
    47 #define VFC_BBOX_COST           30
    48 #define VFC_NODE_OVERHEAD       10
    49 #define VFC_RENDERCALL_OVERHEAD 15
    50 #define VFC_TRIANGLE_COST       1
    51 #define VFC_VERTEX_COST         1
    52 
    53 /*
    54  * Enable bit masks
    55  */
    56 #define NODE_RENDER_BIT 0x1
    57 #define NODE_PICK_BIT   0x2
    58 
    59 /*!
    60  * \internal
    61  * \brief RayIntersection object
    62 */
    63 struct M3GRayIntersectionImpl
    64 {
    65     Node *root;
    66     Camera *camera;
    67     M3Gfloat x;
    68     M3Gfloat y;
    69     M3Gfloat tMin;
    70 	M3Gfloat distance;
    71 	M3Gint submeshIndex;
    72 	M3Gfloat textureS[M3G_NUM_TEXTURE_UNITS];
    73 	M3Gfloat textureT[M3G_NUM_TEXTURE_UNITS];
    74 	M3Gfloat normal[3];
    75     Node *intersected;
    76 };
    77 
    78 /*
    79 RayIntersection Java side result must be in this format.
    80 
    81 Offsets Contents
    82 -------------------------------------
    83 0       distance
    84 1       subMeshIndex
    85 2-3     textureS coordinates
    86 4-5     textureT coordinates
    87 6-8     normal coordinates
    88 9-14    ray coordinates and direction
    89 
    90 */
    91 
    92 /*!
    93  * \internal
    94  * \brief Recursively inherited state passed to each SetupRender call
    95  */
    96 typedef struct 
    97 {
    98     Matrix toCamera;
    99     M3Gbitmask cullMask;
   100 } SetupRenderState;
   101 
   102 /* Function pointer prototypes */
   103 
   104 typedef M3Gbool (*m3gAlignFuncPtr)			    (Node *self, const Node *refNode);
   105 typedef void    (*m3gDoRenderFuncPtr)		    (Node *self, RenderContext *ctx, const Matrix *toCamera, M3Gint patchIndex);
   106 typedef M3Gint  (*m3gGetBBoxFuncPtr)            (Node *self, AABB *bbox);
   107 typedef M3Gbool (*m3gRayIntersectFuncPtr)       (Node *self, M3Gint mask, M3Gfloat *ray, RayIntersection *ri, Matrix *toGroup);
   108 typedef M3Gbool (*m3gSetupRenderFuncPtr)	    (Node *self, const Node *caller, SetupRenderState *rs, RenderQueue *renderQueue);
   109 typedef void    (*m3gUpdateDuplicateRefFuncPtr)	(Node *self, Object **pairs, M3Gint numPairs);
   110 typedef M3Gbool (*m3gValidate)                  (Node *self, M3Gbitmask state, M3Gint scope);
   111 
   112 /*!
   113  * \internal
   114  * \brief Node class virtual functions
   115  */
   116 typedef struct
   117 {
   118 	TransformableVFTable transformable;
   119     
   120 	m3gAlignFuncPtr                 align;
   121 	m3gDoRenderFuncPtr              doRender;
   122     m3gGetBBoxFuncPtr               getBBox;
   123 	m3gRayIntersectFuncPtr          rayIntersect;
   124 	m3gSetupRenderFuncPtr           setupRender;
   125     m3gUpdateDuplicateRefFuncPtr    updateDuplicateReferences;
   126     m3gValidate                     validate;
   127 } NodeVFTable;
   128 
   129 /*!
   130  * \internal
   131  * \brief Node class structure
   132  *
   133  */
   134 struct M3GNodeImpl
   135 {
   136 	Transformable transformable;
   137 
   138 	/* See default values form RI implementation Node.java */
   139 
   140     /* These scene graph pointers are managed by the Group class */
   141     
   142 	Node *parent;
   143 	Node *left;
   144 	Node *right;
   145     
   146 	M3Gint scope;
   147 
   148     /* Alignment references */
   149     
   150     Node *zReference;
   151 	Node *yReference;
   152 
   153     /* Various node flags and other bitfield data */
   154 
   155     M3Guint alphaFactor : NODE_ALPHA_FACTOR_BITS;
   156     
   157 	M3Guint zTarget     : 3; /* Z alignment target */
   158 	M3Guint yTarget     : 3; /* Y alignment target */
   159 
   160 	M3Guint enableBits  : 2; /* Rendering/picking enable bits */
   161 
   162     M3Guint hasBones    : 1; /* Node is part of a SkinnedMesh skeleton */
   163     M3Guint hasRenderables : 1; /* Node has renderables in its subtree */
   164 
   165     M3Guint dirtyBits   : 2; /* BBox and transform dirty bits */
   166 };
   167 
   168 /*!
   169  * \internal \brief Node bounding box in valid/dirty bitmasks
   170  */
   171 #define NODE_BBOX_BIT           0x01
   172 /*!
   173  * \internal \brief Node-child transformations in valid/dirty bitmasks
   174  */
   175 #define NODE_TRANSFORMS_BIT     0x02
   176 
   177 /* Sanity check; check compiler padding settings if this assert fails */
   178 
   179 M3G_CT_ASSERT(sizeof(Node) == sizeof(Transformable) + 28);
   180 
   181               
   182 /*----------------------------------------------------------------------
   183  * Virtual functions
   184  *--------------------------------------------------------------------*/
   185 
   186 static void m3gDestroyNode(Object *obj);
   187 static M3Gbool m3gNodeAlign(Node *self, const Node *refNode);
   188 static M3Gbool m3gNodeDuplicate(const Object *original, Object **clone, Object **pairs, M3Gint *numPairs);
   189 static M3Gint m3gNodeGetBBox(Node *self, AABB *bbox);
   190 static M3Gbool m3gNodeIsCompatible(M3Gint property);
   191 static M3Gbool m3gNodeRayIntersect(Node *self, M3Gint mask, M3Gfloat *ray, RayIntersection *ri, Matrix *toGroup);
   192 static void m3gNodeUpdateProperty(Object *self, M3Gint property, M3Gint valueSize, const M3Gfloat *value);
   193 static void m3gNodeUpdateDuplicateReferences(Node *self, Object **pairs, M3Gint numPairs);
   194 static M3Gbool m3gNodeValidate(Node *self, M3Gbitmask stateBits, M3Gint scope);
   195 
   196 
   197 static M3G_INLINE M3Gint m3gGetNodeBBox(Node *node, AABB *bbox)
   198 {
   199     return M3G_VFUNC(Node, node, getBBox)(node, bbox);
   200 }
   201 
   202 static M3G_INLINE M3Gbool m3gValidateNode(Node *node, M3Gbitmask stateBits, M3Gint scope) 
   203 {
   204     return M3G_VFUNC(Node, node, validate)(node, stateBits, scope);
   205 }
   206 
   207 /*----------------------------------------------------------------------
   208  * Internal functions
   209  *--------------------------------------------------------------------*/
   210 
   211 static void m3gInitNode(Interface *m3g, Node *node, M3GClass classID);
   212 static M3Guint m3gGetTotalAlphaFactor(Node *node, const Node *root);
   213 static M3Gbool m3gHasEnabledPath(const Node *node, const Node *root);
   214 static M3Gbool m3gHasPickablePath(const Node *node, const Node *root);
   215 static M3Gbool m3gIsChildOf(const Node *parent, const Node *child);
   216 static Node *m3gGetRoot(const Node *node);
   217 
   218 static void m3gInvalidateNode(Node *node, M3Gbitmask flags);
   219 
   220 typedef void    (*NodeFuncPtr)  (Node *node, void *params);
   221 static void     m3gForSubtree   (Node *node, NodeFuncPtr func, void *params);
   222 
   223 static void     m3gSetParent    (Node *node, Node *parent);
   224 static Node *m3gGetDuplicatedInstance(Node *self, Object **references, M3Gint numRef);
   225 
   226 #if defined(M3G_ENABLE_VF_CULLING)           
   227 static void m3gUpdateCullingMask(SetupRenderState *s, const Camera *cam, const AABB *bbox);
   228 #endif
   229 
   230 /*!
   231  * \internal
   232  * \brief Type-safe helper function
   233  */
   234 static M3G_INLINE void m3gGetCompositeNodeTransform(const Node *node, Matrix *mtx)
   235 {
   236     m3gGetCompositeTransform((Transformable*) node, mtx);
   237     M3G_ASSERT(m3gIsWUnity(mtx));
   238 }
   239 
   240 /*!
   241  * \internal
   242  * \brief Type-safe helper function
   243  */
   244 static M3G_INLINE M3Gbool m3gGetInverseNodeTransform(const Node *node, Matrix *mtx)
   245 {
   246     M3Gbool ok = m3gGetInverseCompositeTransform((Transformable*) node, mtx);
   247     M3G_ASSERT(m3gIsWUnity(mtx));
   248     return ok;
   249 }
   250 
   251 #endif /*__M3G_NODE_H__*/