RayTracing Translate/Rotate Camera C/C++

Viewed 513

First of all, please excuse my bad english.

I face a problem with my raytracing program. I want to move my camera around my scene with some keys shortcuts. I want to have the possibility to rotate it and moving forward, backword, right, left...

The problem is that I have some difficulties to do it. How my camera is defined ?

t_cam   *CameraInit(t_vec vCamPos, t_vec vUpVector)
{
  t_cam *cam;

  if ((cam = bunny_malloc(sizeof(*cam))) == NULL)
    {
      print_error("Error: bunny_malloc error\n");
      return NULL;
    }
  cam->camPos = vCamPos;
  cam->upVec = vUpVector;
  cam->viewPlanDist = 0.5f;
  cam->viewPlanHeight = 0.35f;
  cam->viewPlanWidth = 0.5f;
  return cam;
}

t_cam   *Camera(t_vec camPos, t_vec lookAtPoint, t_vec upVector, char *name)
{
  t_vec tmp;
  t_vec tmp2;
  t_cam *cam;

  if ((cam = CameraInit(camPos, upVector)) == NULL)
    return NULL;
  cam->name = name;
  cam->lookAtPoint = lookAtPoint;
  cam->vecDir = VecSoustraction(lookAtPoint, camPos);
  Normalize(&cam->vecDir);
  printf("VecDir = x: %f y: %f z: %f\n", cam->vecDir.x, cam->vecDir.y, cam->vecDir.z);
  cam->rightVec = VecMultiply(cam->upVec, cam->vecDir);
  cam->upVec = VecMultiply(cam->vecDir, cam->rightVec);
  tmp = VecScalingMult(cam->vecDir, cam->viewPlanDist);
  tmp2 = VecScalingMult(cam->upVec, cam->viewPlanHeight / 2.0f);
  tmp = VecAddition(tmp, tmp2);
  tmp = VecAddition(tmp, cam->camPos);
  tmp2 = VecScalingMult(cam->rightVec, cam->viewPlanWidth / 2.0f);
  cam->viewPlanUpLeft = VecSoustraction(tmp, tmp2);
  return cam;
}

Where VecScalingMult just muliply each component of the vector (x, y, z) by a coefficient. So I have a UpVector, RightVector, a DirVector Normalized, the point at which looks the camera and the camera position.

Do you have any idea how to rotate and move the camera in the 3D space ?

Thanks

0 Answers
Related