Browse Source

fix directx indexing, lighting

David Rose 20 years ago
parent
commit
e6fb16576b
3 changed files with 104 additions and 73 deletions
  1. 43 23
      panda/src/dxgsg8/dxGraphicsStateGuardian8.cxx
  2. 53 36
      panda/src/gobj/material.I
  3. 8 14
      panda/src/gobj/material.cxx

+ 43 - 23
panda/src/dxgsg8/dxGraphicsStateGuardian8.cxx

@@ -405,6 +405,10 @@ dx_init(void) {
     _projection_mat_stack_count = 0;
     _has_scene_graph_color = false;
 
+    // Apply a default material when materials are turned off.
+    Material empty;
+    apply_material(&empty);
+
 //  GL stuff that hasnt been translated to DX
     // none of these are implemented
     //_multisample_enabled = false;         // bugbug:  translate this to dx_multisample_antialiasing_level?
@@ -521,8 +525,6 @@ dx_init(void) {
     _current_fill_mode = RenderModeAttrib::M_filled;
     _pD3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
 
-    _pD3DDevice->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_COLOR1);  // Use the diffuse vertex color.
-
     // must do SetTSS here because redundant states are filtered out by our code based on current values above, so
     // initial conditions must be correct
 
@@ -2744,7 +2746,7 @@ draw_triangles(const qpGeomTriangles *primitive) {
     _pD3DDevice->DrawIndexedPrimitive
       (D3DPT_TRIANGLELIST,
        primitive->get_min_vertex(),
-       primitive->get_max_vertex() + 1,
+       primitive->get_max_vertex() - primitive->get_min_vertex() + 1,
        0, primitive->get_num_primitives());
 
   } else {
@@ -2753,7 +2755,7 @@ draw_triangles(const qpGeomTriangles *primitive) {
     _pD3DDevice->DrawIndexedPrimitiveUP
       (D3DPT_TRIANGLELIST, 
        primitive->get_min_vertex(),
-       primitive->get_max_vertex() + 1,
+       primitive->get_max_vertex() - primitive->get_min_vertex() + 1,
        primitive->get_num_primitives(), 
        primitive->get_data(),
        index_type,
@@ -2820,7 +2822,7 @@ draw_tristrips(const qpGeomTristrips *primitive) {
         unsigned int max = maxs.get_data1i();
         _pD3DDevice->DrawIndexedPrimitive
           (D3DPT_TRIANGLESTRIP,
-           min, max + 1, 
+           min, max - min + 1, 
            start, ends[i] - start - 2);
         
         start = ends[i] + 2;
@@ -2838,7 +2840,7 @@ draw_tristrips(const qpGeomTristrips *primitive) {
         unsigned int max = maxs.get_data1i();
         _pD3DDevice->DrawIndexedPrimitiveUP
           (D3DPT_TRIANGLESTRIP, 
-           min, max + 1, 
+           min, max - min + 1, 
            ends[i] - start - 2,
            vertices + start * index_stride, index_type,
            array_data, stride);
@@ -2882,7 +2884,7 @@ draw_trifans(const qpGeomTrifans *primitive) {
       unsigned int max = maxs.get_data1i();
       _pD3DDevice->DrawIndexedPrimitive
         (D3DPT_TRIANGLEFAN,
-         min, max + 1,
+         min, max - min + 1,
          start, ends[i] - start - 2);
       
       start = ends[i];
@@ -2900,12 +2902,7 @@ draw_trifans(const qpGeomTrifans *primitive) {
       unsigned int max = maxs.get_data1i();
       _pD3DDevice->DrawIndexedPrimitiveUP
         (D3DPT_TRIANGLEFAN, 
-         // It's not clear whether the third parameter to
-         // DrawIndexedPrimitiveUP() should be (max + 1) or (max - min
-         // + 1).  The documentation seems to imply it should be (max
-         // - min + 1), but empirically it seems that only (max + 1)
-         // works.
-         min, max + 1,
+         min, max - min + 1,
          ends[i] - start - 2,
          vertices + start * index_stride, index_type,
          array_data, stride);
@@ -2931,7 +2928,7 @@ draw_lines(const qpGeomLines *primitive) {
     _pD3DDevice->DrawIndexedPrimitive
       (D3DPT_LINELIST,
        primitive->get_min_vertex(),
-       primitive->get_max_vertex() + 1,
+       primitive->get_max_vertex() - primitive->get_min_vertex() + 1,
        0, primitive->get_num_primitives());
 
   } else {
@@ -2940,7 +2937,7 @@ draw_lines(const qpGeomLines *primitive) {
     _pD3DDevice->DrawIndexedPrimitiveUP
       (D3DPT_LINELIST, 
        primitive->get_min_vertex(),
-       primitive->get_max_vertex() + 1,
+       primitive->get_max_vertex() - primitive->get_min_vertex() + 1,
        primitive->get_num_primitives(), 
        primitive->get_data(),
        index_type,
@@ -3503,14 +3500,37 @@ framebuffer_copy_to_ram(Texture *tex, int z, const DisplayRegion *dr, const Rend
 //       Access: Public, Virtual
 //  Description:
 ////////////////////////////////////////////////////////////////////
-void DXGraphicsStateGuardian8::apply_material( const Material* material ) {
-    D3DMATERIAL8 cur_material;
-    cur_material.Diffuse = *(D3DCOLORVALUE *)(material->get_diffuse().get_data());
-    cur_material.Ambient = *(D3DCOLORVALUE *)(material->get_ambient().get_data());
-    cur_material.Specular = *(D3DCOLORVALUE *)(material->get_specular().get_data());
-    cur_material.Emissive = *(D3DCOLORVALUE *)(material->get_emission().get_data());
-    cur_material.Power   =  material->get_shininess();
-    _pD3DDevice->SetMaterial(&cur_material);
+void DXGraphicsStateGuardian8::
+apply_material(const Material *material) {
+  D3DMATERIAL8 cur_material;
+  cur_material.Diffuse = *(D3DCOLORVALUE *)(material->get_diffuse().get_data());
+  cur_material.Ambient = *(D3DCOLORVALUE *)(material->get_ambient().get_data());
+  cur_material.Specular = *(D3DCOLORVALUE *)(material->get_specular().get_data());
+  cur_material.Emissive = *(D3DCOLORVALUE *)(material->get_emission().get_data());
+  cur_material.Power = material->get_shininess();
+
+  _pD3DDevice->SetMaterial(&cur_material);
+
+  if (material->has_diffuse()) {
+    // If the material specifies an diffuse color, use it.
+    _pD3DDevice->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
+  } else {
+    // Otherwise, the diffuse color comes from the object color.
+    _pD3DDevice->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
+  }
+  if (material->has_ambient()) {
+    // If the material specifies an ambient color, use it.
+    _pD3DDevice->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL);
+  } else {
+    // Otherwise, the ambient color comes from the object color.
+    _pD3DDevice->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_COLOR1);
+  }
+
+  if (material->get_local()) {
+    _pD3DDevice->SetRenderState(D3DRS_LOCALVIEWER, TRUE);
+  } else {
+    _pD3DDevice->SetRenderState(D3DRS_LOCALVIEWER, FALSE);
+  }    
 }
 
 ////////////////////////////////////////////////////////////////////

+ 53 - 36
panda/src/gobj/material.I

@@ -19,18 +19,22 @@
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::Constructor
-//       Access: Public
+//       Access: Published
 //  Description:
 ////////////////////////////////////////////////////////////////////
 INLINE Material::
 Material() {
-  _flags = 0;
+  _ambient.set(0.0f, 0.0f, 0.0f, 0.0f);
+  _diffuse.set(1.0f, 1.0f, 1.0f, 1.0f);
+  _specular.set(0.0f, 0.0f, 0.0f, 0.0f);
+  _emission.set(0.0f, 0.0f, 0.0f, 0.0f);
   _shininess = 0.0;
+  _flags = 0;
 }
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::Copy Constructor
-//       Access: Public
+//       Access: Published
 //  Description:
 ////////////////////////////////////////////////////////////////////
 INLINE Material::
@@ -40,7 +44,7 @@ Material(const Material &copy) {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::Destructor
-//       Access: Public
+//       Access: Published
 //  Description:
 ////////////////////////////////////////////////////////////////////
 INLINE Material::
@@ -49,7 +53,7 @@ INLINE Material::
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::has_ambient
-//       Access: Public
+//       Access: Published
 //  Description: Returns true if the ambient color has been explicitly
 //               set for this material, false otherwise.
 ////////////////////////////////////////////////////////////////////
@@ -60,29 +64,30 @@ has_ambient() const {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::get_ambient
-//       Access: Public
+//       Access: Published
 //  Description: Returns the ambient color setting, if it has been
 //               set.  Returns (0,0,0,0) if the ambient color has not
 //               been set.
 ////////////////////////////////////////////////////////////////////
 INLINE const Colorf &Material::
 get_ambient() const {
-  return (_flags & F_ambient) != 0 ? _ambient : Colorf::zero();
+  return _ambient;
 }
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::clear_ambient
-//       Access: Public
+//       Access: Published
 //  Description: Removes the explicit ambient color from the material.
 ////////////////////////////////////////////////////////////////////
 INLINE void Material::
 clear_ambient() {
   _flags &= ~F_ambient;
+  _ambient.set(0.0f, 0.0f, 0.0f, 0.0f);
 }
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::has_diffuse
-//       Access: Public
+//       Access: Published
 //  Description: Returns true if the diffuse color has been explicitly
 //               set for this material, false otherwise.
 ////////////////////////////////////////////////////////////////////
@@ -93,29 +98,30 @@ has_diffuse() const {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::get_diffuse
-//       Access: Public
+//       Access: Published
 //  Description: Returns the diffuse color setting, if it has been
-//               set.  Returns (0,0,0,0) if the diffuse color has not
+//               set.  Returns (1,1,1,1) if the diffuse color has not
 //               been set.
 ////////////////////////////////////////////////////////////////////
 INLINE const Colorf &Material::
 get_diffuse() const {
-  return (_flags & F_diffuse) != 0 ? _diffuse : Colorf::zero();
+  return _diffuse;
 }
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::clear_diffuse
-//       Access: Public
+//       Access: Published
 //  Description: Removes the explicit diffuse color from the material.
 ////////////////////////////////////////////////////////////////////
 INLINE void Material::
 clear_diffuse() {
   _flags &= ~F_diffuse;
+  _diffuse.set(1.0f, 1.0f, 1.0f, 1.0f);
 }
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::has_specular
-//       Access: Public
+//       Access: Published
 //  Description: Returns true if the specular color has been explicitly
 //               set for this material, false otherwise.
 ////////////////////////////////////////////////////////////////////
@@ -126,29 +132,30 @@ has_specular() const {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::get_specular
-//       Access: Public
+//       Access: Published
 //  Description: Returns the specular color setting, if it has been
 //               set.  Returns (0,0,0,0) if the specular color has not
 //               been set.
 ////////////////////////////////////////////////////////////////////
 INLINE const Colorf &Material::
 get_specular() const {
-  return (_flags & F_specular) != 0 ? _specular : Colorf::zero();
+  return _specular;
 }
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::clear_specular
-//       Access: Public
+//       Access: Published
 //  Description: Removes the explicit specular color from the material.
 ////////////////////////////////////////////////////////////////////
 INLINE void Material::
 clear_specular() {
   _flags &= ~F_specular;
+  _specular.set(0.0f, 0.0f, 0.0f, 0.0f);
 }
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::has_emission
-//       Access: Public
+//       Access: Published
 //  Description: Returns true if the emission color has been explicitly
 //               set for this material, false otherwise.
 ////////////////////////////////////////////////////////////////////
@@ -159,29 +166,30 @@ has_emission() const {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::get_emission
-//       Access: Public
-//  Description: Returns the emmission color setting, if it has been
-//               set.  Returns (0,0,0,0) if the emmission color has not
+//       Access: Published
+//  Description: Returns the emission color setting, if it has been
+//               set.  Returns (0,0,0,0) if the emission color has not
 //               been set.
 ////////////////////////////////////////////////////////////////////
 INLINE const Colorf &Material::
 get_emission() const {
-  return (_flags & F_emission) != 0 ? _emission : Colorf::zero();
+  return _emission;
 }
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::clear_emission
-//       Access: Public
+//       Access: Published
 //  Description: Removes the explicit emission color from the material.
 ////////////////////////////////////////////////////////////////////
 INLINE void Material::
 clear_emission() {
   _flags &= ~F_emission;
+  _emission.set(0.0f, 0.0f, 0.0f, 0.0f);
 }
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::get_shininess
-//       Access: Public
+//       Access: Published
 //  Description: Returns the shininess exponent of the material.
 ////////////////////////////////////////////////////////////////////
 INLINE float Material::
@@ -191,7 +199,7 @@ get_shininess() const {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::set_shininess
-//       Access: Public
+//       Access: Published
 //  Description: Sets the shininess exponent of the material.  This
 //               controls the size of the specular highlight spot.  In
 //               general, larger number produce a smaller specular
@@ -206,8 +214,8 @@ set_shininess(float shininess) {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::get_local
-//       Access: Public
-//  Description:
+//       Access: Published
+//  Description: Returns the local viewer flag.  Set set_local().
 ////////////////////////////////////////////////////////////////////
 INLINE bool Material::
 get_local() const {
@@ -216,8 +224,12 @@ get_local() const {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::set_local
-//       Access: Public
-//  Description:
+//       Access: Published
+//  Description: Sets the local viewer flag.  Set this true to enable
+//               camera-relative specular highlights, or false to use
+//               orthogonal specular highlights.  The default value is
+//               true.  Applications that use orthogonal projection
+//               should specify false.
 ////////////////////////////////////////////////////////////////////
 INLINE void Material::
 set_local(bool local) {
@@ -230,8 +242,9 @@ set_local(bool local) {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::get_twoside
-//       Access: Public
-//  Description:
+//       Access: Published
+//  Description: Returns the state of the two-sided lighting flag.
+//               See set_twoside().
 ////////////////////////////////////////////////////////////////////
 INLINE bool Material::
 get_twoside() const {
@@ -240,8 +253,12 @@ get_twoside() const {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::set_twoside
-//       Access: Public
-//  Description:
+//       Access: Published
+//  Description: Set this true to enable two-sided lighting.  When
+//               two-sided lighting is on, both sides of a polygon
+//               will be lit by this material.  The default is for
+//               two-sided lighting to be off, in which case only the
+//               front surface is lit.
 ////////////////////////////////////////////////////////////////////
 INLINE void Material::
 set_twoside(bool twoside) {
@@ -254,7 +271,7 @@ set_twoside(bool twoside) {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::operator ==
-//       Access: Public
+//       Access: Published
 //  Description:
 ////////////////////////////////////////////////////////////////////
 INLINE bool Material::
@@ -264,7 +281,7 @@ operator == (const Material &other) const {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::operator !=
-//       Access: Public
+//       Access: Published
 //  Description:
 ////////////////////////////////////////////////////////////////////
 INLINE bool Material::
@@ -274,7 +291,7 @@ operator != (const Material &other) const {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::operator <
-//       Access: Public
+//       Access: Published
 //  Description:
 ////////////////////////////////////////////////////////////////////
 INLINE bool Material::

+ 8 - 14
panda/src/gobj/material.cxx

@@ -18,23 +18,17 @@
 
 #include "pandabase.h"
 #include "material.h"
-
 #include "indent.h"
 #include "datagram.h"
 #include "datagramIterator.h"
 #include "bamReader.h"
 #include "bamWriter.h"
 
-#include <stddef.h>
-
-////////////////////////////////////////////////////////////////////
-// Static variables
-////////////////////////////////////////////////////////////////////
 TypeHandle Material::_type_handle;
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::Copy Assignment Operator
-//       Access: Public
+//       Access: Published
 //  Description:
 ////////////////////////////////////////////////////////////////////
 void Material::
@@ -49,7 +43,7 @@ operator = (const Material &copy) {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::set_ambient
-//       Access: Public
+//       Access: Published
 //  Description: Specifies the ambient color setting of the material.
 //               This will be the multiplied by any ambient lights in
 //               effect on the material to set its base color.
@@ -67,7 +61,7 @@ set_ambient(const Colorf &color) {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::set_diffuse
-//       Access: Public
+//       Access: Published
 //  Description: Specifies the diffuse color setting of the material.
 //               This will be multiplied by any lights in effect on
 //               the material to get the color in the parts of the
@@ -87,7 +81,7 @@ set_diffuse(const Colorf &color) {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::set_specular
-//       Access: Public
+//       Access: Published
 //  Description: Specifies the diffuse color setting of the material.
 //               This will be multiplied by any lights in effect on
 //               the material to compute the color of specular
@@ -106,7 +100,7 @@ set_specular(const Colorf &color) {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::set_emission
-//       Access: Public
+//       Access: Published
 //  Description: Specifies the emission color setting of the material.
 //               This is the color of the object as it appears in the
 //               absence of any light whatsover, including ambient
@@ -126,7 +120,7 @@ set_emission(const Colorf &color) {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::compare_to
-//       Access: Public
+//       Access: Published
 //  Description: Returns a number less than zero if this material
 //               sorts before the other one, greater than zero if it
 //               sorts after, or zero if they are equivalent.  The
@@ -159,7 +153,7 @@ compare_to(const Material &other) const {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::output
-//       Access: Public
+//       Access: Published
 //  Description:
 ////////////////////////////////////////////////////////////////////
 void Material::
@@ -184,7 +178,7 @@ output(ostream &out) const {
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Material::write
-//       Access: Public
+//       Access: Published
 //  Description:
 ////////////////////////////////////////////////////////////////////
 void Material::