   Raymarching Terrain
  I am trying to raymarch some simple terrain based on this tutorial:http://www.iquilezles.org/www/articles/terrainmarching/terrainmarching.htmAnd I am having some troubles trying to display what should be a simple terrain y=(sin(x)-sin(z))Here is my code, I think the problem is in the function generateRayForPixel.All I see when I output the image (depending on the y value of the camera position) is either all sky hits, all ground hits, or a slight sliver of sky, not sure what is going wrong here.    int xres = 128;    int yres = 128;    int main(int argc, char *argv[])    {    renderImage();    return 0;    }    void renderImage( )    {   //Set up image saving   BITMAP4 c,*image = NULL;   FILE *fptr;   //Create image pointer   image = Create_Bitmap(xres,yres);   int sky = 0;   int terrain = 0;   for( int j=0; j &lt; yres; j++ )for( int i=0; i &lt; xres; i++ ){Ray ray = generateRayForPixel( i, j );float t;if( castRay( ray.o, ray.d, t ) ){//image[xres*j+i] = terrainColor( ray, t );//int a = 10+4; c.r = 33; c.b = 33; c.g = 33; c.a = 0; image[j*xres+i] = c; // 2D to 1D indexing terrain++;}else{ c.r = 0; c.b = 255; c.g = 0; c.a = 0; image[j*xres+i] = c; // 2D to 1D indexing sky++;fprintf(stderr, "Hit sky at [%03d][%03d]\n", i,j);}}    fprintf(stderr, "Sky - %d , Terrain = %d \n", sky, terrain);    //output the image    fptr = fopen("mask.tga","w"); Write_Bitmap(fptr,image,xres,yres,12);    fclose(fptr);    }    Ray generateRayForPixel(int i, int j) {   //Calculate field of view stuff   float fovx, fovy;   fovx = pi / 4; //45 degrees   fovy = yres / xres * fovx;   //holds the x and y coords of the viewplane point   float x = 0;   float y = 0;   x = (((2* i) - xres) / xres) * tan(fovx);   y = (((2* j) - yres) / yres) * tan(fovy);   //the ray then goes from the camera at (0,0,0) to the view plane at (x,y,-1)   Ray ray;   ray.o = QVector3D(0,1,0);   QVector3D tempvec = QVector3D(x,y,-1) - ray.o;   tempvec.normalize();   ray.d = tempvec;   return ray;    }float f(float x, float z)    {   return sin(x) - sin(z);//return 2;    }    bool castRay( const QVector3D & ro, const QVector3D & rd, float & resT )    {   const float delt = 0.01f;   const float mint = 0.001f;   const float maxt = 10.0f;   for( float t = mint; t &lt; maxt; t += delt )   {  const QVector3D p = ro + rd*t;  if( p.y() &lt; f( p.x(), p.z() ) )  { resT = t - 0.5f*delt; return true;  }   }   return false;    }Any tips would be appreciated, can't seem to figure out what I am doing wrong.Thanks.   
Quote:Original post by paintstripperx = (((2* i) - xres) / xres) * tan(fovx);y = (((2* j) - yres) / yres) * tan(fovy);I'm not really sure of the maths behind the way you use x and y after that.And as it is written, I'm quite sure that it will result in an integer division... which is not what you want.Out of my head, so i'm not sure all is perfect, but you could maybe try this instead :QVector3D worldUp(0.0f, 1.0f, 0.0f);// you can precompute this unless you change fov or resolution ://float halfFovX = pi / 4.0f;float halfFovY = halfFovX * ((float)yres / (float)xres);float tanHalfFovX = tan(halfFovX);float tanHalfFovY = tan(halfFovY);// you can compute this before the whole loop ://QVector3D eye(0.0f, 1.0f, 0.0f); // anything you want (better with y>0 in your case)QVector3D front(1.0f, -1.0f, 10.0f);  // anything you want (except // worldUp)front.normalize();QVector3D left = QVector3D::crossProduct(worldUp, front);left.normalize();QVector3D up = QVector3D::crossProduct(front, left);Ray generateRayForPixel(int i, int j) {  float x = tanHalfFovX * (float)(2*i - xres)/(float)xres;   float y = tanHalfFovY * (float)(2*j - yres)/(float)yres;  Ray ray;  ray.o = eye;  //QVector3D proj = eye + front - up * y - left * x;  //ray.d = proj - eye;  ray.d = front - up * y - left * x;  ray.d.normalize();  return ray;} [Edited by - TiPiou on November 17, 2010 7:58:46 AM];  Thanks! That worked perfectly.Now on to the lighting part I guess.   ;Quote:Original post by paintstripperThanks! That worked perfectly.Glad to hear it ^^As usual, I posted too quickly, then edited and edited again... Best make sure you have the latest version here, I may have missed a few "normalize" and stuff, previously.Quote:Original post by paintstripperNow on to the lighting part I guess.Have a nice creative day ;)   ;  Just a quick change for anyone else that might use this answer.The line:QVector3D left = QVector3D::crossProduct(worldUp, front);Should be:QVector3D left = QVector3D::crossProduct(front, worldUp);Otherwise the image comes out upside down.   ;Quote:Original post by paintstripperJust a quick change for anyone else that might use this answer.The line:QVector3D left = QVector3D::crossProduct(worldUp, front);Should be:QVector3D left = QVector3D::crossProduct(front, worldUp);Otherwise the image comes out upside down.*scratches head*Using my right hand :* Thumb on worldUp, index finger on front, middle finger appears on the left* Thumb on front, index finger on left, middle finger appears raised towards the roof.Then, I reverse the usual (left-to-right, top-to-bottom) way of storing bitmap coordinates by using the minus sign in the formula "front - up * y - left * x"What am I missing ?I'm genuinely interrested by this answer ^^;  Ah, I am generating and storing the rays / image using the left to right and top to bottom method.