Engine:
authorsmaxx
Fri, 30 Jul 2010 19:56:28 +0200
changeset 3691 34fe2149f75d
parent 3689 e2be39ee19f0 (current diff)
parent 3688 fd8c22a358de (diff)
child 3693 09892cdb8f95
Engine: * Added/merged support for anaglyph (red/cyan) rendering (might not be supported by very old cards; don't use it if you've got low fps anyway) Frontend: * Added a checkbox to options to enable anaglyph rendering
QTfrontend/game.cpp
QTfrontend/gameuiconfig.cpp
QTfrontend/gameuiconfig.h
QTfrontend/pages.cpp
QTfrontend/pages.h
hedgewars/adler32.pas
hedgewars/hwengine.pas
hedgewars/uMisc.pas
hedgewars/uStore.pas
hedgewars/uWorld.pas
misc/libopenalbridge/openalbridge_t.h
--- a/.hgeol	Fri Jul 30 18:20:42 2010 +0200
+++ b/.hgeol	Fri Jul 30 19:56:28 2010 +0200
@@ -1,6 +1,7 @@
 [patterns]
 **.h = native
 **.c = native
+**.m = native
 **.cpp = native
 **.lua = native
 **.txt = native
--- a/QTfrontend/game.cpp	Fri Jul 30 18:20:42 2010 +0200
+++ b/QTfrontend/game.cpp	Fri Jul 30 19:56:28 2010 +0200
@@ -292,6 +292,7 @@
     arguments << config->netNick().toUtf8().toBase64();
     arguments << (config->isMusicEnabled() ? "1" : "0");
     arguments << (config->isReducedQuality() ? "1" : "0");
+    arguments << (config->isStereoEnabled() ? "1" : "0");
     return arguments;
 }
 
--- a/QTfrontend/gameuiconfig.cpp	Fri Jul 30 18:20:42 2010 +0200
+++ b/QTfrontend/gameuiconfig.cpp	Fri Jul 30 19:56:28 2010 +0200
@@ -48,6 +48,7 @@
     Form->ui.pageOptions->CBFrontendFullscreen->setChecked(ffscr);
 
     Form->ui.pageOptions->CBReduceQuality->setChecked(value("video/reducequality", false).toBool());
+    Form->ui.pageOptions->CBEnableStereo->setChecked(value("video/anaglyph", false).toBool());
     Form->ui.pageOptions->CBFrontendEffects->setChecked(frontendEffects);
     Form->ui.pageOptions->CBEnableSound->setChecked(value("audio/sound", true).toBool());
     Form->ui.pageOptions->CBEnableFrontendSound->setChecked(value("frontend/sound", true).toBool());
@@ -116,6 +117,7 @@
     setValue("video/fullscreen", vid_Fullscreen());
 
     setValue("video/reducequality", isReducedQuality());
+    setValue("video/anaglyph", isStereoEnabled());
 
     setValue("frontend/effects", isFrontendEffects());
 
@@ -236,6 +238,11 @@
     return Form->ui.pageOptions->CBAltDamage->isChecked();
 }
 
