Browse Source

fix primitive bumpmap support

David Rose 20 years ago
parent
commit
0fb2f183e3

+ 12 - 12
panda/src/gobj/texture.cxx

@@ -151,9 +151,9 @@ generate_normalization_cube_map(int size) {
       vec.normalize();
       vec = scale.xform_point(vec);
 
-      *p++ = (unsigned char)vec[0];
-      *p++ = (unsigned char)vec[1];
       *p++ = (unsigned char)vec[2];
+      *p++ = (unsigned char)vec[1];
+      *p++ = (unsigned char)vec[0];
     }
   }
 
@@ -163,9 +163,9 @@ generate_normalization_cube_map(int size) {
       LVector3f vec(-half_size, center - yi, xi - center);
       vec.normalize();
       vec = scale.xform_point(vec);
-      *p++ = (unsigned char)vec[0];
-      *p++ = (unsigned char)vec[1];
       *p++ = (unsigned char)vec[2];
+      *p++ = (unsigned char)vec[1];
+      *p++ = (unsigned char)vec[0];
     }
   }
 
@@ -175,9 +175,9 @@ generate_normalization_cube_map(int size) {
       LVector3f vec(xi - center, half_size, yi - center);
       vec.normalize();
       vec = scale.xform_point(vec);
-      *p++ = (unsigned char)vec[0];
-      *p++ = (unsigned char)vec[1];
       *p++ = (unsigned char)vec[2];
+      *p++ = (unsigned char)vec[1];
+      *p++ = (unsigned char)vec[0];
     }
   }
 
@@ -187,9 +187,9 @@ generate_normalization_cube_map(int size) {
       LVector3f vec(xi - center, -half_size, center - yi);
       vec.normalize();
       vec = scale.xform_point(vec);
-      *p++ = (unsigned char)vec[0];
-      *p++ = (unsigned char)vec[1];
       *p++ = (unsigned char)vec[2];
+      *p++ = (unsigned char)vec[1];
+      *p++ = (unsigned char)vec[0];
     }
   }
 
@@ -199,9 +199,9 @@ generate_normalization_cube_map(int size) {
       LVector3f vec(xi - center, center - yi, half_size);
       vec.normalize();
       vec = scale.xform_point(vec);
-      *p++ = (unsigned char)vec[0];
-      *p++ = (unsigned char)vec[1];
       *p++ = (unsigned char)vec[2];
+      *p++ = (unsigned char)vec[1];
+      *p++ = (unsigned char)vec[0];
     }
   }
 
@@ -211,9 +211,9 @@ generate_normalization_cube_map(int size) {
       LVector3f vec(center - xi, center - yi, -half_size);
       vec.normalize();
       vec = scale.xform_point(vec);
-      *p++ = (unsigned char)vec[0];
-      *p++ = (unsigned char)vec[1];
       *p++ = (unsigned char)vec[2];
+      *p++ = (unsigned char)vec[1];
+      *p++ = (unsigned char)vec[0];
     }
   }
 }

+ 6 - 9
panda/src/pgraph/cullableObject.cxx

