Saturday, March 6, 2010

PostTwiceDaily2 03/06/2010 (a.m.)

  • tags: no_tag

    • Implementation of the classical recursive ray-tracing algorithm (in pseudo-code):


      #define MAX_RAY_DEPTH 3

      color Trace(const Ray &ray, int depth)
      {
      Object *object = NULL;
      float minDist = INFINITY;
      Point pHit;
      Normal nHit;
      for (int k = 0; k < objects.size(); ++k) {
      if (Intersect(objects[k], ray, &pHit, &nHit)) {
      // ray origin = eye position of it's the prim ray
      float distance = Distance(ray.origin, pHit);
      if (distance < minDistance) {
      object = objects[i];
      }
      }
      }
      if (object == NULL)
      return 0;
      // if the object material is glass, split the ray into a reflection
      // and a refraction ray.
      if (object->isGlass && depth < MAX_RAY_DEPTH) {
      // compute reflection
      Ray reflectionRay;
      reflectionRay = computeReflectionRay(ray.direction, nHit);
      // recurse
      color reflectionColor = Trace(reflectionRay, depth + 1);
      Ray refractioRay;
      refractionRay = computeRefractionRay(
      object->indexOfRefraction,
      ray.direction,
      nHit);
      // recurse
      color refractionColor = Trace(refractionRay, depth + 1);
      float Kr, Kt;
      fresnel(
      object->indexOfRefraction,
      nHit,
      ray.direction,
      &Kr,
      &Kt);
      return reflectionColor * Kr + refractionColor * (1-Kr);
      }
      // object is a diffuse opaque object
      // compute illumination
      Ray shadowRay;
      shadowRay.direction = lightPosition - pHit;
      bool isShadow = false;
      for (int k = 0; k < objects.size(); ++k) {
      if (Intersect(objects[k], shadowRay)) {
      // hit point is in shadow so just return
      return 0;
      }
      }
      // point is illuminated
      return object->color * light.brightness;
      }


      for (int j = 0; j < imageHeight; ++j) {
      for (int i = 0; i < imageWidth; ++i) {
      // compute primary ray direction
      Ray primRay;
      computePrimRay(i, j, &primRay);
      pixels[i][j] = Trace(primRay, 0);
      }
      }


       


      Download full source code

  • tags: no_tag

    • I'm having a little confusion when using gdb. When stepping through phase_2, and when I'm in read_six_numbers function, I cannot use "x/s $eax", it says "<Address <0xaddr> out of bounds>".


      How can i see what's in eax?



    • If you want to see what %eax has in it, you need to use the command "print".


      "x" is for when you know that %eax has an address in it and you want to go to that address.
  • tags: no_tag

    • #ifndef _VECTOR
      #define _VECTOR
      #include <math.h>

      class Vector3D
      {
      private:
      float x, y, z;
      public:
      //default constructor
      Vector3D(float X = 0, float Y = 0, float Z = 0)
      {
      x = X;
      y = Y;
      z = Z;
      }
      ~Vector3D(){};

      //calculate and return the magnitude of this vector
      float GetMagnitude()
      {
      return sqrtf(x * x + y * y + z * z);
      }

      //multiply this vector by a scalar
      Vector3D operator*(float num) const
      {
      return Vector3D(x * num, y * num, z * num);
      }

      //pass in a vector, pass in a scalar, return the product
      friend Vector3D operator*(float num, Vector3D const &vec)
      {
      return Vector3D(vec.x * num, vec.y * num, vec.z * num);
      }

      //add two vectors
      Vector3D operator+(const Vector3D &vec) const
      {
      return Vector3D(x + vec.x, y + vec.y, z + vec.z);
      }

      //subtract two vectors
      Vector3D operator-(const Vector3D &vec) const
      {
      return Vector3D(x - vec.x, y - vec.y, z - vec.z);
      }

      //normalize this vector
      void normalizeVector3D()
      {
      float magnitude = sqrtf(x * x + y * y + z * z);
      x /= magnitude;
      y /= magnitude;
      z /= magnitude;
      }

      //calculate and return dot product
      float dotVector3D(const Vector3D &vec) const
      {
      return x * vec.x + y * vec.y + z * vec.z;
      }

      //calculate and return cross product
      Vector3D crossVector3D(const Vector3D &vec) const
      {
      return Vector3D(y * vec.z - z * vec.y,
      z * vec.x - x * vec.z,
      x * vec.y - y * vec.x);
      }
      };

      #endif
    • In my code, I have dot and cross as free functions. But that is just how I choose to do it.
    • what is a free function?
    • One not in a class. For example, I have:



      float dot(const Vector &one, const Vector &two )
      {
      return /* ... */;
      }

      Vector cross(const Vector &one, const Vector &two )
      {
      return /* ... */;
      }

      // usage:
      void example()
      {
      Vector3 one, two;

      float d = dot(one,two);

      Vector3 c = cross(two,one);
      }
  • tags: no_tag

    • We have to cast a ray, at each one of our pixel to determine its color. This translates as the following C code :




       for (int y = 0; y < myScene.sizey; ++y) {
      for (int x = 0; x < myScene.sizex; ++x) {
      // ...
      }
      }
  • tags: no_tag

    • Z:\Microsoft Visual Studio 9.0\VC\INCLUDE\cstdio(39) : error C2143: syntax error
      : missing '{' before ':'
      Z:\Microsoft Visual Studio 9.0\VC\INCLUDE\cstdio(39) : error C2059: syntax error
      : ':'
    • You have given the program an extension of .c which
      means the compiler will try to compile it according to
      the rules of the C language. But it uses C++ features.


       


      Either change the extension to .cpp or compile with the option /TP

  • tags: no_tag


    • "There is an incompatibility between glut.h and Visual Studio .NET. that results in compile errors: error C2381: 'exit' : redefinition; __declspec(noreturn) differs
      error C3861: 'exit': identifier not found, even with argument-dependent lookup



      To fix the error, right click on the project name in the Solution Explorer tab and select Properties -> C/C++ -> Preprocessor -> Preprocessor definitions and append GLUT_BUILDING_LIB to the existing definitions, seperated by semicolons. "

  • tags: no_tag

    • How/where do I use _CRT_SECURE_NO_WARNINGS ?
    • Project properties->Configuration Properties->C/C++->Preprocessor->Preprocessor Definitions

      Click on the elipses (...)
      type any definitions you like separated by "\n"ie

      _CRT_SECURE_NO_WARNINGS

      hit OK.

Posted from Diigo. The rest of my favorite links are here.