+bool GameUIConfig::isStereoEnabled() const
+{
+    return Form->ui.pageOptions->CBEnableStereo->isChecked();
+}
+
 bool GameUIConfig::appendDateTimeToRecordName()
 {
     return Form->ui.pageOptions->CBNameWithDate->isChecked();
--- a/QTfrontend/gameuiconfig.h	Fri Jul 30 18:20:42 2010 +0200
+++ b/QTfrontend/gameuiconfig.h	Fri Jul 30 19:56:28 2010 +0200
@@ -55,6 +55,7 @@
     bool isFrontendFullscreen() const;
     bool isWeaponTooltip() const;
     void resizeToConfigValues();
+    bool isStereoEnabled() const;
 
 #ifdef __APPLE__
 #ifdef SPARKLE_ENABLED
--- a/QTfrontend/pages.cpp	Fri Jul 30 18:20:42 2010 +0200
+++ b/QTfrontend/pages.cpp	Fri Jul 30 19:56:28 2010 +0200
@@ -612,6 +612,10 @@
             CBReduceQuality->setText(QCheckBox::tr("Reduced quality"));
             GBAlayout->addWidget(CBReduceQuality);
 
+            CBEnableStereo = new QCheckBox(AGGroupBox);
+            CBEnableStereo->setText(QCheckBox::tr("Anaglyph rendering (red/cyan)"));
+            GBAlayout->addWidget(CBEnableStereo);
+
             hr = new QFrame(AGGroupBox);
             hr->setFrameStyle(QFrame::HLine);
             hr->setLineWidth(3);
--- a/QTfrontend/pages.h	Fri Jul 30 18:20:42 2010 +0200
+++ b/QTfrontend/pages.h	Fri Jul 30 19:56:28 2010 +0200
@@ -221,6 +221,7 @@
     QComboBox *CBResolution;
     QCheckBox *CBEnableSound;
     QCheckBox *CBEnableFrontendSound;
+    QCheckBox *CBEnableStereo;
 #ifdef _WIN32
     QCheckBox *CBHardwareSound;
 #endif
--- a/hedgewars/adler32.pas	Fri Jul 30 18:20:42 2010 +0200
+++ b/hedgewars/adler32.pas	Fri Jul 30 19:56:28 2010 +0200
@@ -1,152 +1,152 @@
-unit Adler32;
-
-{ZLib - Adler32 checksum function}
-
-
-interface
-
-(*************************************************************************
-
- DESCRIPTION     :  ZLib - Adler32 checksum function
-
- REQUIREMENTS    :  TP5-7, D1-D7/D9-D10/D12, FPC, VP
-
- EXTERNAL DATA   :  ---
-
- MEMORY USAGE    :  ---
-
- DISPLAY MODE    :  ---
-
- REFERENCES      :  RFC 1950 (http://tools.ietf.org/html/rfc1950)
-
-
- Version  Date      Author      Modification
- -------  --------  -------     ------------------------------------------
- 0.10     30.08.03  W.Ehrhardt  Initial version based on MD5 layout
- 2.10     30.08.03  we          Common vers., XL versions for Win32
- 2.20     27.09.03  we          FPC/go32v2
- 2.30     05.10.03  we          STD.INC, TP5.0
- 2.40     10.10.03  we          common version, english comments
- 3.00     01.12.03  we          Common version 3.0
- 3.01     22.05.05  we          Adler32UpdateXL (i,n: integer)
- 3.02     17.12.05  we          Force $I- in Adler32File
- 3.03     07.08.06  we          $ifdef BIT32: (const fname: shortstring...)
- 3.04     10.02.07  we          Adler32File: no eof, XL and filemode via $ifdef
- 3.05     04.07.07  we          BASM16: speed-up factor 15
- 3.06     12.11.08  we          uses BTypes, Ptr2Inc and/or Str255
- 3.07     25.04.09  we          updated RFC URL(s)
- 3.08     19.07.09  we          D12 fix: assign with typecast string(fname)
-**************************************************************************)
-
-(*-------------------------------------------------------------------------
- (C) Copyright 2002-2009 Wolfgang Ehrhardt
-
- This software is provided 'as-is', without any express or implied warranty.
- In no event will the authors be held liable for any damages arising from
- the use of this software.
-
- Permission is granted to anyone to use this software for any purpose,
- including commercial applications, and to alter it and redistribute it
- freely, subject to the following restrictions:
-
- 1. The origin of this software must not be misrepresented; you must not
-    claim that you wrote the original software. If you use this software in
-    a product, an acknowledgment in the product documentation would be
-    appreciated but is not required.
-
- 2. Altered source versions must be plainly marked as such, and must not be
-    misrepresented as being the original software.
-
- 3. This notice may not be removed or altered from any source distribution.
-----------------------------------------------------------------------------*)
-
-(*
-As per the license above, noting that this implementation of adler32 was stripped of everything we didn't need.
-That means no btypes, file loading, and the assembly version disabled.
-*)
-
-procedure Adler32Update(var adler: longint; Msg: pointer; Len: longint);
-
-implementation
-
-(*
-$ifdef BASM16
-
-procedure Adler32Update(var adler: longint; Msg: pointer; Len: longint);
-  //-update Adler32 with Msg data
-const
-  BASE = 65521; // max. prime < 65536 
-  NMAX =  5552; // max. n with 255n(n+1)/2 + (n+1)(BASE-1) < 2^32
-type
-  LH    = packed record
-            L,H: word;
-          end;
-var
-  s1,s2: longint;
-  n: integer;
-begin
-  s1 := LH(adler).L;
-  s2 := LH(adler).H;
-  while Len > 0 do begin
-    if Len<NMAX then n := Len else n := NMAX;
-    //BASM increases speed from about 52 cyc/byte to about 3.7 cyc/byte
-    asm
-                    mov  cx,[n]
-            db $66; mov  ax,word ptr [s1]
-            db $66; mov  di,word ptr [s2]
-                    les  si,[msg]
-      @@1:  db $66, $26, $0f, $b6, $1c      // movzx ebx,es:[si]
-                    inc  si
-            db $66; add  ax,bx              // inc(s1, pByte(Msg)^)
-            db $66; add  di,ax              // inc(s2, s1
-                    dec  cx
-                    jnz  @@1
-            db $66; sub  cx,cx
-                    mov  cx,BASE
-            db $66; sub  dx,dx
-            db $66; div  cx
-            db $66; mov  word ptr [s1],dx   // s1 := s1 mod BASE
-            db $66; sub  dx,dx
-            db $66; mov  ax,di
-            db $66; div  cx
-            db $66; mov  word ptr [s2],dx   // s2 := s2 mod BASE
-                    mov  word ptr [msg],si  // save offset for next chunk
-    end;
-    dec(len, n);
-  end;
-  LH(adler).L := word(s1);
-  LH(adler).H := word(s2);
-end;
-*)
-
-procedure Adler32Update(var adler: longint; Msg: pointer; Len: longint);
-  {-update Adler32 with Msg data}
-const
-  BASE = 65521; {max. prime < 65536 }
-  NMAX =  3854; {max. n with 255n(n+1)/2 + (n+1)(BASE-1) < 2^31}
-type
-  LH    = packed record
-            L,H: word;
-          end;
-var
-  s1,s2: longint;
-  i,n: integer;
-begin
-  s1 := LH(adler).L;
-  s2 := LH(adler).H;
-  while Len > 0 do begin
-    if Len<NMAX then n := Len else n := NMAX;
-    for i:=1 to n do begin
-      inc(s1, pByte(Msg)^);
-      inc(Msg);
-      inc(s2, s1);
-    end;
-    s1 := s1 mod BASE;
-    s2 := s2 mod BASE;
-    dec(len, n);
-  end;
-  LH(adler).L := word(s1);
-  LH(adler).H := word(s2);
-end;
-
-end.
+unit Adler32;
+
+{ZLib - Adler32 checksum function}
+
+
+interface
+
+(*************************************************************************
+
+ DESCRIPTION     :  ZLib - Adler32 checksum function
+
+ REQUIREMENTS    :  TP5-7, D1-D7/D9-D10/D12, FPC, VP
+
+ EXTERNAL DATA   :  ---
+
+ MEMORY USAGE    :  ---
+
+ DISPLAY MODE    :  ---
+
+ REFERENCES      :  RFC 1950 (http://tools.ietf.org/html/rfc1950)
+
+
+ Version  Date      Author      Modification
+ -------  --------  -------     ------------------------------------------
+ 0.10     30.08.03  W.Ehrhardt  Initial version based on MD5 layout
+ 2.10     30.08.03  we          Common vers., XL versions for Win32
+ 2.20     27.09.03  we          FPC/go32v2
+ 2.30     05.10.03  we          STD.INC, TP5.0
+ 2.40     10.10.03  we          common version, english comments
+ 3.00     01.12.03  we          Common version 3.0
+ 3.01     22.05.05  we          Adler32UpdateXL (i,n: integer)
+ 3.02     17.12.05  we          Force $I- in Adler32File
+ 3.03     07.08.06  we          $ifdef BIT32: (const fname: shortstring...)
+ 3.04     10.02.07  we          Adler32File: no eof, XL and filemode via $ifdef
+ 3.05     04.07.07  we          BASM16: speed-up factor 15
+ 3.06     12.11.08  we          uses BTypes, Ptr2Inc and/or Str255
+ 3.07     25.04.09  we          updated RFC URL(s)
+ 3.08     19.07.09  we          D12 fix: assign with typecast string(fname)
+**************************************************************************)
+
+(*-------------------------------------------------------------------------
+ (C) Copyright 2002-2009 Wolfgang Ehrhardt
+
+ This software is provided 'as-is', without any express or implied warranty.
+ In no event will the authors be held liable for any damages arising from
+ the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+    claim that you wrote the original software. If you use this software in
+    a product, an acknowledgment in the product documentation would be
+    appreciated but is not required.
+
+ 2. Altered source versions must be plainly marked as such, and must not be
+    misrepresented as being the original software.
+
+ 3. This notice may not be removed or altered from any source distribution.
+----------------------------------------------------------------------------*)
+
+(*
+As per the license above, noting that this implementation of adler32 was stripped of everything we didn't need.
+That means no btypes, file loading, and the assembly version disabled.
+*)
+
+procedure Adler32Update(var adler: longint; Msg: pointer; Len: longint);
+
+implementation
+
+(*
+$ifdef BASM16
+
+procedure Adler32Update(var adler: longint; Msg: pointer; Len: longint);
+  //-update Adler32 with Msg data
+const
+  BASE = 65521; // max. prime < 65536 
+  NMAX =  5552; // max. n with 255n(n+1)/2 + (n+1)(BASE-1) < 2^32
+type
+  LH    = packed record
+            L,H: word;
+          end;
+var
+  s1,s2: longint;
+  n: integer;
+begin
+  s1 := LH(adler).L;
+  s2 := LH(adler).H;
+  while Len > 0 do begin
+    if Len<NMAX then n := Len else n := NMAX;
+    //BASM increases speed from about 52 cyc/byte to about 3.7 cyc/byte
+    asm
+                    mov  cx,[n]
+            db $66; mov  ax,word ptr [s1]
+            db $66; mov  di,word ptr [s2]
+                    les  si,[msg]
+      @@1:  db $66, $26, $0f, $b6, $1c      // movzx ebx,es:[si]
+                    inc  si
+            db $66; add  ax,bx              // inc(s1, pByte(Msg)^)
+            db $66; add  di,ax              // inc(s2, s1
+                    dec  cx
+                    jnz  @@1
+            db $66; sub  cx,cx
+                    mov  cx,BASE
+            db $66; sub  dx,dx
+            db $66; div  cx
+            db $66; mov  word ptr [s1],dx   // s1 := s1 mod BASE
+            db $66; sub  dx,dx
+            db $66; mov  ax,di
+            db $66; div  cx
+            db $66; mov  word ptr [s2],dx   // s2 := s2 mod BASE
+                    mov  word ptr [msg],si  // save offset for next chunk
+    end;
+    dec(len, n);
+  end;
+  LH(adler).L := word(s1);
+  LH(adler).H := word(s2);
+end;
+*)
+
+procedure Adler32Update(var adler: longint; Msg: pointer; Len: longint);
+  {-update Adler32 with Msg data}
+const
+  BASE = 65521; {max. prime < 65536 }
+  NMAX =  3854; {max. n with 255n(n+1)/2 + (n+1)(BASE-1) < 2^31}
+type
+  LH    = packed record
+            L,H: word;
+          end;
+var
+  s1,s2: longint;
+  i,n: integer;
+begin
+  s1 := LH(adler).L;
+  s2 := LH(adler).H;
+  while Len > 0 do begin
+    if Len<NMAX then n := Len else n := NMAX;
+    for i:=1 to n do begin
+      inc(s1, pByte(Msg)^);
+      inc(Msg);
+      inc(s2, s1);
+    end;
+    s1 := s1 mod BASE;
+    s2 := s2 mod BASE;
+    dec(len, n);
+  end;
+  LH(adler).L := word(s1);
+  LH(adler).H := word(s2);
+end;
+
+end.
--- a/hedgewars/hwengine.pas	Fri Jul 30 18:20:42 2010 +0200
+++ b/hedgewars/hwengine.pas	Fri Jul 30 19:56:28 2010 +0200
@@ -230,9 +230,11 @@
     recordFileName:= gameArgs[8];
     
     val(gameArgs[9], cReducedQuality);
+    isStereoEnabled:= false; // TODO: Enable anaglyph rendering on iPhone?
 {$ENDIF}
 
     initEverything(true);
+
     WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
 {$IFDEF DEBUGFILE}
     AddFileLog('Prefix: "' + PathPrefix +'"');
@@ -437,7 +439,7 @@
 procedure GetParams;
 begin
     case ParamCount of
-        18: begin
+        19: begin
             val(ParamStr(2), cScreenWidth);
             val(ParamStr(3), cScreenHeight);
             cBitsStr:= ParamStr(4);
@@ -455,6 +457,7 @@
             cAltDamage:= ParamStr(15) = '1';
             UserNick:= DecodeBase64(ParamStr(16));
             isMusicEnabled:= ParamStr(17) = '1';
+            isStereoEnabled:= ParamStr(19) = '1';
 
             if (ParamStr(18) = '1') then        //HACK - always disable rqLowRes as it's a game breaker
                 cReducedQuality:= $FFFFFFFF xor rqLowRes
--- a/hedgewars/uMisc.pas	Fri Jul 30 18:20:42 2010 +0200
+++ b/hedgewars/uMisc.pas	Fri Jul 30 19:56:28 2010 +0200
@@ -35,6 +35,8 @@
     isSpeed         : boolean;
     isFirstFrame    : boolean;
 
+    isStereoEnabled : boolean;
+
     fastUntilLag    : boolean;
 
     GameState       : TGameState;
@@ -757,7 +759,7 @@
     Rewrite(f);
 {$ELSE}
     if (ParamStr(1) <> '') and (ParamStr(2) <> '') then
-        if (ParamCount <> 3) and (ParamCount <> 18) then
+        if (ParamCount <> 3) and (ParamCount <> 19) then
         begin
             for i:= 0 to 7 do
             begin
@@ -782,7 +784,6 @@
 {$ENDIF}
 {$I+}
 {$ENDIF}
-
 end;
 
 procedure freeModule;
--- a/hedgewars/uStore.pas	Fri Jul 30 18:20:42 2010 +0200
+++ b/hedgewars/uStore.pas	Fri Jul 30 19:56:28 2010 +0200
@@ -39,7 +39,9 @@
     rotationQt: GLfloat;
     wScreen: LongInt;
     hScreen: LongInt;
-    
+    framel, framer, depthl, depthr: GLuint;
+    texl, texr: GLuint;
+
 procedure initModule;
 procedure freeModule;
 
@@ -802,6 +804,15 @@
     SDL_FreeSurface(MissionIcons);
     FreeTexture(ropeIconTex);
     FreeTexture(HHTexture);
+    if isStereoEnabled then
+    begin
+        glDeleteTextures(1, @texl);
+        glDeleteRenderbuffersEXT(1, @depthl);
+        glDeleteFramebuffersEXT(1, @framel);
+        glDeleteTextures(1, @texr);
+        glDeleteRenderbuffersEXT(1, @depthr);
+        glDeleteFramebuffersEXT(1, @framer)
+    end
 end;
 
 
@@ -1171,7 +1182,9 @@
 {$ENDIF}
     end;
 
-{$IFNDEF IPHONEOS}
+{$IFDEF IPHONEOS}
+    cGPUVendor:= gvApple;
+{$ELSE}
     if StrPos(Str2PChar(vendor), Str2PChar('nvidia')) <> nil then
         cGPUVendor:= gvNVIDIA
     else if StrPos(Str2PChar(vendor), Str2PChar('intel')) <> nil then
@@ -1179,8 +1192,43 @@
     else if StrPos(Str2PChar(vendor), Str2PChar('ati')) <> nil then
         cGPUVendor:= gvIntel;
 //SupportNPOTT:= glLoadExtension('GL_ARB_texture_non_power_of_two');
-{$ELSE}
-    cGPUVendor:= gvApple;
+
+    if isStereoEnabled then
+    begin
+        // prepare left and right frame buffers and associated textures
+        glLoadExtension('GL_EXT_framebuffer_object');
+
+        // left
+        glGenFramebuffersEXT(1, @framel);
+        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framel);
+        glGenRenderbuffersEXT(1, @depthl);
+        glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthl);
+        glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, cScreenWidth, cScreenHeight);
+        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthl);
+        glGenTextures(1, @texl);
+        glBindTexture(GL_TEXTURE_2D, texl);
+        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,  cScreenWidth, cScreenHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texl, 0);
+
+        // right
+        glGenFramebuffersEXT(1, @framer);
+        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framer);
+        glGenRenderbuffersEXT(1, @depthr);
+        glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthr);
+        glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, cScreenWidth, cScreenHeight);
+        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthr);
+        glGenTextures(1, @texr);
+        glBindTexture(GL_TEXTURE_2D, texr);
+        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,  cScreenWidth, cScreenHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texr, 0);
+
+        // reset
+        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0)
+    end;
 {$ENDIF}
 
 {$IFDEF DEBUGFILE}
