ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ControlEngine / iPhone / Classes / Controllers / GLViewController.m
1 /**
2  *  @file GLViewController.m
3  *
4  * Copyright 2009 Parrot SA. All rights reserved.
5  * @author D HAEYER Frederic
6  * @date 2009/10/26
7  */
8 #include "ConstantsAndMacros.h"
9 #import "GLViewController.h"
10
11 typedef struct
12 {
13         GLint depthFunc;
14         GLboolean depthWriteMask;
15         GLint textureBinding2D;
16         GLint textureEnvMode;
17         GLint blendSrc;
18         GLint blendDst;
19         GLint clientActiveTexture;
20         GLboolean depthTest;
21         GLboolean cullFace;
22         GLboolean texture2D;
23         GLboolean blend;
24         GLboolean lighting;
25         GLboolean vertexArray;
26         GLboolean textureCoordArray;
27         GLboolean colorArray;
28         GLboolean normalArray;
29         GLfloat linewidth;
30 } OpenGLContext;
31
32 static CGFloat const matrixOrthoFront[] = {1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f};
33 static CGFloat const matrixOrthoBack[] = {1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f};
34
35 static CGFloat const matrixOrthoBackLeft[] = {1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f};
36 static CGFloat const matrixOrthoBackRight[] = {-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f};
37
38
39 float normalize_vector(float x, float y, float z)
40 {
41         return sqrtf((x * x) + (y  * y) + (z * z));
42 }
43
44 @interface GLViewController ()
45         OpenGLContext openGLContext;
46         id<NavdataProtocol> navdata_delegate;
47
48 - (void)saveOpenGLContext;
49 - (void)restoreOpenGLContext;
50 @end
51
52 @implementation GLViewController
53
54 - (id)initWithFrame:(CGRect)frame withDelegate:(id<NavdataProtocol>)_navdata_delegate
55 {
56         if((self = [super init]) != nil)
57         {
58                 NSLog(@"Frame : %f, %f", frame.size.width, frame.size.height);
59                 
60                 navdata_delegate = _navdata_delegate;
61                 
62                 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
63                 {
64                         video = [[OpenGLVideo alloc] initWithPath:[[NSBundle mainBundle] pathForResource:@"background-iPad" ofType:@"png"] withScreenSize:frame.size];
65                 }
66                 else
67                 {
68                         video = [[OpenGLVideo alloc] initWithPath:[[NSBundle mainBundle] pathForResource:@"background" ofType:@"png"] withScreenSize:frame.size];
69                 }
70         }
71         
72         return self;
73 }
74
75 - (void)saveOpenGLContext
76 {
77         // Make sure the client active texture is the same as the server active texture
78         GLint activeTexture;
79         glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTexture);       
80         glGetIntegerv(GL_CLIENT_ACTIVE_TEXTURE, &openGLContext.clientActiveTexture);
81         glClientActiveTexture(activeTexture);
82         
83         // Save OpenGL parameters that have been modified
84         glGetIntegerv(GL_DEPTH_FUNC, &openGLContext.depthFunc);
85         glGetBooleanv(GL_DEPTH_WRITEMASK, &openGLContext.depthWriteMask);
86         glGetIntegerv(GL_TEXTURE_BINDING_2D, &openGLContext.textureBinding2D);
87         glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &openGLContext.textureEnvMode);
88         glGetIntegerv(GL_BLEND_SRC, &openGLContext.blendSrc);
89         glGetIntegerv(GL_BLEND_DST, &openGLContext.blendDst);
90         glGetFloatv(GL_LINE_WIDTH, &openGLContext.linewidth);
91         
92         openGLContext.depthTest = glIsEnabled(GL_DEPTH_TEST);
93         openGLContext.cullFace = glIsEnabled(GL_CULL_FACE);
94         openGLContext.texture2D = glIsEnabled(GL_TEXTURE_2D);
95         openGLContext.blend = glIsEnabled(GL_BLEND);
96         openGLContext.lighting = glIsEnabled(GL_LIGHTING);
97         openGLContext.vertexArray = glIsEnabled(GL_VERTEX_ARRAY);
98         openGLContext.textureCoordArray = glIsEnabled(GL_TEXTURE_COORD_ARRAY);
99         openGLContext.colorArray = glIsEnabled(GL_COLOR_ARRAY);
100         openGLContext.normalArray = glIsEnabled(GL_NORMAL_ARRAY);
101 }
102
103 - (void)restoreOpenGLContext
104 {
105         // Restore OpenGL parameters that have been modified
106         openGLContext.normalArray ? glEnableClientState(GL_NORMAL_ARRAY) : glDisableClientState(GL_NORMAL_ARRAY);
107         openGLContext.colorArray ? glEnableClientState(GL_COLOR_ARRAY) : glDisableClientState(GL_COLOR_ARRAY);
108         openGLContext.textureCoordArray ? glEnableClientState(GL_TEXTURE_COORD_ARRAY) : glDisableClientState(GL_TEXTURE_COORD_ARRAY);
109         openGLContext.vertexArray ? glEnableClientState(GL_VERTEX_ARRAY) : glDisableClientState(GL_VERTEX_ARRAY);
110         
111         openGLContext.lighting ? glEnable(GL_LIGHTING) : glDisable(GL_LIGHTING);
112         openGLContext.blend ? glEnable(GL_BLEND) : glDisable(GL_BLEND);
113         openGLContext.texture2D ? glEnable(GL_TEXTURE_2D) : glDisable(GL_TEXTURE_2D);
114         openGLContext.cullFace ? glEnable(GL_CULL_FACE) : glDisable(GL_CULL_FACE);
115         openGLContext.depthTest ? glEnable(GL_DEPTH_TEST) : glDisable(GL_DEPTH_TEST);
116         
117         glBlendFunc(openGLContext.blendSrc, openGLContext.blendDst);
118         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, openGLContext.textureEnvMode);
119         glBindTexture(GL_TEXTURE_2D, openGLContext.textureBinding2D);
120         glDepthMask(openGLContext.depthWriteMask);
121         glDepthFunc(openGLContext.depthFunc);
122         glLineWidth(openGLContext.linewidth);
123
124         // Make sure the client active texture is the same as the server active texture
125         glClientActiveTexture(openGLContext.clientActiveTexture);
126 }
127
128 - (void)setScreenOrientationRight:(BOOL)right
129 {
130         screenOrientationRight = right;
131 }
132
133 - (void)drawView
134 {       
135         // Save OpenGLContext
136         [self saveOpenGLContext];
137         
138         // Setup the quad
139         // WARNING: it is important to set the background color to "transparent black" (0, 0, 0, 0) in Unity!
140         glDepthFunc(GL_LEQUAL);
141         glDepthMask(GL_FALSE);
142         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
143         glBlendFunc(GL_ONE, GL_ONE);
144         
145         //glEnable(GL_DEPTH_TEST);
146         glDisable(GL_CULL_FACE);
147         glEnable(GL_TEXTURE_2D);
148         glEnable(GL_BLEND);
149         glDisable(GL_LIGHTING);
150         
151         glEnableClientState(GL_VERTEX_ARRAY);
152         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
153         glDisableClientState(GL_COLOR_ARRAY);
154         glDisableClientState(GL_NORMAL_ARRAY);
155                 
156         // Set the projection matrix for the background elements
157         glMatrixMode(GL_PROJECTION);
158         glPushMatrix();
159         glLoadIdentity();
160         glMultMatrixf(screenOrientationRight ? matrixOrthoBackRight : matrixOrthoBackLeft);
161         
162         // Set the model view matrix
163         glMatrixMode(GL_MODELVIEW);
164         glPushMatrix();
165         glLoadIdentity();
166
167         // Set the texture matrix
168         glMatrixMode(GL_TEXTURE);
169         glPushMatrix();
170         glLoadIdentity();
171         
172         // Bind the background texture
173         // Draw video
174         [video drawSelf];
175
176         // Restore the model view matrix
177         glMatrixMode(GL_TEXTURE);
178         glPopMatrix();
179
180         // Restore the model view matrix
181         glMatrixMode(GL_MODELVIEW);
182         glPopMatrix();
183         
184         // Restore the projection matrix
185         glMatrixMode(GL_PROJECTION);
186         glPopMatrix();
187
188         // Restore OpenGL context if modified
189         [self restoreOpenGLContext];
190
191 //      CHECK_OPENGL_ERROR();
192 }
193
194 - (void)dealloc 
195 {
196         [video release];
197         [super dealloc];
198 }
199
200 @end