gt:                             + (origin_y - r_origin[1]) * vpn[1]
context: /*
   * Main.java
   * Copyright (C) 2003
   *
   * $Id: Main.java,v 1.6 2008-03-02 14:56:23 cawe Exp $
   */
  /*
  Copyright (C) 1997-2001 Id Software, Inc.
  
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.
  
  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 General Public License for more details.
  
  You should have received a copy of the GNU 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.
  
   */
  package jake2.render.fast;
  
  import jake2.Defines;
  import jake2.client.*;
  import jake2.game.*;
  import jake2.qcommon.*;
  import jake2.render.*;
  import jake2.util.*;
  
  import java.nio.FloatBuffer;
  import java.nio.IntBuffer;
  
  import javax.media.nativewindow.util.Dimension;
  
  /**
   * Main
   * 
   * @author cwei
   */
  public abstract class Main extends Base {
  
      public static int[] d_8to24table = new int[256];
  
      int c_visible_lightmaps;
      int c_visible_textures;
  
      int registration_sequence;
  
      // this a hack for function pointer test
      // default disabled
      boolean qglColorTableEXT = false;
      boolean qglActiveTextureARB = false;
      boolean qglPointParameterfEXT = false;
      boolean qglLockArraysEXT = false;
      boolean qwglSwapIntervalEXT = false;
  
      //	=================
      //  abstract methods
      //	=================
      protected abstract void Draw_GetPalette();
  
      abstract void GL_ImageList_f();
      public abstract void GL_ScreenShot_f();
      abstract void GL_SetTexturePalette(int[] palette);
      abstract void GL_Strings_f();
  
      abstract void Mod_Modellist_f();
      abstract mleaf_t Mod_PointInLeaf(float[] point, model_t model);
  
      abstract void GL_SetDefaultState();
  
      abstract void GL_InitImages();
      abstract void Mod_Init(); // Model.java
      abstract void R_InitParticleTexture(); // MIsc.java
      abstract void R_DrawAliasModel(entity_t e); // Mesh.java
      abstract void R_DrawBrushModel(entity_t e); // Surf.java
      abstract void Draw_InitLocal();
      abstract void R_LightPoint(float[] p, float[] color);
      abstract void R_PushDlights();
      abstract void R_MarkLeaves();
      abstract void R_DrawWorld();
      abstract void R_RenderDlights();
      abstract void R_DrawAlphaSurfaces();
  
      abstract void Mod_FreeAll();
  
      abstract void GL_ShutdownImages();
      abstract void GL_Bind(int texnum);
      abstract void GL_TexEnv(int mode);
      abstract void GL_TextureMode(String string);
      abstract void GL_TextureAlphaMode(String string);
      abstract void GL_TextureSolidMode(String string);
      abstract void GL_UpdateSwapInterval();
  
      /*
  	====================================================================
  
  	from gl_rmain.c
  
  	====================================================================
       */
  
      int TEXTURE0 = GL_TEXTURE0;
      int TEXTURE1 = GL_TEXTURE1;
  
      model_t r_worldmodel;
  
      float gldepthmin, gldepthmax;
  
      glconfig_t gl_config = new glconfig_t();
      glstate_t gl_state = new glstate_t();
  
      image_t r_notexture; // use for bad textures
      image_t r_particletexture; // little dot for particles
  
      entity_t currententity;
      model_t currentmodel;
  
      cplane_t frustum[] = { new cplane_t(), new cplane_t(), new cplane_t(), new cplane_t()};
  
      int r_visframecount; // bumped when going to a new PVS
      int r_framecount; // used for dlight push checking
  
      int c_brush_polys, c_alias_polys;
  
      float v_blend[] = { 0, 0, 0, 0 }; // final blending color
  
      //
      //	   view origin
      //
      float[] vup = { 0, 0, 0 };
      float[] vpn = { 0, 0, 0 };
      float[] vright = { 0, 0, 0 };
      float[] r_origin = { 0, 0, 0 };
  
      //float r_world_matrix[] = new float[16];
      FloatBuffer r_world_matrix = Lib.newFloatBuffer(16);
  
      float r_base_world_matrix[] = new float[16];
  
      //
      //	   screen size info
      //
      refdef_t r_newrefdef = new refdef_t();
  
      int r_viewcluster, r_viewcluster2, r_oldviewcluster, r_oldviewcluster2;
  
      cvar_t r_norefresh;
      cvar_t r_drawentities;
      cvar_t r_drawworld;
      cvar_t r_speeds;
      cvar_t r_fullbright;
      cvar_t r_novis;
      cvar_t r_nocull;
      cvar_t r_lerpmodels;
      cvar_t r_lefthand;
  
      cvar_t r_lightlevel;
      // FIXME: This is a HACK to get the client's light level
  
      cvar_t gl_nosubimage;
      cvar_t gl_allow_software;
  
      cvar_t gl_vertex_arrays;
  
      cvar_t gl_particle_min_size;
      cvar_t gl_particle_max_size;
      cvar_t gl_particle_size;
      cvar_t gl_particle_att_a;
      cvar_t gl_particle_att_b;
      cvar_t gl_particle_att_c;
  
      cvar_t gl_ext_swapinterval;
      cvar_t gl_ext_palettedtexture;
      cvar_t gl_ext_multitexture;
      cvar_t gl_ext_pointparameters;
      cvar_t gl_ext_compiled_vertex_array;
  
      cvar_t gl_log;
      cvar_t gl_bitdepth;
      cvar_t gl_drawbuffer;
      cvar_t gl_driver;
      cvar_t gl_lightmap;
      cvar_t gl_shadows;
      cvar_t gl_mode;
      cvar_t gl_dynamic;
      cvar_t gl_monolightmap;
      cvar_t gl_modulate;
      cvar_t gl_nobind;
      cvar_t gl_round_down;
      cvar_t gl_picmip;
      cvar_t gl_skymip;
      cvar_t gl_showtris;
      cvar_t gl_ztrick;
      cvar_t gl_finish;
      cvar_t gl_clear;
      cvar_t gl_cull;
      cvar_t gl_polyblend;
      cvar_t gl_flashblend;
      cvar_t gl_playermip;
      cvar_t gl_saturatelighting;
      cvar_t gl_swapinterval;
      cvar_t gl_texturemode;
      cvar_t gl_texturealphamode;
      cvar_t gl_texturesolidmode;
      cvar_t gl_lockpvs;
  
      cvar_t gl_3dlabs_broken;
  
      cvar_t vid_gamma;
      cvar_t vid_ref;
  
      // ============================================================================
      // to port from gl_rmain.c, ...
      // ============================================================================
  
      /**
       * R_CullBox
       * Returns true if the box is completely outside the frustum
       */
      final boolean R_CullBox(float[] mins, float[] maxs) {
          assert(mins.length == 3 && maxs.length == 3) : "vec3_t bug";
  
          if (r_nocull.value != 0)
              return false;
  
          for (int i = 0; i < 4; i++) {
              if (Math3D.BoxOnPlaneSide(mins, maxs, frustum[i]) == 2)
                  return true;
          }
          return false;
      }
  
      /**
       * R_RotateForEntity
       */
      final void R_RotateForEntity(entity_t e) {
          gl.glTranslatef(e.origin[0], e.origin[1], e.origin[2]);
  
          gl.glRotatef(e.angles[1], 0, 0, 1);
          gl.glRotatef(-e.angles[0], 0, 1, 0);
          gl.glRotatef(-e.angles[2], 1, 0, 0);
      }
  
      /*
  	=============================================================
  
  	   SPRITE MODELS
  
  	=============================================================
       */
  
      // stack variable
      private final float[] point = { 0, 0, 0 };
      /**
       * R_DrawSpriteModel
       */
      void R_DrawSpriteModel(entity_t e) {
          float alpha = 1.0F;
  
          qfiles.dsprframe_t frame;
          qfiles.dsprite_t psprite;
  
          // don't even bother culling, because it's just a single
          // polygon without a surface cache
  
          psprite = (qfiles.dsprite_t) currentmodel.extradata;
  
          e.frame %= psprite.numframes;
  
          frame = psprite.frames[e.frame];
  
          if ((e.flags & Defines.RF_TRANSLUCENT) != 0)
              alpha = e.alpha;
  
          if (alpha != 1.0F)
              gl.glEnable(GL_BLEND);
  
          gl.glColor4f(1, 1, 1, alpha);
  
          GL_Bind(currentmodel.skins[e.frame].texnum);
  
          GL_TexEnv(GL_MODULATE);
  
          if (alpha == 1.0)
              gl.glEnable(GL_ALPHA_TEST);
          else
              gl.glDisable(GL_ALPHA_TEST);
  
          gl.glBegin(GL_QUADS);
  
          gl.glTexCoord2f(0, 1);
          Math3D.VectorMA(e.origin, -frame.origin_y, vup, point);
          Math3D.VectorMA(point, -frame.origin_x, vright, point);
          gl.glVertex3f(point[0], point[1], point[2]);
  
          gl.glTexCoord2f(0, 0);
          Math3D.VectorMA(e.origin, frame.height - frame.origin_y, vup, point);
          Math3D.VectorMA(point, -frame.origin_x, vright, point);
          gl.glVertex3f(point[0], point[1], point[2]);
  
          gl.glTexCoord2f(1, 0);
          Math3D.VectorMA(e.origin, frame.height - frame.origin_y, vup, point);
          Math3D.VectorMA(point, frame.width - frame.origin_x, vright, point);
          gl.glVertex3f(point[0], point[1], point[2]);
  
          gl.glTexCoord2f(1, 1);
          Math3D.VectorMA(e.origin, -frame.origin_y, vup, point);
          Math3D.VectorMA(point, frame.width - frame.origin_x, vright, point);
          gl.glVertex3f(point[0], point[1], point[2]);
  
          gl.glEnd();
  
          gl.glDisable(GL_ALPHA_TEST);
          GL_TexEnv(GL_REPLACE);
  
          if (alpha != 1.0F)
              gl.glDisable(GL_BLEND);
  
          gl.glColor4f(1, 1, 1, 1);
      }
  
      // ==================================================================================
  
      // stack variable
      private final float[] shadelight = { 0, 0, 0 };
      /**
       * R_DrawNullModel
       */
      void R_DrawNullModel() {
          if ((currententity.flags & Defines.RF_FULLBRIGHT) != 0) {
              // cwei wollte blau: shadelight[0] = shadelight[1] = shadelight[2] = 1.0F;
              shadelight[0] = shadelight[1] = shadelight[2] = 0.0F;
              shadelight[2] = 0.8F;
          }
          else {
              R_LightPoint(currententity.origin, shadelight);
          }
  
          gl.glPushMatrix();
          R_RotateForEntity(currententity);
  
          gl.glDisable(GL_TEXTURE_2D);
          gl.glColor3f(shadelight[0], shadelight[1], shadelight[2]);
  
          // this replaces the TRIANGLE_FAN
          //glut.glutWireCube(gl, 20);
  
          gl.glBegin(GL_TRIANGLE_FAN);
          gl.glVertex3f(0, 0, -16);
          int i;
          for (i=0 ; i<=4 ; i++) {
              gl.glVertex3f((float)(16.0f * Math.cos(i * Math.PI / 2)), (float)(16.0f * Math.sin(i * Math.PI / 2)), 0.0f);
          }
          gl.glEnd();
  
          gl.glBegin(GL_TRIANGLE_FAN);
          gl.glVertex3f (0, 0, 16);
          for (i=4 ; i>=0 ; i--) {
              gl.glVertex3f((float)(16.0f * Math.cos(i * Math.PI / 2)), (float)(16.0f * Math.sin(i * Math.PI / 2)), 0.0f);
          }
          gl.glEnd();
  
  
          gl.glColor3f(1, 1, 1);
          gl.glPopMatrix();
          gl.glEnable(GL_TEXTURE_2D);
      }
  
      /**
       * R_DrawEntitiesOnList
       */
      void R_DrawEntitiesOnList() {
          if (r_drawentities.value == 0.0f)
              return;
  
          // draw non-transparent first
          int i;
          for (i = 0; i < r_newrefdef.num_entities; i++) {
              currententity = r_newrefdef.entities[i];
              if ((currententity.flags & Defines.RF_TRANSLUCENT) != 0)
                  continue; // solid
  
              if ((currententity.flags & Defines.RF_BEAM) != 0) {
                  R_DrawBeam(currententity);
              }
              else {
                  currentmodel = currententity.model;
                  if (currentmodel == null) {
                      R_DrawNullModel();
                      continue;
                  }
                  switch (currentmodel.type) {
                  case mod_alias :
                      R_DrawAliasModel(currententity);
                      break;
                  case mod_brush :
                      R_DrawBrushModel(currententity);
                      break;
                  case mod_sprite :
                      R_DrawSpriteModel(currententity);
                      break;
                  default :
                      Com.Error(Defines.ERR_DROP, "Bad modeltype");
                      break;
                  }
              }
          }
          // draw transparent entities
          // we could sort these if it ever becomes a problem...
          gl.glDepthMask(false); // no z writes
          for (i = 0; i < r_newrefdef.num_entities; i++) {
              currententity = r_newrefdef.entities[i];
              if ((currententity.flags & Defines.RF_TRANSLUCENT) == 0)
                  continue; // solid
  
              if ((currententity.flags & Defines.RF_BEAM) != 0) {
                  R_DrawBeam(currententity);
              }
              else {
                  currentmodel = currententity.model;
  
                  if (currentmodel == null) {
                      R_DrawNullModel();
                      continue;
                  }
                  switch (currentmodel.type) {
                  case mod_alias :
                      R_DrawAliasModel(currententity);
                      break;
                  case mod_brush :
                      R_DrawBrushModel(currententity);
                      break;
                  case mod_sprite :
                      R_DrawSpriteModel(currententity);
                      break;
                  default :
                      Com.Error(Defines.ERR_DROP, "Bad modeltype");
                      break;
                  }
              }
          }
          gl.glDepthMask(true); // back to writing
      }
  
      // stack variable 
      private final float[] up = { 0, 0, 0 };
      private final float[] right = { 0, 0, 0 };
      /**
       * GL_DrawParticles
       */
      void GL_DrawParticles(int num_particles) {
          float origin_x, origin_y, origin_z;
  
          Math3D.VectorScale(vup, 1.5f, up);
          Math3D.VectorScale(vright, 1.5f, right);
  
          GL_Bind(r_particletexture.texnum);
          gl.glDepthMask(false); // no z buffering
          gl.glEnable(GL_BLEND);
          GL_TexEnv(GL_MODULATE);
  
          gl.glBegin(GL_TRIANGLES);
  
          FloatBuffer sourceVertices = particle_t.vertexArray;
          IntBuffer sourceColors = particle_t.colorArray;
          float scale;
          int color;
          for (int j = 0, i = 0; i < num_particles; i++) {
              origin_x = sourceVertices.get(j++);
              origin_y = sourceVertices.get(j++);
              origin_z = sourceVertices.get(j++);
  
              // hack a scale up to keep particles from disapearing
              scale =
                      (origin_x - r_origin[0]) * vpn[0]
  