--- a/hedgewars/uWorld.pas	Fri Jul 30 18:20:42 2010 +0200
+++ b/hedgewars/uWorld.pas	Fri Jul 30 19:56:28 2010 +0200
@@ -22,6 +22,7 @@
 interface
 uses SDLh, uGears, uConsts, uFloat, uRandom;
 
+type TRenderMode = (rmDefault, rmLeftEye, rmRightEye);
 
 var FollowGear: PGear;
     WindBarWidth: LongInt;
@@ -44,6 +45,7 @@
 
 procedure InitWorld;
 procedure DrawWorld(Lag: LongInt);
+procedure DrawWorldStereo(Lag: LongInt; RM: TRenderMode);
 procedure AddCaption(s: shortstring; Color: Longword; Group: TCapGroup);
 procedure ShowMission(caption, subcaption, text: ansistring; icon, time : LongInt);
 procedure HideMission;
@@ -71,6 +73,11 @@
     amSel: TAmmoType = amNothing;
     missionTex: PTexture;
     missionTimer: LongInt;
+    stereoDepth: GLfloat = 0;
+
+const cStereo_Sky     = 0.0750;
+      cStereo_Horizon = 0.0250;
+      cStereo_Water   = 0.0125;
 
 procedure InitWorld;
 var i, t: LongInt;
