Browse Source

finish implementation of rescale-normals for dxgsg

David Rose 21 years ago
parent
commit
9e9cac826c

+ 0 - 9
panda/src/dxgsg7/config_dxgsg7.cxx

@@ -50,15 +50,6 @@ ConfigVariableBool dx_full_screen_antialiasing
 ConfigVariableBool dx_no_vertex_fog
 ("dx-no-vertex-fog", false);
 
-// Configure this true to cause all lighting normals to automatically
-// be normalized by the CPU before rendering.  This is
-// necessary if you intend to render things under scale transforms and
-// expect lighting to work correctly.  Maybe one day there will be
-// another way to set this at runtime, instead of only as a configure
-// variable
-ConfigVariableBool dx_auto_normalize_lighting
-("auto-normalize-lighting", true);
-
 #ifndef NDEBUG
 // debugging flag
 // values are same as D3DCULL enumtype, 0 - no force, 1 - force none, 2 - force CW, 3 - force CCW

+ 0 - 1
panda/src/dxgsg7/config_dxgsg7.h

@@ -32,7 +32,6 @@ extern ConfigVariableBool dx_full_screen;
 extern ConfigVariableBool dx_sync_video;
 extern ConfigVariableBool dx_no_vertex_fog;
 extern ConfigVariableBool dx_full_screen_antialiasing;
-extern ConfigVariableBool dx_auto_normalize_lighting;
 extern ConfigVariableBool dx_use_rangebased_fog;
 extern ConfigVariableBool link_tristrips;
 

+ 63 - 7
panda/src/dxgsg7/dxGraphicsStateGuardian7.cxx

@@ -41,6 +41,7 @@
 #include "texMatrixAttrib.h"
 #include "materialAttrib.h"
 #include "renderModeAttrib.h"
+#include "rescaleNormalAttrib.h"
 #include "fogAttrib.h"
 #include "depthOffsetAttrib.h"
 #include "fog.h"
@@ -252,6 +253,7 @@ DXGraphicsStateGuardian7(const FrameBufferProperties &properties) :
 
     ZeroMemory(&matIdentity,sizeof(D3DMATRIX));
     matIdentity._11 = matIdentity._22 = matIdentity._33 = matIdentity._44 = 1.0f;
