ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / Examples / Multiplatform / Protocol / video.c
1 /*
2  * AR Drone demo
3  *
4  * OpenGL video rendering
5  */
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <math.h>
11 #include <float.h>
12 #include <assert.h>
13
14 #include <GLES/gl.h>
15 #include <GLES/glext.h>
16 #include "app.h"
17
18 static uint8_t    *pixbuf = NULL;
19 static GLuint      texture;
20
21 static long otick = 0;
22
23 void video_init(void)
24 {
25     int tex_width, tex_height;
26     GLint crop[4] = { VIDEO_WIDTH, 0, -VIDEO_WIDTH, VIDEO_HEIGHT };
27
28     INFO("initializing OpenGL video rendering...\n");
29
30     // FIXME: this should be computed (smallest power of 2 > dimension)
31     tex_width = 256;
32     tex_height = 256;
33     otick = 0;
34
35     if (!pixbuf) {
36         pixbuf = malloc(tex_width * tex_height * 2);
37         assert(pixbuf);
38         memcpy(pixbuf, default_image, VIDEO_WIDTH*VIDEO_HEIGHT*2);
39     }
40
41     glEnable(GL_TEXTURE_2D);
42     glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
43     glDeleteTextures(1, &texture);
44     glGenTextures(1, &texture);
45     glBindTexture(GL_TEXTURE_2D, texture);
46     // Call glTexImage2D only once, and use glTexSubImage2D later
47     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex_width, tex_height, 0, GL_RGB,
48                  GL_UNSIGNED_SHORT_5_6_5, pixbuf);
49     glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
50     // use NEAREST instead of LINEAR for faster (but uglier) results
51     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
52     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
53     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
54     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
55     glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
56     glDisable(GL_BLEND);
57     glDisable(GL_DITHER);
58     glDisable(GL_DEPTH_TEST);
59     glDepthMask(GL_FALSE);
60     glDisable(GL_CULL_FACE);
61     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
62     glShadeModel(GL_FLAT);
63     glClear(GL_COLOR_BUFFER_BIT);
64 }
65
66 // Called from the app framework.
67 void video_deinit()
68 {
69     INFO("terminating OpenGL video rendering...\n");
70     //glDeleteTextures(1, &texture);
71     if (pixbuf) {
72         free(pixbuf);
73         pixbuf = NULL;
74     }
75 }
76
77 void video_render(long tick, int width, int height)
78 {
79         static int num = 0;
80
81     glBindTexture(GL_TEXTURE_2D, texture);
82
83         if (num_picture_decoded != num) {
84         /* a new frame is available, update texture */
85         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, VIDEO_WIDTH,VIDEO_HEIGHT,
86                         GL_RGB, GL_UNSIGNED_SHORT_5_6_5, picture_buf);
87                 num = num_picture_decoded;
88     }
89     /* and draw it on the screen */
90     glDrawTexiOES(0, 0, 0, width, height);
91 }
92