@@ -528,14 +535,7 @@
 
 
 procedure DrawWorld(Lag: LongInt);
-var i, t: LongInt;
-    r: TSDL_Rect;
-    tdx, tdy: Double;
-    grp: TCapGroup;
-    s: string[15];
-    highlight: Boolean;
-    offset, offsetX, offsetY, ScreenBottom: LongInt;
-    VertexBuffer: array [0..3] of TVertex2f;
+var cc: array[0..3] of GLfloat;
 begin
     if not isPaused then
     begin
@@ -564,6 +564,101 @@
     if not isPaused then
         MoveCamera;
 
+    if not isStereoEnabled then
+    begin
+        glClear(GL_COLOR_BUFFER_BIT);
+        DrawWorldStereo(Lag, rmDefault)
+    end
+    else
+    begin
+        // create left fb
+        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framel);
+        glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
+        DrawWorldStereo(Lag, rmLeftEye);
+
+        // create right fb
+        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framer);
+        glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
+        DrawWorldStereo(0, rmRightEye);
+
+        // detatch drawing from fbs
+        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
+        glGetFloatv(GL_COLOR_CLEAR_VALUE, @cc);
+        glClearColor(0, 0, 0, 0);
+        glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
+        glClearColor(cc[0], cc[1], cc[2], cc[3]);
+        SetScale(cDefaultZoomLevel);
+
+        // enable gl stuff
+        glEnable(GL_TEXTURE_2D);
+        glEnable(GL_BLEND);
+        glBlendFunc(GL_ONE, GL_ONE);
+
+        // draw left frame
+        glBindTexture(GL_TEXTURE_2D, texl);
+        glColor3f(0.0, 1.0, 1.0);
+        glBegin(GL_QUADS);
+            glTexCoord2f(0.0, 0.0);
+            glVertex2d(cScreenWidth / -2, cScreenHeight);
+            glTexCoord2f(1.0, 0.0);
+            glVertex2d(cScreenWidth / 2, cScreenHeight);
+            glTexCoord2f(1.0, 1.0);
+            glVertex2d(cScreenWidth / 2, 0);
+            glTexCoord2f(0.0, 1.0);
+            glVertex2d(cScreenWidth / -2, 0);
+        glEnd();
+
+        // draw right frame
+        glBindTexture(GL_TEXTURE_2D, texr);
+        glColor3f(1.0, 0.0, 0.0);
+        glBegin(GL_QUADS);
+            glTexCoord2f(0.0, 0.0);
+            glVertex2d(cScreenWidth / -2, cScreenHeight);
+            glTexCoord2f(1.0, 0.0);
+            glVertex2d(cScreenWidth / 2, cScreenHeight);
+            glTexCoord2f(1.0, 1.0);
+            glVertex2d(cScreenWidth / 2, 0);
+            glTexCoord2f(0.0, 1.0);
+            glVertex2d(cScreenWidth / -2, 0);
+        glEnd();
+
+        // reset
+        glColor3f(1.0, 1.0, 1.0);
+        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+        SetScale(zoom);
+    end
+end;
+
+procedure ChangeDepth(rm: TRenderMode; d: GLfloat);
+begin
+    if rm = rmDefault then exit
+    else if rm = rmRightEye then d:= -d;
+    stereoDepth:= stereoDepth + d;
+    glMatrixMode(GL_PROJECTION);
+    glTranslatef(d, 0, 0);
+    glMatrixMode(GL_MODELVIEW)
+end;
+ 
+procedure ResetDepth(rm: TRenderMode);
+begin
+    if rm = rmDefault then exit;
+    glMatrixMode(GL_PROJECTION);
+    glTranslatef(-stereoDepth, 0, 0);
+    glMatrixMode(GL_MODELVIEW);
+    stereoDepth:= 0;
+end;
+ 
+procedure DrawWorldStereo(Lag: LongInt; RM: TRenderMode);
+var i, t: LongInt;
+    r: TSDL_Rect;
+    tdx, tdy: Double;
+    grp: TCapGroup;
+    s: string[15];
+    highlight: Boolean;
+    offset, offsetX, offsetY, screenBottom: LongInt;
+    scale: GLfloat;
+    VertexBuffer: array [0..3] of TVertex2f;
+begin
     if (cReducedQuality and rqNoBackground) = 0 then
     begin
         // Offsets relative to camera - spare them to wimpier cpus, no bg or flakes for them anyway
