// Raytrix API LF Example 01

#include <SDKDDKVer.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <conio.h>

// The Raytrix Core library
#include "Rx.Core\RxCore.h"

// The Raytrix Light Field API
#include "Rx.ApiLF\Rx.ApiLF.h"

using namespace Rx;
using namespace Rx::ApiLF;

int _tmain(int argc, _TCHAR* argv[])
{
	unsigned uImgID = 0;
	CRxString sxFile;
	CRxImage xImage;

	try
	{
		// Initialize Raytrix Library
		printf("Initializing API...\n");
		RX_TRY( RxInit(true, "", "", 0) );

		// Automatic CUDA device selection if no device ID or a negative device ID is given.
		printf("Selecting CUDA device...\n");
		RX_TRY( RxCudaSelectDevice() );

		// Construct path to ray image
		sxFile = "..\\ExampleImages\\Demo_01.ray";

		// Load a ray image
		printf("Loading image '%s'...\n", sxFile.ToCString() );
		RX_TRY( RxRayLoad(uImgID, sxFile) );

		// Bind the loaded ray image. This copies the ray image to the CUDA device.
		printf("Binding image...\n");
		RX_TRY( RxRayBind( uImgID ) );

		// Focus ray image to plane at depth 0.5
		printf("Focusing on plane...\n");
		RX_TRY( RxSetPar( EPar::Focus_Focus, 0.5 ));
		RX_TRY( RxFocusOnPlane( ) );

		// Get focused image from CUDA device
		printf("Reading image from CUDA device...\n");
		RX_TRY( RxGetImage( EImgID::Focus, xImage ) );

		// Now work with the image.
		// ...

		printf("finished.\n");

		printf("Press any key...\n");
		_getch();

	}
	catch( int iError )
	{
		printf("Press any key to end program...\n");
		_getch();
		return iError;
	}

	// Close Raytrix API, free all memory on host and CUDA device
	// and close all open cameras.
	RxFinalize();

	return 0;
}

