home *** CD-ROM | disk | FTP | other *** search
/ Phenomenon / Phenomenon.iso / quake / utils / mdl / meddle16 / mdl14src / mdl_2d.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-06  |  7.9 KB  |  283 lines

  1. //
  2. // this file by brian martin 1996
  3. // brian@phyast.pitt.edu
  4. //
  5. // this is part of the source for the MedDLe quake model viewer/editor
  6. //
  7. //
  8. #include "mdl.h"
  9.  
  10. GrContext *BitMapContext;
  11.  
  12. draw_bitmap_context(GrContext  *, MDL_model *);
  13.  
  14. int mdl_2d(MDL_model *model, MDL_window *w)
  15. {
  16.     FILE *out;
  17.     int i,j,k,l;
  18.     unsigned p1,p2,p3,p4;
  19.     int ontopof=0;
  20.     int selectedx, selectedy, selectedindex;
  21.     int madechanges=0;
  22.     int loop;
  23.     static int draw_triangles=1,draw_vertices=1;
  24.     int draw_3d=0;
  25.     int goto_3d=0;
  26.     int zoom_factor=2;
  27.     static int paint_mode=0;
  28.     static int show_model=0;
  29.  
  30.     BG_ClearScreen(0);
  31.     // make room for bitmap of skins
  32.     BitMapContext = GrCreateContext(model->skinw,model->skinh,NULL,NULL);
  33.  
  34.     draw_bitmap_context(BitMapContext, model);
  35.  
  36.     BG_MouseStatus();
  37.  
  38.     int update=1;
  39.     int refresh=1;
  40.     int oldx=BG_MouseX,oldy=BG_MouseY,oldr=BG_MouseRight,oldl=BG_MouseLeft;
  41.     BG_MouseShow();
  42.     loop=1;
  43.     char cur_key;
  44.     int selected=0;
  45.     while(loop)
  46.     {
  47.         BG_MouseStatus();
  48.  
  49.         if(kbhit())
  50.         {
  51.  
  52.             if((cur_key=getch())==0x0){
  53.              cur_key=getch();
  54.              //    cur_key=getch();
  55.             switch(cur_key)
  56.             {
  57.                 case ARROW_DOWN:    if(BG_MouseY<(BG_ScreenHeight-1)) {BG_MouseY++; BG_MouseToXY(BG_MouseX,BG_MouseY); BG_MouseStatus();} break;
  58.                 case ARROW_UP:        if(BG_MouseY>0) {BG_MouseY--;BG_MouseToXY(BG_MouseX,BG_MouseY); BG_MouseStatus();} break;
  59.                 case ARROW_LEFT:    if(BG_MouseX>0) {BG_MouseX--;BG_MouseToXY(BG_MouseX,BG_MouseY); BG_MouseStatus();} break;
  60.                 case ARROW_RIGHT:   if(BG_MouseX<(BG_ScreenWidth-1)) {BG_MouseX++; BG_MouseToXY(BG_MouseX,BG_MouseY); BG_MouseStatus();} break;
  61.             }
  62.             }
  63.             else{
  64.             switch(cur_key)
  65.             {
  66.                 case 27:               loop=0; draw_3d=0; break;
  67.                 case '3':            loop=0; draw_3d=1; break;
  68.                 case '1':            loop=0; draw_3d=2; break;
  69.                 case 't':case 'T':  if(draw_triangles==0)draw_triangles=1;
  70.                                     else draw_triangles=0; break;
  71.                 case 'v':case 'V':  if(draw_vertices==0)draw_vertices=1;
  72.                                     else draw_vertices=0; break;
  73.                 case 's':case 'S':
  74.                         BG_Text("Saving",0,0,250,0);
  75.  
  76.                         if((out=fopen(model->filename,"wb"))==NULL)
  77.                         {
  78.                             fprintf(stderr,"can't open mdl file... bye\n");
  79.                             exit(1);
  80.                         }
  81.                         rewind(out);
  82.                         write_mdl(out, model);
  83.                         fclose(out);
  84.                         break;
  85.                 case 'p': case 'P': if(paint_mode==1) paint_mode=0; else paint_mode=1; break;
  86.                 case 'm': case 'M': if(show_model==1) show_model=0; else show_model=1; break;
  87.                 case ',': case '<': if(model->cur_skin==0) model->cur_skin=model->num_skins-1;
  88.                                     else model->cur_skin-=1;
  89.                                     draw_bitmap_context(BitMapContext, model);
  90.                                     break;
  91.                 case '.': case '>': model->cur_skin++;
  92.                                     if(model->cur_skin==model->num_skins) model->cur_skin=0;
  93.                                     draw_bitmap_context(BitMapContext, model);
  94.                                     break;
  95.                 case '\"': case '\'': model->bcolor++;
  96.                             if(model->bcolor>13)model->bcolor=0;
  97.                             draw_bitmap_context(BitMapContext, model);
  98.                             break;
  99.                 case ';': case ':': model->bcolor--;
  100.                             if(model->bcolor<0)model->bcolor=13;
  101.                             draw_bitmap_context(BitMapContext, model);
  102.                             break;
  103.  
  104.                 case '}': case ']': model->tcolor++;
  105.                             if(model->tcolor>13)model->tcolor=0;
  106.                             draw_bitmap_context(BitMapContext, model);
  107.                             break;
  108.                 case '{': case '[': model->tcolor--;
  109.                             if(model->tcolor<0) model->tcolor=13;
  110.                             draw_bitmap_context(BitMapContext, model);
  111.                             break;
  112.  
  113.             }
  114.             }
  115.         update=1;
  116.         refresh=1;
  117.         }
  118.  
  119.         if((BG_MouseLeft==1)||(BG_MouseX!=oldx)||(BG_MouseY!=oldy))
  120.         {
  121.             oldx=BG_MouseX;
  122.             oldy=BG_MouseY;
  123.             oldr=BG_MouseLeft;
  124.             update=1;
  125.         }
  126.         // if something changed udate changes and redraw
  127.         if(update==1)
  128.         {
  129.             BG_MouseHide();
  130.             // if moving point
  131.             if(BG_MouseLeft==1)
  132.             {
  133.                 //point was already selected..
  134.                 if(selected==1)
  135.                 {
  136.                     refresh=1;
  137.                     // draw over old position  (and seemed pos)
  138.                     BG_PutPixel(model->vertex[selectedindex*3+1],model->vertex[selectedindex*3+2],model->skin[model->cur_skin].bitmap[model->vertex[selectedindex*3+1]+model->vertex[selectedindex*3+2]*model->skinw]);
  139.                     if(model->vertex[selectedindex*3]==32)
  140.                         BG_PutPixel(model->skinw/2+model->vertex[selectedindex*3+1],model->vertex[selectedindex*3+2],model->skin[model->cur_skin].bitmap[model->skinw/2+model->vertex[selectedindex*3+1]+model->vertex[selectedindex*3+2]*model->skinw]);
  141.                     // store the new position
  142.                     // if on seem store..
  143.                     if((model->vertex[selectedindex*3]==32)&&(BG_MouseX>model->skinw/2))
  144.                     {
  145.                         model->vertex[selectedindex*3+1]=BG_MouseX-model->skinw/2;
  146.                         model->vertex[selectedindex*3+2]=BG_MouseY;
  147.                     }
  148.                     // if not on seem...
  149.                     else
  150.                     {
  151.                         model->vertex[selectedindex*3+1]=BG_MouseX;
  152.                         model->vertex[selectedindex*3+2]=BG_MouseY;
  153.                     }
  154.                 }
  155.             }
  156.             else selected=0;
  157.             // redraw skin
  158.             if(refresh)
  159.                 GrBitBlt(NULL,0,0,BitMapContext,0,0,model->skinw-1,model->skinh-1,GrWRITE);
  160.             //draw 2d polgon mesh
  161.             if(draw_triangles)
  162.             {
  163.                 int x1,x2,x3;
  164.                 for(i=0;i<model->num_triangles;i++)
  165.                 {
  166.                     p1=model->triangle[i*4+1];
  167.                     p2=model->triangle[i*4+2];
  168.                     p3=model->triangle[i*4+3];
  169.                     p4=model->triangle[i*4];
  170.                     x1=x2=x3=0;
  171.                     if((model->vertex[3*p1]==32)&&(p4==0)) x1=model->skinw/2;
  172.                     if((model->vertex[3*p2]==32)&&(p4==0)) x2=model->skinw/2;
  173.                     if((model->vertex[3*p3]==32)&&(p4==0)) x3=model->skinw/2;
  174.                     BG_Line(model->vertex[p1*3+1]+x1,model->vertex[p1*3+2],model->vertex[p2*3+1]+x2,model->vertex[p2*3+2],8);
  175.                     BG_Line(model->vertex[p2*3+1]+x2,model->vertex[p2*3+2],model->vertex[p3*3+1]+x3,model->vertex[p3*3+2],8);
  176.                     BG_Line(model->vertex[p3*3+1]+x3,model->vertex[p3*3+2],model->vertex[p1*3+1]+x1,model->vertex[p1*3+2],8);
  177.                 }
  178.             }
  179.             // check all vertices, if mouse is ontop of one and left
  180.             // button is pushed, select point
  181.             for(i=0;i<model->num_vertices;i++)
  182.             {
  183.                 // not on seem
  184.                 if(model->vertex[i*3]!=32)
  185.                 {
  186.                     if((BG_MouseX==model->vertex[i*3+1])&&(BG_MouseY==model->vertex[i*3+2]))
  187.                     {
  188.                         if(selected==0)
  189.                         {
  190.                             ontopof=1;
  191.                             if(BG_MouseLeft){selectedindex=i;selected=1;}
  192.                         }
  193.                     }
  194.                     else ontopof=0;
  195.                 }
  196.                 //else if on seem, check back of skin too
  197.                 else
  198.                 {
  199.                     if((BG_MouseX==model->skinw/2+model->vertex[i*3+1])&&(BG_MouseY==model->vertex[i*3+2])
  200.                     ||(BG_MouseX==model->vertex[i*3+1])&&(BG_MouseY==model->vertex[i*3+2]))
  201.                     {
  202.                         if(selected==0)
  203.                         {
  204.                             ontopof=1;
  205.                             if(BG_MouseLeft){selectedindex=i;selected=1; }
  206.                         }
  207.                     }
  208.                     else ontopof=0;
  209.                 }
  210.                 // draw vertices
  211.                 if(draw_vertices)
  212.                 {
  213.                     BG_PutPixel(model->vertex[i*3+1],model->vertex[i*3+2],254-4*ontopof);
  214.                     // if seemed draw here too
  215.                     if(model->vertex[i*3]==32)
  216.                     BG_PutPixel(model->skinw/2+model->vertex[i*3+1],model->vertex[i*3+2],254-4*ontopof);
  217.                 }
  218.  
  219.  
  220.             }
  221.  
  222.             BG_MouseShow();
  223.             update=0;
  224.             refresh=0;
  225.         }
  226.  
  227.     }
  228.  
  229.  
  230.     GrDestroyContext(BitMapContext);
  231.  
  232.  
  233.  
  234.     if(draw_3d==0) return 0;
  235.     else if(draw_3d==1) return 1;
  236.     else return 2;  // show help screen
  237.  
  238.  
  239. }
  240.  
  241.  
  242. draw_bitmap_context(GrContext  *BitMapContext, MDL_model *model)
  243. {
  244.     GrSetContext(BitMapContext);
  245.     for(int j=0;j<model->skinh;j++)
  246.     {
  247.         for(int i=0;i<model->skinw;i++)
  248.         {
  249.             int ccolor;
  250.             ccolor=model->skin[model->cur_skin].bitmap[i+j*model->skinw];
  251.             if((ccolor>=96)&&(ccolor<112))
  252.             {
  253.                 if(model->bcolor<8)
  254.                     ccolor+=model->bcolor*16-96;
  255.                 else {
  256.                     ccolor-=96;
  257.                     ccolor=15-ccolor;
  258.                     ccolor+=model->bcolor*16;
  259.                     }
  260.             }
  261.             else if((ccolor>=16)&&(ccolor<32))
  262.             {
  263.                 if(model->tcolor<8)
  264.                     ccolor+=model->tcolor*16-16;
  265.                 else {
  266.                     ccolor-=16;
  267.                     ccolor=15-ccolor;
  268.                     ccolor+=model->tcolor*16;
  269.                     }
  270.             }
  271.             //if(ccolor)
  272.             BG_PutPixel(i,j,ccolor);
  273.         }
  274.     }
  275. //    for (int i=0; i<256;i++)
  276. //            BG_PutPixel(i,1,i);
  277.     GrSetContext(NULL);
  278.     // draw bitmap
  279.     GrBitBlt(NULL,0,0,BitMapContext,0,0,model->skinw-1,model->skinh-1,GrWRITE);
  280.  
  281.  
  282. }
  283.