@@ -575,9 +670,13 @@
             HorizontOffset:= HorizontOffset + ((ScreenBottom-SkyOffset) div 20);
 
         // background
+        ChangeDepth(RM, cStereo_Sky);
         DrawRepeated(sprSky, sprSkyL, sprSkyR, (WorldDx + LAND_WIDTH div 2) * 3 div 8, SkyOffset);
+        ChangeDepth(RM, -cStereo_Horizon);
         DrawRepeated(sprHorizont, sprHorizontL, sprHorizontR, (WorldDx + LAND_WIDTH div 2) * 3 div 5, HorizontOffset);
-    end;
+    end
+    else
+        ChangeDepth(RM, cStereo_Sky - cStereo_Horizon);
 
     DrawVisualGears(0);
     
@@ -585,13 +684,21 @@
     begin
         // Waves
         DrawWater(255, SkyOffset); 
+        ChangeDepth(RM, -cStereo_Water);
         DrawWaves( 1,  0 - WorldDx div 32, - cWaveHeight + offsetY div 35, 64);
+        ChangeDepth(RM, -cStereo_Water);
         DrawWaves( -1,  25 + WorldDx div 25, - cWaveHeight + offsetY div 38, 48);
+        ChangeDepth(RM, -cStereo_Water);
         DrawWaves( 1,  75 - WorldDx div 19, - cWaveHeight + offsetY div 45, 32);
