Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[Manta] r1786 - in trunk: Interface Model/Backgrounds Model/Textures


Chronological Thread 
  • From: boulos@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [Manta] r1786 - in trunk: Interface Model/Backgrounds Model/Textures
  • Date: Thu, 18 Oct 2007 14:27:58 -0600 (MDT)

Author: boulos
Date: Thu Oct 18 14:27:55 2007
New Revision: 1786

Modified:
   trunk/Interface/RayPacket.h
   trunk/Model/Backgrounds/EnvMapBackground.cc
   trunk/Model/Backgrounds/EnvMapBackground.h
   trunk/Model/Textures/ImageTexture.cc
Log:
Interface/RayPacket.h

 Fixing some annoying formatting issues from when Abe used XCode long ago.

Model/Backgrounds/EnvMapBackground.cc
Model/Backgrounds/EnvMapBackground.h

 Making EnvMapBackground take a Texture<Color> instead of a filename
 so we can be agnostic to format and even allow for procedural
 textures.

 Adding some new modes to EnvMapBackground to have different mappings.

Model/Textures/ImageTexture.cc

 Making isRGBE happen before the supported by image magick stuff since
 ImageMagick seems to believe it can handle .hdr values (but it
 doesn't).


Modified: trunk/Interface/RayPacket.h
==============================================================================
--- trunk/Interface/RayPacket.h (original)
+++ trunk/Interface/RayPacket.h Thu Oct 18 14:27:55 2007
@@ -304,15 +304,13 @@
 
     
///////////////////////////////////////////////////////////////////////////
     // GET DIRECTION
-    Vector getDirection(int which) const
-    {
+    Vector getDirection(int which) const {
       return Vector(data->direction[0][which], data->direction[1][which], 
data->direction[2][which]);
     }
 
-    Real &getDirection(int which, int i)
-                        {
-                                return data->direction[i][which];
-                        }
+    Real &getDirection(int which, int i) {
+      return data->direction[i][which];
+    }
     const Real &getDirection(int which, int i) const
     {
       return data->direction[i][which];
@@ -320,8 +318,7 @@
 
     
///////////////////////////////////////////////////////////////////////////
     // GET INVERSE DIRECTION
-    Vector getInverseDirection(int which) const
-    {
+    Vector getInverseDirection(int which) const {
       return Vector(data->inverseDirection[0][which], 
data->inverseDirection[1][which], data->inverseDirection[2][which]);
     }
 

Modified: trunk/Model/Backgrounds/EnvMapBackground.cc
==============================================================================
--- trunk/Model/Backgrounds/EnvMapBackground.cc (original)
+++ trunk/Model/Backgrounds/EnvMapBackground.cc Thu Oct 18 14:27:55 2007
@@ -1,48 +1,114 @@
 
-#include <Image/PPMFile.h>
-#include <Image/Pixel.h>
-#include <Image/SimpleImage.h>
 #include <Interface/RayPacket.h>
 #include <Model/Backgrounds/EnvMapBackground.h>
 #include <SCIRun/Core/Math/Trig.h>
+#include <SCIRun/Core/Exceptions/InternalError.h>
 
 using namespace Manta;
+using SCIRun::InternalError;
 
 static Real OneOverPi=static_cast<Real>(1/M_PI);
 static Real OneOverTwoPi=static_cast<Real>(1/(2*M_PI));
 //static Real PiOverTwo=static_cast<Real>(M_PI/2);
 
-EnvMapBackground::EnvMapBackground(const std::string& filename,
-                                   const Vector& right, const Vector& up)
-{
-  // Create an image texture
-  Image* ppmImage=readPPM(filename);
-  image=new ImageTexture<Color>(ppmImage);
-  // image->setInterpolationMethod(ImageTexture<Color>::Bilinear);
-  // image->setUEdgeBehavior(ImageTexture<Color>::Wrap);
-  // image->setVEdgeBehavior(ImageTexture<Color>::Clamp);
-
+EnvMapBackground::EnvMapBackground(Texture<Color>* image,
+                                   MappingType map_type,
+                                   const Vector& right,
+                                   const Vector& up)
+  : image(image),
+    map_type(map_type) {
   // Initialize an orthonormal basis
   U=right.normal();
   V=Cross(up, U).normal();
   W=Cross(U, V);
 }
 
-EnvMapBackground::~EnvMapBackground(void)
-{
-  if (image)
-    delete image;
+EnvMapBackground::~EnvMapBackground(void) {
 }
 
-void EnvMapBackground::preprocess(const PreprocessContext&)
-{
+void EnvMapBackground::preprocess(const PreprocessContext&) {
   // Do nothing
 }
 
-void EnvMapBackground::shade(const RenderContext& context, RayPacket& rays) 
const
-{
+void EnvMapBackground::shade(const RenderContext& context, RayPacket& rays) 
const {
+  switch (map_type) {
+  case LatLon:
+    throw InternalError("LatLon not finished", __FILE__, __LINE__);
+    break;
+  case CylindricalEqualArea:
+    CylindricalEqualAreaMapping(context, rays);
+    break;
+  case DebevecSphere:
+    throw InternalError("Debevec Sphere probe format not finished", 
__FILE__, __LINE__);
+    break;
+  case OldBehavior:
+    OldBehaviorMapping(context, rays);
+    break;
+  default:
+    throw InternalError("Unknown map_type", __FILE__, __LINE__);
+    break;
+  }
+
+  rays.setFlag(RayPacket::HaveTexture2);
+
+  // Look up the pixel colors
+  Packet<Color> colors;
+  image->mapValues(colors, context, rays);
+
+  for (int i=rays.begin(); i<rays.end(); ++i)
+    rays.setColor(i, colors.get(i));
+}
+
+void EnvMapBackground::CylindricalEqualAreaMapping(const RenderContext& 
context, RayPacket& rays) const {
+  rays.normalizeDirections();
+  for (int i = rays.begin(); i < rays.end(); i++) {
+    Vector local_dir(Dot(rays.getDirection(i), U),
+                     Dot(rays.getDirection(i), V),
+                     Dot(rays.getDirection(i), W));
+
+    // In "standard" spherical coordinates we have:
+    //
+    // x = r * cos(theta) * sin(phi)
+    // y = r * sin(theta) * sin(phi)
+    // z = r * cos(phi)
+    //
+    // So phi = acos(z/r) .
+    //
+    // Dividing equation 2 by equation 1 we get:
+    //
+    // y/x = sin(theta)/cos(theta) = tan(theta)
+    //
+    // So theta = atan(y/x)
+
+    // In this coordinate system W is the up vector, so z is up. To
+    // get the latitude and not colatitude though, we need to compute
+    // PI/2 - acos(z)
+    Real phi = .5 * M_PI - Acos(local_dir[2]);
+
+    // atan2 returns [-PI, PI]
+    Real theta = Atan2(local_dir[1], local_dir[0]);
+    // NOTE(boulos): Since atan2 is [-PI, PI] we want to turn [-PI, 0]
+    // into [PI, 2PI] so we only add 2PI when theta is less than 0.
+    if (theta < 0)
+      theta += 2. * M_PI;
+
+    // x = (longitude - longitude_center), for Lambert the longitude_center 
= 0
+    Real index_u = theta * OneOverTwoPi;
+    // y = sin(phi)
+    // sin([-PI/2, PI/2]) -> [-1, 1] but we want phi = 0 to give the equator 
(index_v = .5)
+    Real index_v = .5 * (1 + Sin(phi));
+
+    rays.setTexCoords(i, VectorT<Real, 2>(index_u, index_v));
+  }
+}
+
+// NOTE(boulos): This code was probably written by James or Christiaan
+// but it's not 100% documented. It looks like a cylindrical mapping,
+// but I'm not sure which one (as there are many). So I'm keeping it
+// around for now.
+void EnvMapBackground::OldBehaviorMapping(const RenderContext& context, 
RayPacket& rays) const {
   for (int i=rays.begin(); i<rays.end(); ++i) {
-    const Vector inDir(rays.getDirection(i));
+    const Vector inDir(rays.getDirection(i).normal());
 #if 1
     // Note:  Don't invert ray direction during projection into the 
cylindrical
     //   basis ...
@@ -74,7 +140,7 @@
     //   result + Pi is in [0, TwoPi]
     //   result/TwoPi is in [0, 1]
     texcoords[0]=(Atan2(dir.y(), -dir.x()) + Pi)*OneOverTwoPi;
-    
+
     // Compute and scale v
     //   result of Acos(z) is in [0, Pi]
     //   PiOverTwo - result is in [PiOver2, -PiOver2]
@@ -86,13 +152,4 @@
 
     rays.setTexCoords(i, texcoords);
   }
-
-  rays.setFlag(RayPacket::HaveTexture2);
-
-  // Look up the pixel colors
-  Packet<Color> colors;
-  image->mapValues(colors, context, rays);
-
-  for (int i=rays.begin(); i<rays.end(); ++i)
-    rays.setColor(i, colors.get(i));
 }

Modified: trunk/Model/Backgrounds/EnvMapBackground.h
==============================================================================
--- trunk/Model/Backgrounds/EnvMapBackground.h  (original)
+++ trunk/Model/Backgrounds/EnvMapBackground.h  Thu Oct 18 14:27:55 2007
@@ -4,15 +4,19 @@
 
 #include <Core/Color/Color.h>
 #include <Interface/Background.h>
-#include <Model/Textures/ImageTexture.h>
+#include <Interface/Texture.h>
 
-#include <string>
-
-namespace Manta
-{
+namespace Manta {
   class EnvMapBackground : public Background {
   public:
-    EnvMapBackground(const std::string& filename,
+    enum MappingType {
+      LatLon,
+      CylindricalEqualArea,
+      DebevecSphere,
+      OldBehavior
+    };
+    EnvMapBackground(Texture<Color>* image,
+                     MappingType map_type,
                      const Vector& right=Vector(1, 0, 0),
                      const Vector& up=Vector(0, 0, 1));
     virtual ~EnvMapBackground(void);
@@ -20,10 +24,14 @@
     virtual void preprocess(const PreprocessContext& context);
     virtual void shade(const RenderContext& context, RayPacket& rays) const;
 
+    void CylindricalEqualAreaMapping(const RenderContext& context, 
RayPacket& rays) const;
+    void OldBehaviorMapping(const RenderContext& context, RayPacket& rays) 
const;
+
   private:
-    ImageTexture<Color>* image;
+    Texture<Color>* image;
+    MappingType map_type;
     Vector U, V, W;
   };
-}
+} // end namespace Manta
 
 #endif

Modified: trunk/Model/Textures/ImageTexture.cc
==============================================================================
--- trunk/Model/Textures/ImageTexture.cc        (original)
+++ trunk/Model/Textures/ImageTexture.cc        Thu Oct 18 14:27:55 2007
@@ -58,12 +58,19 @@
     bool isNrrd = ( (file_name.rfind(".nrrd") == (file_name.size()-5)) ||
                     (file_name.rfind(".nhdr") == (file_name.size()-5))
                     );
+    bool isRGBE = ( (file_name.rfind(".hdr") == (file_name.size() - 4) ||
+                     file_name.rfind(".pic") == (file_name.size() - 4) ||
+                     file_name.rfind(".rgbe") == (file_name.size() - 5)));
+
     if (isNrrd) {
       // Check to see if it is a nrrd before trying to read with
       // something else.  ImageMagick will choke on nrrds and throw an
       // exception.
       image = readNRRD( file_name );
       if (stream) (*stream) << "Read by readNRRD\n";
+    } else if (isRGBE) {
+      image = readRGBE(file_name);
+      if (stream) (*stream) << "Read by readRGBE\n";
     } else if (ImageMagickSupported()) {
       image = readImageMagick( file_name );
       if (stream) (*stream) << "Read by readImageMagick\n";
@@ -74,12 +81,7 @@
         image = readTGA( file_name );
         if (stream) (*stream) << "Read by readTGA\n";
       }
-      else if (file_name.rfind(".hdr") == (file_name.size() - 4) ||
-               file_name.rfind(".pic") == (file_name.size() - 4) ||
-               file_name.rfind(".rgbe") == (file_name.size() - 5)) {
-        image = readRGBE(file_name);
-        if (stream) (*stream) << "Read by readRGBE\n";
-      }
+
       else if (NRRDSupported()) {
         // Try reading the image using teem.
         image = readNRRD( file_name );




  • [Manta] r1786 - in trunk: Interface Model/Backgrounds Model/Textures, boulos, 10/18/2007

Archive powered by MHonArc 2.6.16.

Top of page