The main changes to this example occur in the display() function. For each pixel in the target texture, the function generates the slopes of the camera ray intersecting that point and looks up the correct brightness value in the uncorrected image buffer (as described in Rectifying Image Points). The result is the a texture containing the rectified image.
1 //display the textured quad
2 static void display(void)
4 glMatrixMode(GL_MODELVIEW);
6 glTranslatef(-32, -24, -50); //"Camera" viewpoint
7 glClear(GL_COLOR_BUFFER_BIT);
10 for( float row = 0; row < TEX_HEIGHT; row++ ) {
11 for( float col = 0; col < TEX_WIDTH; col++ ) {
13 //Normalize from pixel xy to range [0..1]
15 input.x = col/TEX_WIDTH;
16 input.y = row/TEX_HEIGHT;
18 //Convert from normalized [0..1] to ray slopes
19 input.x = (input.x - .5) * MAX_FOV;
20 input.y = (input.y - .5) * MAX_FOV;
22 LEAP_VECTOR pixel = LeapRectilinearToPixel(*connection,
23 eLeapPerspectiveType_stereo_left,
25 int dindex = (int)floor(row * TEX_WIDTH + col);
26 int pindex = (int)roundf(pixel.y) * image_width + (int)roundf(pixel.x);
27 if(pixel.x >= 0 && pixel.x < image_width && pixel.y >=0 && pixel.y < image_height){
28 undistorted_image_left[dindex] = ((char*)image_buffer)[pindex];
30 undistorted_image_left[dindex] = 128;
35 for( float row = 0; row < TEX_HEIGHT; row++ ) {
36 for( float col = 0; col < TEX_WIDTH; col++ ) {
38 //Normalize from pixel xy to range [0..1]
40 input.x = col/TEX_WIDTH;
41 input.y = row/TEX_HEIGHT;
43 //Convert from normalized [0..1] to ray slopes
44 input.x = (input.x - .5) * MAX_FOV;
45 input.y = (input.y - .5) * MAX_FOV;
47 LEAP_VECTOR pixel = LeapRectilinearToPixel(*connection,
48 eLeapPerspectiveType_stereo_right,
50 int dindex = (int)floor(row * TEX_WIDTH + col);
51 int pindex = (int)roundf(pixel.y + image_height) * image_width + (int)roundf(pixel.x);
53 if(pixel.x >= 0 && pixel.x < image_width && pixel.y >=0 && pixel.y < image_height){
54 undistorted_image_right[dindex] = ((char*)image_buffer)[pindex];
56 undistorted_image_right[dindex] = 200;
61 DrawImageQuad(0, 0, 64, 24, TEX_WIDTH, TEX_HEIGHT, undistorted_image_left);
62 DrawImageQuad(0, 26, 64, 50, TEX_WIDTH, TEX_HEIGHT, undistorted_image_right);
This example is only supported on platforms for which a working version of GLUT exists. It should not be overly difficult to port the example to a different OpenGL-based context, however.