+        ChangeDepth(RM, -cStereo_Water);
         DrawWaves(-1, 100 + WorldDx div 14, - cWaveHeight + offsetY div 70, 24);
     end
     else
+    begin
+        ChangeDepth(RM, -4 * cStereo_Water);
         DrawWaves(-1, 100, - (cWaveHeight + (cWaveHeight shr 1)), 0);
+    end;
+    ResetDepth(RM);
 
     DrawLand(WorldDx, WorldDy);
 
@@ -628,20 +735,27 @@
     DrawWater(cWaterOpacity, 0);
 
     // Waves
+    ChangeDepth(RM, cStereo_Water);
     DrawWaves( 1, 25 - WorldDx div 9, - cWaveHeight, 12);
 
     if (cReducedQuality and rq2DWater) = 0 then
     begin
         //DrawWater(cWaterOpacity, - offsetY div 40);
+        ChangeDepth(RM, cStereo_Water);
         DrawWaves(-1, 50 + WorldDx div 6, - cWaveHeight - offsetY div 40, 8);
         DrawWater(cWaterOpacity, - offsetY div 20);
+        ChangeDepth(RM, cStereo_Water);
         DrawWaves( 1, 75 - WorldDx div 4, - cWaveHeight - offsetY div 20, 2);
         DrawWater(cWaterOpacity, - offsetY div 10);
