home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / glquake_src / view.c < prev    next >
C/C++ Source or Header  |  2000-03-06  |  25KB  |  1,121 lines

  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  12.  
  13. See the GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. */
  20. // view.c -- player eye positioning
  21.  
  22. #include "quakedef.h"
  23. #include "r_local.h"
  24.  
  25. /*
  26.  
  27. The view is allowed to move slightly from it's true position for bobbing,
  28. but if it exceeds 8 pixels linear distance (spherical, not box), the list of
  29. entities sent from the server may not include everything in the pvs, especially
  30. when crossing a water boudnary.
  31.  
  32. */
  33.  
  34. cvar_t    lcd_x = {"lcd_x","0"};
  35. cvar_t    lcd_yaw = {"lcd_yaw","0"};
  36.  
  37. cvar_t  scr_ofsx = {"scr_ofsx","0", false};
  38. cvar_t  scr_ofsy = {"scr_ofsy","0", false};
  39. cvar_t  scr_ofsz = {"scr_ofsz","0", false};
  40.  
  41. cvar_t  cl_rollspeed = {"cl_rollspeed", "200"};
  42. cvar_t  cl_rollangle = {"cl_rollangle", "2.0"};
  43.  
  44. cvar_t  cl_bob = {"cl_bob","0.02", false};
  45. cvar_t  cl_bobcycle = {"cl_bobcycle","0.6", false};
  46. cvar_t  cl_bobup = {"cl_bobup","0.5", false};
  47.  
  48. cvar_t  v_kicktime = {"v_kicktime", "0.5", false};
  49. cvar_t  v_kickroll = {"v_kickroll", "0.6", false};
  50. cvar_t  v_kickpitch = {"v_kickpitch", "0.6", false};
  51.  
  52. cvar_t  v_iyaw_cycle = {"v_iyaw_cycle", "2", false};
  53. cvar_t  v_iroll_cycle = {"v_iroll_cycle", "0.5", false};
  54. cvar_t  v_ipitch_cycle = {"v_ipitch_cycle", "1", false};
  55. cvar_t  v_iyaw_level = {"v_iyaw_level", "0.3", false};
  56. cvar_t  v_iroll_level = {"v_iroll_level", "0.1", false};
  57. cvar_t  v_ipitch_level = {"v_ipitch_level", "0.3", false};
  58.  
  59. cvar_t  v_idlescale = {"v_idlescale", "0", false};
  60.  
  61. cvar_t  crosshair = {"crosshair", "0", true};
  62. cvar_t  cl_crossx = {"cl_crossx", "0", false};
  63. cvar_t  cl_crossy = {"cl_crossy", "0", false};
  64.  
  65. cvar_t  gl_cshiftpercent = {"gl_cshiftpercent", "100", false};
  66.  
  67. float v_dmg_time, v_dmg_roll, v_dmg_pitch;
  68.  
  69. extern  int     in_forward, in_forward2, in_back;
  70.  
  71.  
  72. /*
  73. ===============
  74. V_CalcRoll
  75.  
  76. Used by view and sv_user
  77. ===============
  78. */
  79. vec3_t  forward, right, up;
  80.  
  81. float V_CalcRoll (vec3_t angles, vec3_t velocity)
  82. {
  83.   float sign;
  84.   float side;
  85.   float value;
  86.   
  87.   AngleVectors (angles, forward, right, up);
  88.   side = DotProduct (velocity, right);
  89.   sign = side < 0 ? -1 : 1;
  90.   side = fabs(side);
  91.   
  92.   value = cl_rollangle.value;
  93. //  if (cl.inwater)
  94. //    value *= 6;
  95.  
  96.   if (side < cl_rollspeed.value)
  97.     side = side * value / cl_rollspeed.value;
  98.   else
  99.     side = value;
  100.   
  101.   return side*sign;
  102.   
  103. }
  104.  
  105.  
  106. /*
  107. ===============
  108. V_CalcBob
  109.  
  110. ===============
  111. */
  112. float V_CalcBob (void)
  113. {
  114.   float bob;
  115.   float cycle;
  116.   
  117.   cycle = cl.time - (int)(cl.time/cl_bobcycle.value)*cl_bobcycle.value;
  118.   cycle /= cl_bobcycle.value;
  119.   if (cycle < cl_bobup.value)
  120.     cycle = M_PI * cycle / cl_bobup.value;
  121.   else
  122.     cycle = M_PI + M_PI*(cycle-cl_bobup.value)/(1.0 - cl_bobup.value);
  123.  
  124. // bob is proportional to velocity in the xy plane
  125. // (don't count Z, or jumping messes it up)
  126.  
  127.   bob = sqrt(cl.velocity[0]*cl.velocity[0] + cl.velocity[1]*cl.velocity[1]) * cl_bob.value;
  128. //Con_Printf ("speed: %5.1f\n", Length(cl.velocity));
  129.   bob = bob*0.3 + bob*0.7*sin(cycle);
  130.   if (bob > 4)
  131.     bob = 4;
  132.   else if (bob < -7)
  133.     bob = -7;
  134.   return bob;
  135.   
  136. }
  137.  
  138.  
  139. //=============================================================================
  140.  
  141.  
  142. cvar_t  v_centermove = {"v_centermove", "0.15", false};
  143. cvar_t  v_centerspeed = {"v_centerspeed","500"};
  144.  
  145.  
  146. void V_StartPitchDrift (void)
  147. {
  148. #if 1
  149.   if (cl.laststop == cl.time)
  150.   {
  151.     return;   // something else is keeping it from drifting
  152.   }
  153. #endif
  154.   if (cl.nodrift || !cl.pitchvel)
  155.   {
  156.     cl.pitchvel = v_centerspeed.value;
  157.     cl.nodrift = false;
  158.     cl.driftmove = 0;
  159.   }
  160. }
  161.  
  162. void V_StopPitchDrift (void)
  163. {
  164.   cl.laststop = cl.time;
  165.   cl.nodrift = true;
  166.   cl.pitchvel = 0;
  167. }
  168.  
  169. /*
  170. ===============
  171. V_DriftPitch
  172.  
  173. Moves the client pitch angle towards cl.idealpitch sent by the server.
  174.  
  175. If the user is adjusting pitch manually, either with lookup/lookdown,
  176. mlook and mouse, or klook and keyboard, pitch drifting is constantly stopped.
  177.  
  178. Drifting is enabled when the center view key is hit, mlook is released and
  179. lookspring is non 0, or when 
  180. ===============
  181. */
  182. void V_DriftPitch (void)
  183. {
  184.   float   delta, move;
  185.  
  186.   if (noclip_anglehack || !cl.onground || cls.demoplayback )
  187.   {
  188.     cl.driftmove = 0;
  189.     cl.pitchvel = 0;
  190.     return;
  191.   }
  192.  
  193. // don't count small mouse motion
  194.   if (cl.nodrift)
  195.   {
  196.     if ( fabs(cl.cmd.forwardmove) < cl_forwardspeed.value)
  197.       cl.driftmove = 0;
  198.     else
  199.       cl.driftmove += host_frametime;
  200.   
  201.     if ( cl.driftmove > v_centermove.value)
  202.     {
  203.       V_StartPitchDrift ();
  204.     }
  205.     return;
  206.   }
  207.   
  208.   delta = cl.idealpitch - cl.viewangles[PITCH];
  209.  
  210.   if (!delta)
  211.   {
  212.     cl.pitchvel = 0;
  213.     return;
  214.   }
  215.  
  216.   move = host_frametime * cl.pitchvel;
  217.   cl.pitchvel += host_frametime * v_centerspeed.value;
  218.   
  219. //Con_Printf ("move: %f (%f)\n", move, host_frametime);
  220.  
  221.   if (delta > 0)
  222.   {
  223.     if (move > delta)
  224.     {
  225.       cl.pitchvel = 0;
  226.       move = delta;
  227.     }
  228.     cl.viewangles[PITCH] += move;
  229.   }
  230.   else if (delta < 0)
  231.   {
  232.     if (move > -delta)
  233.     {
  234.       cl.pitchvel = 0;
  235.       move = -delta;
  236.     }
  237.     cl.viewangles[PITCH] -= move;
  238.   }
  239. }
  240.  
  241.  
  242.  
  243.  
  244.  
  245. /*
  246. ============================================================================== 
  247.  
  248.             PALETTE FLASHES 
  249.  
  250. ============================================================================== 
  251. */ 
  252.  
  253.  
  254. cshift_t  cshift_empty = { {130,80,50}, 0 };
  255. cshift_t  cshift_water = { {130,80,50}, 128 };
  256. cshift_t  cshift_slime = { {0,25,5}, 150 };
  257. cshift_t  cshift_lava = { {255,80,0}, 150 };
  258.  
  259. cvar_t    v_gamma = {"gamma", "1", true};
  260.  
  261. byte    gammatable[256];  // palette is sent through this
  262.  
  263. #ifdef  GLQUAKE
  264. byte    ramps[3][256];
  265. float   v_blend[4];   // rgba 0.0 - 1.0
  266. #endif  // GLQUAKE
  267.  
  268. void BuildGammaTable (float g)
  269. {
  270.   register int   i, inf;
  271.   
  272.   if (g == 1.0)
  273.   {
  274.     for (i=0 ; i<256 ; i++)
  275.       gammatable[i] = i;
  276.     return;
  277.   }
  278.   
  279.   for (i=0 ; i<256 ; i++)
  280.   {
  281.     inf = 255 * pow ( (i+0.5)/255.5 , g ) + 0.5;
  282.     if (inf < 0)
  283.       inf = 0;
  284.     if (inf > 255)
  285.       inf = 255;
  286.     gammatable[i] = inf;
  287.   }
  288. }
  289.  
  290. /*
  291. =================
  292. V_CheckGamma
  293. =================
  294. */
  295. qboolean V_CheckGamma (void)
  296. {
  297.   static float oldgammavalue;
  298.   
  299.   if (v_gamma.value == oldgammavalue)
  300.     return false;
  301.   oldgammavalue = v_gamma.value;
  302.   
  303.   BuildGammaTable (v_gamma.value);
  304.   vid.recalc_refdef = 1;        // force a surface cache flush
  305.   
  306.   return true;
  307. }
  308.  
  309.  
  310.  
  311. /*
  312. ===============
  313. V_ParseDamage
  314. ===============
  315. */
  316. void V_ParseDamage (void)
  317. {
  318.   int   armor, blood;
  319.   vec3_t  from;
  320.   int   i;
  321.   vec3_t  forward, right, up;
  322.   entity_t  *ent;
  323.   float side;
  324.   float count;
  325.   
  326.   armor = MSG_ReadByte ();
  327.   blood = MSG_ReadByte ();
  328.   for (i=0 ; i<3 ; i++)
  329.     from[i] = MSG_ReadCoord ();
  330.  
  331.   count = blood*0.5 + armor*0.5;
  332.   if (count < 10)
  333.     count = 10;
  334.  
  335.   cl.faceanimtime = cl.time + 0.2;    // but sbar face into pain frame
  336.  
  337.   cl.cshifts[CSHIFT_DAMAGE].percent += 3*count;
  338.   if (cl.cshifts[CSHIFT_DAMAGE].percent < 0)
  339.     cl.cshifts[CSHIFT_DAMAGE].percent = 0;
  340.   if (cl.cshifts[CSHIFT_DAMAGE].percent > 150)
  341.     cl.cshifts[CSHIFT_DAMAGE].percent = 150;
  342.  
  343.   if (armor > blood)    
  344.   {
  345.     cl.cshifts[CSHIFT_DAMAGE].destcolor[0] = 200;
  346.     cl.cshifts[CSHIFT_DAMAGE].destcolor[1] = 100;
  347.     cl.cshifts[CSHIFT_DAMAGE].destcolor[2] = 100;
  348.   }
  349.   else if (armor)
  350.   {
  351.     cl.cshifts[CSHIFT_DAMAGE].destcolor[0] = 220;
  352.     cl.cshifts[CSHIFT_DAMAGE].destcolor[1] = 50;
  353.     cl.cshifts[CSHIFT_DAMAGE].destcolor[2] = 50;
  354.   }
  355.   else
  356.   {
  357.     cl.cshifts[CSHIFT_DAMAGE].destcolor[0] = 255;
  358.     cl.cshifts[CSHIFT_DAMAGE].destcolor[1] = 0;
  359.     cl.cshifts[CSHIFT_DAMAGE].destcolor[2] = 0;
  360.   }
  361.  
  362. //
  363. // calculate view angle kicks
  364. //
  365.   ent = &cl_entities[cl.viewentity];
  366.   
  367.   VectorSubtract (from, ent->origin, from);
  368.   VectorNormalize (from);
  369.   
  370.   AngleVectors (ent->angles, forward, right, up);
  371.  
  372.   side = DotProduct (from, right);
  373.   v_dmg_roll = count*side*v_kickroll.value;
  374.   
  375.   side = DotProduct (from, forward);
  376.   v_dmg_pitch = count*side*v_kickpitch.value;
  377.  
  378.   v_dmg_time = v_kicktime.value;
  379. }
  380.  
  381.  
  382. /*
  383. ==================
  384. V_cshift_f
  385. ==================
  386. */
  387. void V_cshift_f (void)
  388. {
  389.   cshift_empty.destcolor[0] = atoi(Cmd_Argv(1));
  390.   cshift_empty.destcolor[1] = atoi(Cmd_Argv(2));
  391.   cshift_empty.destcolor[2] = atoi(Cmd_Argv(3));
  392.   cshift_empty.percent = atoi(Cmd_Argv(4));
  393. }
  394.  
  395.  
  396. /*
  397. ==================
  398. V_BonusFlash_f
  399.  
  400. When you run over an item, the server sends this command
  401. ==================
  402. */
  403. void V_BonusFlash_f (void)
  404. {
  405.   cl.cshifts[CSHIFT_BONUS].destcolor[0] = 215;
  406.   cl.cshifts[CSHIFT_BONUS].destcolor[1] = 186;
  407.   cl.cshifts[CSHIFT_BONUS].destcolor[2] = 69;
  408.   cl.cshifts[CSHIFT_BONUS].percent = 50;
  409. }
  410.  
  411. /*
  412. =============
  413. V_SetContentsColor
  414.  
  415. Underwater, lava, etc each has a color shift
  416. =============
  417. */
  418. void V_SetContentsColor (int contents)
  419. {
  420.   switch (contents)
  421.   {
  422.   case CONTENTS_EMPTY:
  423.   case CONTENTS_SOLID:
  424.     cl.cshifts[CSHIFT_CONTENTS] = cshift_empty;
  425.     break;
  426.   case CONTENTS_LAVA:
  427.     cl.cshifts[CSHIFT_CONTENTS] = cshift_lava;
  428.     break;
  429.   case CONTENTS_SLIME:
  430.     cl.cshifts[CSHIFT_CONTENTS] = cshift_slime;
  431.     break;
  432.   default:
  433.     cl.cshifts[CSHIFT_CONTENTS] = cshift_water;
  434.   }
  435. }
  436.  
  437. /*
  438. =============
  439. V_CalcPowerupCshift
  440. =============
  441. */
  442. void V_CalcPowerupCshift (void)
  443. {
  444.   if (cl.items & IT_QUAD)
  445.   {
  446.     cl.cshifts[CSHIFT_POWERUP].destcolor[0] = 0;
  447.     cl.cshifts[CSHIFT_POWERUP].destcolor[1] = 0;
  448.     cl.cshifts[CSHIFT_POWERUP].destcolor[2] = 255;
  449.     cl.cshifts[CSHIFT_POWERUP].percent = 30;
  450.     return;
  451.   }
  452.   else if (cl.items & IT_SUIT)
  453.   {
  454.     cl.cshifts[CSHIFT_POWERUP].destcolor[0] = 0;
  455.     cl.cshifts[CSHIFT_POWERUP].destcolor[1] = 255;
  456.     cl.cshifts[CSHIFT_POWERUP].destcolor[2] = 0;
  457.     cl.cshifts[CSHIFT_POWERUP].percent = 20;
  458.     return;
  459.   }
  460.   else if (cl.items & IT_INVISIBILITY)
  461.   {
  462.     cl.cshifts[CSHIFT_POWERUP].destcolor[0] = 100;
  463.     cl.cshifts[CSHIFT_POWERUP].destcolor[1] = 100;
  464.     cl.cshifts[CSHIFT_POWERUP].destcolor[2] = 100;
  465.     cl.cshifts[CSHIFT_POWERUP].percent = 100;
  466.     return;
  467.   }
  468.   else if (cl.items & IT_INVULNERABILITY)
  469.   {
  470.     cl.cshifts[CSHIFT_POWERUP].destcolor[0] = 255;
  471.     cl.cshifts[CSHIFT_POWERUP].destcolor[1] = 255;
  472.     cl.cshifts[CSHIFT_POWERUP].destcolor[2] = 0;
  473.     cl.cshifts[CSHIFT_POWERUP].percent = 30;
  474.     return;
  475.   }
  476.   else
  477.     cl.cshifts[CSHIFT_POWERUP].percent = 0;
  478. }
  479.  
  480. /*
  481. =============
  482. V_CalcBlend
  483. =============
  484. */
  485. #ifdef  GLQUAKE
  486. void V_CalcBlend (void)
  487. {
  488.   float r, g, b;
  489.   register float a, a2;
  490.   register int   j;
  491.  
  492.   r = 0;
  493.   g = 0;
  494.   b = 0;
  495.   a = 0;
  496.  
  497.   for (j=0 ; j<NUM_CSHIFTS ; j++) 
  498.   {
  499.     if (!gl_cshiftpercent.value)
  500.       continue;
  501.  
  502.     a2 = ((cl.cshifts[j].percent * gl_cshiftpercent.value) / 100.0) / 255.0;
  503.  
  504. //    a2 = cl.cshifts[j].percent/255.0;
  505.     if (!a2)
  506.       continue;
  507.     a = a + a2*(1-a);
  508. //Con_Printf ("j:%i a:%f\n", j, a);
  509.     a2 = a2/a;
  510.     r = r*(1-a2) + cl.cshifts[j].destcolor[0]*a2;
  511.     g = g*(1-a2) + cl.cshifts[j].destcolor[1]*a2;
  512.     b = b*(1-a2) + cl.cshifts[j].destcolor[2]*a2;
  513.   }
  514.  
  515.   v_blend[0] = r/255.0;
  516.   v_blend[1] = g/255.0;
  517.   v_blend[2] = b/255.0;
  518.   v_blend[3] = a;
  519.   if (v_blend[3] > 1)
  520.     v_blend[3] = 1;
  521.   if (v_blend[3] < 0)
  522.     v_blend[3] = 0;
  523. }
  524. #endif
  525.  
  526. /*
  527. =============
  528. V_UpdatePalette
  529. =============
  530. */
  531. #ifdef  GLQUAKE
  532. void V_UpdatePalette (void)
  533. {
  534.   register int   i, j;
  535.   qboolean  new;
  536.   register byte  *basepal, *newpal;
  537.   static byte  pal[768];
  538.   register float r,g,b,a;
  539.   register int   ir, ig, ib, ia;
  540.   qboolean force;
  541.  
  542.   V_CalcPowerupCshift ();
  543.   
  544.   new = false;
  545.   
  546.   for (i=0 ; i<NUM_CSHIFTS ; i++)
  547.   {
  548.     if (cl.cshifts[i].percent != cl.prev_cshifts[i].percent)
  549.     {
  550.       new = true;
  551.       cl.prev_cshifts[i].percent = cl.cshifts[i].percent;
  552.     }
  553.     for (j=0 ; j<3 ; j++)
  554.       if (cl.cshifts[i].destcolor[j] != cl.prev_cshifts[i].destcolor[j])
  555.       {
  556.         new = true;
  557.         cl.prev_cshifts[i].destcolor[j] = cl.cshifts[i].destcolor[j];
  558.       }
  559.   }
  560.   
  561. // drop the damage value
  562.   cl.cshifts[CSHIFT_DAMAGE].percent -= host_frametime*150;
  563.   if (cl.cshifts[CSHIFT_DAMAGE].percent <= 0)
  564.     cl.cshifts[CSHIFT_DAMAGE].percent = 0;
  565.  
  566. // drop the bonus value
  567.   cl.cshifts[CSHIFT_BONUS].percent -= host_frametime*100;
  568.   if (cl.cshifts[CSHIFT_BONUS].percent <= 0)
  569.     cl.cshifts[CSHIFT_BONUS].percent = 0;
  570.  
  571.   force = V_CheckGamma ();
  572.   if (!new && !force)
  573.     return;
  574.   
  575.   V_CalcBlend ();
  576.  
  577.   a = v_blend[3];
  578.   r = 255*v_blend[0]*a;
  579.   g = 255*v_blend[1]*a;
  580.   b = 255*v_blend[2]*a;
  581.  
  582.   a = 1-a;
  583.   for (i=0 ; i<256 ; i++)
  584.   {
  585.     ia = i*a;
  586.     ir = ia + r;
  587.     ig = ia + g;
  588.     ib = ia + b;
  589.     if (ir > 255)
  590.       ir = 255;
  591.     if (ig > 255)
  592.       ig = 255;
  593.     if (ib > 255)
  594.       ib = 255;
  595.  
  596.     ramps[0][i] = gammatable[ir];
  597.     ramps[1][i] = gammatable[ig];
  598.     ramps[2][i] = gammatable[ib];
  599.   }
  600.  
  601.   basepal = host_basepal;
  602.   newpal = pal;
  603.   
  604.   for (i=0 ; i<256 ; i++)
  605.   {
  606.     ir = basepal[0];
  607.     ig = basepal[1];
  608.     ib = basepal[2];
  609.     basepal += 3;
  610.     
  611.     newpal[0] = ramps[0][ir];
  612.     newpal[1] = ramps[1][ig];
  613.     newpal[2] = ramps[2][ib];
  614.     newpal += 3;
  615.   }
  616.   
  617.   //VID_ShiftPalette (pal); 
  618.   VID_SetPalette (pal); 
  619. }
  620. #else // !GLQUAKE
  621. void V_UpdatePalette (void)
  622. {
  623.   int   i, j;
  624.   qboolean  new;
  625.   byte  *basepal, *newpal;
  626.   byte  pal[768];
  627.   int   r,g,b;
  628.   qboolean force;
  629.  
  630.   V_CalcPowerupCshift ();
  631.   
  632.   new = false;
  633.   
  634.   for (i=0 ; i<NUM_CSHIFTS ; i++)
  635.   {
  636.     if (cl.cshifts[i].percent != cl.prev_cshifts[i].percent)
  637.     {
  638.       new = true;
  639.       cl.prev_cshifts[i].percent = cl.cshifts[i].percent;
  640.     }
  641.     for (j=0 ; j<3 ; j++)
  642.       if (cl.cshifts[i].destcolor[j] != cl.prev_cshifts[i].destcolor[j])
  643.       {
  644.         new = true;
  645.         cl.prev_cshifts[i].destcolor[j] = cl.cshifts[i].destcolor[j];
  646.       }
  647.   }
  648.   
  649. // drop the damage value
  650.   cl.cshifts[CSHIFT_DAMAGE].percent -= host_frametime*150;
  651.   if (cl.cshifts[CSHIFT_DAMAGE].percent <= 0)
  652.     cl.cshifts[CSHIFT_DAMAGE].percent = 0;
  653.  
  654. // drop the bonus value
  655.   cl.cshifts[CSHIFT_BONUS].percent -= host_frametime*100;
  656.   if (cl.cshifts[CSHIFT_BONUS].percent <= 0)
  657.     cl.cshifts[CSHIFT_BONUS].percent = 0;
  658.  
  659.   force = V_CheckGamma ();
  660.   if (!new && !force)
  661.     return;
  662.       
  663.   basepal = host_basepal;
  664.   newpal = pal;
  665.   
  666.   for (i=0 ; i<256 ; i++)
  667.   {
  668.     r = basepal[0];
  669.     g = basepal[1];
  670.     b = basepal[2];
  671.     basepal += 3;
  672.   
  673.     for (j=0 ; j<NUM_CSHIFTS ; j++) 
  674.     {
  675.       r += (cl.cshifts[j].percent*(cl.cshifts[j].destcolor[0]-r))>>8;
  676.       g += (cl.cshifts[j].percent*(cl.cshifts[j].destcolor[1]-g))>>8;
  677.       b += (cl.cshifts[j].percent*(cl.cshifts[j].destcolor[2]-b))>>8;
  678.     }
  679.     
  680.     newpal[0] = gammatable[r];
  681.     newpal[1] = gammatable[g];
  682.     newpal[2] = gammatable[b];
  683.     newpal += 3;
  684.   }
  685.  
  686.   VID_ShiftPalette (pal); 
  687. }
  688. #endif  // !GLQUAKE
  689.  
  690.  
  691. /* 
  692. ============================================================================== 
  693.  
  694.             VIEW RENDERING 
  695.  
  696. ============================================================================== 
  697. */ 
  698.  
  699. float angledelta (float a)
  700. {
  701.   a = anglemod(a);
  702.   if (a > 180)
  703.     a -= 360;
  704.   return a;
  705. }
  706.  
  707. /*
  708. ==================
  709. CalcGunAngle
  710. ==================
  711. */
  712. void CalcGunAngle (void)
  713.   float yaw, pitch, move;
  714.   static float oldyaw = 0;
  715.   static float oldpitch = 0;
  716.   
  717.   yaw = r_refdef.viewangles[YAW];
  718.   pitch = -r_refdef.viewangles[PITCH];
  719.  
  720.   yaw = angledelta(yaw - r_refdef.viewangles[YAW]) * 0.4;
  721.   if (yaw > 10)
  722.     yaw = 10;
  723.   if (yaw < -10)
  724.     yaw = -10;
  725.   pitch = angledelta(-pitch - r_refdef.viewangles[PITCH]) * 0.4;
  726.   if (pitch > 10)
  727.     pitch = 10;
  728.   if (pitch < -10)
  729.     pitch = -10;
  730.   move = host_frametime*20;
  731.   if (yaw > oldyaw)
  732.   {
  733.     if (oldyaw + move < yaw)
  734.       yaw = oldyaw + move;
  735.   }
  736.   else
  737.   {
  738.     if (oldyaw - move > yaw)
  739.       yaw = oldyaw - move;
  740.   }
  741.   
  742.   if (pitch > oldpitch)
  743.   {
  744.     if (oldpitch + move < pitch)
  745.       pitch = oldpitch + move;
  746.   }
  747.   else
  748.   {
  749.     if (oldpitch - move > pitch)
  750.       pitch = oldpitch - move;
  751.   }
  752.   
  753.   oldyaw = yaw;
  754.   oldpitch = pitch;
  755.  
  756.   cl.viewent.angles[YAW] = r_refdef.viewangles[YAW] + yaw;
  757.   cl.viewent.angles[PITCH] = - (r_refdef.viewangles[PITCH] + pitch);
  758.  
  759.   cl.viewent.angles[ROLL] -= v_idlescale.value * sin(cl.time*v_iroll_cycle.value) * v_iroll_level.value;
  760.   cl.viewent.angles[PITCH] -= v_idlescale.value * sin(cl.time*v_ipitch_cycle.value) * v_ipitch_level.value;
  761.   cl.viewent.angles[YAW] -= v_idlescale.value * sin(cl.time*v_iyaw_cycle.value) * v_iyaw_level.value;
  762. }
  763.  
  764. /*
  765. ==============
  766. V_BoundOffsets
  767. ==============
  768. */
  769. void V_BoundOffsets (void)
  770. {
  771.   entity_t  *ent;
  772.   
  773.   ent = &cl_entities[cl.viewentity];
  774.  
  775. // absolutely bound refresh reletive to entity clipping hull
  776. // so the view can never be inside a solid wall
  777.  
  778.   if (r_refdef.vieworg[0] < ent->origin[0] - 14)
  779.     r_refdef.vieworg[0] = ent->origin[0] - 14;
  780.   else if (r_refdef.vieworg[0] > ent->origin[0] + 14)
  781.     r_refdef.vieworg[0] = ent->origin[0] + 14;
  782.   if (r_refdef.vieworg[1] < ent->origin[1] - 14)
  783.     r_refdef.vieworg[1] = ent->origin[1] - 14;
  784.   else if (r_refdef.vieworg[1] > ent->origin[1] + 14)
  785.     r_refdef.vieworg[1] = ent->origin[1] + 14;
  786.   if (r_refdef.vieworg[2] < ent->origin[2] - 22)
  787.     r_refdef.vieworg[2] = ent->origin[2] - 22;
  788.   else if (r_refdef.vieworg[2] > ent->origin[2] + 30)
  789.     r_refdef.vieworg[2] = ent->origin[2] + 30;
  790. }
  791.  
  792. /*
  793. ==============
  794. V_AddIdle
  795.  
  796. Idle swaying
  797. ==============
  798. */
  799. void V_AddIdle (void)
  800. {
  801.   r_refdef.viewangles[ROLL] += v_idlescale.value * sin(cl.time*v_iroll_cycle.value) * v_iroll_level.value;
  802.   r_refdef.viewangles[PITCH] += v_idlescale.value * sin(cl.time*v_ipitch_cycle.value) * v_ipitch_level.value;
  803.   r_refdef.viewangles[YAW] += v_idlescale.value * sin(cl.time*v_iyaw_cycle.value) * v_iyaw_level.value;
  804. }
  805.  
  806.  
  807. /*
  808. ==============
  809. V_CalcViewRoll
  810.  
  811. Roll is induced by movement and damage
  812. ==============
  813. */
  814. void V_CalcViewRoll (void)
  815. {
  816.   float   side;
  817.     
  818.   side = V_CalcRoll (cl_entities[cl.viewentity].angles, cl.velocity);
  819.   r_refdef.viewangles[ROLL] += side;
  820.  
  821.   if (v_dmg_time > 0)
  822.   {
  823.     r_refdef.viewangles[ROLL] += v_dmg_time/v_kicktime.value*v_dmg_roll;
  824.     r_refdef.viewangles[PITCH] += v_dmg_time/v_kicktime.value*v_dmg_pitch;
  825.     v_dmg_time -= host_frametime;
  826.   }
  827.  
  828.   if (cl.stats[STAT_HEALTH] <= 0)
  829.   {
  830.     r_refdef.viewangles[ROLL] = 80; // dead view angle
  831.     return;
  832.   }
  833.  
  834. }
  835.  
  836.  
  837. /*
  838. ==================
  839. V_CalcIntermissionRefdef
  840.  
  841. ==================
  842. */
  843. void V_CalcIntermissionRefdef (void)
  844. {
  845.   entity_t  *ent, *view;
  846.   float   old;
  847.  
  848. // ent is the player model (visible when out of body)
  849.   ent = &cl_entities[cl.viewentity];
  850. // view is the weapon model (only visible from inside body)
  851.   view = &cl.viewent;
  852.  
  853.   VectorCopy (ent->origin, r_refdef.vieworg);
  854.   VectorCopy (ent->angles, r_refdef.viewangles);
  855.   view->model = NULL;
  856.  
  857. // allways idle in intermission
  858.   old = v_idlescale.value;
  859.   v_idlescale.value = 1;
  860.   V_AddIdle ();
  861.   v_idlescale.value = old;
  862. }
  863.  
  864. /*
  865. ==================
  866. V_CalcRefdef
  867.  
  868. ==================
  869. */
  870. void V_CalcRefdef (void)
  871. {
  872.   entity_t  *ent, *view;
  873.   int     i;
  874.   vec3_t    forward, right, up;
  875.   vec3_t    angles;
  876.   float   bob;
  877.   static float oldz = 0;
  878.  
  879.   V_DriftPitch ();
  880.  
  881. // ent is the player model (visible when out of body)
  882.   ent = &cl_entities[cl.viewentity];
  883. // view is the weapon model (only visible from inside body)
  884.   view = &cl.viewent;
  885.   
  886.  
  887. // transform the view offset by the model's matrix to get the offset from
  888. // model origin for the view
  889.   ent->angles[YAW] = cl.viewangles[YAW];  // the model should face
  890.                     // the view dir
  891.   ent->angles[PITCH] = -cl.viewangles[PITCH]; // the model should face
  892.                     // the view dir
  893.                     
  894.   
  895.   bob = V_CalcBob ();
  896.   
  897. // refresh position
  898.   VectorCopy (ent->origin, r_refdef.vieworg);
  899.   r_refdef.vieworg[2] += cl.viewheight + bob;
  900.  
  901. // never let it sit exactly on a node line, because a water plane can
  902. // dissapear when viewed with the eye exactly on it.
  903. // the server protocol only specifies to 1/16 pixel, so add 1/32 in each axis
  904.   r_refdef.vieworg[0] += 1.0/32;
  905.   r_refdef.vieworg[1] += 1.0/32;
  906.   r_refdef.vieworg[2] += 1.0/32;
  907.  
  908.   VectorCopy (cl.viewangles, r_refdef.viewangles);
  909.   V_CalcViewRoll ();
  910.   V_AddIdle ();
  911.  
  912. // offsets
  913.   angles[PITCH] = -ent->angles[PITCH];  // because entity pitches are
  914.                       //  actually backward
  915.   angles[YAW] = ent->angles[YAW];
  916.   angles[ROLL] = ent->angles[ROLL];
  917.  
  918.   AngleVectors (angles, forward, right, up);
  919.  
  920.   for (i=0 ; i<3 ; i++)
  921.     r_refdef.vieworg[i] += scr_ofsx.value*forward[i]
  922.       + scr_ofsy.value*right[i]
  923.       + scr_ofsz.value*up[i];
  924.   
  925.   
  926.   V_BoundOffsets ();
  927.     
  928. // set up gun position
  929.   VectorCopy (cl.viewangles, view->angles);
  930.   
  931.   CalcGunAngle ();
  932.  
  933.   VectorCopy (ent->origin, view->origin);
  934.   view->origin[2] += cl.viewheight;
  935.  
  936.   for (i=0 ; i<3 ; i++)
  937.   {
  938.     view->origin[i] += forward[i]*bob*0.4;
  939. //    view->origin[i] += right[i]*bob*0.4;
  940. //    view->origin[i] += up[i]*bob*0.8;
  941.   }
  942.   view->origin[2] += bob;
  943.  
  944. // fudge position around to keep amount of weapon visible
  945. // roughly equal with different FOV
  946.  
  947. #if 0
  948.   if (cl.model_precache[cl.stats[STAT_WEAPON]] && strcmp (cl.model_precache[cl.stats[STAT_WEAPON]]->name,  "progs/v_shot2.mdl"))
  949. #endif
  950.   if (scr_viewsize.value == 110)
  951.     view->origin[2] += 1;
  952.   else if (scr_viewsize.value == 100)
  953.     view->origin[2] += 2;
  954.   else if (scr_viewsize.value == 90)
  955.     view->origin[2] += 1;
  956.   else if (scr_viewsize.value == 80)
  957.     view->origin[2] += 0.5;
  958.  
  959.   view->model = cl.model_precache[cl.stats[STAT_WEAPON]];
  960.   view->frame = cl.stats[STAT_WEAPONFRAME];
  961.   view->colormap = vid.colormap;
  962.  
  963. // set up the refresh position
  964.   VectorAdd (r_refdef.viewangles, cl.punchangle, r_refdef.viewangles);
  965.  
  966. // smooth out stair step ups
  967. if (cl.onground && ent->origin[2] - oldz > 0)
  968. {
  969.   float steptime;
  970.   
  971.   steptime = cl.time - cl.oldtime;
  972.   if (steptime < 0)
  973. //FIXME   I_Error ("steptime < 0");
  974.     steptime = 0;
  975.  
  976.   oldz += steptime * 80;
  977.   if (oldz > ent->origin[2])
  978.     oldz = ent->origin[2];
  979.   if (ent->origin[2] - oldz > 12)
  980.     oldz = ent->origin[2] - 12;
  981.   r_refdef.vieworg[2] += oldz - ent->origin[2];
  982.   view->origin[2] += oldz - ent->origin[2];
  983. }
  984. else
  985.   oldz = ent->origin[2];
  986.  
  987.   if (chase_active.value)
  988.     Chase_Update ();
  989. }
  990.  
  991. /*
  992. ==================
  993. V_RenderView
  994.  
  995. The player's clipping box goes from (-16 -16 -24) to (16 16 32) from
  996. the entity origin, so any view position inside that will be valid
  997. ==================
  998. */
  999. extern vrect_t  scr_vrect;
  1000.  
  1001. void V_RenderView (void)
  1002. {
  1003.   if (con_forcedup)
  1004.     return;
  1005.  
  1006. // don't allow cheats in multiplayer
  1007.   if (cl.maxclients > 1)
  1008.   {
  1009.     Cvar_Set ("scr_ofsx", "0");
  1010.     Cvar_Set ("scr_ofsy", "0");
  1011.     Cvar_Set ("scr_ofsz", "0");
  1012.   }
  1013.  
  1014.   if (cl.intermission)
  1015.   { // intermission / finale rendering
  1016.     V_CalcIntermissionRefdef ();  
  1017.   }
  1018.   else
  1019.   {
  1020.     if (!cl.paused /* && (sv.maxclients > 1 || key_dest == key_game) */ )
  1021.       V_CalcRefdef ();
  1022.   }
  1023.  
  1024.   R_PushDlights ();
  1025.  
  1026.   if (lcd_x.value)
  1027.   {
  1028.     //
  1029.     // render two interleaved views
  1030.     //
  1031.     int   i;
  1032.  
  1033.     vid.rowbytes <<= 1;
  1034.     vid.aspect *= 0.5;
  1035.  
  1036.     r_refdef.viewangles[YAW] -= lcd_yaw.value;
  1037.     for (i=0 ; i<3 ; i++)
  1038.       r_refdef.vieworg[i] -= right[i]*lcd_x.value;
  1039.     R_RenderView ();
  1040.  
  1041.     vid.buffer += vid.rowbytes>>1;
  1042.  
  1043.     R_PushDlights ();
  1044.  
  1045.     r_refdef.viewangles[YAW] += lcd_yaw.value*2;
  1046.     for (i=0 ; i<3 ; i++)
  1047.       r_refdef.vieworg[i] += 2*right[i]*lcd_x.value;
  1048.     R_RenderView ();
  1049.  
  1050.     vid.buffer -= vid.rowbytes>>1;
  1051.  
  1052.     r_refdef.vrect.height <<= 1;
  1053.  
  1054.     vid.rowbytes >>= 1;
  1055.     vid.aspect *= 2;
  1056.   }
  1057.   else
  1058.   {
  1059.     R_RenderView ();
  1060.   }
  1061.  
  1062. #ifndef GLQUAKE
  1063.   if (crosshair.value)
  1064.     Draw_Character (scr_vrect.x + scr_vrect.width/2 + cl_crossx.value, 
  1065.       scr_vrect.y + scr_vrect.height/2 + cl_crossy.value, '+');
  1066. #endif
  1067.     
  1068. }
  1069.  
  1070. //============================================================================
  1071.  
  1072. /*
  1073. =============
  1074. V_Init
  1075. =============
  1076. */
  1077. void V_Init (void)
  1078. {
  1079.   Cmd_AddCommand ("v_cshift", V_cshift_f);  
  1080.   Cmd_AddCommand ("bf", V_BonusFlash_f);
  1081.   Cmd_AddCommand ("centerview", V_StartPitchDrift);
  1082.  
  1083.   Cvar_RegisterVariable (&lcd_x);
  1084.   Cvar_RegisterVariable (&lcd_yaw);
  1085.  
  1086.   Cvar_RegisterVariable (&v_centermove);
  1087.   Cvar_RegisterVariable (&v_centerspeed);
  1088.  
  1089.   Cvar_RegisterVariable (&v_iyaw_cycle);
  1090.   Cvar_RegisterVariable (&v_iroll_cycle);
  1091.   Cvar_RegisterVariable (&v_ipitch_cycle);
  1092.   Cvar_RegisterVariable (&v_iyaw_level);
  1093.   Cvar_RegisterVariable (&v_iroll_level);
  1094.   Cvar_RegisterVariable (&v_ipitch_level);
  1095.  
  1096.   Cvar_RegisterVariable (&v_idlescale);
  1097.   Cvar_RegisterVariable (&crosshair);
  1098.   Cvar_RegisterVariable (&cl_crossx);
  1099.   Cvar_RegisterVariable (&cl_crossy);
  1100.   Cvar_RegisterVariable (&gl_cshiftpercent);
  1101.  
  1102.   Cvar_RegisterVariable (&scr_ofsx);
  1103.   Cvar_RegisterVariable (&scr_ofsy);
  1104.   Cvar_RegisterVariable (&scr_ofsz);
  1105.   Cvar_RegisterVariable (&cl_rollspeed);
  1106.   Cvar_RegisterVariable (&cl_rollangle);
  1107.   Cvar_RegisterVariable (&cl_bob);
  1108.   Cvar_RegisterVariable (&cl_bobcycle);
  1109.   Cvar_RegisterVariable (&cl_bobup);
  1110.  
  1111.   Cvar_RegisterVariable (&v_kicktime);
  1112.   Cvar_RegisterVariable (&v_kickroll);
  1113.   Cvar_RegisterVariable (&v_kickpitch); 
  1114.   
  1115.   BuildGammaTable (1.0);  // no gamma yet
  1116.   Cvar_RegisterVariable (&v_gamma);
  1117. }
  1118.  
  1119.  
  1120.