X3D libraries
The libraries to work with X3D dataset

render.h

Go to the documentation of this file.
00001 /*
00002  *    libx3d render -- definitions to manage the X3D rendering
00003  *    Copyright (C) 2005  Made to Order Software, Corp.
00004  *
00005  *    This library is free software; you can redistribute it and/or
00006  *    modify it under the terms of the GNU Lesser General Public
00007  *    License as published by the Free Software Foundation; either
00008  *    version 2.1 of the License, or (at your option) any later version.
00009  *
00010  *    This library is distributed in the hope that it will be useful,
00011  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *    Lesser General Public License for more details.
00014  *
00015  *    You should have received a copy of the GNU Lesser General Public
00016  *    License along with this library; if not, write to the Free Software
00017  *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  *
00019  * File: render/include/x3d/render.h
00020  * License: GNU Lesser General Public License
00022  * $Revision: 1.4 $
00023  * $Date: 2005/12/19 01:36:03 $
00024  *
00025  * $Log: render.h,v $
00026  * Revision 1.4  2005/12/19 01:36:03  alexis
00027  * Added the LevelOfSupport object for the render library
00028  *
00029  * Revision 1.3  2005/12/11 09:17:23  alexis
00030  * First working version. This can be used to render a Box.
00031  * Supports the Viewpoint and Materials in an Appearance node.
00032  *
00033  * Revision 1.2  2005/12/07 21:23:14  alexis
00034  * Added header and .c++ files to support OpenGL
00035  *
00036  * Revision 1.1  2005/12/04 18:00:13  alexis
00037  * The start of the render library. Really just the start at this time.
00038  *
00039  *
00040  *
00041  */
00042 #ifndef __LIBX3D_RENDER_H__
00043 #define __LIBX3D_RENDER_H__
00044 
00045 #define X3D_RENDER_VERSION              0.1
00046 
00047 #define X3D_RENDER_RELEASE              0
00048 #define X3D_RENDER_REVISION             1
00049 
00050 
00051 #include        <x3d/objects.h>
00052 #include        <fast_math.h>
00053 
00054 
00055 /// The X3D libraries which handles the rendering process
00056 namespace x3drender
00057 {
00058 
00059 
00060 /** \file render.h
00061  *
00062  * \brief This header file includes the rendering processor and the
00063  * interfaces for the actual render in some output.
00064  *
00065  * Includes the definitions that you need to process a node to get
00066  * it ready for rendering. The processing is important to compute
00067  * all the objects position and then order them properly.
00068  *
00069  * See the main page for more information in that regard. All the
00070  * objects will be rendered from back to front unless hints are
00071  * used for the processors to ask solid objects to be rendered
00072  * from front to back. The problem with solid objects is that
00073  * their edges aren't so solid. Thus even solid objects need to be
00074  * rendered back to front now a days. But on older boards, you
00075  * may want to hint the system to swap that order and forget about
00076  * the antialised edges.
00077  *
00078  * Once ready, you will just need to use one of the available
00079  * render libraries such as the OpenGL one to draw the result
00080  * on a surface.
00081  *
00082  * \sa opengl.h
00083  */
00084 
00085 
00086 
00087 class RenderState;
00088 
00089 
00090 
00091 /// A class representing a shape in the render state
00092 class RenderShape : public x3d::SFNodeListener
00093 {
00094 public:
00095         typedef bool            (RenderShape::*execute_function_t)();
00096 
00097                                 RenderShape(RenderState *render_state);
00098         virtual                 ~RenderShape();
00099 
00100         RenderState *                   GetRenderState() const { return f_render_state; }
00101         unsigned int                    GetParentsCount() const { return f_parents.Count(); }
00102         x3d::SFNode *                   GetParent(int idx) const { return f_parents.Get(idx); }
00103         const fast_math::Matrix4x4&     GetMatrix() const { return f_matrix; }
00104         x3d::Shape *                    GetX3DShape() const { return f_x3d_shape; }
00105 
00106         bool                    HasChanged() const { return f_changed; }
00107         bool                    CanUpdate() const { return f_can_update; }
00108         bool                    Update();
00109 
00110         void                    SetParents(x3d::MFNode& parents);
00111         void                    SetMatrix(const fast_math::Matrix4x4& matrix);
00112         void                    SetX3DShape(x3d::Shape *x3d_shape);
00113 
00114         virtual void            RenderProcessorSetup();
00115 
00116 // SFNodeListener interface
00117         virtual void            SFNodeListener_FieldChanged(x3d::SFNode *node, const x3d::FieldInfo *info);
00118 
00119 private:
00120         RenderState *           f_render_state;
00121         x3d::MFNode             f_parents;
00122         fast_math::Matrix4x4    f_matrix;
00123         x3d::ShapePtr           f_x3d_shape;
00124         bool                    f_changed;
00125         bool                    f_can_update;
00126 
00127 #if 0
00128         x3d::MFVec3f            GetVertexPointer();
00129         x3d::MFVec3f            GetNormalPointer();
00130         x3d::MFVec3f            GetTexturePointer();
00131         x3d::MFColor            GetColorPointer();
00132         x3d::MFColorRGBA        GetColorRGBAPointer();
00133         x3d::MFInt32            GetIndices();
00134 
00135         x3d::MFVec3f            f_vertices;
00136         x3d::MFVec3f            f_normals;
00137         x3d::MFVec3f            f_textures;
00138         x3d::MFColor            f_colors;
00139         x3d::MFColorRGBA        f_colors_rgba;
00140         x3d::MFInt32            f_indices;
00141 #endif
00142 };
00143 typedef pointer::SmartPointer<RenderShape>      RenderShapePtr;
00144 typedef x3d::Vector<RenderShape, 256>           RenderShapeVector;
00145 
00146 
00147 
00148 
00149 
00150 
00151 /// The interface that a rendering processor implements
00152 class RenderProcessor : public pointer::ReferencedObject
00153 {
00154 public:
00155         virtual                 ~RenderProcessor();
00156 
00157         virtual RenderShape *   CreateShape(RenderState *render_state) const = 0;
00158         virtual bool            Render(RenderState *render_state) = 0;
00159 };
00160 typedef pointer::SmartPointer<RenderProcessor>          RenderProcessorPtr;
00161 
00162 
00163 
00164 
00165 /// Create a render state and then render it
00166 class RenderState : public x3d::SFNodeListener
00167 {
00168 public:
00169                                 RenderState(RenderProcessor *render_processor);
00170         virtual                 ~RenderState();
00171 
00172         bool                    Initialize(x3d::SFNodePtr root);
00173         bool                    Update();
00174         void                    ApplyTransform(const x3d::SFNode *node, fast_math::Matrix4x4& matrix);
00175         void                    SetScreenSizes(unsigned int width, unsigned int height);
00176         bool                    Execute(RenderShape::execute_function_t func);
00177 
00178         void                            SetViewpointName(const char *name);
00179         const x3d::SFString&            GetViewpointDescription() const;
00180         const fast_math::Matrix4x4&     GetViewpointPerspectiveMatrix();
00181         const fast_math::Matrix4x4&     GetViewpointPositionMatrix();
00182         const fast_math::Matrix4x4&     GetViewpointInversePerspectiveMatrix();
00183 
00184 private:
00185         bool                    WalkTree(x3d::SFNodePtr node, x3d::MFNode parents, fast_math::Matrix4x4& matrix);
00186         bool                    SetupShape(x3d::SFNodePtr node, x3d::MFNode parents, fast_math::Matrix4x4& matrix);
00187         void                    SetCurrentViewpoint(x3d::Viewpoint *viewpoint);
00188 
00189         x3d::SFString           f_start_viewpoint_name;
00190         x3d::ViewpointPtr       f_current_viewpoint;
00191         x3d::MFNode             f_viewpoint_stack;
00192         bool                    f_viewpoint_perspective_matrix_defined;
00193         bool                    f_viewpoint_position_matrix_defined;
00194         bool                    f_viewpoint_inverse_perspective_matrix_defined;
00195         fast_math::Matrix4x4    f_viewpoint_perspective_matrix;
00196         fast_math::Matrix4x4    f_viewpoint_position_matrix;
00197         fast_math::Matrix4x4    f_viewpoint_inverse_perspective_matrix;
00198         fast_math::Matrix4x4    f_viewpoint_transform;
00199 
00200         unsigned int            f_width;
00201         unsigned int            f_height;
00202 
00203         x3d::SFNodePtr          f_root;
00204         RenderProcessorPtr      f_render_processor;
00205         RenderShapeVector       f_render_shapes;
00206 };
00207 typedef pointer::SmartPointer<RenderState>              RenderStatePtr;
00208 
00209 
00210 
00211 
00212 
00213 
00214 
00215 extern  x3d::LevelOfSupport *   GetRenderSupport();
00216 
00217 
00218 
00219 };              // namespace x3drender
00220 
00221 
00222 
00223 // vim: ts=8
00224 #endif          // #ifndef __LIBX3D_RENDER_H__

00021 * $Id: render.h,v 1.4 2005/12/19 01:36:03 alexis Exp $