Jump to content


Photo

Decimal accuracy


  • Please log in to reply
1 reply to this topic

#1 stokastic

stokastic

    GMC Member

  • GMC Member
  • 6 posts

Posted 27 June 2012 - 07:52 AM

I'm writing a ray tracer simulator in GM. Basically what it does is take an already made image, and make it look as if it's being raytraced. I'm running into a problem however in that after a high number of samples per pixel, the image appears biased towards grey. I'm wondering if this is a decimal accuracy issue or algorithmic.

This is a sampling of a red color. How it works is you have your starting red color, 0 (technically NULL), and each sample picks either full red (255,0,0) or black (0, 0, 0). It then compares this value to a precomputed 'weight' value for that pixel. If the original image has a red value of 120 at (0,0) that becomes the red weight value. If a random number is greater than that weight, it returns full red, else full black. Over time this *should* average to 120, however it doesn't. It will approach 120, then slowly rise and the image becomes washed out. I think this may be because of decimal precision. It is taking large numbers, multiplying them by even larger numbers, and dividing by large numbers, so it seems a thousandth may be lost here or there.
    num = irandom(255);
    if(num < weightr[xx,yy]){
      sat = 255;      
    }else{
      sat = 0;
    }
    red[xx,yy] = (red[xx,yy] * samples[xx,yy] + sat)/(samples[xx,yy] + 1); 
    samples[xx,yy] += 1;

I'm hoping someone with raytracing experience can tell me if this is accurate math/methodology.
  • 0

#2 chance

chance

    GMC Member

  • Reviewer
  • 5768 posts
  • Version:GM:Studio

Posted 27 June 2012 - 12:41 PM

I'm hoping someone with raytracing experience can tell me if this is accurate math/methodology.

Looking at a snippet of code out of context, it's impossible to tell. But GameMaker can easily handle the accuracy required for 8-bits per color channel. So I suspect your problem is improper rendering of the reflected ray, causing the wrong value for that surface pixel.

My experience with homemade ray-tracers is that many of them don't properly calculate the surface normal vectors. So the image has patches where the lighting isn't correct. Or worst case, it has saturated, or even black, patches.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users