hedgewars/uWorld.pas
branchexperimental3D
changeset 3686 617d00c850b0
parent 3663 8c28abf427f5
child 3688 fd8c22a358de
equal deleted inserted replaced
3685:5fdf49ace96c 3686:617d00c850b0
    20 
    20 
    21 unit uWorld;
    21 unit uWorld;
    22 interface
    22 interface
    23 uses SDLh, uGears, uConsts, uFloat, uRandom;
    23 uses SDLh, uGears, uConsts, uFloat, uRandom;
    24 
    24 
       
    25 type TRenderMode = (rmDefault, rmLeftEye, rmRightEye);
    25 
    26 
    26 var FollowGear: PGear;
    27 var FollowGear: PGear;
    27     WindBarWidth: LongInt;
    28     WindBarWidth: LongInt;
    28     bShowAmmoMenu: boolean;
    29     bShowAmmoMenu: boolean;
    29     bSelected: boolean;
    30     bSelected: boolean;
    42 procedure initModule;
    43 procedure initModule;
    43 procedure freeModule;
    44 procedure freeModule;
    44 
    45 
    45 procedure InitWorld;
    46 procedure InitWorld;
    46 procedure DrawWorld(Lag: LongInt);
    47 procedure DrawWorld(Lag: LongInt);
       
    48 procedure DrawWorldStereo(Lag: LongInt; RM: TRenderMode);
    47 procedure AddCaption(s: shortstring; Color: Longword; Group: TCapGroup);
    49 procedure AddCaption(s: shortstring; Color: Longword; Group: TCapGroup);
    48 procedure ShowMission(caption, subcaption, text: ansistring; icon, time : LongInt);
    50 procedure ShowMission(caption, subcaption, text: ansistring; icon, time : LongInt);
    49 procedure HideMission;
    51 procedure HideMission;
    50 procedure ShakeCamera(amount: LongWord);
    52 procedure ShakeCamera(amount: LongWord);
    51 procedure MoveCamera;
    53 procedure MoveCamera;
   526     end
   528     end
   527 end;
   529 end;
   528 
   530 
   529 
   531 
   530 procedure DrawWorld(Lag: LongInt);
   532 procedure DrawWorld(Lag: LongInt);
   531 var i, t: LongInt;
   533 var cc: array[0..3] of GLfloat;
   532     r: TSDL_Rect;
       
   533     tdx, tdy: Double;
       
   534     grp: TCapGroup;
       
   535     s: string[15];
       
   536     highlight: Boolean;
       
   537     offset, offsetX, offsetY, ScreenBottom: LongInt;
       
   538     VertexBuffer: array [0..3] of TVertex2f;
       
   539 begin
   534 begin
   540     if not isPaused then
   535     if not isPaused then
   541     begin
   536     begin
   542         if ZoomValue < zoom then
   537         if ZoomValue < zoom then
   543         begin
   538         begin
   562     //glScalef(1.0, 1.0, 1.0);
   557     //glScalef(1.0, 1.0, 1.0);
   563 
   558 
   564     if not isPaused then
   559     if not isPaused then
   565         MoveCamera;
   560         MoveCamera;
   566 
   561 
       
   562     if not isStereoEnabled then
       
   563     begin
       
   564         glClear(GL_COLOR_BUFFER_BIT);
       
   565         DrawWorldStereo(Lag, rmDefault)
       
   566     end
       
   567     else
       
   568     begin
       
   569         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framel);
       
   570         glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
       
   571         DrawWorldStereo(Lag, rmLeftEye);
       
   572         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framer);
       
   573         glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
       
   574         DrawWorldStereo(0, rmRightEye);
       
   575         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
       
   576         glGetFloatv(GL_COLOR_CLEAR_VALUE, @cc);
       
   577         glClearColor(0, 0, 0, 0);
       
   578         glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
       
   579         glClearColor(cc[0], cc[1], cc[2], cc[3]);
       
   580         SetScale(2.0);
       
   581         glEnable(GL_TEXTURE_2D);
       
   582         glEnable(GL_BLEND);
       
   583         glBlendFunc(GL_ONE, GL_ONE);
       
   584         glBindTexture(GL_TEXTURE_2D, texl);
       
   585         glColor3f(0.0, 1.0, 1.0);
       
   586         glBegin(GL_QUADS);
       
   587             glTexCoord2f(0.0, 0.0);
       
   588             glVertex2d(cScreenWidth / -2, cScreenHeight);
       
   589             glTexCoord2f(1.0, 0.0);
       
   590             glVertex2d(cScreenWidth / 2, cScreenHeight);
       
   591             glTexCoord2f(1.0, 1.0);
       
   592             glVertex2d(cScreenWidth / 2, 0);
       
   593             glTexCoord2f(0.0, 1.0);
       
   594             glVertex2d(cScreenWidth / -2, 0);
       
   595         glEnd();
       
   596         glBindTexture(GL_TEXTURE_2D, texr);
       
   597         glColor3f(1.0, 0.0, 0.0);
       
   598         glBegin(GL_QUADS);
       
   599             glTexCoord2f(0.0, 0.0);
       
   600             glVertex2d(cScreenWidth / -2, cScreenHeight);
       
   601             glTexCoord2f(1.0, 0.0);
       
   602             glVertex2d(cScreenWidth / 2, cScreenHeight);
       
   603             glTexCoord2f(1.0, 1.0);
       
   604             glVertex2d(cScreenWidth / 2, 0);
       
   605             glTexCoord2f(0.0, 1.0);
       
   606             glVertex2d(cScreenWidth / -2, 0);
       
   607         glEnd();
       
   608         glColor3f(1.0, 1.0, 1.0);
       
   609         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
       
   610         SetScale(zoom);
       
   611     end
       
   612 end;
       
   613 
       
   614  const cStereo_Sky     = 0.0750;
       
   615        cStereo_Horizon = 0.0250;
       
   616        cStereo_Water   = 0.0125;
       
   617  var   stereoDepth: GLfloat = 0;
       
   618  
       
   619  procedure ChangeDepth(rm: TRenderMode; d: GLfloat);
       
   620  begin
       
   621      if rm = rmDefault then exit
       
   622      else if rm = rmRightEye then d:= -d;
       
   623      stereoDepth:= stereoDepth + d;
       
   624      glMatrixMode(GL_PROJECTION);
       
   625      glTranslatef(d, 0, 0);
       
   626      glMatrixMode(GL_MODELVIEW)
       
   627  end;
       
   628  
       
   629  procedure ResetDepth(rm: TRenderMode);
       
   630  begin
       
   631      if rm = rmDefault then exit;
       
   632      glMatrixMode(GL_PROJECTION);
       
   633      glTranslatef(-stereoDepth, 0, 0);
       
   634      glMatrixMode(GL_MODELVIEW);
       
   635      stereoDepth:= 0;
       
   636  end;
       
   637  
       
   638  procedure DrawWorldStereo(Lag: LongInt; RM: TRenderMode);
       
   639  var i, t: LongInt;
       
   640      r: TSDL_Rect;
       
   641      tdx, tdy: Double;
       
   642      grp: TCapGroup;
       
   643      s: string[15];
       
   644      highlight: Boolean;
       
   645      offset, offsetX, offsetY, screenBottom: LongInt;
       
   646      scale: GLfloat;
       
   647      VertexBuffer: array [0..3] of TVertex2f;
       
   648  begin
   567     if (cReducedQuality and rqNoBackground) = 0 then
   649     if (cReducedQuality and rqNoBackground) = 0 then
   568     begin
   650     begin
   569         // Offsets relative to camera - spare them to wimpier cpus, no bg or flakes for them anyway
   651         // Offsets relative to camera - spare them to wimpier cpus, no bg or flakes for them anyway
   570         ScreenBottom:= (WorldDy - trunc(cScreenHeight/cScaleFactor) - (cScreenHeight div 2) + cWaterLine);
   652         ScreenBottom:= (WorldDy - trunc(cScreenHeight/cScaleFactor) - (cScreenHeight div 2) + cWaterLine);
   571         offsetY:= 10 * min(0, -145 - ScreenBottom);
   653         offsetY:= 10 * min(0, -145 - ScreenBottom);
   573         HorizontOffset:= SkyOffset;
   655         HorizontOffset:= SkyOffset;
   574         if ScreenBottom > SkyOffset then
   656         if ScreenBottom > SkyOffset then
   575             HorizontOffset:= HorizontOffset + ((ScreenBottom-SkyOffset) div 20);
   657             HorizontOffset:= HorizontOffset + ((ScreenBottom-SkyOffset) div 20);
   576 
   658 
   577         // background
   659         // background
       
   660 ChangeDepth(RM, cStereo_Sky);
   578         DrawRepeated(sprSky, sprSkyL, sprSkyR, (WorldDx + LAND_WIDTH div 2) * 3 div 8, SkyOffset);
   661         DrawRepeated(sprSky, sprSkyL, sprSkyR, (WorldDx + LAND_WIDTH div 2) * 3 div 8, SkyOffset);
       
   662 ChangeDepth(RM, -cStereo_Horizon);
   579         DrawRepeated(sprHorizont, sprHorizontL, sprHorizontR, (WorldDx + LAND_WIDTH div 2) * 3 div 5, HorizontOffset);
   663         DrawRepeated(sprHorizont, sprHorizontL, sprHorizontR, (WorldDx + LAND_WIDTH div 2) * 3 div 5, HorizontOffset);
   580     end;
   664     end;
   581 
   665 
   582     DrawVisualGears(0);
   666     DrawVisualGears(0);
   583     
   667     
   584     if (cReducedQuality and rq2DWater) = 0 then
   668     if (cReducedQuality and rq2DWater) = 0 then
   585     begin
   669     begin
   586         // Waves
   670         // Waves
   587         DrawWater(255, SkyOffset); 
   671         DrawWater(255, SkyOffset); 
       
   672 ChangeDepth(RM, -cStereo_Water);
   588         DrawWaves( 1,  0 - WorldDx div 32, - cWaveHeight + offsetY div 35, 64);
   673         DrawWaves( 1,  0 - WorldDx div 32, - cWaveHeight + offsetY div 35, 64);
       
   674 ChangeDepth(RM, -cStereo_Water);
   589         DrawWaves( -1,  25 + WorldDx div 25, - cWaveHeight + offsetY div 38, 48);
   675         DrawWaves( -1,  25 + WorldDx div 25, - cWaveHeight + offsetY div 38, 48);
       
   676 ChangeDepth(RM, -cStereo_Water);
   590         DrawWaves( 1,  75 - WorldDx div 19, - cWaveHeight + offsetY div 45, 32);
   677         DrawWaves( 1,  75 - WorldDx div 19, - cWaveHeight + offsetY div 45, 32);
       
   678 ChangeDepth(RM, -cStereo_Water);
   591         DrawWaves(-1, 100 + WorldDx div 14, - cWaveHeight + offsetY div 70, 24);
   679         DrawWaves(-1, 100 + WorldDx div 14, - cWaveHeight + offsetY div 70, 24);
       
   680 ResetDepth(RM);
   592     end
   681     end
   593     else
   682     else
   594         DrawWaves(-1, 100, - (cWaveHeight + (cWaveHeight shr 1)), 0);
   683         DrawWaves(-1, 100, - (cWaveHeight + (cWaveHeight shr 1)), 0);
   595 
   684 
   596     DrawLand(WorldDx, WorldDy);
   685     DrawLand(WorldDx, WorldDy);
   626     DrawVisualGears(2);
   715     DrawVisualGears(2);
   627 
   716 
   628     DrawWater(cWaterOpacity, 0);
   717     DrawWater(cWaterOpacity, 0);
   629 
   718 
   630     // Waves
   719     // Waves
       
   720 ChangeDepth(RM, cStereo_Water);
   631     DrawWaves( 1, 25 - WorldDx div 9, - cWaveHeight, 12);
   721     DrawWaves( 1, 25 - WorldDx div 9, - cWaveHeight, 12);
   632 
   722 
   633     if (cReducedQuality and rq2DWater) = 0 then
   723     if (cReducedQuality and rq2DWater) = 0 then
   634     begin
   724     begin
   635         //DrawWater(cWaterOpacity, - offsetY div 40);
   725         //DrawWater(cWaterOpacity, - offsetY div 40);
       
   726 ChangeDepth(RM, cStereo_Water);
   636         DrawWaves(-1, 50 + WorldDx div 6, - cWaveHeight - offsetY div 40, 8);
   727         DrawWaves(-1, 50 + WorldDx div 6, - cWaveHeight - offsetY div 40, 8);
   637         DrawWater(cWaterOpacity, - offsetY div 20);
   728         DrawWater(cWaterOpacity, - offsetY div 20);
       
   729 ChangeDepth(RM, cStereo_Water);
   638         DrawWaves( 1, 75 - WorldDx div 4, - cWaveHeight - offsetY div 20, 2);
   730         DrawWaves( 1, 75 - WorldDx div 4, - cWaveHeight - offsetY div 20, 2);
   639         DrawWater(cWaterOpacity, - offsetY div 10);
   731         DrawWater(cWaterOpacity, - offsetY div 10);
       
   732 ChangeDepth(RM, cStereo_Water);
   640         DrawWaves( -1, 25 + WorldDx div 3, - cWaveHeight - offsetY div 10, 0);
   733         DrawWaves( -1, 25 + WorldDx div 3, - cWaveHeight - offsetY div 10, 0);
       
   734 ResetDepth(RM);
   641     end
   735     end
   642     else
   736     else
   643         DrawWaves(-1, 50, - (cWaveHeight shr 1), 0);
   737         DrawWaves(-1, 50, - (cWaveHeight shr 1), 0);
   644 
   738 
   645 
   739 
   860 offsetX:= 8;
   954 offsetX:= 8;
   861 {$ELSE}
   955 {$ELSE}
   862 offsetX:= 10;
   956 offsetX:= 10;
   863 {$ENDIF}
   957 {$ENDIF}
   864 offsetY:= cOffsetY;
   958 offsetY:= cOffsetY;
       
   959 if (RM = rmDefault) or (RM = rmLeftEye) then
       
   960 begin
   865 inc(Frames);
   961 inc(Frames);
   866 
   962 
   867 if cShowFPS or (GameType = gmtDemo) then inc(CountTicks, Lag);
   963 if cShowFPS or (GameType = gmtDemo) then inc(CountTicks, Lag);
   868 if (GameType = gmtDemo) and (CountTicks >= 1000) then
   964 if (GameType = gmtDemo) and (CountTicks >= 1000) then
   869    begin
   965    begin
   912 
  1008 
   913 if CountTicks >= 1000 then CountTicks:= 0;
  1009 if CountTicks >= 1000 then CountTicks:= 0;
   914 
  1010 
   915 // lag warning (?)
  1011 // lag warning (?)
   916 inc(SoundTimerTicks, Lag);
  1012 inc(SoundTimerTicks, Lag);
       
  1013 end;
       
  1014 
   917 if SoundTimerTicks >= 50 then
  1015 if SoundTimerTicks >= 50 then
   918    begin
  1016    begin
   919    SoundTimerTicks:= 0;
  1017    SoundTimerTicks:= 0;
   920    if cVolumeDelta <> 0 then
  1018    if cVolumeDelta <> 0 then
   921       begin
  1019       begin