2 * @file GLViewController.m
4 * Copyright 2009 Parrot SA. All rights reserved.
5 * @author D HAEYER Frederic
8 #include "ConstantsAndMacros.h"
9 #import "GLViewController.h"
14 GLboolean depthWriteMask;
15 GLint textureBinding2D;
19 GLint clientActiveTexture;
25 GLboolean vertexArray;
26 GLboolean textureCoordArray;
28 GLboolean normalArray;
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};
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};
39 float normalize_vector(float x, float y, float z)
41 return sqrtf((x * x) + (y * y) + (z * z));
44 @interface GLViewController ()
45 OpenGLContext openGLContext;
46 id<NavdataProtocol> navdata_delegate;
48 - (void)saveOpenGLContext;
49 - (void)restoreOpenGLContext;
52 @implementation GLViewController
54 - (id)initWithFrame:(CGRect)frame withDelegate:(id<NavdataProtocol>)_navdata_delegate
56 if((self = [super init]) != nil)
58 NSLog(@"Frame : %f, %f", frame.size.width, frame.size.height);
60 navdata_delegate = _navdata_delegate;
62 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
64 video = [[OpenGLVideo alloc] initWithPath:[[NSBundle mainBundle] pathForResource:@"background-iPad" ofType:@"png"] withScreenSize:frame.size];
68 video = [[OpenGLVideo alloc] initWithPath:[[NSBundle mainBundle] pathForResource:@"background" ofType:@"png"] withScreenSize:frame.size];
75 - (void)saveOpenGLContext
77 // Make sure the client active texture is the same as the server active texture
79 glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTexture);
80 glGetIntegerv(GL_CLIENT_ACTIVE_TEXTURE, &openGLContext.clientActiveTexture);
81 glClientActiveTexture(activeTexture);
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);
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);
103 - (void)restoreOpenGLContext
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);
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);
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);
124 // Make sure the client active texture is the same as the server active texture
125 glClientActiveTexture(openGLContext.clientActiveTexture);
128 - (void)setScreenOrientationRight:(BOOL)right
130 screenOrientationRight = right;
135 // Save OpenGLContext
136 [self saveOpenGLContext];
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);
145 //glEnable(GL_DEPTH_TEST);
146 glDisable(GL_CULL_FACE);
147 glEnable(GL_TEXTURE_2D);
149 glDisable(GL_LIGHTING);
151 glEnableClientState(GL_VERTEX_ARRAY);
152 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
153 glDisableClientState(GL_COLOR_ARRAY);
154 glDisableClientState(GL_NORMAL_ARRAY);
156 // Set the projection matrix for the background elements
157 glMatrixMode(GL_PROJECTION);
160 glMultMatrixf(screenOrientationRight ? matrixOrthoBackRight : matrixOrthoBackLeft);
162 // Set the model view matrix
163 glMatrixMode(GL_MODELVIEW);
167 // Set the texture matrix
168 glMatrixMode(GL_TEXTURE);
172 // Bind the background texture
176 // Restore the model view matrix
177 glMatrixMode(GL_TEXTURE);
180 // Restore the model view matrix
181 glMatrixMode(GL_MODELVIEW);
184 // Restore the projection matrix
185 glMatrixMode(GL_PROJECTION);
188 // Restore OpenGL context if modified
189 [self restoreOpenGLContext];
191 // CHECK_OPENGL_ERROR();