00001 // Filename: graphicsStateGuardian.I 00002 // Created by: drose (24Sep99) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 00020 //////////////////////////////////////////////////////////////////// 00021 // Function: GraphicsStateGuardian::LightInfo::Constructor 00022 // Access: Public 00023 // Description: 00024 //////////////////////////////////////////////////////////////////// 00025 INLINE GraphicsStateGuardian::LightInfo:: 00026 LightInfo() { 00027 _enabled = false; 00028 _next_enabled = false; 00029 } 00030 00031 //////////////////////////////////////////////////////////////////// 00032 // Function: GraphicsStateGuardian::ClipPlaneInfo::Constructor 00033 // Access: Public 00034 // Description: 00035 //////////////////////////////////////////////////////////////////// 00036 INLINE GraphicsStateGuardian::ClipPlaneInfo:: 00037 ClipPlaneInfo() { 00038 _enabled = false; 00039 _next_enabled = false; 00040 } 00041 00042 00043 //////////////////////////////////////////////////////////////////// 00044 // Function: GraphicsStateGuardian::get_properties 00045 // Access: Public 00046 // Description: Returns the frame buffer properties requested for 00047 // this GSG. All windows created for this GSG must be 00048 // created with the same properties. 00049 //////////////////////////////////////////////////////////////////// 00050 INLINE const FrameBufferProperties &GraphicsStateGuardian:: 00051 get_properties() const { 00052 return _properties; 00053 } 00054 00055 //////////////////////////////////////////////////////////////////// 00056 // Function: GraphicsStateGuardian::get_pipe 00057 // Access: Public 00058 // Description: Returns the graphics pipe on which this GSG was 00059 // created. 00060 //////////////////////////////////////////////////////////////////// 00061 INLINE GraphicsPipe *GraphicsStateGuardian:: 00062 get_pipe() const { 00063 return _pipe; 00064 } 00065 00066 //////////////////////////////////////////////////////////////////// 00067 // Function: GraphicsStateGuardian::get_threading_model 00068 // Access: Public 00069 // Description: Returns the threading model that was used to create 00070 // this GSG. 00071 //////////////////////////////////////////////////////////////////// 00072 INLINE const GraphicsThreadingModel &GraphicsStateGuardian:: 00073 get_threading_model() const { 00074 return _threading_model; 00075 } 00076 00077 //////////////////////////////////////////////////////////////////// 00078 // Function: GraphicsStateGuardian::set_scene 00079 // Access: Public 00080 // Description: Sets the SceneSetup object that indicates the initial 00081 // camera position, etc. This must be called before 00082 // traversal begins. 00083 //////////////////////////////////////////////////////////////////// 00084 INLINE void GraphicsStateGuardian:: 00085 set_scene(SceneSetup *scene_setup) { 00086 _scene_setup = scene_setup; 00087 } 00088 00089 //////////////////////////////////////////////////////////////////// 00090 // Function: GraphicsStateGuardian::get_scene 00091 // Access: Public 00092 // Description: Returns the SceneSetup object. 00093 //////////////////////////////////////////////////////////////////// 00094 INLINE SceneSetup *GraphicsStateGuardian:: 00095 get_scene() const { 00096 return _scene_setup; 00097 } 00098 00099 //////////////////////////////////////////////////////////////////// 00100 // Function: GraphicsStateGuardian::clear 00101 // Access: Public 00102 // Description: Clears the framebuffer within the indicated 00103 // DisplayRegion, according to the flags indicated by 00104 // the DisplayRegion object (inheriting from 00105 // ClearableRegion). Note that by default, a 00106 // DisplayRegion does not have any clear flags set, in 00107 // which case this function will do nothing. 00108 //////////////////////////////////////////////////////////////////// 00109 INLINE void GraphicsStateGuardian:: 00110 clear(DisplayRegion *dr) { 00111 DisplayRegionStack old_dr = push_display_region(dr); 00112 prepare_display_region(); 00113 clear((ClearableRegion *)dr); 00114 pop_display_region(old_dr); 00115 } 00116 00117 //////////////////////////////////////////////////////////////////// 00118 // Function: GraphicsStateGuardian::reset_if_new 00119 // Access: Public 00120 // Description: Calls reset() to initialize the GSG, but only if it 00121 // hasn't been called yet. 00122 //////////////////////////////////////////////////////////////////// 00123 INLINE void GraphicsStateGuardian:: 00124 reset_if_new() { 00125 if (_needs_reset) { 00126 reset(); 00127 } 00128 } 00129 00130 //////////////////////////////////////////////////////////////////// 00131 // Function: GraphicsStateGuardian::modify_state 00132 // Access: Public 00133 // Description: Applies the attributes indicated in the state set to 00134 // the current state, and issues the changes to the 00135 // graphics hardware. 00136 // 00137 // Any transitions not mentioned are left unchanged. 00138 //////////////////////////////////////////////////////////////////// 00139 INLINE void GraphicsStateGuardian:: 00140 modify_state(const RenderState *state) { 00141 #ifndef NDEBUG 00142 if (gsg_cat.is_spam()) { 00143 gsg_cat.spam() << "Modifying GSG state with:\n"; 00144 state->write(gsg_cat.spam(false), 2); 00145 } 00146 #endif 00147 _state = _state->issue_delta_modify(state, this); 00148 } 00149 00150 //////////////////////////////////////////////////////////////////// 00151 // Function: GraphicsStateGuardian::set_state 00152 // Access: Public 00153 // Description: Applies the attributes indicated in the state set to 00154 // the current state, and issues the changes to the 00155 // graphics hardware. 00156 // 00157 // The state is taken to be a complete description of 00158 // what the graphics state should be; any transitions 00159 // not mentioned are implicitly reset to their initial 00160 // values. 00161 //////////////////////////////////////////////////////////////////// 00162 INLINE void GraphicsStateGuardian:: 00163 set_state(const RenderState *state) { 00164 #ifndef NDEBUG 00165 if (gsg_cat.is_spam()) { 00166 gsg_cat.spam() << "Setting GSG state to:\n"; 00167 state->write(gsg_cat.spam(false), 2); 00168 } 00169 #endif 00170 _state = _state->issue_delta_set(state, this); 00171 } 00172 00173 //////////////////////////////////////////////////////////////////// 00174 // Function: GraphicsStateGuardian::set_transform 00175 // Access: Public 00176 // Description: Sets the world transform that will be applied to 00177 // subsequent geometry. This is normally called only 00178 // during the draw process, immediately before issuing 00179 // geometry commands. 00180 //////////////////////////////////////////////////////////////////// 00181 INLINE void GraphicsStateGuardian:: 00182 set_transform(const TransformState *transform) { 00183 if (transform != _transform) { 00184 _transform = transform; 00185 issue_transform(transform); 00186 } 00187 } 00188 00189 //////////////////////////////////////////////////////////////////// 00190 // Function: GraphicsStateGuardian::get_current_display_region 00191 // Access: Public 00192 // Description: Returns the current display region being rendered to, 00193 // as set by the last call to push_display_region() (or 00194 // restored by pop_display_region()). This display 00195 // region will be made active (if it is not already) by 00196 // a call to prepare_display_region(). 00197 //////////////////////////////////////////////////////////////////// 00198 INLINE const DisplayRegion *GraphicsStateGuardian:: 00199 get_current_display_region(void) const { 00200 return _current_display_region; 00201 } 00202 00203 //////////////////////////////////////////////////////////////////// 00204 // Function: GraphicsStateGuardian::get_current_lens 00205 // Access: Public 00206 // Description: Returns the current lens being rendered with, as set 00207 // by the last call to push_lens() (or restored by 00208 // pop_lens()). This lens will be made active (if it is 00209 // not already) by a call to prepare_lens(). 00210 //////////////////////////////////////////////////////////////////// 00211 INLINE const Lens *GraphicsStateGuardian:: 00212 get_current_lens() const { 00213 return _current_lens; 00214 } 00215 00216 //////////////////////////////////////////////////////////////////// 00217 // Function: GraphicsStateGuardian::push_display_region 00218 // Access: Public 00219 // Description: Saves the current display region information and sets 00220 // up a new display region for rendering. The return 00221 // value from this function must eventually be passed to 00222 // a matching pop_display_region() call. 00223 // 00224 // The new display region will not actually be made 00225 // active for rendering until the next call to 00226 // prepare_display_region(). This is a state-changing 00227 // optimization. 00228 //////////////////////////////////////////////////////////////////// 00229 INLINE DisplayRegionStack GraphicsStateGuardian:: 00230 push_display_region(const DisplayRegion *dr) { 00231 DisplayRegionStack old; 00232 old._display_region = _current_display_region; 00233 old._stack_level = _display_region_stack_level; 00234 _display_region_stack_level++; 00235 _current_display_region = dr; 00236 return old; 00237 } 00238 00239 //////////////////////////////////////////////////////////////////// 00240 // Function: GraphicsStateGuardian::pop_display_region 00241 // Access: Public 00242 // Description: Restores the display region previously in effect, 00243 // before the matching call to push_display_region(). 00244 // 00245 // The newly-restored display region will not actually 00246 // be made active for rendering until the next call to 00247 // prepare_display_region(). This is a state-changing 00248 // optimization. 00249 //////////////////////////////////////////////////////////////////// 00250 INLINE void GraphicsStateGuardian:: 00251 pop_display_region(DisplayRegionStack &node) { 00252 nassertv(_display_region_stack_level > 0); 00253 _display_region_stack_level--; 00254 nassertv(node._stack_level == _display_region_stack_level); 00255 _current_display_region = node._display_region; 00256 node._stack_level = -1; 00257 } 00258 00259 //////////////////////////////////////////////////////////////////// 00260 // Function: GraphicsStateGuardian::push_frame_buffer 00261 // Access: Public 00262 // Description: Saves the contents of the frame buffer (within the 00263 // indicated display region only) so that rendering may 00264 // be performed (for instance, to render a partial in a 00265 // multipass algorithm) and the frame buffer later 00266 // restored via a matching call to pop_frame_buffer(). 00267 //////////////////////////////////////////////////////////////////// 00268 INLINE FrameBufferStack GraphicsStateGuardian:: 00269 push_frame_buffer(const RenderBuffer &buffer, 00270 const DisplayRegion *dr) { 00271 FrameBufferStack old; 00272 old._frame_buffer = save_frame_buffer(buffer, dr); 00273 old._stack_level = _frame_buffer_stack_level; 00274 _frame_buffer_stack_level++; 00275 return old; 00276 } 00277 00278 //////////////////////////////////////////////////////////////////// 00279 // Function: GraphicsStateGuardian::pop_frame_buffer 00280 // Access: Public 00281 // Description: Restores the contents of the frame buffer as saved by 00282 // a previous call to push_frame_buffer(). 00283 //////////////////////////////////////////////////////////////////// 00284 INLINE void GraphicsStateGuardian:: 00285 pop_frame_buffer(FrameBufferStack &node) { 00286 nassertv(_frame_buffer_stack_level > 0); 00287 _frame_buffer_stack_level--; 00288 nassertv(node._stack_level == _frame_buffer_stack_level); 00289 restore_frame_buffer(node._frame_buffer); 00290 node._stack_level = -1; 00291 } 00292 00293 //////////////////////////////////////////////////////////////////// 00294 // Function: GraphicsStateGuardian::push_lens 00295 // Access: Public 00296 // Description: Saves the current lens information and sets up a new 00297 // lens for rendering. The return value from this 00298 // function must eventually be passed to a matching 00299 // pop_lens() call. 00300 // 00301 // The new lens will not actually be made active for 00302 // rendering until the next call to prepare_lens(). 00303 // This is a state-changing optimization. 00304 //////////////////////////////////////////////////////////////////// 00305 INLINE LensStack GraphicsStateGuardian:: 00306 push_lens(const Lens *lens) { 00307 LensStack old; 00308 old._lens = _current_lens; 00309 old._stack_level = _lens_stack_level; 00310 _lens_stack_level++; 00311 _current_lens = lens; 00312 return old; 00313 } 00314 00315 //////////////////////////////////////////////////////////////////// 00316 // Function: GraphicsStateGuardian::pop_lens 00317 // Access: Public 00318 // Description: Restores the lens previously in effect, before the 00319 // matching call to push_lens(). 00320 // 00321 // The newly-restored lens will not actually be made 00322 // active for rendering until the next call to 00323 // prepare_lens(). This is a state-changing 00324 // optimization. 00325 //////////////////////////////////////////////////////////////////// 00326 INLINE void GraphicsStateGuardian:: 00327 pop_lens(LensStack &node) { 00328 nassertv(_lens_stack_level > 0); 00329 _lens_stack_level--; 00330 nassertv(node._stack_level == _lens_stack_level); 00331 _current_lens = node._lens; 00332 node._stack_level = -1; 00333 } 00334 00335 //////////////////////////////////////////////////////////////////// 00336 // Function: GraphicsStateGuardian::set_lens 00337 // Access: Public 00338 // Description: Sets a new lens for rendering without bothering to 00339 // push or pop. This replaces the lens most recently 00340 // pushed, if any. There is no need to call 00341 // prepare_lens() following this call. 00342 // 00343 // The return value is true if the lens is acceptable, 00344 // false if it is not. 00345 //////////////////////////////////////////////////////////////////// 00346 INLINE bool GraphicsStateGuardian:: 00347 set_lens(const Lens *lens) { 00348 _current_lens = lens; 00349 return prepare_lens(); 00350 } 00351 00352 //////////////////////////////////////////////////////////////////// 00353 // Function: GraphicsStateGuardian::set_coordinate_system 00354 // Access: Public 00355 // Description: Changes the coordinate system in effect on this 00356 // particular gsg. Normally, this will be the default 00357 // coordinate system, but it might be set differently at 00358 // runtime. 00359 //////////////////////////////////////////////////////////////////// 00360 INLINE void GraphicsStateGuardian:: 00361 set_coordinate_system(CoordinateSystem cs) { 00362 _coordinate_system = cs; 00363 } 00364 00365 //////////////////////////////////////////////////////////////////// 00366 // Function: GraphicsStateGuardian::get_coordinate_system 00367 // Access: Public 00368 // Description: Returns the coordinate system in effect on this 00369 // particular gsg. Normally, this will be the default 00370 // coordinate system, but it might be set differently at 00371 // runtime. 00372 //////////////////////////////////////////////////////////////////// 00373 INLINE CoordinateSystem GraphicsStateGuardian:: 00374 get_coordinate_system() const { 00375 return _coordinate_system; 00376 } 00377 00378 //////////////////////////////////////////////////////////////////// 00379 // Function: GraphicsStateGuardian::get_light 00380 // Access: Protected 00381 // Description: Returns the Light object that is bound to the 00382 // indicated id, or NULL if no Light is bound. 00383 //////////////////////////////////////////////////////////////////// 00384 INLINE Light *GraphicsStateGuardian:: 00385 get_light(int light_id) const { 00386 nassertr(light_id >= 0 && light_id < (int)_light_info.size(), (Light *)NULL); 00387 return _light_info[light_id]._light; 00388 } 00389 00390 //////////////////////////////////////////////////////////////////// 00391 // Function: GraphicsStateGuardian::get_clip_plane 00392 // Access: Protected 00393 // Description: Returns the PlaneNode object that is bound to the 00394 // indicated id, or NULL if no PlaneNode is bound. 00395 //////////////////////////////////////////////////////////////////// 00396 INLINE PlaneNode *GraphicsStateGuardian:: 00397 get_clip_plane(int plane_id) const { 00398 nassertr(plane_id >= 0 && plane_id < (int)_clip_plane_info.size(), (PlaneNode *)NULL); 00399 return _clip_plane_info[plane_id]._plane; 00400 }