+     _auto_rescale_normal = false;
 
     // All implementations have the following buffers.
     _buffer_mask = (RenderBuffer::T_color |
@@ -590,9 +592,6 @@ dx_init( void) {
 
     _pScrn->pD3DDevice->SetRenderState(D3DRENDERSTATE_AMBIENTMATERIALSOURCE, D3DMCS_COLOR1);
 
-    if(dx_auto_normalize_lighting)
-         _pScrn->pD3DDevice->SetRenderState(D3DRENDERSTATE_NORMALIZENORMALS, true);
-
     // initial clip rect
     SetRect(&_pScrn->clip_rect, 0,0,0,0);     // no clip rect set
 
@@ -3755,6 +3754,11 @@ issue_transform(const TransformState *transform) {
 
   _pScrn->pD3DDevice->SetTransform(D3DTRANSFORMSTATE_WORLD,
                                 (LPD3DMATRIX)transform->get_mat().get_data());
+ 
+  _transform = transform;
+  if (_auto_rescale_normal) {
+    do_auto_rescale_normal();
+  }
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -3846,6 +3850,38 @@ issue_render_mode(const RenderModeAttrib *attrib) {
 
   _current_fill_mode = mode;
 }
+ 
+////////////////////////////////////////////////////////////////////
+//     Function: DXGraphicsStateGuardian7::issue_rescale_normal
+//       Access: Public, Virtual
+//  Description:
+////////////////////////////////////////////////////////////////////
+void DXGraphicsStateGuardian7::
+issue_rescale_normal(const RescaleNormalAttrib *attrib) {
+  RescaleNormalAttrib::Mode mode = attrib->get_mode();
+
+  _auto_rescale_normal = false;
+
+  switch (mode) {
+  case RescaleNormalAttrib::M_none:
+    _pD3DDevice->SetRenderState(D3DRENDERSTATE_NORMALIZENORMALS, false);
+    break;
+
+  case RescaleNormalAttrib::M_rescale:
+  case RescaleNormalAttrib::M_normalize:
+    _pD3DDevice->SetRenderState(D3DRENDERSTATE_NORMALIZENORMALS, true);
+    break;
+
+  case RescaleNormalAttrib::M_auto:
+    _auto_rescale_normal = true;
+    do_auto_rescale_normal();
+    break;
+
+  default:
+    dxgsg7_cat.error()
+      << "Unknown rescale_normal mode " << (int)mode << endl;
+  }
+}
 
 ////////////////////////////////////////////////////////////////////
 //     Function: DXGraphicsStateGuardian7::issue_texture_apply
@@ -4262,7 +4298,7 @@ end_frame() {
 //       Access: Public, Virtual
 //  Description:
 ////////////////////////////////////////////////////////////////////
-INLINE bool DXGraphicsStateGuardian7::
+bool DXGraphicsStateGuardian7::
 wants_texcoords() const {
     return _texturing_enabled;
 }
@@ -4363,7 +4399,7 @@ set_read_buffer(const RenderBuffer &rb) {
 //  Description: Maps from the Texture's internal wrap mode symbols to
 //               GL's.
 ////////////////////////////////////////////////////////////////////
-INLINE D3DTEXTUREADDRESS DXGraphicsStateGuardian7::
+D3DTEXTUREADDRESS DXGraphicsStateGuardian7::
 get_texture_wrap_mode(Texture::WrapMode wm) const {
 
     if (wm == Texture::WM_clamp)
@@ -4382,7 +4418,7 @@ get_texture_wrap_mode(Texture::WrapMode wm) const {
 //       Access: Protected
 //  Description: Maps from the fog types to gl version
 ////////////////////////////////////////////////////////////////////
-INLINE D3DFOGMODE DXGraphicsStateGuardian7::
+D3DFOGMODE DXGraphicsStateGuardian7::
 get_fog_mode_type(Fog::Mode m) const {
   switch (m) {
   case Fog::M_linear:
@@ -4396,6 +4432,26 @@ get_fog_mode_type(Fog::Mode m) const {
   return D3DFOG_EXP;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: DXGraphicsStateGuardian7::do_auto_rescale_normal
+//       Access: Protected
+//  Description: Issues the appropriate GL commands to either rescale
+//               or normalize the normals according to the current
+//               transform.
+////////////////////////////////////////////////////////////////////
+void DXGraphicsStateGuardian7::
+do_auto_rescale_normal() {
+  if (_transform->has_uniform_scale() &&
+      IS_NEARLY_EQUAL(_transform->get_uniform_scale(), 1.0f)) {
+    // If there's no scale, don't normalize anything.
+    _pD3DDevice->SetRenderState(D3DRENDERSTATE_NORMALIZENORMALS, false);
+
+  } else {
+    // If there is a scale, turn on normalization.
+    _pD3DDevice->SetRenderState(D3DRENDERSTATE_NORMALIZENORMALS, true);
+  }
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: DXGraphicsStateGuardian7::enable_lighting
 //       Access: Protected, Virtual
@@ -4467,7 +4523,7 @@ slot_new_clip_plane(int plane_id) {
 //               PlaneNode will already have been bound to this id via
 //               bind_clip_plane().
 ////////////////////////////////////////////////////////////////////
-INLINE void DXGraphicsStateGuardian7::
+void DXGraphicsStateGuardian7::
 enable_clip_plane(int plane_id, bool enable) {
   assert(plane_id < D3DMAXUSERCLIPPLANES);
 

+ 6 - 2
panda/src/dxgsg7/dxGraphicsStateGuardian7.h

@@ -112,6 +112,7 @@ public:
   virtual void issue_texture(const TextureAttrib *attrib);
   virtual void issue_material(const MaterialAttrib *attrib);
   virtual void issue_render_mode(const RenderModeAttrib *attrib);
+  virtual void issue_rescale_normal(const RescaleNormalAttrib *attrib);
   virtual void issue_texture_apply(const TextureApplyAttrib *attrib);
   virtual void issue_alpha_test(const AlphaTestAttrib *attrib);
   virtual void issue_depth_test(const DepthTestAttrib *attrib);
@@ -181,6 +182,7 @@ protected:
   D3DMATRIX         _SavedTransform;   
 
   RenderBuffer::Type _cur_read_pixel_buffer;  // source for copy_pixel_buffer operation
+  bool _auto_rescale_normal;
 
   void GenerateSphere(void *pVertexSpace,DWORD dwVertSpaceByteSize,
                     void *pIndexSpace,DWORD dwIndexSpaceByteSize,
@@ -202,8 +204,10 @@ protected:
   INLINE void enable_zwritemask(bool val);
   INLINE void set_shademode(D3DSHADEMODE val);
 
-  INLINE D3DTEXTUREADDRESS get_texture_wrap_mode(Texture::WrapMode wm) const;
-  INLINE D3DFOGMODE get_fog_mode_type(Fog::Mode m) const;
+  D3DTEXTUREADDRESS get_texture_wrap_mode(Texture::WrapMode wm) const;
+  D3DFOGMODE get_fog_mode_type(Fog::Mode m) const;
+
+  void do_auto_rescale_normal();
 
   INLINE void enable_primitive_clipping(bool val);
   INLINE void enable_alpha_test(bool val);

+ 0 - 9
panda/src/dxgsg8/config_dxgsg8.cxx

@@ -65,15 +65,6 @@ ConfigVariableBool dx_show_cursor_watermark
 ConfigVariableBool dx_use_triangle_mipgen_filter
 ("dx-use-triangle-mipgen-filter", false);
 
-// Configure this true to cause all lighting normals to automatically
-// be normalized by the CPU before rendering.  This is
-// necessary if you intend to render things under scale transforms and
-// expect lighting to work correctly.  Maybe one day there will be
-// another way to set this at runtime, instead of only as a configure
-// variable
-ConfigVariableBool dx_auto_normalize_lighting
-("auto-normalize-lighting", true);
-
 #ifndef NDEBUG
 // debugging flag
 // values are same as D3DCULL enumtype, 0 - no force, 1 - force none, 2 - force CW, 3 - force CCW

+ 0 - 1
panda/src/dxgsg8/config_dxgsg8.h

@@ -33,7 +33,6 @@ extern ConfigVariableBool dx_sync_video;
 extern ConfigVariableBool dx_no_vertex_fog;
 extern ConfigVariableBool dx_show_cursor_watermark;
 extern ConfigVariableBool dx_full_screen_antialiasing;
-extern ConfigVariableBool dx_auto_normalize_lighting;
 extern ConfigVariableBool dx_use_rangebased_fog;
 extern ConfigVariableBool link_tristrips;
 extern ConfigVariableInt dx_multisample_antialiasing_level;

+ 60 - 3
panda/src/dxgsg8/dxGraphicsStateGuardian8.cxx

@@ -43,6 +43,7 @@
 #include "texMatrixAttrib.h"
 #include "materialAttrib.h"
 #include "renderModeAttrib.h"
+#include "rescaleNormalAttrib.h"
 #include "fogAttrib.h"
 #include "depthOffsetAttrib.h"
 #include "fog.h"
@@ -152,6 +153,8 @@ void DXGraphicsStateGuardian8::
 reset_panda_gsg(void) {
     GraphicsStateGuardian::reset();
 
+    _auto_rescale_normal = false;
+
     // overwrite gsg defaults with these values
 
     // All implementations have the following buffers.
@@ -480,9 +483,6 @@ dx_init(void) {
     }
     */
 
-    if(dx_auto_normalize_lighting)
-         _pD3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
-
     // must do SetTSS here because redundant states are filtered out by our code based on current values above, so
     // initial conditions must be correct
 
@@ -3146,6 +3146,11 @@ issue_transform(const TransformState *transform) {
   // if we're using ONLY vertex shaders, could get avoid calling SetTrans
   D3DMATRIX *pMat = (D3DMATRIX*)transform->get_mat().get_data();
   _pD3DDevice->SetTransform(D3DTS_WORLD,pMat);
+
+  _transform = transform;
+  if (_auto_rescale_normal) {
+    do_auto_rescale_normal();
+  }
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -3286,6 +3291,38 @@ issue_render_mode(const RenderModeAttrib *attrib) {
 
   _current_fill_mode = mode;
 }
+ 
+////////////////////////////////////////////////////////////////////
+//     Function: DXGraphicsStateGuardian8::issue_rescale_normal
+//       Access: Public, Virtual
+//  Description:
+////////////////////////////////////////////////////////////////////
+void DXGraphicsStateGuardian8::
+issue_rescale_normal(const RescaleNormalAttrib *attrib) {
+  RescaleNormalAttrib::Mode mode = attrib->get_mode();
+
+  _auto_rescale_normal = false;
+
+  switch (mode) {
+  case RescaleNormalAttrib::M_none:
+    _pD3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, false);
+    break;
+
+  case RescaleNormalAttrib::M_rescale:
+  case RescaleNormalAttrib::M_normalize:
+    _pD3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
+    break;
+
+  case RescaleNormalAttrib::M_auto:
+    _auto_rescale_normal = true;
+    do_auto_rescale_normal();
+    break;
+
+  default:
+    dxgsg8_cat.error()
+      << "Unknown rescale_normal mode " << (int)mode << endl;
+  }
+}
 
 ////////////////////////////////////////////////////////////////////
 //     Function: DXGraphicsStateGuardian8::issue_texture_apply
@@ -3765,6 +3802,26 @@ set_read_buffer(const RenderBuffer &rb) {
     return;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: DXGraphicsStateGuardian8::do_auto_rescale_normal
+//       Access: Protected
+//  Description: Issues the appropriate GL commands to either rescale
+//               or normalize the normals according to the current
+//               transform.
+////////////////////////////////////////////////////////////////////
+void DXGraphicsStateGuardian8::
+do_auto_rescale_normal() {
+  if (_transform->has_uniform_scale() &&
+      IS_NEARLY_EQUAL(_transform->get_uniform_scale(), 1.0f)) {
+    // If there's no scale, don't normalize anything.
+    _pD3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, false);
+
+  } else {
+    // If there is a scale, turn on normalization.
+    _pD3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
+  }
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: DXGraphicsStateGuardian8::enable_lighting
 //       Access: Protected, Virtual

+ 4 - 0
panda/src/dxgsg8/dxGraphicsStateGuardian8.h

@@ -114,6 +114,7 @@ public:
   virtual void issue_texture(const TextureAttrib *attrib);
   virtual void issue_material(const MaterialAttrib *attrib);
   virtual void issue_render_mode(const RenderModeAttrib *attrib);
+  virtual void issue_rescale_normal(const RescaleNormalAttrib *attrib);
   virtual void issue_texture_apply(const TextureApplyAttrib *attrib);
   virtual void issue_alpha_test(const AlphaTestAttrib *attrib);
   virtual void issue_depth_test(const DepthTestAttrib *attrib);
@@ -169,6 +170,8 @@ protected:
 
   INLINE void add_to_FVFBuf(void *data,  size_t bytes) ;
 
+  void do_auto_rescale_normal();
+
   bool                  _bDXisReady;
   HRESULT               _last_testcooplevel_result;
   DXTextureContext8  *_pCurTexContext;
@@ -177,6 +180,7 @@ protected:
   D3DMATRIX         _SavedTransform;
 
   RenderBuffer::Type _cur_read_pixel_buffer;  // source for copy_pixel_buffer operation
+  bool _auto_rescale_normal;
 
   void GenerateSphere(void *pVertexSpace,DWORD dwVertSpaceByteSize,
                     void *pIndexSpace,DWORD dwIndexSpaceByteSize,

+ 0 - 9
panda/src/dxgsg9/config_dxgsg9.cxx

@@ -65,15 +65,6 @@ ConfigVariableBool dx_show_cursor_watermark
 ConfigVariableBool dx_use_triangle_mipgen_filter
 ("dx-use-triangle-mipgen-filter", false);
 
-// Configure this true to cause all lighting normals to automatically
-// be normalized by the CPU before rendering.  This is
-// necessary if you intend to render things under scale transforms and
-// expect lighting to work correctly.  Maybe one day there will be
-// another way to set this at runtime, instead of only as a configure
-// variable
-ConfigVariableBool dx_auto_normalize_lighting
-("auto-normalize-lighting", true);
-
 #ifndef NDEBUG
 // debugging flag
 // values are same as D3DCULL enumtype, 0 - no force, 1 - force none, 2 - force CW, 3 - force CCW

+ 0 - 1
panda/src/dxgsg9/config_dxgsg9.h

@@ -33,7 +33,6 @@ extern ConfigVariableBool dx_sync_video;
 extern ConfigVariableBool dx_no_vertex_fog;
 extern ConfigVariableBool dx_show_cursor_watermark;
 extern ConfigVariableBool dx_full_screen_antialiasing;
-extern ConfigVariableBool dx_auto_normalize_lighting;
 extern ConfigVariableBool dx_use_rangebased_fog;
 extern ConfigVariableBool link_tristrips;
 extern ConfigVariableInt dx_multisample_antialiasing_level;

+ 60 - 3
panda/src/dxgsg9/dxGraphicsStateGuardian9.cxx

@@ -42,6 +42,7 @@
 #include "texMatrixAttrib.h"
 #include "materialAttrib.h"
 #include "renderModeAttrib.h"
+#include "rescaleNormalAttrib.h"
 #include "fogAttrib.h"
 #include "depthOffsetAttrib.h"
 #include "fog.h"
@@ -151,6 +152,8 @@ void DXGraphicsStateGuardian9::
 reset_panda_gsg(void) {
     GraphicsStateGuardian::reset();
 
+    _auto_rescale_normal = false;
+
     // overwrite gsg defaults with these values
 
     // All implementations have the following buffers.
@@ -479,9 +482,6 @@ dx_init(void) {
     }
     */
 
-    if(dx_auto_normalize_lighting)
-         _pD3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
-
     // must do SetTSS here because redundant states are filtered out by our code based on current values above, so
     // initial conditions must be correct
 
@@ -3134,6 +3134,11 @@ issue_transform(const TransformState *transform) {
   // if we're using ONLY vertex shaders, could get avoid calling SetTrans
   D3DMATRIX *pMat = (D3DMATRIX*)transform->get_mat().get_data();
   _pD3DDevice->SetTransform(D3DTS_WORLD,pMat);
+
+  _transform = transform;
+  if (_auto_rescale_normal) {
+    do_auto_rescale_normal();
+  }
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -3274,6 +3279,38 @@ issue_render_mode(const RenderModeAttrib *attrib) {
 
   _current_fill_mode = mode;
 }
+ 
+////////////////////////////////////////////////////////////////////
+//     Function: DXGraphicsStateGuardian9::issue_rescale_normal
+//       Access: Public, Virtual
+//  Description:
+////////////////////////////////////////////////////////////////////
+void DXGraphicsStateGuardian9::
+issue_rescale_normal(const RescaleNormalAttrib *attrib) {
+  RescaleNormalAttrib::Mode mode = attrib->get_mode();
+
+  _auto_rescale_normal = false;
+
+  switch (mode) {
+  case RescaleNormalAttrib::M_none:
+    _pD3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, false);
+    break;
+
+  case RescaleNormalAttrib::M_rescale:
+  case RescaleNormalAttrib::M_normalize:
+    _pD3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
+    break;
+
+  case RescaleNormalAttrib::M_auto:
+    _auto_rescale_normal = true;
+    do_auto_rescale_normal();
+    break;
+
+  default:
+    dxgsg9_cat.error()
+      << "Unknown rescale_normal mode " << (int)mode << endl;
+  }
+}
 
 ////////////////////////////////////////////////////////////////////
 //     Function: DXGraphicsStateGuardian9::issue_texture_apply
@@ -3753,6 +3790,26 @@ set_read_buffer(const RenderBuffer &rb) {
     return;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: DXGraphicsStateGuardian9::do_auto_rescale_normal
+//       Access: Protected
+//  Description: Issues the appropriate GL commands to either rescale
+//               or normalize the normals according to the current
+//               transform.
+////////////////////////////////////////////////////////////////////
+void DXGraphicsStateGuardian9::
+do_auto_rescale_normal() {
+  if (_transform->has_uniform_scale() &&
+      IS_NEARLY_EQUAL(_transform->get_uniform_scale(), 1.0f)) {
+    // If there's no scale, don't normalize anything.
+    _pD3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, false);
+
+  } else {
+    // If there is a scale, turn on normalization.
+    _pD3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
+  }
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: DXGraphicsStateGuardian9::enable_lighting
 //       Access: Protected, Virtual

+ 4 - 0
panda/src/dxgsg9/dxGraphicsStateGuardian9.h

@@ -116,6 +116,7 @@ public:
   virtual void issue_texture(const TextureAttrib *attrib);
   virtual void issue_material(const MaterialAttrib *attrib);
   virtual void issue_render_mode(const RenderModeAttrib *attrib);
+  virtual void issue_rescale_normal(const RescaleNormalAttrib *attrib);
   virtual void issue_texture_apply(const TextureApplyAttrib *attrib);
   virtual void issue_alpha_test(const AlphaTestAttrib *attrib);
   virtual void issue_depth_test(const DepthTestAttrib *attrib);
@@ -170,6 +171,8 @@ protected:
 
   INLINE void add_to_FVFBuf(void *data,  size_t bytes) ;
 
+  void do_auto_rescale_normal();
+
   bool                  _bDXisReady;
   HRESULT               _last_testcooplevel_result;
   DXTextureContext9  *_pCurTexContext;
@@ -178,6 +181,7 @@ protected:
   D3DMATRIX         _SavedTransform;
 
   RenderBuffer::Type _cur_read_pixel_buffer;  // source for copy_pixel_buffer operation
+  bool _auto_rescale_normal;
 
   void GenerateSphere(void *pVertexSpace,DWORD dwVertSpaceByteSize,
                     void *pIndexSpace,DWORD dwIndexSpaceByteSize,