+        ChangeDepth(RM, cStereo_Water);
         DrawWaves( -1, 25 + WorldDx div 3, - cWaveHeight - offsetY div 10, 0);
     end
     else
+    begin
+        ChangeDepth(RM, cStereo_Water);
         DrawWaves(-1, 50, - (cWaveHeight shr 1), 0);
-
+    end;
+    ResetDepth(RM);
 
 {$WARNINGS OFF}
 // Target
@@ -862,58 +976,65 @@
 offsetX:= 10;
 {$ENDIF}
 offsetY:= cOffsetY;
-inc(Frames);
+
+// don't increment fps when drawing the right frame
+if (RM = rmDefault) or (RM = rmLeftEye) then
+begin
+    inc(Frames);
 
-if cShowFPS or (GameType = gmtDemo) then inc(CountTicks, Lag);
-if (GameType = gmtDemo) and (CountTicks >= 1000) then
-   begin
-   i:=GameTicks div 1000;
-   t:= i mod 60;
-   s:= inttostr(t);
-   if t < 10 then s:= '0' + s;
-   i:= i div 60;
-   t:= i mod 60;
-   s:= inttostr(t) + ':' + s;
-   if t < 10 then s:= '0' + s;
-   s:= inttostr(i div 60) + ':' + s;
+    if cShowFPS or (GameType = gmtDemo) then
+        inc(CountTicks, Lag);
+    if (GameType = gmtDemo) and (CountTicks >= 1000) then
+    begin
+        i:=GameTicks div 1000;
+        t:= i mod 60;
+        s:= inttostr(t);
+        if t < 10 then s:= '0' + s;
+        i:= i div 60;
+        t:= i mod 60;
+        s:= inttostr(t) + ':' + s;
+        if t < 10 then s:= '0' + s;
+        s:= inttostr(i div 60) + ':' + s;
    
-   if timeTexture <> nil then
-        FreeTexture(timeTexture);
-    timeTexture:= nil;
+        if timeTexture <> nil then
+            FreeTexture(timeTexture);
+        timeTexture:= nil;
     
-   tmpSurface:= TTF_RenderUTF8_Blended(Fontz[fnt16].Handle, Str2PChar(s), cWhiteColorChannels);
-   tmpSurface:= doSurfaceConversion(tmpSurface);
-   timeTexture:= Surface2Tex(tmpSurface, false);
-   SDL_FreeSurface(tmpSurface)
-   end;
+        tmpSurface:= TTF_RenderUTF8_Blended(Fontz[fnt16].Handle, Str2PChar(s), cWhiteColorChannels);
+        tmpSurface:= doSurfaceConversion(tmpSurface);
+        timeTexture:= Surface2Tex(tmpSurface, false);
+        SDL_FreeSurface(tmpSurface)
+    end;
 
-if timeTexture <> nil then
-   DrawTexture((cScreenWidth shr 1) - 20 - timeTexture^.w - offsetY, offsetX + timeTexture^.h+5, timeTexture);
+    if timeTexture <> nil then
+        DrawTexture((cScreenWidth shr 1) - 20 - timeTexture^.w - offsetY, offsetX + timeTexture^.h+5, timeTexture);
 
-if cShowFPS then
-   begin
-   if CountTicks >= 1000 then
-      begin
-      FPS:= Frames;
-      Frames:= 0;
-      CountTicks:= 0;
-      s:= inttostr(FPS) + ' fps';
-      if fpsTexture <> nil then
-        FreeTexture(fpsTexture);
-    fpsTexture:= nil;
-      tmpSurface:= TTF_RenderUTF8_Blended(Fontz[fnt16].Handle, Str2PChar(s), cWhiteColorChannels);
-      tmpSurface:= doSurfaceConversion(tmpSurface);
-      fpsTexture:= Surface2Tex(tmpSurface, false);
-      SDL_FreeSurface(tmpSurface)
-      end;
-   if fpsTexture <> nil then
-      DrawTexture((cScreenWidth shr 1) - 60 - offsetY, offsetX, fpsTexture);
-   end;
+    if cShowFPS then
+    begin
+        if CountTicks >= 1000 then
+        begin
+            FPS:= Frames;
+            Frames:= 0;
+            CountTicks:= 0;
+            s:= inttostr(FPS) + ' fps';
+            if fpsTexture <> nil then
+                FreeTexture(fpsTexture);
+            fpsTexture:= nil;
+            tmpSurface:= TTF_RenderUTF8_Blended(Fontz[fnt16].Handle, Str2PChar(s), cWhiteColorChannels);
+            tmpSurface:= doSurfaceConversion(tmpSurface);
+            fpsTexture:= Surface2Tex(tmpSurface, false);
+            SDL_FreeSurface(tmpSurface)
+        end;
+        if fpsTexture <> nil then
+            DrawTexture((cScreenWidth shr 1) - 60 - offsetY, offsetX, fpsTexture);
+    end;
 
