24 |
#include <GL/gl.h> |
#include <GL/gl.h> |
25 |
#include <GL/glu.h> |
#include <GL/glu.h> |
26 |
|
|
27 |
|
#include <math.h> |
28 |
|
|
29 |
#include <Foundation/NSTimer.h> |
#include <Foundation/NSTimer.h> |
30 |
#include <AppKit/NSOpenGL.h> |
#include <AppKit/NSOpenGL.h> |
31 |
#include <AppKit/NSResponder.h> |
#include <AppKit/NSResponder.h> |
47 |
|
|
48 |
const GLfloat lightAmbient[4] = {0.75f, 0.75f, 0.75f, 1.0f}; |
const GLfloat lightAmbient[4] = {0.75f, 0.75f, 0.75f, 1.0f}; |
49 |
const GLfloat lightDiffuse[4] = {0.7f, 0.7f, 0.7f, 1.0f}; |
const GLfloat lightDiffuse[4] = {0.7f, 0.7f, 0.7f, 1.0f}; |
50 |
const GLfloat lightPosition[4] = {10.0f, 350.0f, 10.0f}; |
const GLfloat lightPosition[4] = {0.0f, 2350.0f, 0.0f}; |
51 |
const GLfloat fogColor[4]= {1.0f, 1.0f, 1.0f, 1.0f}; |
const GLfloat fogColor[4]= {1.0f, 1.0f, 1.0f, 1.0f}; |
52 |
|
|
|
|
|
53 |
- (id) initWithFrame: (NSRect)frameRect |
- (id) initWithFrame: (NSRect)frameRect |
54 |
pixelFormat: (NSOpenGLPixelFormat *)format |
pixelFormat: (NSOpenGLPixelFormat *)format |
55 |
{ |
{ |
59 |
{ |
{ |
60 |
_init = NO; |
_init = NO; |
61 |
_display = NO; |
_display = NO; |
62 |
|
_displayBorder = YES; |
63 |
|
_mouseLoc.x = 0; |
64 |
|
_mouseLoc.y = 0; |
65 |
printf("INIT MapView\n"); |
printf("INIT MapView\n"); |
66 |
} |
} |
67 |
return self; |
return self; |
78 |
|
|
79 |
- (BOOL) initGL |
- (BOOL) initGL |
80 |
{ |
{ |
|
glEnable(GL_LIGHTING); |
|
|
glEnable(GL_LIGHT0); |
|
|
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient); |
|
|
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse); |
|
|
|
|
81 |
if (_mode) |
if (_mode) |
82 |
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); |
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); |
83 |
else |
else |
95 |
selector: @selector(display) |
selector: @selector(display) |
96 |
userInfo: nil |
userInfo: nil |
97 |
repeats: YES]; |
repeats: YES]; |
98 |
|
|
99 |
|
//[[self window] setAcceptsMouseMovedEvents: YES]; |
100 |
|
//[[self superview] setAcceptsMouseMovedEvents: YES]; |
101 |
|
|
102 |
return YES; |
return YES; |
103 |
} |
} |
104 |
|
|
106 |
{ |
{ |
107 |
glClearColor(fogColor[0], fogColor[1], fogColor[2], fogColor[3]); |
glClearColor(fogColor[0], fogColor[1], fogColor[2], fogColor[3]); |
108 |
glFogi(GL_FOG_MODE, GL_LINEAR); |
glFogi(GL_FOG_MODE, GL_LINEAR); |
109 |
glFogf(GL_FOG_START, [_camera zFar] - 10.0); |
glFogf(GL_FOG_START, [_camera zFar] - 30.0); |
110 |
glFogf(GL_FOG_END, [_camera zFar] + 1.0); |
glFogf(GL_FOG_END, [_camera zFar] + 10.0); |
|
//GLfloat fog_c[] = {0.5f, 0.5f, 0.5f, 1.0f}; |
|
111 |
glFogfv(GL_FOG_COLOR, fogColor); |
glFogfv(GL_FOG_COLOR, fogColor); |
112 |
} |
} |
113 |
|
|
114 |
|
- (void) _displayMapBorder |
115 |
|
{ |
116 |
|
NSParameterAssert (_map); |
117 |
|
glDisable(GL_LIGHTING); |
118 |
|
//glEnable(GL_LIGHTING); |
119 |
|
glLineWidth(2.0); |
120 |
|
|
121 |
|
int xLength = [_map xLength]; |
122 |
|
int zLength = [_map zLength]; |
123 |
|
|
124 |
|
glEnable (GL_DEPTH_TEST); |
125 |
|
|
126 |
|
glBegin(GL_LINE_LOOP); |
127 |
|
glColor3f(1.0,0.0,0.0); |
128 |
|
|
129 |
|
int x; |
130 |
|
for (x = 0; x < xLength; x++) |
131 |
|
glVertex3i(x, [_map heightAtPosition: MakeMapCoord (x, 0)] + 1, 0); |
132 |
|
|
133 |
|
int z; |
134 |
|
for (z = 0; z < zLength; z++) |
135 |
|
glVertex3i(x, [_map heightAtPosition: MakeMapCoord (x, z)] + 1, z); |
136 |
|
|
137 |
|
for (x = xLength-1; x >= 0; x--) |
138 |
|
glVertex3i(x, [_map heightAtPosition: MakeMapCoord (x, z)] + 1, z); |
139 |
|
|
140 |
|
for (z = zLength-1; z >= 0; z--) |
141 |
|
glVertex3i(0, [_map heightAtPosition: MakeMapCoord (0, z)] + 1, z); |
142 |
|
glEnd(); |
143 |
|
|
144 |
|
//glDisable(GL_LIGHTING); |
145 |
|
//glEnable(GL_LIGHTING); |
146 |
|
glLineWidth(1.0); |
147 |
|
glDisable (GL_DEPTH_TEST); |
148 |
|
} |
149 |
|
|
150 |
|
- (void) _displayCursorPosition |
151 |
|
{ |
152 |
|
NSParameterAssert (_map); |
153 |
|
NSParameterAssert (_camera); |
154 |
|
NSParameterAssert ([_camera aspect] == 1.0); |
155 |
|
|
156 |
|
NSRect sceneBounds = [self bounds]; |
157 |
|
double l = sceneBounds.size.width; |
158 |
|
double h = sceneBounds.size.height; |
159 |
|
printf ("l %lf h %lf\n" ,l,h); |
160 |
|
|
161 |
|
coord_t camEye = [_camera eye]; |
162 |
|
|
163 |
|
double height = tan(DEGTORAD ([_camera fovy] / 2.0)) * camEye.y; |
164 |
|
|
165 |
|
NSPoint downLeft, upRight; |
166 |
|
downLeft.x = camEye.z - height; |
167 |
|
downLeft.y = camEye.x - height; |
168 |
|
|
169 |
|
upRight.x = camEye.z + height; |
170 |
|
upRight.y = camEye.x + height; |
171 |
|
|
172 |
|
_mouseMap.x = (upRight.x - downLeft.x) |
173 |
|
* _mouseLoc.x / l + downLeft.x; // + 50.0; |
174 |
|
_mouseMap.y = (upRight.y - downLeft.y) |
175 |
|
* (_mouseLoc.y - 5.0) / (h - 130.0) + downLeft.y + 20.0 / _mouseLoc.y; |
176 |
|
// _mouseMap.x = sqrt(SQUARE(downLeft.x) + SQUARE(upRight.x)) |
177 |
|
// * _mouseLoc.x / l + downLeft.x + 50.0; |
178 |
|
// _mouseMap.y = sqrt(SQUARE(downLeft.y) + SQUARE(upRight.y)) |
179 |
|
// * _mouseLoc.y / h + downLeft.y + 90.0; |
180 |
|
|
181 |
|
printf("\n\n mouseLoc %lf %lf\n", _mouseLoc.x, _mouseLoc.y); |
182 |
|
printf ("_mouseMap %lf %lf\n" ,_mouseMap.x, _mouseMap.y); |
183 |
|
|
184 |
|
|
185 |
|
glDisable(GL_LIGHTING); |
186 |
|
glDisable (GL_DEPTH_TEST); |
187 |
|
glLineWidth(2.0); |
188 |
|
|
189 |
|
glBegin(GL_LINES); |
190 |
|
glColor3f(1.0,0.0,0.0); |
191 |
|
|
192 |
|
//horizontal line |
193 |
|
glVertex3f(_mouseMap.y, 0, downLeft.x); |
194 |
|
glVertex3f(_mouseMap.y, 0, upRight.x); |
195 |
|
|
196 |
|
//vertical line |
197 |
|
glColor3f(0.0,1.0,0.0); |
198 |
|
glVertex3f(downLeft.y, 0, _mouseMap.x); |
199 |
|
glVertex3f(upRight.y, 0, _mouseMap.x); |
200 |
|
|
201 |
|
glEnd(); |
202 |
|
|
203 |
|
glLineWidth(1.0); |
204 |
|
} |
205 |
|
|
206 |
|
- (map_coord_t) positionOnTerrain |
207 |
|
{ |
208 |
|
return MakeMapCoord (_mouseMap.y, _mouseMap.x); |
209 |
|
} |
210 |
|
|
211 |
- (void) reshape |
- (void) reshape |
212 |
{ |
{ |
213 |
NSRect sceneBounds; |
NSRect sceneBounds; |
247 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
248 |
glLoadIdentity(); |
glLoadIdentity(); |
249 |
|
|
250 |
|
glEnable(GL_LIGHTING); |
251 |
|
glEnable(GL_LIGHT0); |
252 |
|
if (!_enableTexture) |
253 |
|
{ |
254 |
|
//glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient); |
255 |
|
//glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse); |
256 |
|
} |
257 |
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); |
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); |
258 |
|
|
259 |
gluLookAt ((GLdouble)[_camera eye].x, |
gluLookAt ((GLdouble)[_camera eye].x, |
260 |
(GLdouble)[_camera eye].y, |
(GLdouble)[_camera eye].y, |
261 |
(GLdouble)[_camera eye].z, |
(GLdouble)[_camera eye].z, |
275 |
[_map displayWithCamera: _camera |
[_map displayWithCamera: _camera |
276 |
numberOfRay: _nbRay |
numberOfRay: _nbRay |
277 |
lastStep: _lastStep]; |
lastStep: _lastStep]; |
278 |
|
|
279 |
|
if (_displayBorder) |
280 |
|
[self _displayMapBorder]; |
281 |
|
|
282 |
|
if (_viewTop & _editingMap) |
283 |
|
[self _displayCursorPosition]; |
284 |
|
|
285 |
|
//NSPoint mouseLoc = [[self window] mouseLocationOutsideOfEventStream]; |
286 |
|
//NSPoint mouseLoc = [self mouseLocation]; |
287 |
|
//printf("\n\n mouseLoc %lf %lf\n", mouseLoc.x, mouseLoc.y); |
288 |
|
|
289 |
|
glDisable(GL_LIGHTING); |
290 |
|
glDisable(GL_LIGHT0); |
291 |
} |
} |
292 |
|
|
293 |
[self update]; |
[self update]; |
304 |
{ |
{ |
305 |
TEST_RELEASE (_map); |
TEST_RELEASE (_map); |
306 |
_map = TEST_RETAIN (map); |
_map = TEST_RETAIN (map); |
307 |
// printf("setTerrain\n"); |
} |
308 |
|
|
309 |
|
- (void) swapTextureColor |
310 |
|
{ |
311 |
|
//_enableTexture = [_map isTexturesEnable]; |
312 |
|
_enableTexture = !_enableTexture; |
313 |
} |
} |
314 |
|
|
315 |
- (void) displayTerrain: (BOOL)display |
- (void) displayTerrain: (BOOL)display |
316 |
{ |
{ |
317 |
|
_enableTexture = [_map isTexturesEnable]; |
318 |
_display = display; |
_display = display; |
319 |
} |
} |
320 |
|
|
325 |
glDisable (GL_FOG); |
glDisable (GL_FOG); |
326 |
else |
else |
327 |
glEnable (GL_FOG); |
glEnable (GL_FOG); |
328 |
|
} |
329 |
|
|
330 |
|
- (void) setEditMap: (BOOL)editingMap |
331 |
|
{ |
332 |
|
_editingMap = editingMap; |
333 |
} |
} |
334 |
|
|
335 |
// - (void) displayMapBorder |
- (void) displayMapBorder |
336 |
// { |
{ |
337 |
// repere=glGenLists(1); |
_displayBorder = !_displayBorder; |
338 |
// glNewList(repere,GL_COMPILE); |
} |
|
// glDisable(GL_LIGHTING); |
|
|
// glLineWidth(2.0); |
|
|
// glBegin(GL_LINES); |
|
|
// glColor3f(1.0,0.0,0.0); |
|
|
// glVertex3f(0.0,200.0,0.0); |
|
|
// glVertex3f(3.0,200.0,0.0); |
|
|
|
|
|
// glColor3f(0.0,1.0,0.0); |
|
|
// glVertex3f(0.0,200.0,0.0); |
|
|
// glVertex3f(0.0,203.0,0.0); |
|
|
|
|
|
// glColor3f(0.0,0.0,1.0); |
|
|
// glVertex3f(0.0,200.0,0.0); |
|
|
// glVertex3f(0.0,200.0,3.0); |
|
|
// glEnd(); |
|
|
// glEnable(GL_LIGHTING); |
|
|
// glLineWidth(1.0); |
|
|
// glEndList(); |
|
|
// } |
|
339 |
|
|
340 |
- (BOOL) performKeyEquivalent: (NSEvent *)theEvent |
- (BOOL) performKeyEquivalent: (NSEvent *)theEvent |
341 |
{ |
{ |
388 |
return YES; |
return YES; |
389 |
} |
} |
390 |
|
|
391 |
|
|
392 |
|
// - (void) mouseMoved: (NSEvent *)theEvent |
393 |
|
// { |
394 |
|
// NSPoint mouseLoc = [theEvent locationInWindow]; |
395 |
|
// printf("\n\n mouseLoc %lf %lf\n", mouseLoc.x, mouseLoc.y); |
396 |
|
// } |
397 |
|
|
398 |
|
- (void) mouseDown: (NSEvent *)theEvent |
399 |
|
{ |
400 |
|
_mouseLoc = [theEvent locationInWindow]; |
401 |
|
//printf("\n\n mouseLoc %lf %lf\n", _mouseLoc.x, _mouseLoc.y); |
402 |
|
//_displayCursor = YES; |
403 |
|
//printf("mouse NSLeftMouseDown\n"); |
404 |
|
} |
405 |
|
|
406 |
|
- (void)mouseDragged: (NSEvent *) theEvent |
407 |
|
{ |
408 |
|
_mouseLoc = [theEvent locationInWindow]; |
409 |
|
//printf("mouse NSLeftMouseDragged\n"); |
410 |
|
} |
411 |
|
|
412 |
|
- (void) mouseUp: (NSEvent *)theEvent |
413 |
|
{ |
414 |
|
//printf("mouse NSLeftMouseUp\n"); |
415 |
|
_mouseLoc = [theEvent locationInWindow]; |
416 |
|
//_displayCursor = NO; |
417 |
|
} |
418 |
|
|
419 |
|
// return; |
420 |
|
// } |
421 |
|
|
422 |
|
// - (void) mouseDown: (NSEvent *)theEvent |
423 |
|
// { |
424 |
|
// BOOL keepOn = YES; |
425 |
|
// BOOL isInside = YES; |
426 |
|
// //NSPoint mouseLoc; |
427 |
|
|
428 |
|
// NSRect sceneBounds = [self bounds]; |
429 |
|
// int l = sceneBounds.size.width; |
430 |
|
// int h = sceneBounds.size.height; |
431 |
|
|
432 |
|
// printf ("l %d h %d\n" ,l,h); |
433 |
|
|
434 |
|
// while (keepOn) { |
435 |
|
// //theEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask | |
436 |
|
// // NSLeftMouseDraggedMask]; |
437 |
|
// // mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; |
438 |
|
// NSPoint mouseLoc = [theEvent locationInWindow]; |
439 |
|
// printf("\n\n mouseLoc %lf %lf\n", mouseLoc.x, mouseLoc.y); |
440 |
|
|
441 |
|
// //NSLog (@" mouseLoc %@ \n", mouseLoc); |
442 |
|
// isInside = [self mouse:mouseLoc inRect:[self bounds]]; |
443 |
|
|
444 |
|
// switch ([theEvent type]) { |
445 |
|
// case NSLeftMouseDragged: |
446 |
|
// [self _displayCursorPosition: [theEvent locationInWindow]]; |
447 |
|
// // [self highlight:isInside]; |
448 |
|
// break; |
449 |
|
// case NSLeftMouseUp: |
450 |
|
// if (isInside) //[self doSomethingSignificant]; |
451 |
|
// printf("mouse\n"); |
452 |
|
// printf(" mouseLoc %lf %lf\n", mouseLoc.x, mouseLoc.y); |
453 |
|
// //[self highlight:NO]; |
454 |
|
// keepOn = NO; |
455 |
|
// break; |
456 |
|
// default: |
457 |
|
// /* Ignore any other kind of event. */ |
458 |
|
// break; |
459 |
|
// } |
460 |
|
|
461 |
|
// }; |
462 |
|
|
463 |
|
// return; |
464 |
|
|
465 |
|
// while (keepOn) |
466 |
|
// { |
467 |
|
// //theEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask | |
468 |
|
// // NSLeftMouseDraggedMask]; |
469 |
|
|
470 |
|
// //mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; |
471 |
|
// NSPoint mouseLoc = [theEvent locationInWindow]; |
472 |
|
// printf("\n\n mouseLoc %lf %lf\n", mouseLoc.x, mouseLoc.y); |
473 |
|
|
474 |
|
// //NSLog (@" mouseLoc %@ \n", mouseLoc); |
475 |
|
// isInside = [self mouse:mouseLoc inRect:[self bounds]]; |
476 |
|
|
477 |
|
// switch ([theEvent type]) |
478 |
|
// { |
479 |
|
// //case NSMouseMoved: |
480 |
|
// [self _displayCursorPosition: [theEvent locationInWindow]]; |
481 |
|
// break; |
482 |
|
// case NSLeftMouseUp: |
483 |
|
// if (isInside) //[self doSomethingSignificant]; |
484 |
|
// printf("mouse\n"); |
485 |
|
// printf(" mouseLoc %lf %lf\n", mouseLoc.x, mouseLoc.y); |
486 |
|
// keepOn = NO; |
487 |
|
// break; |
488 |
|
// default: |
489 |
|
// // Ignore any other kind of event. |
490 |
|
// break; |
491 |
|
// } |
492 |
|
// } |
493 |
|
// return; |
494 |
|
//} |
495 |
|
|
496 |
|
// - (void)mouseDown:(NSEvent *)theEvent |
497 |
|
// { |
498 |
|
// BOOL keepOn = YES; |
499 |
|
// BOOL isInside = YES; |
500 |
|
// NSPoint mouseLoc; |
501 |
|
|
502 |
|
// while (keepOn) { |
503 |
|
// theEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask | |
504 |
|
// NSLeftMouseDraggedMask]; |
505 |
|
// mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; |
506 |
|
// isInside = [self mouse:mouseLoc inRect:[self bounds]]; |
507 |
|
|
508 |
|
// switch ([theEvent type]) { |
509 |
|
// case NSLeftMouseDragged: |
510 |
|
// [self highlight:isInside]; |
511 |
|
// break; |
512 |
|
// case NSLeftMouseUp: |
513 |
|
// if (isInside) [self doSomethingSignificant]; |
514 |
|
// [self highlight:NO]; |
515 |
|
// keepOn = NO; |
516 |
|
// break; |
517 |
|
// default: |
518 |
|
// /* Ignore any other kind of event. */ |
519 |
|
// break; |
520 |
|
// } |
521 |
|
|
522 |
|
// }; |
523 |
|
|
524 |
|
// return; |
525 |
|
// } |
526 |
|
|
527 |
@end |
@end |