@@ -480,22 +480,19 @@ munge_texcoord_light_vector(const CullTraverser *traverser) {
   for (lvi = light_vectors.begin();
        lvi != light_vectors.end();
        ++lvi) {
-    TextureStage *stage = (*lvi).first;
-    const NodePath &light = (*lvi).second;
+    TextureStage *stage = (*lvi);
+    NodePath light = tex_gen->get_light(stage);
     nassertv(!light.is_empty());
+    string source_name = tex_gen->get_source_name(stage);
     Light *light_obj = light.node()->as_light();
     nassertv(light_obj != (Light *)NULL);
 
     // Determine the names of the tangent and binormal columns
     // associated with the stage's texcoord name.
-    CPT(InternalName) texcoord_name = stage->get_texcoord_name();
-    string basename;
-    if (texcoord_name != InternalName::get_texcoord()) {
-      basename = texcoord_name->get_basename();
-    }
+    CPT(InternalName) tangent_name = InternalName::get_tangent_name(source_name);
+    CPT(InternalName) binormal_name = InternalName::get_binormal_name(source_name);
 
-    CPT(InternalName) tangent_name = InternalName::get_tangent_name(basename);
-    CPT(InternalName) binormal_name = InternalName::get_binormal_name(basename);
+    CPT(InternalName) texcoord_name = stage->get_texcoord_name();
 
     if (_munged_data->has_column(tangent_name) &&
         _munged_data->has_column(binormal_name)) {

+ 1 - 1
panda/src/pgraph/directionalLight.cxx

@@ -154,7 +154,7 @@ bool DirectionalLight::
 get_vector_to_light(LVector3f &result, const LPoint3f &, 
                     const LMatrix4f &to_object_space) {
   CDReader cdata(_cycler);
-  result = cdata->_direction * to_object_space;
+  result = -(cdata->_direction * to_object_space);
 
   return true;
 }

+ 5 - 3
panda/src/pgraph/nodePath.cxx

@@ -3253,11 +3253,13 @@ set_tex_gen(TextureStage *stage, RenderAttrib::TexGenMode mode, int priority) {
 //  Description: Enables automatic texture coordinate generation for
 //               the indicated texture stage.  This version of this
 //               method is useful when setting M_light_vector, which
-//               requires a specific light.
+//               requires the name of the texture coordinate set that
+//               supplies the tangent and binormal, as well as the
+//               specific light to generate coordinates for.
 ////////////////////////////////////////////////////////////////////
 void NodePath::
 set_tex_gen(TextureStage *stage, RenderAttrib::TexGenMode mode, 
-            const NodePath &light, int priority) {
+            const string &source_name, const NodePath &light, int priority) {
   nassertv_always(!is_empty());
 
   const RenderAttrib *attrib =
@@ -3274,7 +3276,7 @@ set_tex_gen(TextureStage *stage, RenderAttrib::TexGenMode mode,
     tga = DCAST(TexGenAttrib, TexGenAttrib::make());
   }
 
-  node()->set_attrib(tga->add_stage(stage, mode, light), priority);
+  node()->set_attrib(tga->add_stage(stage, mode, source_name, light), priority);
 }
 
 ////////////////////////////////////////////////////////////////////

+ 3 - 1
panda/src/pgraph/nodePath.h

@@ -581,7 +581,9 @@ PUBLISHED:
   INLINE LVecBase2f get_tex_scale(const NodePath &other, TextureStage *stage) const;
 
   void set_tex_gen(TextureStage *stage, RenderAttrib::TexGenMode mode, int priority = 0);
-  void set_tex_gen(TextureStage *stage, RenderAttrib::TexGenMode mode, const NodePath &light, int priority = 0);
+  void set_tex_gen(TextureStage *stage, RenderAttrib::TexGenMode mode, 
+                   const string &source_name, const NodePath &light, 
+                   int priority = 0);
   void clear_tex_gen();
   void clear_tex_gen(TextureStage *stage);
   bool has_tex_gen(TextureStage *stage) const;

+ 5 - 1
panda/src/pgraph/texGenAttrib.I

@@ -113,6 +113,10 @@ compare_to(const TexGenAttrib::ModeDef &other) const {
   if (_mode != other._mode) {
     return (int)_mode < (int)other._mode ? -1 : 1;
   }
-  return _light.compare_to(other._light);
+  int compare = _light.compare_to(other._light);
+  if (compare != 0) {
+    return compare;
+  }
+  return strcmp(_source_name.c_str(), other._source_name.c_str());
 }
 

+ 31 - 9
panda/src/pgraph/texGenAttrib.cxx

@@ -61,8 +61,9 @@ make() {
 //               indicated stage.
 ////////////////////////////////////////////////////////////////////
 CPT(RenderAttrib) TexGenAttrib::
-make(TextureStage *stage, TexGenAttrib::Mode mode, const NodePath &light) {
-  return DCAST(TexGenAttrib, make())->add_stage(stage, mode, light);
+make(TextureStage *stage, TexGenAttrib::Mode mode, 
+     const string &source_name, const NodePath &light) {
+  return DCAST(TexGenAttrib, make())->add_stage(stage, mode, source_name, light);
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -74,12 +75,14 @@ make(TextureStage *stage, TexGenAttrib::Mode mode, const NodePath &light) {
 //               replaced.
 ////////////////////////////////////////////////////////////////////
 CPT(RenderAttrib) TexGenAttrib::
-add_stage(TextureStage *stage, TexGenAttrib::Mode mode, const NodePath &light) const {
+add_stage(TextureStage *stage, TexGenAttrib::Mode mode, 
+          const string &source_name, const NodePath &light) const {
   CPT(RenderAttrib) removed = remove_stage(stage);
   TexGenAttrib *attrib = new TexGenAttrib(*DCAST(TexGenAttrib, removed));
 
   ModeDef &mode_def = attrib->_stages[stage];
   mode_def._mode = mode;
+  mode_def._source_name = source_name;
   mode_def._light = light;
   switch (mode) {
   case M_point_sprite:
@@ -100,7 +103,7 @@ add_stage(TextureStage *stage, TexGenAttrib::Mode mode, const NodePath &light) c
         nassert_raise(strm.str());
 
       } else {
-        attrib->_light_vectors[stage] = light;
+        attrib->_light_vectors.insert(stage);
         attrib->_geom_rendering |= Geom::GR_texcoord_light_vector;
         attrib->_num_light_vectors++;
       }
@@ -189,6 +192,26 @@ get_mode(TextureStage *stage) const {
   return M_off;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: TexGenAttrib::get_source_name
+//       Access: Published
+//  Description: Returns the source name associated with the named
+//               texture stage, or the empty string if no name is
+//               associated with the indicated stage.  This is only
+//               meaningful if the mode is M_light_vector, in which
+//               case it indicates the name of the source texture
+//               coordinate set from which the tangent and binormal
+//               are derived.
+////////////////////////////////////////////////////////////////////
+string TexGenAttrib::
+get_source_name(TextureStage *stage) const {
+  Stages::const_iterator mi = _stages.find(stage);
+  if (mi != _stages.end()) {
+    return (*mi).second._source_name;
+  }
+  return string();
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: TexGenAttrib::get_light
 //       Access: Published
@@ -272,7 +295,8 @@ output(ostream &out) const {
       break;
 
     case M_light_vector:
-      out << "light_vector: " << mode_def._light;
+      out << "light_vector: \"" << mode_def._source_name << "\", "
+          << mode_def._light;
       break;
     }
     out << ")";
@@ -402,7 +426,6 @@ compose_impl(const RenderAttrib *other) const {
     TextureStage *stage = (*ri).first;
     const ModeDef &mode_def = (*ri).second;
     Mode mode = mode_def._mode;
-    const NodePath &light = mode_def._light;
 
     switch (mode) {
     case M_point_sprite:
@@ -412,7 +435,7 @@ compose_impl(const RenderAttrib *other) const {
       break;
       
     case M_light_vector:
-      attrib->_light_vectors[stage] = light;
+      attrib->_light_vectors.insert(stage);
       attrib->_geom_rendering |= Geom::GR_texcoord_light_vector;
       attrib->_num_light_vectors++;
       break;
@@ -487,7 +510,6 @@ invert_compose_impl(const RenderAttrib *other) const {
     TextureStage *stage = (*ri).first;
     const ModeDef &mode_def = (*ri).second;
     Mode mode = mode_def._mode;
-    const NodePath &light = mode_def._light;
 
     switch (mode) {
     case M_point_sprite:
@@ -497,7 +519,7 @@ invert_compose_impl(const RenderAttrib *other) const {
       break;
       
     case M_light_vector:
-      attrib->_light_vectors[stage] = light;
+      attrib->_light_vectors.insert(stage);
       attrib->_geom_rendering |= Geom::GR_texcoord_light_vector;
       attrib->_num_light_vectors++;
       break;

+ 6 - 5
panda/src/pgraph/texGenAttrib.h

@@ -54,14 +54,15 @@ public:
 
 PUBLISHED:
   static CPT(RenderAttrib) make();
-  static CPT(RenderAttrib) make(TextureStage *stage, Mode mode, const NodePath &light = NodePath());
+  static CPT(RenderAttrib) make(TextureStage *stage, Mode mode, const string &source_name = string(), const NodePath &light = NodePath());
 
-  CPT(RenderAttrib) add_stage(TextureStage *stage, Mode mode, const NodePath &light = NodePath()) const;
+  CPT(RenderAttrib) add_stage(TextureStage *stage, Mode mode, const string &source_name = string(), const NodePath &light = NodePath()) const;
   CPT(RenderAttrib) remove_stage(TextureStage *stage) const;
 
   bool is_empty() const;
   bool has_stage(TextureStage *stage) const;
   Mode get_mode(TextureStage *stage) const;
+  string get_source_name(TextureStage *stage) const;
   NodePath get_light(TextureStage *stage) const;
 
   INLINE int get_geom_rendering(int geom_rendering) const;
@@ -69,7 +70,7 @@ PUBLISHED:
 public:
   INLINE const Geom::NoTexCoordStages &get_no_texcoords() const;
 
-  typedef pmap<TextureStage *, NodePath> LightVectors;
+  typedef pset<TextureStage *> LightVectors;
   INLINE const LightVectors &get_light_vectors() const;
 
   virtual void issue(GraphicsStateGuardianBase *gsg) const;
@@ -87,6 +88,7 @@ private:
     INLINE ModeDef();
     INLINE int compare_to(const ModeDef &other) const;
     Mode _mode;
+    string _source_name;
     NodePath _light;
   };
   typedef pmap<PT(TextureStage), ModeDef> Stages;
@@ -99,8 +101,7 @@ private:
   Geom::NoTexCoordStages _no_texcoords;
 
   // This is another optimization during rendering; it lists the
-  // texture stages (if any) that use M_light_vector, and their
-  // associated lights.
+  // texture stages (if any) that use M_light_vector.
   LightVectors _light_vectors;
 
   // This element is only used during reading from a bam file.  It has