-if CountTicks >= 1000 then CountTicks:= 0;
+    if CountTicks >= 1000 then CountTicks:= 0;
 
-// lag warning (?)
-inc(SoundTimerTicks, Lag);
+    // lag warning (?)
+    inc(SoundTimerTicks, Lag);
+end;
+
 if SoundTimerTicks >= 50 then
    begin
    SoundTimerTicks:= 0;
--- a/misc/libopenalbridge/openalbridge_t.h	Fri Jul 30 18:20:42 2010 +0200
+++ b/misc/libopenalbridge/openalbridge_t.h	Fri Jul 30 19:56:28 2010 +0200
@@ -1,74 +1,74 @@
-/*
- * OpenAL Bridge - a simple portable library for OpenAL interface
- * Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation; version 2 of the License
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
- */
-
-#include <stdint.h>
-#include "al.h"
-
-#ifndef _OALB_INTERFACE_TYPES_H
-#define _OALB_INTERFACE_TYPES_H
-
-enum al_fade_enum {AL_FADE_IN, AL_FADE_OUT};
-typedef enum al_fade_enum al_fade_t;
-
-
-// data type to handle which source source is playing what
-#pragma pack(1)
-typedef struct _al_sound_t {
-    const char *filename;       // name of the sound file
-    ALuint buffer;              // actual sound content
-    uint32_t source_index;      // index of the associated source
-    ALboolean is_used;          // tells if the element can be overwritten
-} al_sound_t;
-#pragma pack()
-
-
-// data type for passing data between threads
-#pragma pack(1)
-typedef struct _fade_t {
-    uint32_t index;
-    uint16_t quantity;
-    al_fade_t type;
-} fade_t;
-#pragma pack()
-
-
-// data type for WAV header
-#pragma pack(1)
-typedef struct _WAV_header_t {
-    uint32_t ChunkID;
-    uint32_t ChunkSize;
-    uint32_t Format;
-    uint32_t Subchunk1ID;
-    uint32_t Subchunk1Size;
-    uint16_t AudioFormat;
-    uint16_t NumChannels;
-    uint32_t SampleRate;
-    uint32_t ByteRate;
-    uint16_t BlockAlign;
-    uint16_t BitsPerSample;
-    uint32_t Subchunk2ID;
-    uint32_t Subchunk2Size;
-} WAV_header_t;
-#pragma pack()
-
-
-#ifdef __CPLUSPLUS
-}
-#endif
-
-#endif /*_OALB_INTERFACE_TYPES_H*/
+/*
+ * OpenAL Bridge - a simple portable library for OpenAL interface
+ * Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#include <stdint.h>
+#include "al.h"
+
+#ifndef _OALB_INTERFACE_TYPES_H
+#define _OALB_INTERFACE_TYPES_H
+
+enum al_fade_enum {AL_FADE_IN, AL_FADE_OUT};
+typedef enum al_fade_enum al_fade_t;
+
+
+// data type to handle which source source is playing what
+#pragma pack(1)
+typedef struct _al_sound_t {
+    const char *filename;       // name of the sound file
+    ALuint buffer;              // actual sound content
+    uint32_t source_index;      // index of the associated source
+    ALboolean is_used;          // tells if the element can be overwritten
+} al_sound_t;
+#pragma pack()
+
+
+// data type for passing data between threads
+#pragma pack(1)
+typedef struct _fade_t {
+    uint32_t index;
+    uint16_t quantity;
+    al_fade_t type;
+} fade_t;
+#pragma pack()
+
+
+// data type for WAV header
+#pragma pack(1)
+typedef struct _WAV_header_t {
+    uint32_t ChunkID;
+    uint32_t ChunkSize;
+    uint32_t Format;
+    uint32_t Subchunk1ID;
+    uint32_t Subchunk1Size;
+    uint16_t AudioFormat;
+    uint16_t NumChannels;
+    uint32_t SampleRate;
+    uint32_t ByteRate;
+    uint16_t BlockAlign;
+    uint16_t BitsPerSample;
+    uint32_t Subchunk2ID;
+    uint32_t Subchunk2Size;
+} WAV_header_t;
+#pragma pack()
+
+
+#ifdef __CPLUSPLUS
+}
+#endif
+
+#endif /*_OALB_INTERFACE_TYPES_H*/