cleaned all trailing white space from source files.
[sdk] / ecere / src / gfx / drivers / CocoaOpenGLDisplayDriver.ec
1 #define USEPBUFFER
2
3 namespace gfx::drivers;
4
5 #include <OpenGl/gl.h>
6
7 import "CocoaInterface.h"
8
9 import "Display"
10 import "Window"
11
12 #define glLoadMatrix glLoadMatrixd
13 #define glMultMatrix glMultMatrixd
14 #define glGetMatrix  glGetDoublev
15 #define glTranslate glTranslated
16 #define glScale glScaled
17
18 #define GL_ARRAY_BUFFER_ARB            0x8892
19 #define GL_ELEMENT_ARRAY_BUFFER_ARB    0x8893
20 #define GL_STATIC_DRAW_ARB             0x88E4
21 #define GL_LIGHT_MODEL_COLOR_CONTROL   0x81F8
22 #define GL_SEPARATE_SPECULAR_COLOR     0x81FA
23
24 #define GL_MULTISAMPLE_ARB             0x809D
25
26 //static int displayWidth, displayHeight;
27
28 #define GL_CLAMP_TO_EDGE 0x812F
29
30 class DisplayData : struct
31 {
32    ColorAlpha * flippingBuffer;
33    int flipBufH, flipBufW;
34    bool depthWrite;
35    int x, y;
36    uint stride;
37    byte * picture;
38 };
39
40 class SystemData : struct
41 {
42    bool loadingFont;
43 };
44
45 class SurfaceData : struct
46 {
47    Font font;
48    bool opaqueText;
49    int  xOffset;
50    bool writingText;
51
52    float foreground[4], background[4], bitmapMult[4];
53 };
54
55 class MeshData : struct
56 {
57    int vertices;
58    int normals;
59    int texCoords;
60    int texCoords2;
61    int colors;
62 };
63
64 class IndexData : struct
65 {
66    uint16 *indices;
67    int buffer;
68    int nIndices;
69 };
70
71 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
72 static int primitiveTypes[RenderPrimitiveType] =
73 {
74    GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_LINE_STRIP
75 };
76 #endif
77
78 int current;
79 void *previous;
80
81 void CocoaGlAssert()
82 {
83     GLenum error = glGetError();
84
85     if(error) {
86         printf("**** glGetError():%i ****\n", error);
87         exit(1);
88     }
89 }
90
91 class CocoaOpenGLDisplayDriver : DisplayDriver
92 {
93    class_property(name) = "CocoaOpenGL";
94
95    bool LockSystem(DisplaySystem displaySystem)
96    {
97       printf("CocoaOpenGLDisplayDriver:LockSystem STUB! %s:%i\n", __FILE__, __LINE__);
98       return true;
99    }
100
101    void UnlockSystem(DisplaySystem displaySystem)
102    {
103       printf("CocoaOpenGLDisplayDriver:UnlockSystem STUB! %s:%i\n", __FILE__, __LINE__);
104    }
105
106    bool Lock(Display display)
107    {
108       printf("CocoaOpenGLDisplayDriver:Lock %s:%i\n", __FILE__, __LINE__);
109       return CocoaLock(display.window);
110    }
111
112    void Unlock(Display display)
113    {
114       printf("CocoaOpenGLDisplayDriver:Unlock %s:%i\n", __FILE__, __LINE__);
115       CocoaUnlock(display.window);
116    }
117
118    void DestroyDisplay(Display display)
119    {
120       printf("CocoaOpenGLDisplayDriver:DestroyDisplay STUB! %s:%i\n", __FILE__, __LINE__);
121    }
122
123    bool CreateDisplaySystem(DisplaySystem displaySystem)
124    {
125       bool result = true;
126
127       SystemData system = SystemData { };
128       displaySystem.driverData = system;
129
130       printf("CocoaOpenGLDisplayDriver:CreateDisplaySystem %s:%i\n", __FILE__, __LINE__);
131       return result;
132    }
133
134    void DestroyDisplaySystem(DisplaySystem displaySystem)
135    {
136       printf("CocoaOpenGLDisplayDriver:DestroyDisplaySystem STUB! %s:%i\n", __FILE__, __LINE__);
137    }
138
139    bool CreateDisplay(Display display)
140    {
141       bool result = true;
142
143       DisplayData displayData = display.driverData;
144       SystemData systemData = display.displaySystem.driverData;
145
146       displayData = display.driverData = DisplayData { };
147
148       CocoaOpenGLMakeCurrentContext(display.window);
149
150       CocoaGlAssert();
151       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
152       CocoaGlAssert();
153       glEnable(GL_BLEND);
154       CocoaGlAssert();
155
156       glMatrixMode(GL_MODELVIEW);
157       CocoaGlAssert();
158       glScalef(1.0f, 1.0f, -1.0f);
159       CocoaGlAssert();
160       glMatrixMode(GL_PROJECTION);
161       CocoaGlAssert();
162       glShadeModel(GL_FLAT);
163       CocoaGlAssert();
164
165       glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
166       CocoaGlAssert();
167       glFogi(GL_FOG_MODE, GL_EXP);
168       CocoaGlAssert();
169       glFogf(GL_FOG_DENSITY, 0);
170       CocoaGlAssert();
171       glEnable(GL_NORMALIZE);
172       CocoaGlAssert();
173       glDepthFunc(GL_LESS);
174       CocoaGlAssert();
175       glClearDepth(1.0);
176       CocoaGlAssert();
177       glDisable(GL_MULTISAMPLE_ARB);
178       CocoaGlAssert();
179
180 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
181       display.ambient = Color { 50,50,50 };
182 #endif
183
184       printf("CocoaOpenGLDisplayDriver:CreateDisplay %p %s:%i\n", display.window, __FILE__, __LINE__);
185       return result;
186    }
187
188    bool DisplaySize(Display display, int width, int height)
189    {
190       bool result = true;
191
192       DisplayData displayData = display.driverData;
193       SystemData systemData = display.displaySystem.driverData;
194
195       printf("CocoaOpenGLDisplayDriver:DisplaySize(%i,%i) %s:%i\n", width, height, __FILE__, __LINE__);
196
197       CocoaOpenGLMakeCurrentContext(display.window);
198
199       glViewport(0,0,width,height);
200       CocoaGlAssert();
201       glLoadIdentity();
202       CocoaGlAssert();
203       glOrtho(0,width,height,0,0.0,1.0);
204       CocoaGlAssert();
205       display.width = width;
206       display.height = height;
207
208       if(!displayData.flippingBuffer || displayData.flipBufW < width || displayData.flipBufH < height)
209       {
210          displayData.flipBufW = width;
211          displayData.flipBufH = height;
212          displayData.flippingBuffer = renew displayData.flippingBuffer ColorAlpha [width * height];
213       }
214       if(displayData.flippingBuffer)
215          result = true;
216
217       return result;
218    }
219
220    void DisplayPosition(Display display, int x, int y)
221    {
222       DisplayData displayData = display.driverData;
223
224       printf("CocoaOpenGLDisplayDriver:DisplayPosition(%i,%i) %s:%i\n", x, y, __FILE__, __LINE__);
225
226       displayData.x = x;
227       displayData.y = y;
228       printf("glGetError():%i\n", glGetError());
229    }
230
231    void SetPalette(Display display, ColorAlpha * palette, bool colorMatch)
232    {
233       printf("CocoaOpenGLDisplayDriver:SetPalette STUB! %s:%i\n", __FILE__, __LINE__);
234    }
235
236    void RestorePalette(Display display)
237    {
238       printf("CocoaOpenGLDisplayDriver:RestorePalette STUB! %s:%i\n", __FILE__, __LINE__);
239    }
240
241    void StartUpdate(Display display)
242    {
243       printf("CocoaOpenGLDisplayDriver:StartUpdate STUB! %s:%i\n", __FILE__, __LINE__);
244    }
245
246    void EndUpdate(Display display)
247    {
248       printf("CocoaOpenGLDisplayDriver:EndUpdate STUB! %s:%i\n", __FILE__, __LINE__);
249    }
250
251    void Scroll(Display display, Box scroll, int x, int y, Extent dirty)
252    {
253       printf("CocoaOpenGLDisplayDriver:Scroll STUB! %s:%i\n", __FILE__, __LINE__);
254    }
255
256    void Update(Display display, Box updateBox)
257    {
258       //CocoaOpenGLUpdate(display.window);
259       printf("CocoaOpenGLDisplayDriver:Update %s:%i\n", __FILE__, __LINE__);
260    }
261
262    void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
263    {
264       glDeleteTextures(1, (int *)&bitmap.driverData);
265       bitmap.driverData = 0;
266
267       bitmap.driver = class(LFBDisplayDriver);
268
269       printf("CocoaOpenGLDisplayDriver:FreeBitmap %s:%i\n", __FILE__, __LINE__);
270    }
271
272    bool AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
273    {
274       bool result = false;
275       Bitmap mipMap { };
276       int glBitmap = -1;
277
278       uint w = pow2i(Min(width, 1024)), h = pow2i(Min(height, 1024));
279
280       printf("CocoaOpenGLDisplayDriver:AllocateBitmap %s:%i\n", __FILE__, __LINE__);
281
282       CocoaGlAssert();
283       glGenTextures(1, &glBitmap);
284       CocoaGlAssert();
285       glBindTexture(GL_TEXTURE_2D, glBitmap);
286
287       CocoaGlAssert();
288       glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
289
290       CocoaGlAssert();
291       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
292       CocoaGlAssert();
293       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
294
295       CocoaGlAssert();
296       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
297       CocoaGlAssert();
298       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
299
300       CocoaGlAssert();
301       glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
302
303       mipMap.Allocate(null, w, h, w, pixelFormatRGBA, false);
304
305       CocoaGlAssert();
306       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
307       delete mipMap;
308
309       bitmap.driverData = (void *)glBitmap;
310       bitmap.driver = displaySystem.driver;
311       bitmap.width = w;
312       bitmap.height = h;
313
314       result = true;
315
316       return result;
317    }
318
319    bool MakeDDBitmap(DisplaySystem displaySystem, Bitmap bitmap, bool mipMaps)
320    {
321       bool result = false;
322       SystemData systemData = displaySystem.driverData;
323
324       // Pre process the bitmap... First make it 32 bit
325       if(bitmap.Convert(null, pixelFormat888, null))
326       {
327          int c, level;
328          uint w = pow2i(Min(bitmap.width, 1024)), h = pow2i(Min(bitmap.height, 1024));
329          int glBitmap = -1;
330
331          // Switch ARGB to RGBA
332          //if(bitmap.format != pixelFormatRGBA)
333          {
334             for(c=0; c<bitmap.size; c++)
335             {
336                // TODO:
337                ColorAlpha color = ((ColorAlpha *)bitmap.picture)[c];
338                ((ColorRGBA *)bitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
339             }
340          }
341          bitmap.pixelFormat = pixelFormat888;
342
343       CocoaGlAssert();
344          glGenTextures(1, &glBitmap);
345       CocoaGlAssert();
346          if(glBitmap == -1)
347          {
348             int error = glGetError();
349             return false;
350             //Print("");
351          }
352
353       CocoaGlAssert();
354          glBindTexture(GL_TEXTURE_2D, glBitmap);
355       CocoaGlAssert();
356          glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
357       CocoaGlAssert();
358
359          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
360       CocoaGlAssert();
361          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
362       CocoaGlAssert();
363
364          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
365       CocoaGlAssert();
366          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
367       CocoaGlAssert();
368
369          glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
370       CocoaGlAssert();
371
372          result = true;
373
374          for(level = 0; result && (w > 1 || h > 1); level++, w >>= 1, h >>= 1)
375          {
376             Bitmap mipMap;
377             if(bitmap.width != w || bitmap.height != h)
378             {
379                mipMap = Bitmap { };
380                if(mipMap.Allocate(null, w, h, w, bitmap.pixelFormat, false))
381                {
382                   Surface mipSurface = mipMap.GetSurface(0,0,null);
383                   mipSurface.Filter(bitmap, 0,0,0,0, w, h, bitmap.width, bitmap.height);
384                   delete mipSurface;
385                }
386                else
387                {
388                   result = false;
389                   delete mipMap;
390                }
391             }
392             else
393                mipMap = bitmap;
394
395             if(result)
396             {
397                int error;
398                glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
399       CocoaGlAssert();
400                if((error = glGetError()))
401                {
402                   result = false;
403                }
404             }
405             if(mipMap != bitmap)
406                delete mipMap;
407             if(!mipMaps) break;
408          }
409
410          if(!bitmap.keepData)
411             bitmap.driver.FreeBitmap(bitmap.displaySystem, bitmap);
412          bitmap.driverData = (void *)glBitmap;
413          bitmap.driver = displaySystem.driver;
414
415          if(!result)
416             FreeBitmap(displaySystem, bitmap);
417          else if(systemData.loadingFont)
418          {
419             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
420       CocoaGlAssert();
421             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
422       CocoaGlAssert();
423             systemData.loadingFont = false;
424          }
425       }
426
427       printf("CocoaOpenGLDisplayDriver:MakeDDBitmap %i %s:%i\n", result, __FILE__, __LINE__);
428       return result;
429    }
430
431    void ReleaseSurface(Display display, Surface surface)
432    {
433       printf("CocoaOpenGLDisplayDriver:ReleaseSurface %s:%i\n", __FILE__, __LINE__);
434
435       glDisable(GL_SCISSOR_TEST);
436       CocoaGlAssert();
437       delete surface.driverData;
438       surface.driverData = null;
439    }
440
441    bool GetBitmapSurface(DisplaySystem displaySystem, Surface surface, Bitmap bitmap, int x, int y, Box clip)
442    {
443       printf("CocoaOpenGLDisplayDriver:GetBitmapSurface STUB! %s:%i\n", __FILE__, __LINE__);
444       return false;
445    }
446
447    bool GetSurface(Display display, Surface surface, int x,int y, Box clip)
448    {
449       bool result = true;
450
451       SurfaceData surfaceData = SurfaceData { };
452       surface.driverData = surfaceData;
453
454       printf("CocoaOpenGLDisplayDriver:GetSurface %p %s:%i\n", surfaceData, __FILE__, __LINE__);
455
456       if(surfaceData)
457       {
458       CocoaGlAssert();
459             glViewport(0,0,display.width,display.height);
460       CocoaGlAssert();
461             glLoadIdentity();
462       CocoaGlAssert();
463             printf("display:%i, %i\n", display.width, display.height);
464             glOrtho(0, display.width, display.height, 0, 0.0f, 1.0f);
465       CocoaGlAssert();
466
467          surface.offset.x = x;
468          surface.offset.y = y;
469          surface.unclippedBox = surface.box = clip;
470          surfaceData.bitmapMult[0] = 1;
471          surfaceData.bitmapMult[1] = 1;
472          surfaceData.bitmapMult[2] = 1;
473          surfaceData.bitmapMult[3] = 1;
474
475          glEnable(GL_SCISSOR_TEST);
476       CocoaGlAssert();
477          glScissor(
478             x+clip.left,
479             (display.height) -(y+clip.bottom)-1,
480             clip.right-clip.left+1,
481             clip.bottom-clip.top+1);
482       CocoaGlAssert();
483          result = true;
484       }
485
486       return result;
487    }
488
489    void Clip(Display display, Surface surface, Box clip)
490    {
491       Box box;
492
493       printf("CocoaOpenGLDisplayDriver:Clip STUB! %s:%i\n", __FILE__, __LINE__);
494
495       if(clip != null)
496       {
497          box = clip;
498          box.Clip(surface.unclippedBox);
499          surface.box = box;
500       }
501       else
502       {
503          box = surface.box = surface.unclippedBox;
504       }
505
506       box.left += surface.offset.x;
507       box.top  += surface.offset.y;
508       box.right+= surface.offset.x;
509       box.bottom += surface.offset.y;
510
511       glScissor(
512          box.left,display.height - box.bottom - 1,
513          box.right-box.left+1, box.bottom-box.top+1);
514    }
515
516    bool GrabScreen(Display display, Bitmap bitmap, int x, int y, unsigned int w, unsigned int h)
517    {
518       bool result = false;
519       printf("CocoaOpenGLDisplayDriver:GrabScreen STUB! %s:%i\n", __FILE__, __LINE__);
520       return result;
521    }
522
523    void SetForeground(Display display, Surface surface, ColorAlpha color)
524    {
525       SurfaceData surfaceData = surface.driverData;
526
527       printf("CocoaOpenGLDisplayDriver:SetForeground(%i,%i,%i,%i) %s:%i\n", color.color.r, color.color.g, color.color.b, color.a, __FILE__, __LINE__);
528
529       surfaceData.foreground[0] = color.color.r/255.0f;
530       surfaceData.foreground[1] = color.color.g/255.0f;
531       surfaceData.foreground[2] = color.color.b/255.0f;
532       surfaceData.foreground[3] = color.a/255.0f;
533    }
534
535    void SetBackground(Display display, Surface surface, ColorAlpha color)
536    {
537       SurfaceData surfaceData = surface.driverData;
538
539       printf("CocoaOpenGLDisplayDriver:SetBackground(%i,%i,%i,%i) %s:%i\n", color.color.r, color.color.g, color.color.b, color.a, __FILE__, __LINE__);
540
541       surfaceData.background[0] = color.color.r/255.0f;
542       surfaceData.background[1] = color.color.g/255.0f;
543       surfaceData.background[2] = color.color.b/255.0f;
544       surfaceData.background[3] = color.a/255.0f;
545    }
546
547    void SetBlitTint(Display display, Surface surface, ColorAlpha color)
548    {
549       SurfaceData surfaceData = surface.driverData;
550
551       printf("CocoaOpenGLDisplayDriver:SetBlitTint(%i,%i,%i,%i) %s:%i\n", color.color.r, color.color.g, color.color.b, color.a, __FILE__, __LINE__);
552
553       surfaceData.bitmapMult[0] = color.color.r/255.0f;
554       surfaceData.bitmapMult[1] = color.color.g/255.0f;
555       surfaceData.bitmapMult[2] = color.color.b/255.0f;
556       surfaceData.bitmapMult[3] = color.a/255.0f;
557    }
558
559
560    ColorAlpha GetPixel(Display display, Surface surface,int x,int y)
561    {
562       printf("CocoaOpenGLDisplayDriver:GetPixel STUB! %s:%i\n", __FILE__, __LINE__);
563       return 0;
564    }
565
566    void PutPixel(Display display, Surface surface,int x,int y)
567    {
568       printf("CocoaOpenGLDisplayDriver:PutPixel %s:%i\n", __FILE__, __LINE__);
569
570       glBegin(GL_POINTS);
571       CocoaGlAssert();
572       glVertex2f(x+surface.offset.x + 0.5f, y+surface.offset.y + 0.5f);
573       glEnd();
574       CocoaGlAssert();
575    }
576
577    void DrawLine(Display display, Surface surface, int x1, int y1, int x2, int y2)
578    {
579       SurfaceData surfaceData = surface.driverData;
580
581       printf("CocoaOpenGLDisplayDriver:DrawLine %s:%i\n", __FILE__, __LINE__);
582       printf("--DrawLine(x1:%i, y1:%i, x2:%i, y2:%i)\n", x1, y1, x2, y2);
583
584       glColor4fv(surfaceData.foreground);
585       glBegin(GL_LINES);
586
587       glVertex2f(x1+surface.offset.x + 0.5f, y1+surface.offset.y + 0.5f);
588       glVertex2f(x2+surface.offset.x + 0.5f, y2+surface.offset.y + 0.5f);
589
590       glEnd();
591       CocoaGlAssert();
592    }
593
594    void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
595    {
596       printf("CocoaOpenGLDisplayDriver:Rectangle %s:%i\n", __FILE__, __LINE__);
597       printf("--Rectangle(x1:%i, y1:%i, x2:%i, y2:%i)\n", x1, y1, x2, y2);
598       glBegin(GL_LINE_LOOP);
599
600       glVertex2f(x1+surface.offset.x + 0.5f, y1+surface.offset.y + 0.5f);
601       glVertex2f(x1+surface.offset.x + 0.5f, y2+surface.offset.y + 0.5f);
602       glVertex2f(x2+surface.offset.x + 0.5f, y2+surface.offset.y + 0.5f);
603       glVertex2f(x2+surface.offset.x + 0.5f, y1+surface.offset.y + 0.5f);
604
605       glEnd();
606       CocoaGlAssert();
607    }
608
609    void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
610    {
611       SurfaceData surfaceData = surface.driverData;
612
613       printf("CocoaOpenGLDisplayDriver:Area %s:%i\n", __FILE__, __LINE__);
614
615       CocoaGlAssert();
616       glColor4fv(surfaceData.background);
617
618       glRecti(x1+surface.offset.x, y1+surface.offset.y,
619               x2+surface.offset.x + 1, y2+surface.offset.y + 1);
620       CocoaGlAssert();
621    }
622
623    void Clear(Display display, Surface surface, ClearType type)
624    {
625       DisplayData displayData = display.driverData;
626       SurfaceData surfaceData = surface.driverData;
627
628       if(type != depthBuffer)
629       {
630          glClearColor(surfaceData.background[0], surfaceData.background[1], surfaceData.background[2], surfaceData.background[3]);
631       CocoaGlAssert();
632       }
633       if(type != colorBuffer && !displayData.depthWrite)
634       {
635          glDepthMask((byte)bool::true);
636       CocoaGlAssert();
637       }
638       glClear(((type != depthBuffer) ? GL_COLOR_BUFFER_BIT : 0) |
639               ((type != colorBuffer) ? GL_DEPTH_BUFFER_BIT : 0));
640       CocoaGlAssert();
641
642       if(type != colorBuffer && !displayData.depthWrite)
643       {
644          glDepthMask((byte)bool::false);
645       CocoaGlAssert();
646       }
647       if(type != depthBuffer)
648       {
649          glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
650       CocoaGlAssert();
651       }
652       if(type != colorBuffer)
653       {
654          glDepthMask((byte)bool::true);
655       CocoaGlAssert();
656       }
657
658       glClear(((type != depthBuffer) ? GL_COLOR_BUFFER_BIT : 0) |
659               ((type != colorBuffer) ? GL_DEPTH_BUFFER_BIT : 0));
660       CocoaGlAssert();
661
662       if(type != colorBuffer)
663       {
664          glDepthMask((byte)bool::false);
665       CocoaGlAssert();
666       }
667       printf("CocoaOpenGLDisplayDriver:Clear %s:%i\n", __FILE__, __LINE__);
668    }
669
670    bool ConvertBitmap(DisplaySystem displaySystem, Bitmap bitmap, PixelFormat format, ColorAlpha * palette)
671    {
672       bool result = false;
673
674       printf("CocoaOpenGLDisplayDriver:ConvertBitmap %s:%i\n", __FILE__, __LINE__);
675
676       return result;
677    }
678
679    void Blit(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
680    {
681       printf("CocoaOpenGLDisplayDriver:Blit %s:%i\n", __FILE__, __LINE__);
682       printf("--BLIT(dx:%i, dy:%i, sx:%i, sy:%i, w:%i, h:%i)\n", dx, dy, sx, sy, w, h);
683
684       glEnable(GL_TEXTURE_2D);
685       glBindTexture(GL_TEXTURE_2D, (uint)bitmap.driverData);
686       glBegin(GL_QUADS);
687
688       if(h < 0)
689       {
690          glTexCoord2f((float)sx/ bitmap.width, (float)(sy-h)/ bitmap.height);
691          glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
692          glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy-h)/ bitmap.height);
693          glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
694          glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
695          glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
696          glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
697          glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
698       }
699       else
700       {
701          glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
702          glVertex2f((float)dx+surface.offset.x, (float)dy+surface.offset.y);
703          glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
704          glVertex2f((float)dx+w+surface.offset.x, (float)dy+surface.offset.y);
705          glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
706          glVertex2f((float)dx+w+surface.offset.x, (float)dy+h+surface.offset.y);
707          glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
708          glVertex2f((float)dx+surface.offset.x, (float)dy+h+surface.offset.y);
709       }
710       glEnd();
711       CocoaGlAssert();
712    }
713
714    void Stretch(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
715    {
716       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
717       glEnable(GL_TEXTURE_2D);
718       CocoaGlAssert();
719       glBindTexture(GL_TEXTURE_2D, (uint)bitmap.driverData);
720       CocoaGlAssert();
721
722       glBegin(GL_QUADS);
723       CocoaGlAssert();
724
725       if(h < 0)
726       {
727          glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
728       CocoaGlAssert();
729          glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
730       CocoaGlAssert();
731
732          glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
733       CocoaGlAssert();
734          glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
735       CocoaGlAssert();
736
737          glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
738       CocoaGlAssert();
739          glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
740       CocoaGlAssert();
741
742          glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
743       CocoaGlAssert();
744          glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
745       CocoaGlAssert();
746       }
747       else
748       {
749          glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
750       CocoaGlAssert();
751          glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
752       CocoaGlAssert();
753
754          glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
755       CocoaGlAssert();
756          glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
757       CocoaGlAssert();
758
759          glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
760       CocoaGlAssert();
761          glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
762       CocoaGlAssert();
763
764          glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
765       CocoaGlAssert();
766          glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
767       CocoaGlAssert();
768       }
769
770       glEnd();
771       CocoaGlAssert();
772
773       glDisable(GL_TEXTURE_2D);
774       CocoaGlAssert();
775    }
776
777    void Filter(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
778    {
779       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
780       Stretch(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
781    }
782
783    void StretchDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
784    {
785       float s2dw,s2dh,d2sw,d2sh;
786       bool flipX = false, flipY = false;
787
788       printf("CocoaOpenGLDisplayDriver: %s:%i\n", __FILE__, __LINE__);
789       if(Sgn(w) != Sgn(sw))
790       {
791          w = Abs(w);
792          sw = Abs(sw);
793          flipX = true;
794       }
795       if(Sgn(h) != Sgn(sh))
796       {
797          h = Abs(h);
798          sh = Abs(sh);
799          flipY = true;
800       }
801
802       s2dw=(float)w / sw;
803       s2dh=(float)h / sh;
804       d2sw=(float)sw / w;
805       d2sh=(float)sh / h;
806
807       //Clip against the edges of the source
808       if(sx<0)
809       {
810          dx+=(int)((0-sx) * s2dw);
811          w-=(int)((0-sx) * s2dw);
812          sw-=0-sx;
813          sx=0;
814       }
815       if(sy<0)
816       {
817          dy+=(int)((0-sy) * s2dh);
818          h-=(int)((0-sy) * s2dh);
819
820          sh-=0-sy;
821          sy=0;
822       }
823       if(sx+sw>bitmap.width-1)
824       {
825          w-=(int)((sx+sw-(bitmap.width-1)-1)*s2dw);
826          sw-=sx+sw-(bitmap.width-1)-1;
827       }
828       if(sy+sh>(bitmap.height-1))
829       {
830          h-=(int)((sy+sh-(bitmap.height-1)-1)*s2dh);
831          sh-=sy+sh-(bitmap.height-1)-1;
832       }
833       //Clip against the edges of the surfaceination
834       if(dx<surface.box.left)
835       {
836          //if(!flip)
837          sx+=(int)((surface.box.left-dx)*d2sw);
838          sw-=(int)((surface.box.left-dx)*d2sw);
839          w-=surface.box.left-dx;
840          dx=surface.box.left;
841       }
842       if(dy<surface.box.top)
843       {
844          sy+=(int)((surface.box.top-dy)*d2sh);
845          sh-=(int)((surface.box.top-dy)*d2sh);
846          h-=surface.box.top-dy;
847          dy=surface.box.top;
848       }
849       if(dx+w>surface.box.right)
850       {
851          //if(flip) sx+=(int)((dx+w-surface.box.right-1)*d2sw);
852          sw-=(int)((dx+w-surface.box.right-1)*d2sw);
853          w-=dx+w-surface.box.right-1;
854       }
855       if(dy+h>surface.box.bottom)
856       {
857          sh-=(int)((dy+h-surface.box.bottom-1)*d2sh);
858          h-=dy+h-surface.box.bottom-1;
859       }
860       if((w<=0)||(h<=0)||(sw<=0)||(sh<=0)) return;
861
862       dx += surface.offset.x;
863       dy += surface.offset.y;
864
865       if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
866       {
867          glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
868       CocoaGlAssert();
869          glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
870       CocoaGlAssert();
871          glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
872       CocoaGlAssert();
873          glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
874       CocoaGlAssert();
875          glRasterPos2d(dx,dy);
876       CocoaGlAssert();
877          //glPixelZoom(flipX ? -s2dw : s2dw, flipY ? s2dh : -s2dh);
878       CocoaGlAssert();
879          glPixelZoom(s2dw, -s2dh);
880       CocoaGlAssert();
881          glDrawPixels(sw,sh,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
882       CocoaGlAssert();
883          glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
884       CocoaGlAssert();
885          glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
886       CocoaGlAssert();
887       }
888    }
889
890    void BlitDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
891    {
892       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
893       //Clip against the edges of the source
894       if(sx<0)
895       {
896          dx+=-sx;
897          w-=-sx;
898          sx=0;
899       }
900       if(sy<0)
901       {
902          dy+=0-sy;
903          h-=0-sy;
904          sy=0;
905       }
906       if(sx+w>bitmap.width-1)
907          w-=sx+w-(bitmap.width-1)-1;
908       if(sy+h>bitmap.height-1)
909          h-=sy+h-(bitmap.height-1)-1;
910       //Clip against the edges of the surfaceination
911       if(dx<surface.box.left)
912       {
913          //if(!flip)
914          sx+=surface.box.left-dx;
915          w-=surface.box.left-dx;
916          dx=surface.box.left;
917       }
918       if(dy<surface.box.top)
919       {
920          sy+=surface.box.top-dy;
921          h-=surface.box.top-dy;
922          dy=surface.box.top;
923       }
924       if(dx+w>surface.box.right)
925       {
926          //if(flip) sx+=dx+w-surface.box.right-1;
927          w-=dx+w-surface.box.right-1;
928       }
929       if(dy+h>surface.box.bottom)
930          h-=dy+h-surface.box.bottom-1;
931       if((w<=0)||(h<=0))
932          return;
933
934       dx += surface.offset.x;
935       dy += surface.offset.y;
936
937       if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
938       {
939          glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
940       CocoaGlAssert();
941          glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
942       CocoaGlAssert();
943          glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
944       CocoaGlAssert();
945          glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
946       CocoaGlAssert();
947          glRasterPos2d(dx,dy);
948       CocoaGlAssert();
949          glPixelZoom(1,-1);
950       CocoaGlAssert();
951          glDrawPixels(w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
952       CocoaGlAssert();
953          glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
954       CocoaGlAssert();
955          glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
956       CocoaGlAssert();
957       }
958    }
959
960    void FilterDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
961    {
962       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
963       StretchDI(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
964    }
965
966    void UnloadFont(DisplaySystem displaySystem, Font font)
967    {
968       printf("CocoaOpenGLDisplayDriver:UnloadFont %s:%i\n", __FILE__, __LINE__);
969       LFBDisplayDriver::UnloadFont(displaySystem, font);
970    }
971
972    Font LoadFont(DisplaySystem displaySystem, char * faceName, float size, FontFlags flags)
973    {
974       Font font = LFBDisplayDriver::LoadFont(displaySystem, faceName, size, flags);
975
976       printf("CocoaOpenGLDisplayDriver:LoadFont(%s):%p %s:%i\n", faceName, font, __FILE__, __LINE__);
977
978       return font;
979    }
980
981    void FontExtent(DisplaySystem displaySystem, Font font, char * text, int len, int * width, int * height)
982    {
983       printf("CocoaOpenGLDisplayDriver:FontExtent() %s:%i\n", __FILE__, __LINE__);
984       LFBDisplayDriver::FontExtent(displaySystem, font, text, len, width, height);
985    }
986
987    void WriteText(Display display, Surface surface, int x, int y, char * text, int len)
988    {
989       SurfaceData surfaceData = surface.driverData;
990       SystemData systemData = display.displaySystem.driverData;
991
992       printf("CocoaOpenGLDisplayDriver:WriteText %s:%i\n", __FILE__, __LINE__);
993
994       systemData.loadingFont = true;
995
996       if(surface.textOpacity)
997       {
998          int w, h;
999          FontExtent(display.displaySystem, surface.font, text, len, &w, &h);
1000          Area(display, surface,x,y,x+w-1,y+h-1);
1001       }
1002
1003       surfaceData.writingText = true;
1004
1005       glEnable(GL_TEXTURE_2D);
1006       CocoaGlAssert();
1007       glColor4fv(surfaceData.foreground);
1008       CocoaGlAssert();
1009
1010       LFBDisplayDriver::WriteText(display, surface, x, y, text, len);
1011       surfaceData.writingText = false;
1012       systemData.loadingFont = false;
1013
1014       glDisable(GL_TEXTURE_2D);
1015       CocoaGlAssert();
1016    }
1017
1018    void TextFont(Display display, Surface surface, Font font)
1019    {
1020       LFBDisplayDriver::TextFont(display, surface, font);
1021       printf("CocoaOpenGLDisplayDriver:TextFont %s:%i\n", __FILE__, __LINE__);
1022    }
1023
1024    void TextOpacity(Display display, Surface surface, bool opaque)
1025    {
1026       SurfaceData surfaceData = surface.driverData;
1027       surfaceData.opaqueText = opaque;
1028       printf("CocoaOpenGLDisplayDriver:TextOpacity(%i) %s:%i\n", opaque, __FILE__, __LINE__);
1029    }
1030
1031    void TextExtent(Display display, Surface surface, char * text, int len, int * width, int * height)
1032    {
1033       SurfaceData surfaceData = surface.driverData;
1034       SystemData systemData = display.displaySystem.driverData;
1035       systemData.loadingFont = true;
1036       FontExtent(display.displaySystem, surfaceData.font, text, len, width, height);
1037       systemData.loadingFont = false;
1038
1039       printf("CocoaOpenGLDisplayDriver:TextExtent STUB! %s:%i\n", __FILE__, __LINE__);
1040    }
1041
1042    void DrawingChar(Display display, Surface surface, char character)
1043    {
1044       printf("CocoaOpenGLDisplayDriver:DrawingChar STUB! %s:%i\n", __FILE__, __LINE__);
1045
1046    }
1047
1048    void LineStipple(Display display, Surface surface, uint32 stipple)
1049    {
1050       printf("CocoaOpenGLDisplayDriver:LineStipple %s:%i\n", __FILE__, __LINE__);
1051       if(stipple)
1052       {
1053          glLineStipple(1, (uint16)stipple);
1054       CocoaGlAssert();
1055          glEnable(GL_LINE_STIPPLE);
1056       CocoaGlAssert();
1057       }
1058       else
1059       {
1060          glDisable(GL_LINE_STIPPLE);
1061       CocoaGlAssert();
1062       }
1063       printf("glGetError():%i\n", glGetError());
1064    }
1065
1066 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1067    void SetRenderState(Display display, RenderState state, uint value)
1068    {
1069       printf("CocoaOpenGLDisplayDriver:SetRenderState STUB! %s:%i\n", __FILE__, __LINE__);
1070       switch(state)
1071       {
1072          case antiAlias:
1073             if(value)
1074                glEnable(GL_MULTISAMPLE_ARB);
1075             else
1076                glDisable(GL_MULTISAMPLE_ARB);
1077       CocoaGlAssert();
1078             break;
1079          case fillMode:
1080             glPolygonMode(GL_FRONT_AND_BACK, ((FillModeValue)value == solid) ? GL_FILL : GL_LINE);
1081       CocoaGlAssert();
1082             break;
1083          case depthTest:
1084             if(value) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
1085       CocoaGlAssert();
1086             break;
1087          case depthWrite:
1088             if(value) glDepthMask((byte)bool::true); else glDepthMask((byte)bool::false);
1089       CocoaGlAssert();
1090             break;
1091          case fogColor:
1092          {
1093             float color[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
1094             glFogfv(GL_FOG_COLOR, (float *)&color);
1095       CocoaGlAssert();
1096             break;
1097          }
1098          case fogDensity:
1099             glFogf(GL_FOG_DENSITY, *(float *)(void *)&value);
1100       CocoaGlAssert();
1101             break;
1102          case blend:
1103             if(value) glEnable(GL_BLEND); else glDisable(GL_BLEND);
1104       CocoaGlAssert();
1105             break;
1106          case ambient:
1107          {
1108             float ambient[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
1109             glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
1110       CocoaGlAssert();
1111             break;
1112          }
1113          case alphaWrite:
1114          {
1115             if(value) glColorMask(1,1,1,1); else glColorMask(1,1,1,0);
1116       CocoaGlAssert();
1117             break;
1118          }
1119          case vSync:
1120          {
1121             break;
1122          }
1123       }
1124    }
1125
1126    void SetLight(Display display, int id, Light light)
1127    {
1128       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
1129       if(light != null)
1130       {
1131          Object lightObject = light.lightObject;
1132          float position[4] = { 0, 0, 0, 0 };
1133          float color[4] = { 0, 0, 0, 1 };
1134
1135          glEnable(GL_LIGHT0 + id);
1136       CocoaGlAssert();
1137
1138          if(!light.multiplier) light.multiplier = 1.0f;
1139
1140          color[0] = light.diffuse.r * light.multiplier;
1141          color[1] = light.diffuse.g * light.multiplier;
1142          color[2] = light.diffuse.b * light.multiplier;
1143          glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, color);
1144
1145          color[0] = light.ambient.r * light.multiplier;
1146          color[1] = light.ambient.g * light.multiplier;
1147          color[2] = light.ambient.b * light.multiplier;
1148          glLightfv(GL_LIGHT0 + id, GL_AMBIENT, color);
1149          color[0] = light.specular.r * light.multiplier;
1150          color[1] = light.specular.g * light.multiplier;
1151          color[2] = light.specular.b * light.multiplier;
1152          glLightfv(GL_LIGHT0 + id, GL_SPECULAR,color);
1153
1154          if(lightObject)
1155          {
1156             Vector3D positionVector;
1157             if(light.flags.spot)
1158             {
1159                if(lightObject.flags.root || !lightObject.parent)
1160                {
1161                   positionVector = lightObject.transform.position;
1162                   positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
1163                }
1164                else
1165                {
1166                   positionVector.MultMatrix(lightObject.transform.position, lightObject.parent.matrix);
1167                   if(display.display3D.camera)
1168                      positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
1169                }
1170                position[3] = 1;
1171             }
1172             else
1173             {
1174                if(!light.direction.x && !light.direction.y && !light.direction.z)
1175                {
1176                   Vector3Df vector { 0,0,-1 };
1177                   Matrix mat;
1178                   mat.RotationQuaternion(light.orientation);
1179                   positionVector.MultMatrixf(vector, mat);
1180                }
1181                else
1182                {
1183                   positionVector = light.direction;
1184                   position[3] = 1;
1185                }
1186             }
1187
1188             position[0] = (float)positionVector.x;
1189             position[1] = (float)positionVector.y;
1190             position[2] = (float)positionVector.z;
1191
1192             glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
1193       CocoaGlAssert();
1194
1195             if(light.flags.attenuation)
1196             {
1197                glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, light.Kc);
1198       CocoaGlAssert();
1199                glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, light.Kl);
1200       CocoaGlAssert();
1201                glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, light.Kq);
1202       CocoaGlAssert();
1203             }
1204
1205             if(light.flags.spot)
1206             {
1207                float exponent = 0;
1208                #define MAXLIGHT  0.9
1209                float direction[4] = { (float)light.direction.x, (float)light.direction.y, (float)light.direction.z, 1 };
1210                // Figure out exponent out of the hot spot
1211                exponent = (float)(log(MAXLIGHT) / log(cos((light.hotSpot / 2))));
1212
1213                glLightfv(GL_LIGHT0 + id, GL_SPOT_DIRECTION, direction);
1214       CocoaGlAssert();
1215                glLightf(GL_LIGHT0 + id, GL_SPOT_CUTOFF, (float)(light.fallOff / 2));
1216       CocoaGlAssert();
1217                glLightf(GL_LIGHT0 + id, GL_SPOT_EXPONENT, exponent);
1218       CocoaGlAssert();
1219             }
1220          }
1221          else
1222          {
1223             Vector3Df vector { 0,0,-1 };
1224             Vector3Df direction;
1225             Matrix mat;
1226
1227             mat.RotationQuaternion(light.orientation);
1228             direction.MultMatrix(vector, mat);
1229
1230             position[0] = direction.x;
1231             position[1] = direction.y;
1232             position[2] = direction.z;
1233
1234             glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
1235       CocoaGlAssert();
1236          }
1237       }
1238       else
1239          glDisable(GL_LIGHT0 + id);
1240       CocoaGlAssert();
1241    }
1242
1243    void SetCamera(Display display, Surface surface, Camera camera)
1244    {
1245       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
1246       if(camera)
1247       {
1248          int left = surface.box.left + surface.offset.x;
1249          int top = surface.box.top  + surface.offset.y;
1250          int right = surface.box.right + surface.offset.x;
1251          int bottom = surface.box.bottom + surface.offset.y;
1252          float origX = surface.offset.x + camera.origin.x;
1253          float origY = surface.offset.y + camera.origin.y;
1254          int x = left;
1255          int y = display.height - bottom - 1;
1256          int w = right - left + 1;
1257          int h = bottom - top + 1;
1258
1259          // *** ViewPort ***
1260          glViewport(x, y, w, h);
1261
1262          // *** Projection Matrix ***
1263          if(!display.display3D.camera)
1264             glPushMatrix();
1265          else
1266             glMatrixMode(GL_PROJECTION);
1267          if(display.display3D.collectingHits)
1268          {
1269             float pickX = display.display3D.pickX + surface.offset.x;
1270             float pickY = display.height - (display.display3D.pickY + surface.offset.y) - 1;
1271             Matrix pickMatrix =
1272             {
1273                {
1274                   w / display.display3D.pickWidth, 0, 0, 0,
1275                   0, h / display.display3D.pickHeight, 0, 0,
1276                   0, 0, 1, 0,
1277                   (w + 2.0f * (x - pickX)) / display.display3D.pickWidth,
1278                   (h + 2.0f * (y - pickY)) / display.display3D.pickHeight, 0, 1
1279                }
1280             };
1281             glLoadMatrixd(pickMatrix.array);
1282          }
1283          else
1284             glLoadIdentity();
1285          glFrustum(
1286             (left   - origX) * camera.zMin / camera.focalX,
1287             (right  - origX) * camera.zMin / camera.focalX,
1288             (bottom - origY) * camera.zMin / camera.focalY,
1289             (top    - origY) * camera.zMin / camera.focalY,
1290             camera.zMin, camera.zMax);
1291
1292          glDisable(GL_BLEND);
1293
1294          // *** Z Inverted Identity Matrix ***
1295          glMatrixMode(GL_MODELVIEW);
1296          if(!display.display3D.camera)
1297             glPushMatrix();
1298
1299          glLoadIdentity();
1300          glScalef(1.0f, 1.0f, -1.0f);
1301
1302          // *** View Matrix ***
1303          glMultMatrixd(camera.viewMatrix.array);
1304
1305          // *** Lights ***
1306          // ...
1307
1308          glEnable(GL_DEPTH_TEST);
1309          glEnable(GL_LIGHTING);
1310          glShadeModel(GL_SMOOTH);
1311          glDepthMask((byte)bool::true);
1312
1313          glEnable(GL_MULTISAMPLE_ARB);
1314       }
1315       else if(display.display3D.camera)
1316       {
1317          glViewport(0,0,display.width,display.height);
1318
1319          glDisable(GL_CULL_FACE);
1320          glDisable(GL_DEPTH_TEST);
1321          glDisable(GL_LIGHTING);
1322          glDisable(GL_FOG);
1323          glDisable(GL_TEXTURE_2D);
1324          glShadeModel(GL_FLAT);
1325          glEnable(GL_BLEND);
1326          glDisable(GL_MULTISAMPLE_ARB);
1327
1328          // *** Restore 2D MODELVIEW Matrix ***
1329          glPopMatrix();
1330
1331          // *** Restore 2D PROJECTION Matrix ***
1332          glMatrixMode(GL_PROJECTION);
1333          glPopMatrix();
1334       }
1335
1336       if(glBindBufferARB)
1337          glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
1338       printf("glGetError():%i\n", glGetError());
1339    }
1340
1341    void ApplyMaterial(Display display, Material material, Mesh mesh)
1342    {
1343       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
1344       // Basic Properties
1345       if(material.flags.doubleSided)
1346       {
1347          glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, !material.flags.singleSideLight);
1348          glDisable(GL_CULL_FACE);
1349       }
1350       else
1351       {
1352          glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, bool::false);
1353          glEnable(GL_CULL_FACE);
1354       }
1355
1356       // Fog
1357       if(material.flags.noFog)
1358          glDisable(GL_FOG);
1359       else
1360          glEnable(GL_FOG);
1361
1362       // Maps
1363       if(material.baseMap && mesh.texCoords)
1364       {
1365          Bitmap map = material.baseMap;
1366          glEnable(GL_TEXTURE_2D);
1367          glBindTexture(GL_TEXTURE_2D, (uint)map.driverData);
1368
1369          if(material.flags.tile)
1370          {
1371             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1372             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1373          }
1374          else
1375          {
1376             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1377             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1378          }
1379       }
1380       else
1381          glDisable(GL_TEXTURE_2D);
1382
1383       if(mesh.flags.colors)
1384       {
1385          glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
1386          glEnable(GL_COLOR_MATERIAL);
1387       }
1388       else
1389       {
1390          glDisable(GL_COLOR_MATERIAL);
1391          {
1392             float color[4] = { material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity };
1393             glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
1394          }
1395          {
1396             float color[4] = { material.ambient.r, material.ambient.g, material.ambient.b, 0 };
1397             glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
1398          }
1399       }
1400       {
1401          float color[4] = { material.specular.r, material.specular.g, material.specular.b, 0 };
1402          glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
1403       }
1404       {
1405          float color[4] = { material.emissive.r, material.emissive.g, material.emissive.b, 0 };
1406          glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
1407       }
1408
1409       glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &material.power);
1410    }
1411
1412    void FreeMesh(DisplaySystem displaySystem, Mesh mesh)
1413    {
1414       MeshData meshData = mesh.data;
1415       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
1416       if(meshData)
1417       {
1418          if(!mesh.flags.vertices)
1419          {
1420             if(meshData.vertices)
1421             {
1422                glDeleteBuffersARB(1, &meshData.vertices);
1423                meshData.vertices = 0;
1424             }
1425             delete mesh.vertices;
1426          }
1427          if(!mesh.flags.normals)
1428          {
1429             if(meshData.normals)
1430             {
1431                glDeleteBuffersARB(1, &meshData.normals);
1432                meshData.normals = 0;
1433             }
1434             delete mesh.normals;
1435          }
1436          if(!mesh.flags.texCoords1)
1437          {
1438             if(meshData.texCoords)
1439             {
1440                glDeleteBuffersARB(1, &meshData.texCoords);
1441                meshData.texCoords = 0;
1442             }
1443             delete mesh.texCoords;
1444          }
1445          if(!mesh.flags.texCoords2)
1446          {
1447             if(meshData.texCoords2)
1448             {
1449                glDeleteBuffersARB(1, &meshData.texCoords2);
1450                meshData.texCoords2 = 0;
1451             }
1452          }
1453          if(!mesh.flags.colors)
1454          {
1455             if(meshData.colors)
1456             {
1457                glDeleteBuffersARB(1, &meshData.colors);
1458                meshData.colors = 0;
1459             }
1460          }
1461          if(!mesh.flags)
1462          {
1463             delete meshData;
1464             mesh.data = null;
1465          }
1466       }
1467    }
1468
1469    bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh)
1470    {
1471       bool result = false;
1472       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
1473
1474       if(!mesh.data)
1475          mesh.data = MeshData { };
1476
1477       if(mesh.data)
1478       {
1479          MeshData meshData = mesh.data;
1480
1481          if(mesh.flags.vertices && !meshData.vertices && !mesh.vertices)
1482          {
1483             mesh.vertices = mesh.flags.doubleVertices ? (Vector3Df *)new Vector3D[mesh.nVertices] : new Vector3Df[mesh.nVertices];
1484             if(glGenBuffersARB)
1485                glGenBuffersARB(1, &meshData.vertices);
1486          }
1487          if(mesh.flags.normals && !meshData.normals && !mesh.normals)
1488          {
1489             if(glGenBuffersARB)
1490                glGenBuffersARB( 1, &meshData.normals);
1491             mesh.normals = mesh.flags.doubleNormals ? (Vector3Df *)new Vector3D[mesh.nVertices] : new Vector3Df[mesh.nVertices];
1492          }
1493          if(mesh.flags.texCoords1 && !meshData.texCoords && !mesh.texCoords)
1494          {
1495             if(glGenBuffersARB)
1496                glGenBuffersARB( 1, &meshData.texCoords);
1497             mesh.texCoords = new Pointf[mesh.nVertices];
1498          }
1499          if(mesh.flags.colors && !meshData.colors && !mesh.colors)
1500          {
1501             if(glGenBuffersARB)
1502                glGenBuffersARB( 1, &meshData.colors);
1503             mesh.colors = new ColorRGBAf[mesh.nVertices];
1504          }
1505          result = true;
1506       }
1507       return result;
1508    }
1509
1510    void UnlockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
1511    {
1512       MeshData meshData = mesh.data;
1513       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
1514
1515       if(!flags) flags = mesh.flags;
1516
1517       if(glGenBuffersARB)
1518       {
1519          if(!(flags.vertices) || meshData.vertices)
1520          {
1521             glBindBufferARB( GL_ARRAY_BUFFER_ARB, meshData.vertices);
1522             glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleVertices ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.vertices, GL_STATIC_DRAW_ARB );
1523          }
1524
1525          if(!(flags.normals) || meshData.normals)
1526          {
1527             glBindBufferARB( GL_ARRAY_BUFFER_ARB, meshData.normals);
1528             glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleNormals ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.normals, GL_STATIC_DRAW_ARB );
1529          }
1530
1531          if(!(flags.texCoords1) || meshData.texCoords)
1532          {
1533             glBindBufferARB( GL_ARRAY_BUFFER_ARB, meshData.texCoords);
1534             glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(Pointf), mesh.texCoords, GL_STATIC_DRAW_ARB );
1535          }
1536
1537          if(!(flags.colors) || meshData.colors)
1538          {
1539             glBindBufferARB( GL_ARRAY_BUFFER_ARB, meshData.colors);
1540             glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(ColorRGBAf), mesh.colors, GL_STATIC_DRAW_ARB );
1541          }
1542
1543          glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
1544       }
1545    }
1546
1547    bool LockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
1548    {
1549       bool result = true;
1550       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
1551       return result;
1552    }
1553
1554    void FreeIndices(DisplaySystem displaySystem, IndexData indexData)
1555    {
1556       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
1557
1558       if(indexData)
1559       {
1560          if(indexData.buffer)
1561             glDeleteBuffersARB(1, &indexData.buffer);
1562          delete indexData.indices;
1563          delete indexData;
1564       }
1565    }
1566
1567    void * AllocateIndices(DisplaySystem displaySystem, int nIndices, bool indices32bit)
1568    {
1569       IndexData indexData = IndexData { };
1570       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
1571       if(indexData)
1572       {
1573          indexData.indices = (void *)(indices32bit ? new uint32[nIndices] : new uint16[nIndices]);
1574          if(glGenBuffersARB)
1575             glGenBuffersARB( 1, &indexData.buffer);
1576          indexData.nIndices = nIndices;
1577       }
1578       return indexData;
1579    }
1580
1581    void UnlockIndices(DisplaySystem displaySystem, IndexData indexData, bool indices32bit, int nIndices)
1582    {
1583       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
1584       if(glGenBuffersARB)
1585       {
1586          glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, indexData.buffer);
1587          glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)),
1588             indexData.indices, GL_STATIC_DRAW_ARB);
1589          glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
1590       }
1591    }
1592
1593    uint16 * LockIndices(DisplaySystem displaySystem, IndexData indexData)
1594    {
1595       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
1596
1597       return indexData.indices;
1598    }
1599
1600    void SelectMesh(Display display, Mesh mesh)
1601    {
1602       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
1603
1604       /*if(display.display3D.mesh && glUnlockArraysEXT)
1605          glUnlockArraysEXT();*/
1606
1607       if(mesh)
1608       {
1609          DisplayData displayData = display.driverData;
1610          Mesh meshData = mesh.data;
1611
1612          // *** Vertex Stream ***
1613          glEnableClientState(GL_VERTEX_ARRAY);
1614          if(!display.display3D.collectingHits && meshData)
1615          {
1616             if(glBindBufferARB)
1617                glBindBufferARB(GL_ARRAY_BUFFER_ARB, (long)meshData.vertices);
1618             glVertexPointer(3, mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT, 0, glBindBufferARB ? null : mesh.vertices);
1619
1620             // *** Normals Stream ***
1621             if(mesh.normals)
1622             {
1623                glEnableClientState(GL_NORMAL_ARRAY);
1624                if(glBindBufferARB)
1625                   glBindBufferARB(GL_ARRAY_BUFFER_ARB, (long)meshData.normals);
1626                glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, glBindBufferARB ? null : mesh.normals);
1627             }
1628             else
1629             {
1630                glDisableClientState(GL_NORMAL_ARRAY);
1631             }
1632
1633             // *** Texture Coordinates Stream ***
1634             if(mesh.texCoords)
1635             {
1636                glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1637                if(glBindBufferARB)
1638                   glBindBufferARB( GL_ARRAY_BUFFER_ARB, (long)meshData.texCoords);
1639                glTexCoordPointer(2, GL_FLOAT, 0, glBindBufferARB ? null : mesh.texCoords);
1640             }
1641             else
1642             {
1643                glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1644             }
1645
1646             // *** Color Stream ***
1647             if(mesh.colors)
1648             {
1649                glEnableClientState(GL_COLOR_ARRAY);
1650                if(glBindBufferARB)
1651                   glBindBufferARB( GL_ARRAY_BUFFER_ARB, (long)meshData.colors);
1652                glColorPointer(4, GL_FLOAT, 0, glBindBufferARB ? null : mesh.colors);
1653             }
1654             else
1655             {
1656                glDisableClientState(GL_COLOR_ARRAY);
1657             }
1658          }
1659          else
1660          {
1661             if(glBindBufferARB)
1662                glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
1663             glVertexPointer(3,mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT,0,mesh.vertices);
1664             if(mesh.normals && !display.display3D.collectingHits)
1665             {
1666                glEnableClientState(GL_NORMAL_ARRAY);
1667                glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, mesh.normals);
1668             }
1669             else
1670                glDisableClientState(GL_NORMAL_ARRAY);
1671             if(mesh.texCoords && !display.display3D.collectingHits)
1672             {
1673                glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1674                glTexCoordPointer(2, GL_FLOAT, 0, mesh.texCoords);
1675             }
1676             else
1677                glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1678             if(mesh.colors && !display.display3D.collectingHits)
1679             {
1680                glEnableClientState(GL_COLOR_ARRAY);
1681                glColorPointer(4, GL_FLOAT, 0, mesh.colors);
1682             }
1683             else
1684                glDisableClientState(GL_COLOR_ARRAY);
1685          }
1686
1687          //if(glLockArraysEXT) glLockArraysEXT(0, mesh.nVertices);
1688       }
1689       else if(glBindBufferARB)
1690          glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
1691    }
1692
1693    void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
1694    {
1695       DisplayData displayData = display.driverData;
1696       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
1697
1698       if(primitive->type.vertexRange)
1699          glDrawArrays(primitiveTypes[primitive->type.primitiveType], primitive->first, primitive->nVertices);
1700       else
1701       {
1702          //    *** Hoping the data won't be uploaded at all (Won't really work if another group of the mesh is using the mesh ) ***
1703          // HACK TO SPEED THINGS UP...
1704          if(primitive->nIndices < (mesh.nVertices >> 2) && !primitive->type.indices32bit)
1705          {
1706             int c;
1707             glBegin(primitiveTypes[primitive->type.primitiveType]);
1708             if(primitive->data)
1709             {
1710                IndexData indexData = primitive->data;
1711                MeshFeatures flags = mesh.flags;
1712                for(c = 0; c < primitive->nIndices; c++)
1713                {
1714                   short index = ((short *) indexData.indices)[c];
1715                   if(flags.normals) glNormal3fv((float *)&mesh.normals[index]);
1716                   if(flags.texCoords1) glTexCoord2fv((float *)&mesh.texCoords[index]);
1717                   if(flags.colors) glColor4fv((float *)&mesh.colors[index]);
1718                   glVertex3fv((float *)&mesh.vertices[index]);
1719                }
1720             }
1721             glEnd();
1722          }
1723          else
1724          {
1725             IndexData indexData = primitive->data;
1726
1727             if(!display.display3D.collectingHits && glBindBufferARB && indexData)
1728             {
1729                glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, indexData.buffer);
1730                glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
1731                   primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
1732                glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
1733             }
1734             else if(indexData)
1735                glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
1736                   primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, indexData.indices);
1737             else
1738                glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
1739                   primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, primitive->indices);
1740          }
1741       }
1742    }
1743
1744    void PushMatrix(Display display)
1745    {
1746       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
1747       glPushMatrix();
1748    }
1749
1750    void PopMatrix(Display display, bool setMatrix)
1751    {
1752       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
1753       glPopMatrix();
1754    }
1755
1756    void SetTransform(Display display, Matrix transMatrix, bool viewSpace, bool useCamera)
1757    {
1758       Matrix matrix = transMatrix;
1759       Camera camera = useCamera ? display.display3D.camera : null;
1760
1761       printf("CocoaOpenGLDisplayDriver: STUB! %s:%i\n", __FILE__, __LINE__);
1762       if(viewSpace)
1763       {
1764          glLoadIdentity();
1765          glScalef(1.0f, 1.0f, -1.0f);
1766       }
1767       else if(camera)
1768       {
1769          glTranslated(
1770             matrix.m[3][0] - camera.cPosition.x,
1771             matrix.m[3][1] - camera.cPosition.y,
1772             matrix.m[3][2] - camera.cPosition.z);
1773       }
1774       else
1775          glTranslated(
1776             matrix.m[3][0],
1777             matrix.m[3][1],
1778             matrix.m[3][2]);
1779
1780       matrix.m[3][0] = 0;
1781       matrix.m[3][1] = 0;
1782       matrix.m[3][2] = 0;
1783
1784       glMultMatrixd(matrix.array);
1785    }
1786 #endif
1787 }