File GFX.C:
     static int nmatrix = 0;^J     static int   ndisplist = 0;^J     static int npixel = 0;^J     static int    nrectfillpixel = 0;^J     static int    ntexel = 0;^J     ^J     static int     _mtxdepth = 0;^J     static const GLfloat       id[4][4] = {^J       { 1.0, 0.0, 0.0, 0.0 },^J       { 0.0, 1.0, 0.0, 0.0 },^J       { 0.0, 0.0, 1.0, 0.0 },^J       { 0.0, 0.0, 0.0, 1.0 },^J     };^J     ^Jvoid^J_GfxSwapBuffers()^J{^J    if (renderOpenGL)^J       glXSwapBuffers(gfxDisplay, gfxWindow);^J}^J^Jvoid^J_GfxTask(Gfx *glist, int n, int topLevel)^J{^J  int fullsync = 0;^J^J  if (turboVersion)^J      return;^J^J  /* Endless loop; we require that ALL display lists end with^J   * an G_ENDDL command. THIS IS REDUNDANT. The macro in^J   * mbi.h has also been changed...^J   */^J  while (1) {^J    /*fprintf(stderr,"_GfxTask loop [glist= %08x] ",(int)glist);*/^J    /*fprintf (stderr,"%08x %08x\n", *(unsigned long *)glist,*((unsigned long *)glist+1));*/^J    if ((*(unsigned long *)glist & 0x80000000) == 0) {^J      if( gdma[GDMACMD(glist->dma.cmd)] == NULL )^J   fprintf(stderr,"Illegal DMA command %08x\n", (*(unsigned long *)glist) );^J      else^J         gdma[GDMACMD(glist->dma.cmd)](glist, &gfxState);^J      if (glist->dma.cmd == G_DL && (glist->dma.par & 1)) {^J       /* gBranchList() -- break out after call */^J   break;^J      }^J    } else {^J      /* XXX hack to get at the 8 bit cmds w/o decoding the structure */^J      if (glist->dma.cmd <= G_IMMFIRST) {^J    if (glist->dma.cmd == G_ENDDL) {^J        break;^J      }^J      if( gimm[GIMMCMD(glist->dma.cmd)] == NULL )^J    fprintf(stderr,"Illegal IMM command %08x\n", (*(unsigned long *)glist) );^J      else^J       gimm[GIMMCMD(glist->dma.cmd)](glist, &gfxState);^J      } else {^J      /* XXX hack to get at the 8 bit cmds w/o decoding the structure */^J      if( grdp[GRDPCMD(0x100 + glist->dma.cmd)] == NULL )^J   fprintf(stderr,"Illegal IMM command %08x\n", (*(unsigned long *)glist) );^J      else^J       grdp[GRDPCMD(0x100 + glist->dma.cmd)](glist, &gfxState);^J^J    if(!_emRSPWriteBack && ((0xff & glist->dma.cmd) == G_RDPFULLSYNC))^J    {^J         fullsync = 1;^J     }^J^J^J      }^J    }^J    glist++;^J  }^J^J  if (fullsync) {^J    kill(getppid(), SIGDI);^J  }^J}^J^Jstatic void^JgfxDisplayList(Gfx *g, _Gfx_state *gc)^J{^J  ndisplist++;^J^J  _GfxTask((Gfx *)segmentToDram(g->dma.addr), g->dma.len, 0);^J}^J^Jstatic void^JgfxRDPBlock(Gfx *g, _Gfx_state *gc)^J{^J  signed int   cmd;^J  static GLubyte  junkcolor[4] = {0xff, 0xff, 0xff, 0xff};^J  float               minS, minT, maxS, maxT, xh, yh, xl, yl;^J  float sx,sy;^J^J  cmd = *(signed int *)segmentToDram(g->dma.addr) >> 24;^J  cmd &= 0x000000ff;^J^J  if (cmd == G_TEXRECT || cmd == G_TEXRECTFLIP) {^J^J    Gtexrect  *t;^J    Gsettilesize   *ts;^J^J    t = (Gtexrect *)segmentToDram(g->dma.addr);^J#ifdef GL_DEBUGGING^J    fprintf(stderr, "EM: gfxRDPBlock: Found TEXRECT\n");^J#endif^J^J    if (renderOpenGL) {^J     glMatrixMode (GL_TEXTURE);^J    glPushMatrix ();^J      glLoadMatrixf (&id[0][0]);^J/*  glTranslatef (0, 1, 0);         */^J    ts = &(gc->tilesize[t->tile]);^J        sx = gc->tilescale[t->tile].sx;^J       sy = gc->tilescale[t->tile].sy;^J       if( (sx != 0.0) && (sy != 0.0) ) {^J        glScalef (sx/(ts->sh - ts->sl), sy/(ts->th - ts->tl), 1);^J#ifdef GL_DEBUGGING^J        fprintf(stderr, "\tScale (s,t) = (%g,%g)\n",^J                  sx/(ts->sh - ts->sl), sy/(ts->th - ts->tl) );^J#endif^J     } else^J            glScalef (1.0/(ts->sh - ts->sl), 1.0/(ts->th - ts->tl), 1);^J^J^J   glMatrixMode (GL_PROJECTION);^J glPushMatrix ();^J      glLoadMatrixf (&id[0][0]);^J    glOrtho (0, screenWidth, screenHeight, 0, 0, 1);^J^J    glMatrixMode (GL_MODELVIEW);^J  glPushMatrix ();^J      glLoadMatrixf (&id[0][0]);^J^J/*^J      if (gc->rendermodes & G_LIGHTING) {^J     glDisable(GL_LIGHTING);^J     }^J*/^J^J       setcolor(gc, junkcolor);^J^J    xh = (float)t->xh/4.0;^J        xl = (float)t->xl/4.0;^J        yh = (float)t->yh/4.0;^J        yl = (float)t->yl/4.0;^J        minS = (float)t->s/32.0;^J      minT = (float)t->t/32.0;^J      if( getcycletype(gc->othermodesH) == G_CYC_COPY )^J         maxS = (minS + ((xl - xh - 1.0) * ^J                            (float)t->dsdx/4096.0));^J  else^J      maxS = (minS + ((xl - xh - 1.0) * ^J                            (float)t->dsdx/1024.0));^J  maxT = (minT + ((yl - yh - 1.0) * ^J                    (float)t->dtdy/1024.0));^J      ^J      if (cmd == G_TEXRECTFLIP) {^J     glBegin (GL_POLYGON);^J^J       glTexCoord2f(maxS, minT);^J     glVertex2f(xl, yl);^J^J         glTexCoord2f(minS, minT);^J     glVertex2f(xl, yh);^J^J         glTexCoord2f(minS, maxT);^J     glVertex2f(xh, yh);^J^J         glTexCoord2f(maxS, maxT);^J     glVertex2i(xh, yl);^J   glEnd ();^J   } else {^J#ifdef GL_DEBUGGING^J   fprintf(stderr, "\t(xl,yl) = (%g,%g), (xh,yh) = (%g,%g)\n",^J           xl,yl,xh,yh);^J^J       fprintf(stderr, "\t(sl,tl) = (%g,%g), (sh,th) = (%g,%g)\n",^J           maxS,maxT,minS,minT);^J#endif^J^J       glBegin (GL_POLYGON);^J         glTexCoord2f(maxS, maxT);^J     glVertex2f(xl, yl);^J^J         glTexCoord2f(maxS, minT);^J     glVertex2f(xl, yh);^J^J         glTexCoord2f(minS, minT);^J     glVertex2f(xh, yh);^J^J         glTexCoord2f(minS, maxT);^J     glVertex2i(xh, yl);^J   glEnd ();^J   }^J^J/*^J       if ((gc->rendermodes & G_LIGHTING) && _emEnableLighting) {^J      glEnable(GL_LIGHTING);^J      }^J*/^J^J       glMatrixMode (GL_TEXTURE);^J    glPopMatrix ();^J^J     glMatrixMode (GL_PROJECTION);^J glPopMatrix ();^J^J     glMatrixMode (GL_MODELVIEW);^J  glPopMatrix ();^J      }^J#ifdef XXXXX^J   } else {^J#ifdef GL_DEBUGGING^J      fprintf(stderr, "EM: gfxRDPBlock: Found %d\n", cmd);^J#endif^J#endif^J   };^J}^J^Jstatic Gfx texrec[2];^J^Jstatic void^JgfxTexRect1(Gfx *g, _Gfx_state *gc)^J{^J        texrec[0] = *g;^J}^J^Jstatic void^JgfxTexRect2(Gfx *g, _Gfx_state *gc)^J{^J     texrec[1].words.w0 = g->words.w1;^J}^J^Jstatic int      texture_width = 0;^Jstatic int  texture_xoff  = 0;^Jstatic int  texture_yoff  = 0;^J^Jstatic void^JgfxTexRect3(Gfx *g, _Gfx_state *gc)^J{^J  signed int cmd;^J  static GLubyte  junkcolor[4] = {0xff, 0xff, 0xff, 0xff};^J  float               minS, minT, maxS, maxT, xh, yh, xl, yl;^J  float sx,sy;^J^J  texrec[1].words.w1 = g->words.w1;^J^J  cmd = texrec[0].words.w0 >> 24;^J  cmd &= 0x000000ff;^J^J  if (cmd == G_TEXRECT || cmd == G_TEXRECTFLIP) {^J^J    Gtexrect  *t;^J    Gsettilesize   *ts;^J^J    t = (Gtexrect *)texrec;^J^J    if (renderOpenGL) {^J        glMatrixMode (GL_TEXTURE);^J    glPushMatrix ();^J      glLoadMatrixf (&id[0][0]);^J/*  glTranslatef (0, 1, 0);         */^J    ts = &(gc->tilesize[t->tile]);^J        sx = gc->tilescale[t->tile].sx;^J       sy = gc->tilescale[t->tile].sy;^J       if( (sx != 0.0) && (sy != 0.0) ) {^J        glScalef (sx/(ts->sh - ts->sl), sy/(ts->th - ts->tl), 1);^J#ifdef GL_DEBUGGING^J        fprintf(stderr, "\tScale (s,t) = (%g,%g)\n",^J                  sx/(ts->sh - ts->sl), sy/(ts->th - ts->tl) );^J#endif^J     } else^J            glScalef (1.0/(ts->sh - ts->sl), 1.0/(ts->th - ts->tl), 1);^J^J^J   glMatrixMode (GL_PROJECTION);^J glPushMatrix ();^J      glLoadMatrixf (&id[0][0]);^J    glOrtho (0, screenWidth, screenHeight, 0, 0, 1);^J^J    glMatrixMode (GL_MODELVIEW);^J  glPushMatrix ();^J      glLoadMatrixf (&id[0][0]);^J^J  setcolor(gc, junkcolor);^J^J    xh = (float)t->xh/4.0;^J        xl = (float)t->xl/4.0;^J        yh = (float)t->yh/4.0;^J        yl = (float)t->yl/4.0;^J        minS = (float)t->s/32.0;^J      minT = (float)t->t/32.0;^J      if( getcycletype(gc->othermodesH) == G_CYC_COPY )^J         maxS = (minS + ((xl - xh - 1.0) * ^J                            (float)t->dsdx/4096.0));^J  else^J      maxS = (minS + ((xl - xh - 1.0) * ^J                            (float)t->dsdx/1024.0));^J  maxT = (minT + ((yl - yh - 1.0) * ^J                    (float)t->dtdy/1024.0));^J      ^J      if (cmd == G_TEXRECTFLIP) {^J     glBegin (GL_POLYGON);^J^J       glTexCoord2f(maxS, minT);^J     glVertex2f(xl, yl);^J^J         glTexCoord2f(minS, minT);^J     glVertex2f(xl, yh);^J^J         glTexCoord2f(minS, maxT);^J     glVertex2f(xh, yh);^J^J         glTexCoord2f(maxS, maxT);^J     glVertex2i(xh, yl);^J   glEnd ();^J   } else {^J#ifdef PIXEL_FAST_COPY^J        if( getcycletype(gc->othermodesH) == G_CYC_COPY ) {^J       glDisable(GL_BLEND);^J          glDisable(GL_ALPHA_TEST);^J             glPixelTransferi( GL_MAP_COLOR,  FALSE);^J              glPixelStorei( GL_UNPACK_ROW_LENGTH,  texture_width);^J         glPixelStorei( GL_UNPACK_SKIP_ROWS,   texture_yoff );^J         glPixelStorei( GL_UNPACK_SKIP_PIXELS, texture_xoff );^J         glPixelZoom(   1.0, -1.0 );^J^J         glRasterPos2f( xh, yh );^J              glDrawPixels( (int) (maxS-minS+1), (int)(maxT-minT+1), ^J                     GL_RGBA, GL_UNSIGNED_BYTE, texture );^J^J         glPixelStorei( GL_UNPACK_ROW_LENGTH,  0);^J             glPixelStorei( GL_UNPACK_SKIP_ROWS,   0 );^J            glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );^J            glPixelZoom(   1.0, 1.0 );^J#ifdef GL_DEBUGGING^J   fprintf(stderr, "\t(xl,yl) = (%g,%g), tw=%d, txoff=%d, tyoff=%d\n",^J           xl,yl,texture_width,texture_xoff,texture_yoff);^J^J     fprintf(stderr, "\tsize = (%d,%d)\n",^J                 (int)(maxS-minS),(int)(maxT-minT));^J#endif^J^J       };^J#else^J^J   if( getcycletype(gc->othermodesH) == G_CYC_COPY ) {^J     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);^J    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);^J  };^J#ifdef GL_DEBUGGING^J         fprintf(stderr, "\t(xl,yl) = (%g,%g), (xh,yh) = (%g,%g)\n",^J           xl,yl,xh,yh);^J^J       fprintf(stderr, "\t(sl,tl) = (%g,%g), (sh,th) = (%g,%g)\n",^J           maxS,maxT,minS,minT);^J#endif^J^J     glBegin (GL_POLYGON);^J glTexCoord2f(maxS, maxT);^J     glVertex2f(xl, yl);^J^J glTexCoord2f(maxS, minT);^J     glVertex2f(xl, yh);^J^J glTexCoord2f(minS, minT);^J     glVertex2f(xh, yh);^J^J glTexCoord2f(minS, maxT);^J     glVertex2i(xh, yl);^J   glEnd ();^J^J   settexturefilter (gc->othermodesH);^J^J#endif^J };^J^J/*^J      if ((gc->rendermodes & G_LIGHTING) && _emEnableLighting) {^J      glEnable(GL_LIGHTING);^J      }^J*/^J^J       glMatrixMode (GL_TEXTURE);^J    glPopMatrix ();^J^J     glMatrixMode (GL_PROJECTION);^J glPopMatrix ();^J^J     glMatrixMode (GL_MODELVIEW);^J  glPopMatrix ();^J      }^J#ifdef XXXXX^J   } else {^J#ifdef GL_DEBUGGING^J      fprintf(stderr, "EM: gfxRDPHalf2 without texrect command: cmd=%d\n", cmd);^J#endif^J#endif^J   };^J}^J^Jstatic void^JgfxMtx(Gfx *g, _Gfx_state *gc)^J{^J  GLfloat       mf[4][4];^J  int        p;^J^J  nmatrix++;^J^J  ltofMtx((Mtx *)segmentToDram(g->dma.addr), mf);^J  if (renderOpenGL) {^J    p = g->dma.par;^J^J    if (p & G_MTX_PROJECTION) {^J      glMatrixMode (GL_PROJECTION);^J    }^J^J    if (p & G_MTX_PUSH) {^J      if (_mtxdepth++ < MAX_MATRIXDEPTH)^J     glPushMatrix();^J      else^J   fprintf(stderr,^J               "emulate: pushmtx: can't push, empty matrix full\n");^J    }^J^J    if (p & G_MTX_LOAD)^J      glLoadMatrixf(&mf[0][0]);^J    else^J      glMultMatrixf(&mf[0][0]);^J^J    if (p & G_MTX_PROJECTION) {^J      glMatrixMode (GL_MODELVIEW);^J    }^J  }^J}^J^Jstatic void^JgfxPopMtx(Gfx *g, _Gfx_state *gc)^J{^J  int   param;^J^J  param = g->popmtx.param;^J  if (_mtxdepth > 0) {^J    _mtxdepth--;^J    if (renderOpenGL) {^J      if (param == G_MTX_PROJECTION) {^J       glMatrixMode (GL_PROJECTION);^J      }^J      glPopMatrix();^J      if (param == G_MTX_PROJECTION) {^J  glMatrixMode (GL_MODELVIEW);^J      }^J    } else {^J      fprintf(stderr,^J          "emulate: popmtx: can't pop, empty matrix stack\n");^J    }^J  }^J}^J^Jstatic void^JgfxSegment(Gfx *g, _Gfx_state *gc)^J{^J  gc->segmap[g->segment.number/4] = g->segment.base;^J}^J^Jstatic void^JgfxVtx(Gfx *g, _Gfx_state *gc)^J{^J  int       n;^J  Vtx       *d, *s;^J^J  d = &(gc->vtx[g->dma.par & 0xf]);^J  s = (Vtx *)segmentToDram(g->dma.addr);^J^J  n = g->dma.par >> 4;^J  n++;                              /* n is really (n-1) */^J  nvertex += n;                        /* accounting */^J^J  while (n--) {^J    *d++ = *s++;^J  }^J}^J^Jstatic void^JgfxMoveWord(Gfx *g, _Gfx_state *gc)^J{^J  switch ((g->words.w0 >> 8) & 0xff) {^J  case G_MW_SEGMENT:^J            gfxSegment(g, gc);^J            break;^J        default:^J              break;^J  }^J}^J^Jstatic void^JgfxLight(Gfx *g, _Gfx_state *gc)^J{^J  int i;^J  int Number,n;^J  GLfloat light_direction[4], light_ambient[4], light_diffuse[4], light_specular[4];^J  float light_length = 0;^J  ^J  Ambient ambient;^J^J  n = (g->dma.par/24)+1;      /* # of lights */^J^J  for (Number=0; Number<n; Number++)^J    gc->light[Number] = ^J                 *(Light *)segmentToDram((g->dma.addr)+(Number*sizeof(Light)));^J^J  ambient = *(Ambient *)segmentToDram((g->dma.addr)+(n*sizeof(Light)));^J^J  if (renderOpenGL) {^J   for (Number=0; Number<n; Number++) {^J^J    /* Have to convert supplied values into the range GL is happy with.^J       Note that integer version of the glLight command would also require^J       a rescaling since we're using bytes and GL is using ints, so the^J       maximum values would not map to the same intensity */^J^J    for (i=0; i<3; i++)^J      {^J   light_direction[i] = gc->light[Number].l.dir[i];^J      light_length += light_direction[i] * light_direction[i];^J      }^J^J    light_length = fsqrt(light_length);^J    for (i=0; i<3; i++)^J      light_direction[i] /= light_length;^J^J    for (i=0; i<3; i++)^J      {^J  light_diffuse[i] = (float) gc->light[Number].l.col[i] / 255.0;^J        light_specular[i] = 0.0;^J      if (Number)^J          light_ambient[i] = (float) 0.0;^J        else^J          light_ambient[i] = (float) ambient.l.col[i];^J      }^J^J    glLightfv(Number, GL_POSITION, light_direction);    ^J^J    glLightfv(Number, GL_AMBIENT, light_ambient);^J    glLightfv(Number, GL_DIFFUSE, light_diffuse);^J    glLightfv(Number, GL_SPECULAR, light_specular);^J^J    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);^J    glEnable(Number);^J^J   }^J   for (Number=n; Number<8; Number++) {^J    glDisable(Number);^J   }^J  }^J}^J^J#if 0 /* old light routine */^Jstatic void^JgfxLight(Gfx *g, _Gfx_state *gc)^J{^J  int Number;^J  Light tempLight;^J  GLfloat light_direction[4], light_ambient[4], light_diffuse[4], light_specular[4];^J  float light_length = 0;^J  int i;^J^J  tempLight = *(Light *)segmentToDram(g->dma.addr);^J  gc->light[tempLight.l.type & 0x07] = tempLight;^J^J  if (renderOpenGL) {^J^J    /* Have to convert supplied values into the range GL is happy with.^J       Note that integer version of the glLight command would also require^J       a rescaling since we're using bytes and GL is using ints, so the^J       maximum values would not map to the same intensity */^J^J    for (i=0; i<3; i++)^J      {^J   light_direction[i] = tempLight.l.dir[i];^J      light_length += light_direction[i] * light_direction[i];^J      }^J^J    light_length = fsqrt(light_length);^J    for (i=0; i<3; i++)^J      light_direction[i] /= light_length;^J^J    light_direction[3] = 0;^J^J    for (i=0; i<4; i++)^J      {^J   light_ambient[i] = (float) tempLight.l.amb[i] / 255.0;^J        light_diffuse[i] = (float) tempLight.l.diff[i] / 255.0;^J       light_specular[i] = (float) tempLight.l.spec[i] / 255.0;^J      }^J^J    Number = glLightLookup[tempLight.l.type & 0x07];^J^J    glLightfv(Number, GL_POSITION, light_direction);    ^J^J    glLightfv(Number, GL_AMBIENT, light_ambient);^J    glLightfv(Number, GL_DIFFUSE, light_diffuse);^J    glLightfv(Number, GL_SPECULAR, light_specular);^J^J    /* The below isn't really doing what we want it to.  The microcode^J       will be able to have one/two sided capability per light, but GL^J       really has it defined as a per object thing - thus either all the^J       lights are one sided or all the lights are two sided whenever a^J       particular object gets sent down the pipe. */^J^J    if ((tempLight.l.type & 8) == G_LIGHT_TWO_SIDED)^J      glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);^J    else^J      glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);^J^J    if ((tempLight.l.type & 128) == G_LIGHT_ON)^J      glEnable(Number);^J    else if ((tempLight.l.type & 128) == G_LIGHT_OFF)^J      glDisable(Number);^J    else^J      fprintf(stderr, "Error in light on/off parameter \n");^J  }^J}^J#endif /* 0 */ /* end of old light routine */^J^Jstatic void^JgfxViewPort(Gfx *g, _Gfx_state *gc)^J{^J  gc->vp = *(Vp *)segmentToDram(g->dma.addr);^J^J  if (renderOpenGL) {^J    /* viewport structure has 2 fractional bits in the elements;^J     * this also allows it to do the screen-space sub-pixel packing^J     * for the hardware. So we must divide this factor out for GL.^J     */^J    glViewport((gc->vp.vp.vtrans[0] - gc->vp.vp.vscale[0])/4,^J        (gc->vp.vp.vtrans[1] - gc->vp.vp.vscale[1])/4,^J        gc->vp.vp.vscale[0]/2,^J        gc->vp.vp.vscale[1]/2);^J                     ^J    glDepthRange(0.0, 1.0);^J  }^J}^J^J^Jstatic void^JgfxMoveMem(Gfx *g, _Gfx_state *gc)^J{^J  switch ((g->words.w0 >> 16) & 0xff) {^J   case G_MV_VIEWPORT:^J           gfxViewPort(g, gc);^J           break;^J        case G_MV_L0:^J case G_MV_L1:^J case G_MV_L2:^J case G_MV_L3:^J case G_MV_L4:^J case G_MV_L5:^J case G_MV_L6:^J case G_MV_L7:^J         gfxLight(g, gc);^J              break;^J        default:^J              break;^J  }^J}^J^Jstatic void^JgfxTri1(Gfx *g, _Gfx_state *gc)^J{^J  int        i;^J  int       v[4];^J^J  v[0] = g->tri.tri.v[(g->tri.tri.flag & 0x3)];^J  for (i=0; i<3; i++)^J    v[i+1] = (g->tri.tri.v[i])/10;     /* un-do ucode optimization */^J^J  drawTriangles(gc, v, 1, g->settilesize.tile);^J}^J^Jstatic void^JgfxLine3D(Gfx *g, _Gfx_state *gc)^J{^J  int        i;^J  int       v[3];^J^J  v[0] = g->tri.tri.v[(g->tri.tri.flag & 0x3)];^J  for (i=0; i<2; i++)^J    v[i+1] = (g->tri.tri.v[i])/10;     /* un-do ucode optimization */^J^J  drawLines(gc, v, 1, g->settilesize.tile);^J}^J^Jstatic void^JgfxTexture(Gfx *g, _Gfx_state *gc)^J{^J  Gsettile      tile;^J  float          s, t;^J^J  gc->tileN = g->texture.tile;^J  tile = gc->tile[gc->tileN];^J^J  if (renderOpenGL) {^J    /*^J     * setup texture coord scaling^J     */^J^J    s = (float)g->texture.s / (float)(1<<G_TEXTURE_SCALE_FRAC); ^J    t = (float)g->texture.t / (float)(1<<G_TEXTURE_SCALE_FRAC); ^J^J    glMatrixMode(GL_TEXTURE);^J    glLoadMatrixf(&id[0][0]);^J^J    /*^J     * We want to scale the image^J     */^J^J    /* handle the S10.5 conversion in here too... */^J    s *= (1.0/32.0);^J    t *= (1.0/32.0);^J    ^J    glScalef(s, t, 1.0);^J    glMatrixMode(GL_MODELVIEW);^J  }^J}^J^Jstatic void^JgfxSetGeometryMode(Gfx *m, _Gfx_state *gc)^J{^J  int      mode = m->words.w1;^J^J  gc->rendermodes |= mode;^J^J  if (renderOpenGL) {^J^J/*^J    if ((mode & G_LIGHTING) && _emEnableLighting) {^J      glEnable (GL_LIGHTING);      ^J    }^J*/^J    if (mode & G_SHADING_SMOOTH) {^J    }^J    if (mode & G_ZBUFFER) {^J      glEnable (GL_DEPTH_TEST);      ^J    }^J    if (mode & G_TEXTURE_GEN) {^J    }^J    if (mode & G_TEXTURE_GEN) {^J    }^J    if (mode & G_CULL_BOTH) {^J      glEnable (GL_CULL_FACE);^J      switch (gc->rendermodes & G_CULL_BOTH) {^J      case G_CULL_FRONT:^J  glCullFace (GL_FRONT);^J        break;^J      case G_CULL_BACK:^J       glCullFace (GL_BACK);^J break;^J      case G_CULL_BOTH:^J       glCullFace (GL_FRONT_AND_BACK);^J       break;^J      }^J    }^J  }^J}^J^J/*^J * dummy function to keep emulator happy while rsp scales w^J * to maintain precision in 1/w^J */^J^Jstatic void^JgfxClearGeometryMode(Gfx *m, _Gfx_state *gc)^J{^J  int  mode = m->words.w1;^J^J  gc->rendermodes &= ~mode;^J^J  if (renderOpenGL) {^J/*^J    if (mode & G_LIGHTING) {^J      glDisable (GL_LIGHTING);      ^J    }^J*/^J    if (mode & G_SHADING_SMOOTH) {^J    }^J    if (mode & G_ZBUFFER) {^J      glDisable (GL_DEPTH_TEST);      ^J    }^J    if (mode & G_TEXTURE_GEN) {^J    }^J    if (mode & G_TEXTURE_GEN) {^J    }^J    if (mode & G_CULL_BOTH) {^J      switch (gc->rendermodes & G_CULL_BOTH) {^J      case 0:^J   glDisable (GL_CULL_FACE);^J     break;^J      case G_CULL_FRONT:^J      glCullFace (GL_FRONT);^J        break;^J      case G_CULL_BACK:^J       glCullFace (GL_BACK);^J break;^J      }^J    }^J  }^J}^J^Jstatic void^JgfxSetCImg(Gfx *m, _Gfx_state *gc)^J{^J  gc->cimg = m->setimg;^J  gc->cimg.wd += 1;^J}^J^Jstatic void^JgfxSetMImg(Gfx *m, _Gfx_state *gc)^J{^J  gc->mimg = m->setimg;^J  gc->mimg.wd += 1;^J}^J^Jstatic void^JgfxSetTImg(Gfx *m, _Gfx_state *gc)^J{^J  gc->timg = m->setimg;^J  gc->timg.wd += 1;^J#ifdef GL_DEBUGGING^J      fprintf(stderr, "EM: SetTextureImage addr=0x%08x, width=%d\n", gc->timg.dram, gc->timg.wd);^J#endif^J}^J^Jstatic void^JgfxSetCombine(Gfx *m, _Gfx_state *gc)^J{^J  Gfx     testmodi = gsDPSetCombineMode(G_CC_MODULATEI, G_CC_MODULATEI);^J  Gfx   testmodia = gsDPSetCombineMode(G_CC_MODULATEIA, G_CC_MODULATEIA);^J  Gfx        testprimi = gsDPSetCombineMode(G_CC_MODULATEI_PRIM, G_CC_MODULATEI_PRIM);^J  Gfx        testprimia = gsDPSetCombineMode(G_CC_MODULATEIA_PRIM, G_CC_MODULATEIA_PRIM);^J  Gfx     testdecrgb = gsDPSetCombineMode(G_CC_DECALRGB, G_CC_DECALRGB);^J  Gfx   testdecrgba = gsDPSetCombineMode(G_CC_DECALRGBA, G_CC_DECALRGBA);^J  Gfx        testprim = gsDPSetCombineMode(G_CC_PRIMITIVE, G_CC_PRIMITIVE);^J  Gfx   testshade = gsDPSetCombineMode(G_CC_SHADE, G_CC_SHADE);^J  Gfx  mode;^J^J  gc->combine = m->setcombine;^J^J  if (renderOpenGL) {^J^J    /*^J     * determine texture environment^J     */^J^J    mode.setcombine = gc->combine;^J    if (       mode.words.w0 == testmodi.words.w0 &&^J   mode.words.w1 == testmodi.words.w1) {^J      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);^J      gc->cc = CC_SHADE;^J      gc->ccgltexcomp = 3;^J      enabletexture(1);^J    }^J^J    if (   mode.words.w0 == testmodia.words.w0 &&^J          mode.words.w1 == testmodia.words.w1) {^J      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);^J      gc->cc = CC_SHADE;^J      gc->ccgltexcomp = 4;^J      enabletexture(1);^J    }^J^J    if (  mode.words.w0 == testdecrgb.words.w0 &&^J         mode.words.w1 == testdecrgb.words.w1) {^J      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);^J      gc->cc = CC_NONE;^J      gc->ccgltexcomp = 3;^J      enabletexture(1);^J    }^J^J    if (     mode.words.w0 == testdecrgba.words.w0 &&^J        mode.words.w1 == testdecrgba.words.w1) {^J      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);^J      gc->cc = CC_NONE;^J      gc->ccgltexcomp = 4;^J      enabletexture(1);^J    }^J^J    if (    mode.words.w0 == testprimi.words.w0 &&^J          mode.words.w1 == testprimi.words.w1) {^J      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);^J      gc->cc = CC_PRIMITIVE_MODULATE;^J      gc->ccgltexcomp = 3;^J      enabletexture(1);^J    }^J^J    if (     mode.words.w0 == testprimia.words.w0 &&^J         mode.words.w1 == testprimia.words.w1) {^J      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);^J      gc->cc = CC_PRIMITIVE_MODULATE;^J      gc->ccgltexcomp = 4;^J      enabletexture(1);^J    }^J^J    if (  mode.words.w0 == testprim.words.w0 &&^J     mode.words.w1 == testprim.words.w1) {^J      gc->cc = CC_PRIMITIVE;^J      enabletexture(0);^J    }^J^J    if (  mode.words.w0 == testshade.words.w0 &&^J       mode.words.w1 == testshade.words.w1) {^J      gc->cc = CC_SHADE;^J      enabletexture(0);^J    }^J  }^J}^J^Jstatic void^JgfxSetEnvColor(Gfx *m, _Gfx_state *gc)^J{^J  gc->color.env = m->setcolor.color;^J}^J^Jstatic void^JgfxSetPrimColor(Gfx *m, _Gfx_state *gc)^J{^J  gc->color.prim = m->setcolor.color;^J}^J^Jstatic void^JgfxSetBlendColor(Gfx *m, _Gfx_state *gc)^J{^J  gc->color.blend = m->setcolor.color;^J}^J^Jvoid^JgfxSetFogColor(Gfx *m, _Gfx_state *gc)^J{^J  gc->color.fog = m->setcolor.color;^J}^J^Jstatic void^JgfxSetFillColor(Gfx *m, _Gfx_state *gc)^J{^J  Gsetimg             cimg = gc->cimg;^J^J  gc->color.fill = toC32(cimg.fmt << 3 | cimg.siz,^J                         m->setcolor.color & 0xffff, gc);^J}^J^Jstatic void^JgfxFillRect(Gfx *m, _Gfx_state *gc)^J{^J  static GLubyte   junkcolor[4] = {0xff, 0xff, 0xff, 0xff};^J  GLboolean   texture = GL_FALSE;^J^J  if (renderOpenGL) {^J^J^J      glGetBooleanv(GL_TEXTURE_2D, &texture);^J      glDisable(GL_TEXTURE_2D);^J    glDisable(GL_STENCIL_TEST);^J    glDisable (GL_CULL_FACE);^J    if (gc->cimg.dram == gc->mimg.dram) {^J      glClearDepth(1.0);^J      glClear(GL_DEPTH_BUFFER_BIT);^J^J      nrectfillpixel += screenWidth*screenHeight;^J    }^J    else {^J/*^J      if (gc->rendermodes & G_LIGHTING)^J       glDisable(GL_LIGHTING);^J*/^J^J      setcolor(gc, junkcolor);^J^J      glMatrixMode (GL_PROJECTION);^J      glPushMatrix();^J      glLoadIdentity();^J      glOrtho(0.0, (GLdouble)(screenWidth-1),^J         (GLdouble)(screenHeight-1), 0.0,^J              -1.0, 1.0);^J      glMatrixMode (GL_MODELVIEW);^J      glPushMatrix();^J      glLoadIdentity();^J^J  /*^J      glRecti(m->fillrect.x0, screenHeight - m->fillrect.y0 - 1,^J             m->fillrect.x1, screenHeight - m->fillrect.y1 - 1);^J  */^J#ifdef GL_DEBUGGING^J      fprintf(stderr, "EM: FillRect (%d,%d) to (%d,%d)\n",^J              m->fillrect.x1, m->fillrect.y0,^J               m->fillrect.x0, m->fillrect.y1);^J^J#endif^J      glRecti(m->fillrect.x1, m->fillrect.y0,^J           m->fillrect.x0, m->fillrect.y1);^J^J/*^J      if ((gc->rendermodes & G_LIGHTING) && _emEnableLighting)^J  glEnable(GL_LIGHTING);^J*/^J^J      glMatrixMode (GL_PROJECTION);^J      glPopMatrix();^J^J      glMatrixMode (GL_MODELVIEW);^J      glPopMatrix();^J^J      nrectfillpixel += (m->fillrect.x1 - m->fillrect.x0) *^J    (m->fillrect.y1 - m->fillrect.y0);^J^J    }^J^J      if (texture)^J     glEnable(GL_TEXTURE_2D);^J^J    glEnable(GL_STENCIL_TEST);^J^J    if (gc->rendermodes & G_CULL_BOTH)^J      glEnable (GL_CULL_FACE);^J  }^J}^J^Jstatic void^JgfxSetTile(Gfx *m, _Gfx_state *gc)^J{^J  int               tile;^J^J  tile = m->settile.tile;^J  gc->tile[tile] = m->settile;^J}^J^Jstatic void^JgfxLoadTile(Gfx *m, _Gfx_state *gc)^J{^J#ifdef GL_DEBUGGING^J  fprintf(stderr, "emulator: LoadTile not implemented\n");^J#endif^J}^J^Jstatic void^JgfxLoadBlock(Gfx *m, _Gfx_state *gc)^J{^J#ifdef GL_DEBUGGING^J  fprintf(stderr, "emulator: LoadBlock is empty\n");^J#endif^J}^J^Jstatic void^JgfxSetTileSize(Gfx *m, _Gfx_state *gc)^J{^J  Gsettilesize        *ts;^J^J  ts = &(gc->tilesize[m->settilesize.tile]);^J  *ts = m->settilesize;^J  ts->sl >>= G_TEXTURE_IMAGE_FRAC;^J  ts->sh >>= G_TEXTURE_IMAGE_FRAC;^J  ts->tl >>= G_TEXTURE_IMAGE_FRAC;^J  ts->th >>= G_TEXTURE_IMAGE_FRAC;^J^J  resolveTextureParameters(m->settilesize.tile, gc);^J}^J^Jstatic void^JgfxRDPSetOtherMode(Gfx *m, _Gfx_state *gc)^J{^J}^J^Jstatic short em_tlut[256];^J^Jstatic void^JgfxLoadTLUT(Gfx *m, _Gfx_state *gc)^J{^J  Gsetimg       timg = gc->timg;^J  Gsettile    tile;^J  int            t;^J  int low_indx, hi_indx;^J  extern int main();^J  short *lut;^J^J  gc->tlut = m->loadtlut;^J^J  if (renderOpenGL) {^J      t = m->loadtlut.tile;^J      tile = gc->tile[t];^J^J      low_indx = (m->loadtlut.sl >> 2) + tile.tmem - 256;^J       hi_indx = (m->loadtlut.sh >> 2) + tile.tmem - 256;^J      lut = (short *)segmentToDram(timg.dram);^J^J#ifdef GL_DEBUGGING^J  emPrintf("EM: Got LoadTLUT( start=%d, high=%d, addr=0x%08x, tile=%d, tmem=%d )\n",^J        low_indx, hi_indx, lut, t, tile.tmem );^J#endif^J^J      if( (low_indx<0) || (hi_indx > 255 ) || (hi_indx <= low_indx) )^J          emPrintf("EM: Problem with LoadTLUT( tile=%d(tmem=%d), low=%d, high=%d, addr=0x%08x)\n",^J                  t, tile.tmem, low_indx, hi_indx, lut );^J      else^J          for(t=low_indx; t<=hi_indx; t++)^J     em_tlut[t] = lut[t-low_indx];^J^J#ifdef GL_DEBUGGING^J  emPrintf("EM: LoadTLUT: Got it!!!\n");^J#endif^J^J  };^J^J}^J^Jstatic void^Jcheckblending(int mode)^J{^J  int       testalpha = _SHIFTL(G_BL_1MA, 16, 16);^J^J  int testoff = _SHIFTL(G_BL_A_MEM, 16, 16);^J^J  mode &= 0x00030000;^J^J  if (renderOpenGL) {^J    if (mode == testalpha) {^J      glEnable(GL_BLEND);^J      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);^J    }^J    if (mode == testoff) {^J      glDisable(GL_BLEND);^J    }^J  }^J}^J^Jstatic void^JgfxSetOtherMode_L(Gfx *m, _Gfx_state *gc)^J{^J  unsigned int  mask;^J  unsigned int   mode;^J^J  mask = ((1 << m->setothermodeL.len) - 1) << m->setothermodeL.sft;^J  mode = m->setothermodeL.data & mask;^J  gc->othermodesL = (gc->othermodesL & ~mask) | mode;^J^J  if (renderOpenGL) {^J    if (mask & Z_CMP) {^J      if (mode & Z_CMP)^J        glEnable(GL_DEPTH_TEST);^J      else^J  glDisable(GL_DEPTH_TEST);^J    }^J^J    if (mask & (0x3 << G_MDSFT_ALPHACOMPARE)) {^J      if (((mode & 0x3) << G_MDSFT_ALPHACOMPARE) == G_AC_THRESHOLD) {^J    glAlphaFunc (GL_GEQUAL, (gc->color.blend & 0xff) / 255.0);^J    glEnable (GL_ALPHA_TEST);^J      }^J      else^J        glDisable (GL_ALPHA_TEST);^J    }^J^J    if (mask & (0xffff << G_MDSFT_BLENDER))^J      checkblending (gc->othermodesL);^J  }^J}^J^Jstatic void^JgfxSetOtherMode_H(Gfx *m, _Gfx_state *gc)^J{^J  unsigned int   mask;^J  unsigned int   mode;^J^J  mask = ((1 << m->setothermodeH.len) - 1) << m->setothermodeH.sft;^J  mode = m->setothermodeL.data & mask;^J  gc->othermodesH = (gc->othermodesH & ~mask) | mode;^J^J  if (mask & (0x3 << G_MDSFT_TEXTFILT))^J    settexturefilter (gc->othermodesH);^J}^J^Jstatic void^JgfxEndDisplayList(Gfx *m, _Gfx_state *gc)^J{^J  /* Will never be called because G_ENDDL is trapped in the parser */^J^J  fprintf (stderr, "emulator internal error: gfxEndDisplayList() was "^J         "called.\n");^J}^J^Jstatic void^JgfxSetPrimDepth(Gfx *m, _Gfx_state *gc)^J{^J}^J^Jstatic void^JgfxSetScissor(Gfx *m, _Gfx_state *gc)^J{^J  int       xh, yh, xl, yl;^J^J  if (renderOpenGL) {^J    xh = m->fillrect.x0;^J    yh = m->fillrect.y0;^J    xl = m->fillrect.x1;^J    yl = m->fillrect.y1;^J^J    glScissor (xh, screenHeight - yl, xl - xh, yl - yh);^J    glEnable (GL_SCISSOR_TEST);^J  }^J}^J^Jstatic void^JgfxSetConvert(Gfx *m, _Gfx_state *gc)^J{^J}^J^Jstatic void^JgfxSetKeyR(Gfx *m, _Gfx_state *gc)^J{^J}^J^Jstatic void^JgfxSetKeyGB(Gfx *m, _Gfx_state *gc)^J{^J}^J^Jstatic void^JgfxRDPFullSync(Gfx *m, _Gfx_state *gc)^J{^J}^J^Jstatic void^JgfxRDPTileSync(Gfx *m, _Gfx_state *gc)^J{^J}^J^Jstatic void^JgfxRDPPipeSync(Gfx *m, _Gfx_state *gc)^J{^J}^J^Jstatic void^JgfxRDPLoadSync(Gfx *m, _Gfx_state *gc)^J{^J}^J^Jstatic void^JgfxNoop(Gfx *m, _Gfx_state *gc)^J{^J}^J^Jint cur_tile_offset;^J^Jstatic unsigned long^JtoC32(int pixtype, unsigned short c16, _Gfx_state *gc)^J{^J  unsigned long      c32 = 0;^J  unsigned long       ch;^J^J  if ((pixtype >> 3) == G_IM_FMT_CI) {^J    if (gc->othermodesH & G_TT_IA16) {^J      if( pixtype == ((G_IM_FMT_CI << 3) | G_IM_SIZ_4b) )^J        c32 = em_tlut[c16 + cur_tile_offset];^J      else^J     c32 = em_tlut[c16];^J      if (gc->othermodesH & (1<<G_MDSFT_TEXTLUT)) {^J    pixtype = (G_IM_FMT_IA << 3) | G_IM_SIZ_16b;^J      } else {^J  pixtype = (G_IM_FMT_RGBA << 3) | G_IM_SIZ_16b;^J      }^J      c16 = c32;^J      c32 = 0;^J    } else {^J^J#ifdef GL_DEBUGGING2^J      fprintf (stderr, "emulate: CI texture but TLUT not "^J          "enabled.\n");^J#endif^J^J      return 0;^J    }^J  }^J  switch (pixtype) {^J  case G_IM_FMT_RGBA << 3 | G_IM_SIZ_16b:^J    ch = (c16 & 0xf800) >> 11;^J    c32 |= ((ch << 3) | (ch >> 2)) << 24;^J    ch = (c16 & 0x7c0) >> 6;^J    c32 |= ((ch << 3) | (ch >> 2)) << 16;^J    ch = (c16 & 0x3e) >> 1;^J    c32 |= ((ch << 3) | (ch >> 2)) << 8;^J    c32 |= (c16 & 0x1) ? 0xff : 0;^J    break;^J  case G_IM_FMT_IA << 3 | G_IM_SIZ_16b:^J    ch = (c16 & 0xff00) >> 8;^J    c32 |= ch << 24;^J    c32 |= ch << 16;^J    c32 |= ch << 8;^J    c32 |= c16 & 0xff;^J    break;^J  case G_IM_FMT_IA << 3 | G_IM_SIZ_8b:^J    ch = (c16 & 0xf0) | (c16 >> 4);^J    c32 |= ch << 24;                /* R, G, B */^J    c32 |= ch << 16;^J    c32 |= ch << 8;^J    ch = c16 & 0x0f;^J    c32 |= (ch << 4) | ch;      /* A */^J    break;^J  case G_IM_FMT_I << 3 | G_IM_SIZ_4b: /* 4 */^J    c32 = c16 & 0xf;^J    c32 |= c32 << 4;^J    c32 |= c32 << 8;^J    c32 |= c32 << 16;^J    c32 |= 0xff;^J    break;^J  case G_IM_FMT_IA << 3 | G_IM_SIZ_4b: /* 3/1 */^J    ch = c16 >> 1;^J    ch = (ch << 5) | (ch << 2) | (ch >> 1);^J    c32 |= ch << 24;^J    c32 |= ch << 16;^J    c32 |= ch << 8;^J    c32 |= ~((c16 & 0x1) - 1) & 0xff;^J    break;^J  default:^J#ifdef GL_DEBUGGING2^J    if (!errSurpress)^J    fprintf(stderr,^J            "emulate: toC32: this pixel type (%d,%d) not supported yet\n",^J        pixtype>>3, pixtype&7 );^J#endif^J  break;^J  }^J^J  return (c32);^J}^J^Jstatic void^JltofMtx(Mtx *mfix, GLfloat mf[][4])^J{^J#define NEW_MTX^J#ifdef NEW_MTX^J^J  int i;^J  u32 *data,dati,datf;^J  GLfloat *mfptr;^J^J^J  mfptr = (GLfloat *) &mf[0][0];^J  data  = (u32 *) mfix;^J  for (i=0; i<16; i+=2) {^J    datf = data[8];^J    dati = *(data++);^J    if (dati & 0x80000000)^J      *(mfptr++) = -(float) ((-((dati & 0xffff0000) | ((datf & 0xffff0000)>>16)))&0x7fffffff)/65536.0;^J    else^J      *(mfptr++) = (float) ((dati & 0x7fff0000) | ((datf & 0xffff0000)>>16))/65536.0;^J    if (dati & 0x8000)^J      *(mfptr++) = -(float) ((-(((dati & 0x7fff)<<16) | (datf & 0xffff)))&0x7fffffff)/65536.0;^J    else^J      *(mfptr++) = (float) (((dati & 0x7fff)<<16) | (datf & 0xffff))/65536.0;^J  }^J^J#else /* NEW_MTX */^J^J  int       i, j;^J  for (i=0; i<4; i++)^J    for (j=0; j<4; j++)^J      mf[i][j] = mfix->m[i][j] / (float)0x10000;^J^J#endif /* NEW_MTX */^J}^J^J/*^J * vi[] = blocks of 4 vertex indices^J * 0 = normal^J * 1 = v0^J * 2 = v1^J * 3 = v2^J *^J * n = face count^J */^J^Jstatic void^JdrawTriangles(_Gfx_state *gc, int *vi, int n, int tile)^J{^J  Vtx    *v;^J  float Normal[3];^J  int i, j;^J  float Normal_length, tcoords[2];^J  Gsettilesize        tilesize = gc->tilesize[tile];^J  int           h, w;^J^J/* texture hack */^J  h = tilesize.th - tilesize.tl + 1;^J  w = tilesize.sh - tilesize.sl + 1;^J^J  ^J  /* XXX set prim colors here if CombineMode indicates that */^J  ^J  npoly++;                   /* accounting */^J^J    if (renderOpenGL) {^J      ^J      glBegin(GL_TRIANGLES);^J      while (n--) ^J {^J       if ((gc->rendermodes & G_SHADING_SMOOTH) == 0)^J          {^J       v = &(gc->vtx[*vi++]);^J^J#if 0^J       if ((gc->rendermodes & G_LIGHTING) && _emEnableLighting)^J                {^J               /* This normalization really shouldn't need to be^J                done, but if it isn't then when we convert from^J               ints to floats the resulting numerical inaccuracy^J                     causes GL to spazz on the lighting calculation^J                and create spots where there is a flashing effect */^J^J             Normal_length = 0.0;^J                  for (i=0; i<3; i++)^J             {^J               Normal[i] = (2.0 * (float) (v->v.cn)[i]) / 255.0 - 1.0; ^J                      Normal_length += Normal[i] * Normal[i];^J             }^J           Normal_length = fsqrt(Normal_length);^J                 for (i=0; i<3; i++)^J             Normal[i] /= Normal_length;^J^J               glNormal3fv(Normal);              ^J          }^J           else {^J#endif^J          setcolor(gc, (GLubyte *)&v->v.cn);^J/*^J              }^J*/^J       }^J   else^J            vi++;^J^J     for (j=0; j<3; j++)^J     {^J       v = &(gc->vtx[*vi++]);^J        if (gc->rendermodes & G_SHADING_SMOOTH)^J         {^J#if 0^J                if ((gc->rendermodes & G_LIGHTING) && _emEnableLighting)^J                {^J               /* See comment above for why we are renormalizing here */^J^J                   Normal_length = 0.0;^J                  for (i=0; i<3; i++)^J                     {^J                       Normal[i] = (2.0 * (float) (v->v.cn)[i]) / 255.0 - 1.0; ^J                      Normal_length += Normal[i] * Normal[i];^J                     }^J                   Normal_length = fsqrt(Normal_length);^J                 for (i=0; i<3; i++)^J                     Normal[i] /= Normal_length;^J                 ^J                      glNormal3fv(Normal);^J                }^J           else {^J#endif /* 0 */^J                  setcolor(gc, (GLubyte *)&v->v.cn);^J/*^J              }^J*/^J               }^J^J         /*^J             * factor out tile size, convert from RCP coords to GL:^J        */^J           tcoords[0] = (float)v->v.tc[0]/((float)w);^J            tcoords[1] = (float)v->v.tc[1]/((float)h);^J            glTexCoord2fv(tcoords);^J       glVertex3sv(v->v.ob);^J       }^J }^J      ^J      glEnd();^J    }^J}^J^J^J/*^J * vi[] = blocks of 3 vertex indices^J * 0 = normal^J * 1 = v0^J * 2 = v1^J *^J * n = line count^J */^J^Jstatic void^JdrawLines(_Gfx_state *gc, int *vi, int n, int tile)^J{^J  Vtx        *v;^J  float Normal[3];^J  int i, j;^J  float Normal_length, tcoords[2];^J  Gsettilesize        tilesize = gc->tilesize[tile];^J  int           h, w;^J^J/* texture hack */^J  h = tilesize.th - tilesize.tl + 1;^J  w = tilesize.sh - tilesize.sl + 1;^J^J  ^J  /* XXX set prim colors here if CombineMode indicates that */^J  ^J  npoly++;                   /* accounting */^J^J    if (renderOpenGL) {^J      ^J      glBegin(GL_LINES);^J      while (n--) ^J     {^J       if ((gc->rendermodes & G_SHADING_SMOOTH) == 0)^J          {^J       v = &(gc->vtx[*vi++]);^J^J              if ((gc->rendermodes & G_LIGHTING) && _emEnableLighting)^J                {^J               /* This normalization really shouldn't need to be^J                done, but if it isn't then when we convert from^J               ints to floats the resulting numerical inaccuracy^J                     causes GL to spazz on the lighting calculation^J                and create spots where there is a flashing effect */^J^J             Normal_length = 0.0;^J                  for (i=0; i<3; i++)^J             {^J               Normal[i] = (2.0 * (float) (v->v.cn)[i]) / 255.0 - 1.0; ^J                      Normal_length += Normal[i] * Normal[i];^J             }^J           Normal_length = fsqrt(Normal_length);^J                 for (i=0; i<3; i++)^J             Normal[i] /= Normal_length;^J^J               glNormal3fv(Normal);              ^J          }^J           else {^J          setcolor(gc, (GLubyte *)&v->v.cn);^J          }^J           }^J   else^J            vi++;^J^J     for (j=0; j<2; j++)^J     {^J       v = &(gc->vtx[*vi++]);^J        if (gc->rendermodes & G_SHADING_SMOOTH)^J         {^J               if ((gc->rendermodes & G_LIGHTING) && _emEnableLighting)^J                {^J               /* See comment above for why we are renormalizing here */^J^J                   Normal_length = 0.0;^J                  for (i=0; i<3; i++)^J                     {^J                       Normal[i] = (2.0 * (float) (v->v.cn)[i]) / 255.0 - 1.0; ^J                      Normal_length += Normal[i] * Normal[i];^J                     }^J                   Normal_length = fsqrt(Normal_length);^J                 for (i=0; i<3; i++)^J                     Normal[i] /= Normal_length;^J                 ^J                      glNormal3fv(Normal);^J                }^J           else {^J                  setcolor(gc, (GLubyte *)&v->v.cn);^J                  }^J           }^J^J         /*^J             * factor out tile size, convert from RCP coords to GL:^J        */^J           tcoords[0] = (float)v->v.tc[0]/((float)w);^J            tcoords[1] = (float)v->v.tc[1]/((float)h);^J            glTexCoord2fv(tcoords);^J       glVertex3sv(v->v.ob);^J       }^J }^J      ^J      glEnd();^J    }^J}^J^Jstatic unsigned int^JsegmentToDram(unsigned int addr)^J{^J  return(gfxState.segmap[SEGMENT_NUMBER(addr)] +^J      SEGMENT_OFFSET(addr) + KSEG0_BASE);^J}^J^Jint p2rnd( int x );^J^Jstatic void^JresolveTextureParameters(int t, _Gfx_state *gc)^J{^J  Gsettile   tile = gc->tile[t];^J  Gsettilesize     tilesize = gc->tilesize[t];^J  int              h, w;^J  int            fmt;^J  Gsetimg timg = gc->timg;^J  int old_h, old_w;^J  GLubyte        *texture = NULL;^J^J  if (!_emEnableTexture) {^J    return;^J  }^J^J  h = tilesize.th - tilesize.tl + 1;^J  w = tilesize.sh - tilesize.sl + 1;^J^J  old_h = h;^J  old_w = w;^J^J  ntexel += h*w;^J^J  if (renderOpenGL) {^J^J#ifdef GL_DEBUGGING^J    fprintf(stderr,"EM: ResTexParms: HxW= %d x %d", h, w );^J#endif^J      fmt = gc->tile[t].fmt;^J^J    /*^J     * determine clamp/wrap^J     * can check for proper setting of mask bits here^J     */^J^J    if (tile.masks == 0)^J      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);^J    else^J      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);^J^J    if (tile.maskt == 0)^J      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);^J    else^J      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);^J^J#define RWW_TEX_FUDGE (1)^J#define TEX_BORDER (1)^J    gc->tilescale[t].sx = (1.0);^J    gc->tilescale[t].sy = (1.0);^J    texture_xoff  = TEX_BORDER;^J    texture_yoff  = TEX_BORDER;^J^J^J      /*^J       * Use OpenGL objects to cache textures down in the pipeline texture^J       * memory if the same texture address pointer is used. If you are^J       * animating texture addresses, don't use FixedTextureAddress mode.^J       */^J^J      if (glIsList(timg.dram) && _emFixedTextureAddress) {^J      glCallList(timg.dram);^J          w = p2rnd( w );^J       h = p2rnd( h );^J       gc->tilesize[t].th = tilesize.tl + h - RWW_TEX_FUDGE;^J         gc->tilesize[t].sh = tilesize.sl + w - RWW_TEX_FUDGE;^J         texture_width = w+2*TEX_BORDER;^J     return ;^J      }^J^J      cur_tile_offset = 16*gc->tile[t].palette;^J      texture = textureRDP2GL(fmt << 3 | gc->tile[t].siz, &w, &h,^J       segmentToDram(timg.dram), TEX_BORDER, gc);^J^J      if( texture == NULL ) {^J     fprintf(stderr," Bad Texture!\n");^J            return;^J      };^J^J      gc->tilesize[t].th = tilesize.tl + h - RWW_TEX_FUDGE;^J      gc->tilesize[t].sh = tilesize.sl + w - RWW_TEX_FUDGE;^J      texture_width = w+2*TEX_BORDER;^J^J      if (_emFixedTextureAddress) glNewList(timg.dram, GL_COMPILE_AND_EXECUTE);^J      glTexImage2D(GL_TEXTURE_2D, 0, gc->ccgltexcomp,^J      w+2*TEX_BORDER, h+2*TEX_BORDER, TEX_BORDER,^J   GL_RGBA, GL_UNSIGNED_BYTE, texture);^J      if (_emFixedTextureAddress) glEndList();^J^J#ifdef GL_DEBUGGING^J      fprintf(stderr,", NewTex (%dx%d)  0x%08x = { 0x%02x%02x%02x%02x ... }",^J      w,h, timg.dram, texture[0], texture[1], texture[2], texture[3] );^J    fprintf(stderr,"\n");^J#endif^J^J  }^J}^J^Jint^Jp2rnd( int x )^J{^J  int c;^J^J  for(c=1; c < x; c <<= 1)^J    ;^J^J  return( c );^J}^J^Jstatic GLubyte *^JtextureRDP2GL(int fmtsiz, int *pw, int *ph, unsigned int src, int border, _Gfx_state *gc)^J{^J  int   i;^J  GLubyte       *texture;^J  unsigned short        *sdram = (unsigned short *)src;^J  unsigned char *cdram = (unsigned char  *)src;^J  unsigned long *tex;^J  unsigned long prev;^J  unsigned long rgba;^J  int c;^J  int w, h;^J  int x, y;^J^J  w = *pw;^J  h = *ph;^J^J  w = p2rnd( w );^J  h = p2rnd( h );^J^J  c = (w+2*border) * (h+2*border);^J^J  texture = (GLubyte *)malloc(c * 4 * sizeof(GLubyte));^J  tex = (unsigned long *)texture;^J  tex += border*(w+2*border) + border;^J  switch (fmtsiz & 0x07) {^J  case G_IM_SIZ_4b:^J    for (y=0; y<h; y++, tex += 2*border)^J    for (x=0; x<w; x += 2)^J      if( (x >= *pw) || (y >= *ph) ) {^J        *tex++ = prev;^J        *tex++ = prev;^J      } else {^J        *tex++ = toC32(fmtsiz, *cdram >> 4, gc);^J        prev = toC32(fmtsiz, *cdram & 0x0f, gc);^J        *tex++ = prev;^J        cdram++;^J      };^J    break;^J  case G_IM_SIZ_8b:^J    for (y=0; y<h; y++, tex += 2*border)^J    for (x=0; x<w; x++)^J      if( (x >= *pw) || (y >= *ph) ) {^J        *tex++ = prev;^J      } else {^J      prev = toC32(fmtsiz, *cdram++, gc);^J      *tex++ = prev;^J    }^J    break;^J  case G_IM_SIZ_16b:^J    for (y=0; y<h; y++, tex += 2*border)^J    for (x=0; x<w; x++)^J      if( (x >= *pw) || (y >= *ph) ) {^J#ifdef DEBUG_TEX_PAD^J        fprintf(stderr,"Tex Padding <0x%08x>(%d,%d) = 0x%08x\n",^J                texture, x,y, prev );^J#endif^J        *tex++ = prev;^J      } else {^J      prev = toC32(fmtsiz, *sdram++, gc);^J      *tex++ = prev;^J    }^J    break;^J  case G_IM_SIZ_32b:^J    ^J    for (y=0; y<h; y++, tex += 2*border)^J    for (x=0; x<w; x++)^J      if( (x >= *pw) || (y >= *ph) ) {^J        *tex++ = prev;^J      } else {^J      rgba = *cdram++;^J      rgba = (rgba<<8) + *cdram++;^J      rgba = (rgba<<8) + *cdram++;^J      rgba = (rgba<<8) + *cdram++;^J      prev = rgba;^J      *tex++ = rgba;^J    }^J/*^J    fprintf (stderr, "textureRDP2GL(): 32 bit textures"^J             " are not currently supported.\n");^J*/^J    break;^J  };^J^J  if( border ) {^J    unsigned long *tex2;^J^J    tex = (unsigned long *)texture;^J    tex2 = tex + (w+2) + 1;             /* r2 is ref */^J^J    *tex++ = *tex2;^J#ifdef DEBUG_TEX_PAD^J    fprintf(stderr,"Padding (-1,-1) = 0x%08x\n", *tex2 );^J#endif^J^J    for (x=0; x<w; x++) {^J#ifdef DEBUG_TEX_PAD^J        fprintf(stderr,"Padding (%d,-1) = 0x%08x\n", x, *tex2 );^J#endif^J        *tex++ = *tex2++;^J    };^J^J    *tex++ = tex2[-1];^J#ifdef DEBUG_TEX_PAD^J    fprintf(stderr,"Padding (%d,-1) = 0x%08x\n", x, tex2[-1] );^J#endif^J^J    tex2 += 2;^J    tex2 -= (w+2);              /* Go up one line */^J^J    for (y=0; y<h; y++ ) {^J        *tex = *tex2;^J#ifdef DEBUG_TEX_PAD^J        fprintf(stderr,"Padding (-1,%d) = 0x%08x\n", y, *tex2 );^J#endif^J        tex += w + 1;^J        tex2 += w;^J        *tex++ = tex2[ -1 ];^J#ifdef DEBUG_TEX_PAD^J        fprintf(stderr,"Padding (%d,%d) = 0x%08x\n", w, y, tex2[-1] );^J#endif^J        tex2 += 2;^J    };^J^J    tex2 -= (w+2);              /* Go up one line */^J    *tex++ = *tex2;^J#ifdef DEBUG_TEX_PAD^J    fprintf(stderr,"Padding (-1,%d) = 0x%08x\n", y, *tex2 );^J#endif^J    ^J    for (x=0; x<w; x++) {^J#ifdef DEBUG_TEX_PAD^J        fprintf(stderr,"Padding (%d,%d) = 0x%08x\n", x,y, *tex2 );^J#endif^J        *tex++ = *tex2++;^J    };^J^J#ifdef DEBUG_TEX_PAD^J    fprintf(stderr,"Padding (%d,%d) = 0x%08x\n", x,y, tex2[-1] );^J#endif^J    *tex++ = tex2[-1];^J^J  };^J^J  *ph = h;^J  *pw = w;^J^J  return (texture);^J}^J^J^J/*^J * return size of pixel in bits^J */^J^Jstatic int^JsizeOfPixels(int size)^J{^J  switch (size) {^J  case G_IM_SIZ_4b: return 4;^J  case G_IM_SIZ_8b: return 8;^J  case G_IM_SIZ_16b: return 16;^J  case G_IM_SIZ_32b: return 32;^J  }^J}^J^Jvoid^J_GfxInitialization(void)^J{^J^J  if (renderOpenGL)^J    openWindow(_emWidth, _emHeight, _emSingleBuffer);^J^J  /*^J   * graphics state initialization^J   */^J^J  gfxState.cimg.dram = gfxState.timg.dram = gfxState.mimg.dram = -1;^J  gfxState.rendermodes |= G_SHADING_SMOOTH;^J^J  /*^J   * Stash these away for later use by fillRect().^J   */^J  screenWidth = 320;^J  screenHeight = 240;^J^J  /*^J   * Setup function tables.^J   * Since these might be generated, don't hardwire.^J   */^J  gdma[GDMACMD(G_MTX)] = gfxMtx;^J  gdma[GDMACMD(G_MOVEMEM)] = gfxMoveMem;^J  gdma[GDMACMD(G_VTX)] = gfxVtx;^J  gdma[GDMACMD(G_DL)] = gfxDisplayList;^J/*^J  gdma[GDMACMD(G_RDPBLOCK)] = gfxRDPBlock;^J*/^J^J  gimm[GIMMCMD(G_TRI1)] = gfxTri1;^J  gimm[GIMMCMD(G_LINE3D)] = gfxLine3D;^J  gimm[GIMMCMD(G_POPMTX)] = gfxPopMtx;^J  gimm[GIMMCMD(G_MOVEWORD)] = gfxMoveWord;^J  gimm[GIMMCMD(G_TEXTURE)] = gfxTexture;^J  gimm[GIMMCMD(G_SETOTHERMODE_H)] = gfxSetOtherMode_H;^J  gimm[GIMMCMD(G_SETOTHERMODE_L)] = gfxSetOtherMode_L;^J  gimm[GIMMCMD(G_ENDDL)] = gfxEndDisplayList;^J  gimm[GIMMCMD(G_SETGEOMETRYMODE)] = gfxSetGeometryMode;^J  gimm[GIMMCMD(G_CLEARGEOMETRYMODE)] = gfxClearGeometryMode;^J  gimm[GIMMCMD(G_RDPHALF_1)] = gfxTexRect2;^J  gimm[GIMMCMD(G_RDPHALF_2)] = gfxTexRect3;^J^J^J  grdp[GRDPCMD(G_SETCIMG)] = gfxSetCImg;^J  grdp[GRDPCMD(G_SETZIMG)] = gfxSetMImg;^J  grdp[GRDPCMD(G_SETTIMG)] = gfxSetTImg;^J  grdp[GRDPCMD(G_SETCOMBINE)] = gfxSetCombine;^J  grdp[GRDPCMD(G_SETENVCOLOR)] = gfxSetEnvColor;^J  grdp[GRDPCMD(G_SETPRIMCOLOR)] = gfxSetPrimColor;^J  grdp[GRDPCMD(G_SETBLENDCOLOR)] = gfxSetBlendColor;^J  grdp[GRDPCMD(G_SETFOGCOLOR)] = gfxSetFogColor;^J  grdp[GRDPCMD(G_SETFILLCOLOR)] = gfxSetFillColor;^J  grdp[GRDPCMD(G_FILLRECT)] = gfxFillRect;^J  grdp[GRDPCMD(G_SETTILE)] = gfxSetTile;^J  grdp[GRDPCMD(G_LOADTILE)] = gfxLoadTile;^J  grdp[GRDPCMD(G_LOADBLOCK)] = gfxLoadBlock;^J  grdp[GRDPCMD(G_SETTILESIZE)] = gfxSetTileSize;^J  grdp[GRDPCMD(G_LOADTLUT)] = gfxLoadTLUT;^J  grdp[GRDPCMD(G_RDPSETOTHERMODE)] = gfxRDPSetOtherMode;^J  grdp[GRDPCMD(G_SETPRIMDEPTH)] = gfxSetPrimDepth;^J  grdp[GRDPCMD(G_SETSCISSOR)] = gfxSetScissor;^J  grdp[GRDPCMD(G_SETCONVERT)] = gfxSetConvert;^J  grdp[GRDPCMD(G_SETKEYR)] = gfxSetKeyR;^J  grdp[GRDPCMD(G_SETKEYGB)] = gfxSetKeyGB;^J  grdp[GRDPCMD(G_RDPFULLSYNC)] = gfxRDPFullSync;^J  grdp[GRDPCMD(G_RDPTILESYNC)] = gfxRDPTileSync;^J  grdp[GRDPCMD(G_RDPPIPESYNC)] = gfxRDPPipeSync;^J  grdp[GRDPCMD(G_RDPLOADSYNC)] = gfxRDPLoadSync;^J  grdp[GRDPCMD(G_TEXRECTFLIP)] = gfxTexRect1;^J  grdp[GRDPCMD(G_TEXRECT)] = gfxTexRect1;^J  grdp[GRDPCMD(G_NOOP)] = gfxNoop;^J^J  if (renderOpenGL) {^J    /* gl setup stuff */^J    /* Some of what we set are actually the default values,^J       but this makes it more clear as to what we want */^J^J    GLfloat lmodel_ambient[] = {0.0, 0.0, 0.0, 1.0};^J^J    glMatrixMode(GL_PROJECTION);^J    glLoadIdentity();^J    ^J    glMatrixMode(GL_MODELVIEW);^J    glLoadIdentity();^J^J    glPixelStorei(GL_UNPACK_ALIGNMENT, 8);^J^J    glClearDepth(1.0);^J    glClear(GL_DEPTH_BUFFER_BIT); ^J    glDisable(GL_DEPTH_TEST);^J^J    glEnable(GL_STENCIL_TEST);^J^J    glDisable(GL_CULL_FACE);^J^J    glStencilOp(GL_INCR, GL_INCR, GL_INCR);^J    glStencilFunc(GL_ALWAYS, 0, 0xff);^J^J    glDisable(GL_LIGHTING);^J    glDisable(GL_NORMALIZE); ^J    glDisable(GL_COLOR_MATERIAL);^J    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);^J    glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);^J    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); ^J  }     ^J}^J^Jvoid^J_magnifyScreen(float mag)^J{^J  if (renderOpenGL) {^J    if (mag == 2.0) {^J      static _Gfx_state *gc = &gfxState;^J      GLboolean      texture = GL_FALSE;^J      GLboolean    zbuffer = GL_FALSE;^J      unsigned char        *cbuf;^J^J      cbuf = (unsigned char *)malloc(screenWidth*screenHeight*3);^J      glMatrixMode(GL_TEXTURE);^J      glLoadMatrixf(&id[0][0]);^J      glMatrixMode(GL_MODELVIEW);^J^J      glGetBooleanv(GL_TEXTURE_2D, &texture);^J      glGetBooleanv(GL_DEPTH_TEST, &zbuffer);^J      glEnable(GL_TEXTURE_2D);^J      glDisable(GL_DEPTH_TEST);^J^J      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);^J      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);^J      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);^J      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);^J      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);^J^J      glViewport(0, 0, _emWidth, _emHeight);^J      glMatrixMode (GL_PROJECTION);^J      glPushMatrix();^J      glLoadIdentity();^J      glOrtho(0.0, (GLdouble)(_emWidth-1),^J             0.0, (GLdouble)(_emHeight-1),^J         -1.0, 1.0);^J      glMatrixMode (GL_MODELVIEW);^J      glPushMatrix();^J      glLoadIdentity();^J^J      glReadPixels(257, 0, 66, 258, GL_RGB, GL_UNSIGNED_BYTE,^J                   (GLvoid *)cbuf);^J      glTexImage2D(GL_TEXTURE_2D, 0, 3, 66, 258, 1, GL_RGB,^J                 GL_UNSIGNED_BYTE, cbuf);^J^J/*^J      if (gc->rendermodes & G_LIGHTING)^J    glDisable(GL_LIGHTING);^J*/^J^J      glBegin(GL_POLYGON);^J      glColor3f(1.0, 1.0, 1.0);^J      glTexCoord2f(0, 0);^J      glVertex2i(512, 0);^J      glTexCoord2f(1, 0);^J      glVertex2i(512+128, 0);^J      glTexCoord2f(1, .9375);^J      glVertex2i(512+128, 480);^J      glTexCoord2f(0, .9375);^J      glVertex2i(512, 480);^J      glEnd();^J^J/*^J      if ((gc->rendermodes & G_LIGHTING) && _emEnableLighting)^J  glEnable(GL_LIGHTING);^J*/^J^J      glReadPixels(0, 0, 258, 258, GL_RGB, GL_UNSIGNED_BYTE,^J               (GLvoid *)cbuf);^J      glTexImage2D(GL_TEXTURE_2D, 0, 3, 258, 258, 1, GL_RGB,^J                GL_UNSIGNED_BYTE, cbuf);^J^J/*^J      if (gc->rendermodes & G_LIGHTING)^J    glDisable(GL_LIGHTING);^J*/^J^J      glBegin(GL_POLYGON);^J      glColor3f(1.0, 1.0, 1.0);^J      glTexCoord2f(0, 0);^J      glVertex2i(0, 0);^J      glTexCoord2f(1, 0);^J      glVertex2i(512, 0);^J      glTexCoord2f(1, .9375);^J      glVertex2i(512, 480);^J      glTexCoord2f(0, .9375);^J      glVertex2i(0, 480);^J      glEnd();^J^J/*^J      if ((gc->rendermodes & G_LIGHTING) && _emEnableLighting)^J      glEnable(GL_LIGHTING);^J*/^J^J      free(cbuf);^J^J      glMatrixMode (GL_PROJECTION);^J      glPopMatrix();^J      glMatrixMode (GL_MODELVIEW);^J      glPopMatrix();^J^J    /* viewport structure has 2 fractional bits in the elements;^J     * this also allows it to do the screen-space sub-pixel packing^J     * for the hardware. So we must divide this factor out for GL.^J     */^J      glViewport((gc->vp.vp.vtrans[0] - gc->vp.vp.vscale[0])/4,^J          (gc->vp.vp.vtrans[1] - gc->vp.vp.vscale[1])/4,^J                gc->vp.vp.vscale[0]/2,^J                gc->vp.vp.vscale[1]/2);^J                   ^J      if (!texture)^J    glDisable(GL_TEXTURE_2D);^J      if (zbuffer)^J glEnable(GL_DEPTH_TEST);^J    }^J  }^J}^J^J#define VTXBUG       /* make everything work on VTXs for now */^J^Jvoid^J_reportPerformance(void)^J{^J  int  i;^J^J    if (renderOpenGL) {^J^J#ifdef VTXBUG^J    unsigned char       *stenbuf, *stenp;^J^J    /*^J     * XXX VTX has bug!!!^J     *^J     * reading float stencils return 0^J     * reading unsigned bytes has valid bits in upper 4 bits only^J     */^J^J    stenp = stenbuf = (unsigned char *)malloc(screenWidth*screenHeight*sizeof(unsigned char));^J    glReadPixels(0, 0, screenWidth, screenHeight, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, (GLvoid *)stenp);^J^J    for (i=0; i<screenWidth*screenHeight; i++)^J      npixel += *stenp++;^J    npixel /= 16;^J#else^J    float *stenbuf, *stenp;^J^J    stenp = stenbuf = (float *)malloc(screenWidth*screenHeight*sizeof(float));^J    glReadPixels(0, 0, screenWidth, screenHeight, GL_STENCIL_INDEX, GL_FLOAT, (GLvoid *)stenp);^J    for (i=0; i<screenWidth*screenHeight; i++)^J      npixel += *stenp++;^J#endif^J^J    free(stenbuf);^J^J    glClearStencil(0);^J    glClear(GL_STENCIL_BUFFER_BIT);^J^J    printf("vtx %d poly %d mtx %d dl %d texel %d pix %d rectfill %d\n",^J          nvertex, npoly, nmatrix, ndisplist, ntexel, npixel, nrectfillpixel);^J    nvertex = 0;^J    npoly = 0;^J    nmatrix = 0;^J    ndisplist = 0;^J    nclip = 0;^J    npixel = 0;^J    nrectfillpixel = 0;^J    ntexel = 0;^J  }^J}^J^Jstatic void^Jsetcolor(_Gfx_state *gc, GLubyte *shade)^J{^J    float       color[4];^J^J    color[0] = 1.0;^J    color[1] = 1.0;^J    color[2] = 1.0;^J    color[3] = 1.0;^J    if (renderOpenGL) {^J^J      switch (getcycletype(gc->othermodesH)) {^J^J      case G_CYC_FILL:^J#ifdef GL_DEBUGGING^J       fprintf(stderr,"EM: Using Fill color 0x%08x\n",^J             gc->color.fill );^J#endif^J^J     glColor4ubv((const GLubyte *)&gc->color.fill);^J        break;^J^J      case G_CYC_1CYCLE:^J      case G_CYC_2CYCLE:^J          switch (gc->cc) {^J     case CC_NONE:^J         case CC_SHADE:^J#ifdef GL_DEBUGGING2^J                  fprintf(stderr,"EM: Using junk color\n" );^J#endif^J            glColor4ubv(shade);^J           break;^J        case CC_PRIMITIVE:^J            glColor4ubv((GLubyte *)&gc->color.prim);^J#ifdef GL_DEBUGGING^J                 fprintf(stderr,"EM: Using Prim color 0x%08x\n",^J                     gc->color.prim );^J#endif^J^J             break;^J        case CC_PRIMITIVE_MODULATE:^J#ifdef GL_DEBUGGING^J              fprintf(stderr,"EM: Using Prim Mod color\n" );^J#endif^J                color[0] = shade[0] * ((gc->color.prim >> 24) & 0xff) / 65025.0;^J              color[1] = shade[1] * ((gc->color.prim >> 16) & 0xff) / 65025.0;^J              color[2] = shade[2] * ((gc->color.prim >> 8) & 0xff) / 65025.0;^J               color[3] = shade[3] * ((gc->color.prim >> 0) & 0xff) / 65025.0;^J               glColor4fv(color);^J            break;^J        }^J     break;^J^J      case G_CYC_COPY:^J    glColor4fv(color);^J        if (!errSurpress)^J   fprintf(stderr, "setcolor: don't support copy mode yet\n");^J break;^J      }^J  }^J}^J^Jstatic int^Jgetcycletype(int modeH)^J{^J     return (modeH & (0x3 << G_MDSFT_CYCLETYPE));^J}^J^Jstatic void^Jenabletexture(int on)^J{^J  if (renderOpenGL) {^J    if (on && _emEnableTexture) {^J      glEnable(GL_TEXTURE_2D);^J    }^J    else {^J      glDisable(GL_TEXTURE_2D);^J      return;^J    }^J^J  }^J}^J^Jstatic void^Jsettexturefilter(int modeH)^J{^J  if (renderOpenGL) {^J    if (modeH & (2 << G_MDSFT_TEXTFILT)) {^J      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);^J      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);^J    }^J    else {^J      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);^J      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);^J    }^J  }^J}^J^Jstatic void^JopenWindow(int wd, int ht, int singlebuffer)^J{^J      XVisualInfo *vi;^J      int attributes[20];^J   int *ap;^J      GLXContext context;^J   Colormap cmap;^J        XSetWindowAttributes swa;^J     XEvent event;^J XSizeHints *size_hints;^J^J     gfxDisplay = XOpenDisplay(0);^J ap = attributes;^J^J    *ap++ = GLX_RGBA;^J     *ap++ = GLX_RED_SIZE;   *ap++ = 1;^J    *ap++ = GLX_GREEN_SIZE; *ap++ = 1;^J    *ap++ = GLX_BLUE_SIZE;  *ap++ = 1;^J    *ap++ = GLX_DEPTH_SIZE; *ap++ = 1;^J    *ap++ = GLX_STENCIL_SIZE; *ap++ = 1;^J  if (!singlebuffer)^J            *ap++ = GLX_DOUBLEBUFFER;^J     *ap = None;^J^J vi = glXChooseVisual(gfxDisplay, DefaultScreen(gfxDisplay),^J        attributes);^J     context = glXCreateContext(gfxDisplay, vi, 0, GL_TRUE);^J       cmap = XCreateColormap(gfxDisplay, RootWindow(gfxDisplay, vi->screen),^J                               vi->visual, AllocNone);^J^J      swa.colormap = cmap;^J  swa.border_pixel = 0;^J swa.event_mask = StructureNotifyMask;^J^J       size_hints = XAllocSizeHints();^J       size_hints->flags = USPosition|USSize|PPosition|PSize;^J^J      gfxWindow = XCreateWindow(gfxDisplay,^J                     RootWindow(gfxDisplay, vi->screen), 0, 0, wd, ht,^J                     0, vi->depth, InputOutput, vi->visual,^J                        CWBorderPixel|CWColormap|CWEventMask, &swa);^J^J    /*^J     * Provide hints to the window manager so that the window will be^J      * automatically placed on the display (no user interaction necessary). ^J       */^J   XSetWMNormalHints(gfxDisplay, gfxWindow, size_hints);^J free(size_hints);^J     XMapWindow(gfxDisplay, gfxWindow);^J    XIfEvent(gfxDisplay, &event, waitForNotify, (char *)gfxWindow);^J       glXMakeCurrent(gfxDisplay, gfxWindow, context);^J}^J^Jstatic Bool waitForNotify(Display *d, XEvent *e, char *arg)^J{^J  return(e->type == MapNotify) && (e->xmap.window == (Window)arg);^J}
