26 |
|
|
27 |
#include <Foundation/NSTimer.h> |
#include <Foundation/NSTimer.h> |
28 |
#include <AppKit/NSOpenGL.h> |
#include <AppKit/NSOpenGL.h> |
29 |
|
#include <AppKit/NSResponder.h> |
30 |
|
#include <AppKit/NSEvent.h> |
31 |
|
|
32 |
#include "Common/Camera.h" |
#include "Common/Camera.h" |
33 |
#include "Map/MapDisplay.h" |
#include "Map/MapDisplay.h" |
36 |
|
|
37 |
@implementation MapView |
@implementation MapView |
38 |
|
|
39 |
|
static int _nbRay = 70; |
40 |
|
static int _lastStep = 4; |
41 |
|
static int _mode = 1; |
42 |
|
static int _view = 0; |
43 |
|
|
44 |
|
static const GLfloat lightAmbient[] = { 0.75f, 0.75f, 0.75f, 1.0f }; |
45 |
|
static const GLfloat lightDiffuse[] = { 0.7f, 0.7f, 0.7f, 1.0f }; |
46 |
|
static const GLfloat lightPosition[] = { 10.0f, 350.0f, 10.0f}; |
47 |
|
|
48 |
|
|
49 |
- (id) initWithFrame: (NSRect)frameRect |
- (id) initWithFrame: (NSRect)frameRect |
50 |
pixelFormat: (NSOpenGLPixelFormat *)format |
pixelFormat: (NSOpenGLPixelFormat *)format |
51 |
{ |
{ |
57 |
_camera = [[Camera alloc] initWithFovy: 50.0 |
_camera = [[Camera alloc] initWithFovy: 50.0 |
58 |
aspect: 1.0 |
aspect: 1.0 |
59 |
zNear: 5.0 |
zNear: 5.0 |
60 |
zFar: 750.0 |
zFar: 250.0 |
61 |
eye: MakeCoord (0.0, 150.0, 0.0) |
eye: MakeCoord (0.0, 150.0, 0.0) |
62 |
center: MakeCoord (0.0, 150.0, 30.0) |
center: MakeCoord (0.0, 150.0, 30.0) |
63 |
up: MakeCoord (0.0, 1.0, 0.0)]; |
up: MakeCoord (0.0, 1.0, 0.0)]; |
64 |
|
|
65 |
_map = [[MapDisplay alloc] initWithImageName: @"carte.tiff" |
_map = [[MapDisplay alloc] initWithImageName: @"carteBasse2.tiff" |
66 |
colorsImageName: @"degrades.tiff" |
colorsImageName: @"degrades.tiff" |
67 |
mapShadow: nil]; |
mapShadow: nil]; |
68 |
} |
} |
70 |
return self; |
return self; |
71 |
} |
} |
72 |
|
|
73 |
|
-(void) setCamera: (Camera *)camera |
74 |
|
{ |
75 |
|
_camera = camera; |
76 |
|
} |
77 |
|
|
78 |
- (void) dealloc |
- (void) dealloc |
79 |
{ |
{ |
80 |
RELEASE(_camera); |
RELEASE(_camera); |
85 |
|
|
86 |
- (BOOL) initGL |
- (BOOL) initGL |
87 |
{ |
{ |
88 |
glShadeModel (GL_SMOOTH); |
glEnable(GL_LIGHTING); |
89 |
glClearColor (0.0f, 0.0f, 0.0f, 0.0f); |
glEnable(GL_LIGHT0); |
90 |
|
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient); |
91 |
|
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse); |
92 |
|
|
93 |
|
if (_mode) |
94 |
|
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); |
95 |
|
else |
96 |
|
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); |
97 |
|
glPolygonMode (GL_FRONT, GL_FILL); |
98 |
|
glColorMaterial (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); |
99 |
|
|
100 |
|
// glPushAttrib (GL_ALL_ATTRIB_BITS); |
101 |
glEnable (GL_DEPTH_TEST); |
glEnable (GL_DEPTH_TEST); |
102 |
|
glEnable (GL_SMOOTH); |
103 |
|
glEnable (GL_NORMALIZE); |
104 |
|
glEnable (GL_ALPHA_TEST); |
105 |
|
glEnable (GL_COLOR_MATERIAL); |
106 |
|
|
107 |
[NSTimer scheduledTimerWithTimeInterval: 0.001 |
[NSTimer scheduledTimerWithTimeInterval: 0.001 |
108 |
target: self |
target: self |
118 |
NSRect sceneBounds; |
NSRect sceneBounds; |
119 |
|
|
120 |
sceneBounds = [self bounds]; |
sceneBounds = [self bounds]; |
121 |
glViewport (0, 0, sceneBounds.size.width, sceneBounds.size.height); |
int l = sceneBounds.size.width; |
122 |
|
int h = sceneBounds.size.height; |
123 |
|
if (l<h) |
124 |
|
glViewport(0,(h-l)/2,l,l); |
125 |
|
else |
126 |
|
glViewport((l-h)/2,0,h,h); |
127 |
|
|
128 |
glMatrixMode (GL_PROJECTION); |
glMatrixMode (GL_PROJECTION); |
129 |
glLoadIdentity (); |
glLoadIdentity (); |
145 |
_init = YES; |
_init = YES; |
146 |
} |
} |
147 |
|
|
148 |
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
149 |
|
glLoadIdentity(); |
150 |
|
|
151 |
|
glLightfv( GL_LIGHT0, GL_POSITION, lightPosition ); |
152 |
|
|
|
glLoadIdentity (); |
|
153 |
gluLookAt ((GLdouble)[_camera eye].x, |
gluLookAt ((GLdouble)[_camera eye].x, |
154 |
(GLdouble)[_camera eye].y, |
(GLdouble)[_camera eye].y, |
155 |
(GLdouble)[_camera eye].z, |
(GLdouble)[_camera eye].z, |
160 |
(GLdouble)[_camera up].y, |
(GLdouble)[_camera up].y, |
161 |
(GLdouble)[_camera up].z); |
(GLdouble)[_camera up].z); |
162 |
|
|
163 |
[_map displayWithCamera: _camera |
if (_view) |
164 |
numberOfRay: 70 |
[_map displayWithCamera: _camera |
165 |
firstStep: 150 |
numberOfRay: _nbRay]; |
166 |
secondStep: 200]; |
else |
167 |
|
[_map displayWithCamera: _camera |
168 |
|
numberOfRay: _nbRay |
169 |
|
lastStep: _lastStep]; |
170 |
|
|
171 |
[self update]; |
[self update]; |
172 |
[[self openGLContext] flushBuffer]; |
[[self openGLContext] flushBuffer]; |
173 |
} |
} |
174 |
|
|
175 |
|
- (BOOL)performKeyEquivalent:(NSEvent *)theEvent |
176 |
|
{ |
177 |
|
NSString *keyString; |
178 |
|
unichar keyChar; |
179 |
|
double f = 3.0; |
180 |
|
double h = 0.01; |
181 |
|
double p = 0.1; |
182 |
|
|
183 |
|
keyString = [theEvent charactersIgnoringModifiers]; |
184 |
|
keyChar = [keyString characterAtIndex:0]; |
185 |
|
|
186 |
|
switch(keyChar) |
187 |
|
{ |
188 |
|
case NSUpArrowFunctionKey: |
189 |
|
case 'z': |
190 |
|
[_camera forward: f]; |
191 |
|
break; |
192 |
|
|
193 |
|
case NSDownArrowFunctionKey: |
194 |
|
case 's': |
195 |
|
[_camera backward: f]; |
196 |
|
break; |
197 |
|
|
198 |
|
case NSLeftArrowFunctionKey: |
199 |
|
case 'q': |
200 |
|
[_camera strafLeft: f]; |
201 |
|
break; |
202 |
|
|
203 |
|
case NSRightArrowFunctionKey: |
204 |
|
case 'd': |
205 |
|
[_camera strafRight: f]; |
206 |
|
break; |
207 |
|
|
208 |
|
case NSPageUpFunctionKey: |
209 |
|
case 'a': |
210 |
|
[_camera moveUp: f]; |
211 |
|
break; |
212 |
|
|
213 |
|
case NSPageDownFunctionKey: |
214 |
|
case 'e': |
215 |
|
[_camera moveDown: f]; |
216 |
|
break; |
217 |
|
|
218 |
|
case '4': |
219 |
|
[_camera rotatePitch: p]; |
220 |
|
break; |
221 |
|
case '6': |
222 |
|
[_camera rotatePitch: -p]; |
223 |
|
break; |
224 |
|
case '8': |
225 |
|
[_camera rotateHeading: h]; |
226 |
|
break; |
227 |
|
case '2': |
228 |
|
[_camera rotateHeading: -h]; |
229 |
|
break; |
230 |
|
|
231 |
|
case 'f': |
232 |
|
_mode = 1 - _mode; |
233 |
|
if (_mode) |
234 |
|
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); |
235 |
|
else |
236 |
|
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); |
237 |
|
break; |
238 |
|
|
239 |
|
case '+': |
240 |
|
_nbRay++; |
241 |
|
printf("nbRay: %d\n", _nbRay); |
242 |
|
break; |
243 |
|
case '-': |
244 |
|
_nbRay--; |
245 |
|
if (_nbRay == 2) |
246 |
|
_nbRay = 3; |
247 |
|
printf("nbRay: %d\n", _nbRay); |
248 |
|
break; |
249 |
|
|
250 |
|
case 'P': |
251 |
|
_lastStep++; |
252 |
|
printf("step: %d\n", _lastStep); |
253 |
|
break; |
254 |
|
case 'p': |
255 |
|
_lastStep--; |
256 |
|
if (_lastStep == 0) |
257 |
|
_lastStep = 1; |
258 |
|
printf("lastStep: %d\n", _lastStep); |
259 |
|
break; |
260 |
|
|
261 |
|
case 'v': |
262 |
|
_view = 1 - _view; |
263 |
|
coord_t eye = [_camera eye]; |
264 |
|
if (_view) |
265 |
|
[_camera initWithFovy: 45.0 |
266 |
|
aspect: 1.0 |
267 |
|
zNear: 5.0 |
268 |
|
zFar: 855.0 |
269 |
|
eye: eye |
270 |
|
center: MakeCoord (eye.x, eye.y - 10.0, eye.z) |
271 |
|
up: MakeCoord (0.0, 0.0, 1.0)]; |
272 |
|
else |
273 |
|
[_camera initWithFovy: 45.0 |
274 |
|
aspect: 1.0 |
275 |
|
zNear: 5.0 |
276 |
|
zFar: 255.0 |
277 |
|
eye: eye |
278 |
|
center: MakeCoord (eye.x, eye.y, eye.z + 30.0) |
279 |
|
up: MakeCoord (0.0, 1.0, 0.0)]; |
280 |
|
[self reshape]; |
281 |
|
break; |
282 |
|
|
283 |
|
default: |
284 |
|
break; |
285 |
|
} |
286 |
|
return YES; |
287 |
|
} |
288 |
|
|
289 |
@end |
@end |