Преглед изворни кода

Support for 1D, 3D textures and cubemaps

rdb пре 16 година
родитељ
комит
0e24827b9d
2 измењених фајлова са 57 додато и 8 уклоњено
  1. 56 8
      panda/src/pgraphnodes/shaderGenerator.cxx
  2. 1 0
      panda/src/pgraphnodes/shaderGenerator.h

+ 56 - 8
panda/src/pgraphnodes/shaderGenerator.cxx

@@ -540,11 +540,11 @@ update_shadow_buffer(NodePath light_np) {
 //               - color scale attrib
 //               - color scale attrib
 //               - light ramps (for cartoon shading)
 //               - light ramps (for cartoon shading)
 //               - shadow mapping
 //               - shadow mapping
-//               - texgen
+//               - most texgen modes
 //               - texmatrix
 //               - texmatrix
+//               - 1D/2D/3D textures, cube textures
 //
 //
 //               Not yet supported:
 //               Not yet supported:
-//               - 3D textures, cube textures
 //               - dot3_rgb and dot3_rgba combine modes
 //               - dot3_rgb and dot3_rgba combine modes
 //               - fog
 //               - fog
 //
 //
@@ -741,7 +741,9 @@ synthesize_shader(const RenderState *rs) {
   const TexMatrixAttrib *tex_matrix = DCAST(TexMatrixAttrib, rs->get_attrib_def(TexMatrixAttrib::get_class_slot()));
   const TexMatrixAttrib *tex_matrix = DCAST(TexMatrixAttrib, rs->get_attrib_def(TexMatrixAttrib::get_class_slot()));
   for (int i=0; i<_num_textures; i++) {
   for (int i=0; i<_num_textures; i++) {
     TextureStage *stage = texture->get_on_stage(i);
     TextureStage *stage = texture->get_on_stage(i);
-    text << "\t uniform sampler2D tex_" << i << ",\n";
+    Texture *tex = texture->get_on_texture(stage);
+    nassertr(tex != NULL, NULL);
+    text << "\t uniform sampler" << texture_type_as_string(tex->get_texture_type()) << " tex_" << i << ",\n";
     if (!tex_gen->has_stage(stage)) {
     if (!tex_gen->has_stage(stage)) {
       text << "\t in float4 l_texcoord" << i << " : " << texcoord_freg[i] << ",\n";
       text << "\t in float4 l_texcoord" << i << " : " << texcoord_freg[i] << ",\n";
     }
     }
@@ -852,24 +854,43 @@ synthesize_shader(const RenderState *rs) {
   }
   }
   text << "\t // Fetch all textures.\n";
   text << "\t // Fetch all textures.\n";
   if (_map_index_height >= 0) {
   if (_map_index_height >= 0) {
-    text << "\t float4 tex" << _map_index_height << " = tex2D(tex_" << _map_index_height << ", l_texcoord" << _map_index_height << ".xy);\n";
-    text << "\t float2 parallax_offset = l_eyevec.xy * (tex" << _map_index_height;
+    Texture *tex = texture->get_on_texture(texture->get_on_stage(_map_index_height));
+    nassertr(tex != NULL, NULL);
+    text << "\t float4 tex" << _map_index_height << " = tex" << texture_type_as_string(tex->get_texture_type());
+    text << "(tex_" << _map_index_height << ", l_texcoord" << _map_index_height << ".";
+    switch(tex->get_texture_type()) {
+      case Texture::TT_cube_map:
+      case Texture::TT_3d_texture: text << "xyz"; break;
+      case Texture::TT_2d_texture: text << "xy";  break;
+      case Texture::TT_1d_texture: text << "x";   break;
+    }
+    text << ");\n\t float2 parallax_offset = l_eyevec.xyz * (tex" << _map_index_height;
     if (_map_height_in_alpha) {
     if (_map_height_in_alpha) {
-      text << ".aa";
+      text << ".aaa";
     } else {
     } else {
-      text << ".rg";
+      text << ".rgb";
     }
     }
     text << " * 0.2 - 0.1);\n";
     text << " * 0.2 - 0.1);\n";
   }
   }
   for (int i=0; i<_num_textures; i++) {
   for (int i=0; i<_num_textures; i++) {
     if (i != _map_index_height) {
     if (i != _map_index_height) {
+      Texture *tex = texture->get_on_texture(texture->get_on_stage(i));
+      nassertr(tex != NULL, NULL);
       // Parallax mapping pushes the texture coordinates of the other textures away from the camera.
       // Parallax mapping pushes the texture coordinates of the other textures away from the camera.
       // The normal map coordinates aren't pushed (since that would give inconsistent behaviour when
       // The normal map coordinates aren't pushed (since that would give inconsistent behaviour when
       // the height map is packed with the normal map together).
       // the height map is packed with the normal map together).
       if (_map_index_height >= 0 && i != _map_index_normal) {
       if (_map_index_height >= 0 && i != _map_index_normal) {
         text << "\t l_texcoord" << i << ".xy += parallax_offset;\n";
         text << "\t l_texcoord" << i << ".xy += parallax_offset;\n";
       }
       }
-      text << "\t float4 tex" << i << " = tex2D(tex_" << i << ", l_texcoord" << i << ".xy);\n";
+      text << "\t float4 tex" << i << " = tex" << texture_type_as_string(tex->get_texture_type());
+      text << "(tex_" << i << ", l_texcoord" << i << ".";
+      switch(tex->get_texture_type()) {
+        case Texture::TT_cube_map:
+        case Texture::TT_3d_texture: text << "xyz"; break;
+        case Texture::TT_2d_texture: text << "xy";  break;
+        case Texture::TT_1d_texture: text << "x";   break;
+      }
+      text << ");\n";
     }
     }
   }
   }
   if (_lighting) {
   if (_lighting) {
@@ -1356,3 +1377,30 @@ combine_source_as_string(CPT(TextureStage) stage, short num, bool single_value,
   return csource.str();
   return csource.str();
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: ShaderGenerator::texture_type_as_string
+//       Access: Protected, Static
+//  Description: Returns 1D, 2D, 3D or CUBE, depending on the given
+//               texture type.
+////////////////////////////////////////////////////////////////////
+const string ShaderGenerator::
+texture_type_as_string(Texture::TextureType ttype) {
+  switch (ttype) {
+    case Texture::TT_1d_texture:
+      return "1D";
+      break;
+    case Texture::TT_2d_texture:
+      return "2D";
+      break;
+    case Texture::TT_3d_texture:
+      return "3D";
+      break;
+    case Texture::TT_cube_map:
+      return "CUBE";
+      break;
+    default:
+      pgraph_cat.error() << "Unsupported texture type!\n";
+      return "2D";
+  }
+}
+

+ 1 - 0
panda/src/pgraphnodes/shaderGenerator.h

@@ -76,6 +76,7 @@ protected:
                TextureStage::CombineMode c_mode, bool single_value, short texindex);
                TextureStage::CombineMode c_mode, bool single_value, short texindex);
   static const string combine_source_as_string(CPT(TextureStage) stage,
   static const string combine_source_as_string(CPT(TextureStage) stage,
                                       short num, bool single_value, short texindex);
                                       short num, bool single_value, short texindex);
+  static const string texture_type_as_string(Texture::TextureType ttype);
 
 
   // Shader register allocation:
   // Shader register allocation: