home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / alt / sources / 2611 / draw_objects_xgl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-23  |  5.7 KB  |  270 lines

  1. #ifndef lint
  2. static    char    sccsid[] = "@(#)draw_objects_xgl.c 1.1 92/05/28 SMI" ;
  3.     /* from draw_objects_xgl.c 1.1 90/07/23 SMI */
  4. #endif
  5.  
  6. /*
  7.  * Copyright (c) 1986 by Sun Microsystems, Inc.
  8.  */
  9.  
  10. /*
  11.  * this file goes through the object list and draws all of the objects
  12.  * on the screen
  13.  */
  14.  
  15.  
  16. #define    MAX_TMP    100    /* how many points to buffer */
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <math.h>
  21. #include "graphics.h"
  22. #include "dstar.h"
  23. #include "line_object.h"
  24.  
  25. extern    int    debug_level ;
  26.  
  27.  
  28.  
  29.  
  30. static
  31. rebuild_sort_list()
  32. {
  33. register Object    *object = &objects[0] ;
  34. register int    i ;
  35.  
  36.  
  37. for(i=0; i<nobjects; ++i)
  38. object = &objects[sort_list[i]] ;
  39.  
  40.     nobjects = 0 ;
  41.     for( i=0; i<MAX_OBJECTS; ++i)
  42.     {
  43.       if( object->class != OBJ_EMPTY )
  44.         sort_list[nobjects++] = i ;
  45.       ++object ;
  46.     }
  47. }
  48.  
  49.  
  50.  
  51. static    int
  52. draw_object_lines(desc)
  53.     Object_Desc    *desc ;
  54. {
  55.     XYZF    *xyzf ;
  56.     int    color ;
  57.     Xgl_pt_flag_f3d    tmp_pts[MAX_TMP], *op ;
  58.     Xgl_pt_list    tmp_pt_list ;
  59.  
  60.     xyzf = (XYZF *) desc->first ;
  61.  
  62.     color = -1 ;
  63.     op = tmp_pts ;
  64.     tmp_pt_list.pt_type = XGL_PT_FLAG_F3D ;
  65.     tmp_pt_list.bbox = NULL ;
  66.     tmp_pt_list.num_pts = 0 ;
  67.     tmp_pt_list.pts.flag_f3d = tmp_pts ;
  68.     while(xyzf->color != LINE_END)
  69.     {
  70.       if(xyzf->color != color  ||  tmp_pt_list.num_pts >= MAX_TMP)
  71.       {
  72.         if( tmp_pt_list.num_pts > 1 )
  73.         {
  74.           xgl_multipolyline(Main_gfx, NULL, 1, &tmp_pt_list) ;
  75.           tmp_pts[0] = tmp_pts[tmp_pt_list.num_pts-1] ;
  76.           tmp_pt_list.num_pts = 1 ;
  77.           op = &tmp_pts[1] ;
  78.         }
  79.         Set_color(Main_gfx, color=xyzf->color) ;
  80.       }
  81.       op->x = xyzf->p.x ;
  82.       op->y = xyzf->p.y ;
  83.       op->z = xyzf->p.z ;
  84.       op->flag = xyzf->color != MOVE ;
  85.       ++op ;
  86.       ++tmp_pt_list.num_pts ;
  87.       ++xyzf ;
  88.     }
  89.     if( tmp_pt_list.num_pts > 1 )
  90.       xgl_multipolyline(Main_gfx, NULL, 1, &tmp_pt_list) ;
  91. }
  92.  
  93.  
  94.  
  95. static    int
  96. draw_laser_beams(object)
  97.     Object    *object ;
  98. {
  99.     Pt3d  beam[2] ;
  100.     Xgl_pt_list    tmp_pt_list ;
  101.  
  102.     Set_color(Main_gfx,RED) ;
  103.     beam[0] = beam[1] = object->Posn ;
  104.     beam[1].x += object->Pointing.x * LASER_RANGE ;
  105.     beam[1].y += object->Pointing.y * LASER_RANGE ;
  106.     beam[1].z += object->Pointing.z * LASER_RANGE ;
  107.     tmp_pt_list.pt_type = XGL_PT_F3D ;
  108.     tmp_pt_list.bbox = NULL ;
  109.     tmp_pt_list.num_pts = 2 ;
  110.     tmp_pt_list.pts.f3d = beam ;
  111.     xgl_multipolyline(Main_gfx, NULL, 1, &tmp_pt_list) ;
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121. draw_object(object)
  122.     Object        *object ;
  123. {
  124.     Mat3d        WtoM, MtoW ;
  125.     Object_Desc    *desc ;
  126.     float        Dist ;
  127. register float        dx, dy, dz ;
  128. extern    int        draw_object_bsp() ;
  129.  
  130.     /* build transformation matrix (model space to world) */
  131.     /* and at the same time build  (world to model space) */
  132.  
  133.     ident_mat(MtoW) ;
  134.     ident_mat(WtoM) ;
  135.  
  136.     WtoM[0][2] = (MtoW[2][0] = object->Forward.x) ;
  137.     WtoM[1][2] = (MtoW[2][1] = object->Forward.y) ;
  138.     WtoM[2][2] = (MtoW[2][2] = object->Forward.z) ;
  139.     WtoM[0][1] = (MtoW[1][0] = object->Up.x) ;
  140.     WtoM[1][1] = (MtoW[1][1] = object->Up.y) ;
  141.     WtoM[2][1] = (MtoW[1][2] = object->Up.z) ;
  142.     WtoM[0][0] = (MtoW[0][0] = object->Right.x) ;
  143.     WtoM[1][0] = (MtoW[0][1] = object->Right.y) ;
  144.     WtoM[2][0] = (MtoW[0][2] = object->Right.z) ;
  145.     MtoW[3][0] = object->Posn.x ;
  146.     MtoW[3][1] = object->Posn.y ;
  147.     MtoW[3][2] = object->Posn.z ;
  148.  
  149.     xgl_transform_write( TM1, MtoW) ;
  150.     xgl_transform_multiply( TM2, TM1, WIN_MAT) ;
  151.     xgl_object_set(Main_gfx, XGL_CTX_GLOBAL_MODEL_TRANS, TM2, 0) ;
  152.  
  153.     dx = object->Posn.x - Me->Posn.x ;
  154.     dy = object->Posn.y - Me->Posn.y ;
  155.     dz = object->Posn.z - Me->Posn.z ;
  156.     Dist = dx*dx + dy*dy + dz*dz ;
  157.  
  158.     desc = descriptions[object->description] ;
  159.     while(Dist < desc->thresh && desc->next)
  160.       desc = desc->next ;
  161.  
  162.     if(desc->type == LINE_DRAWING)
  163.       draw_object_lines(desc) ;
  164.  
  165.     /* Transform the user's position (Me->Posn) into
  166.        the object's model space coordinates */
  167.     else if(desc->type == BSP_TREE) {
  168.       Pt3d        viewer;
  169.       Xgl_pt    Xfpt ;
  170.  
  171.       xgl_transform_write( TM1, WtoM);
  172.  
  173.       Xfpt.pt_type = XGL_PT_F3D ;
  174.       Xfpt.pt.f3d = &viewer ;
  175.       viewer.x = -object->Posn.x ;
  176.       viewer.y = -object->Posn.y ;
  177.       viewer.z = -object->Posn.z ;
  178.       xgl_transform_translate(TM1, &Xfpt, XGL_TRANS_PRECONCAT) ;
  179.  
  180.       viewer = Me->Posn ;
  181.       xgl_transform_point(TM1, &Xfpt) ;
  182.       draw_object_bsp(desc, &viewer) ;
  183.     }
  184.  
  185.     else
  186.       printf("undefined object type %d\n",desc->type) ;
  187.  
  188.     if(object->flags & LASER_FLAG)
  189.     {
  190.       xgl_object_set(Main_gfx, XGL_CTX_GLOBAL_MODEL_TRANS, WIN_MAT, 0) ;
  191.       draw_laser_beams(object) ;
  192.       xgl_object_set(Main_gfx, XGL_CTX_GLOBAL_MODEL_TRANS, TM2, 0) ;
  193.     }
  194. }
  195.  
  196.  
  197.  
  198. draw_objects()
  199. {
  200. register int    i ;
  201. register Object    *object ;
  202.  
  203.  
  204.     /* sort all objects by distance along Me->Forward */
  205.     /* a simple bubble sort will do very well with the
  206.        frame-to-frame coherence that exists */
  207.  
  208.     {
  209.       int        n, next_n ;
  210.       float        distance[MAX_OBJECTS] ;
  211.       register int    j ;
  212.       register Object **optr ;
  213.  
  214.       object = &objects[0] ;
  215.       for(i=0; i<MAX_OBJECTS; ++i)
  216.       {
  217.         if(object->class != OBJ_EMPTY  &&
  218.            object->status != OBJ_SLEEPING)
  219.         {
  220.           distance[i] = Me->Forward.x*(object->Posn.x - Me->Posn.x) +
  221.                 Me->Forward.y*(object->Posn.y - Me->Posn.y) +
  222.                 Me->Forward.z*(object->Posn.z - Me->Posn.z) ;
  223.         }
  224.         ++object ;
  225.       }
  226.  
  227.       n = nobjects-1 ;
  228.  
  229.       while(n > 0)
  230.       {
  231.         next_n = 0 ;
  232.         for(i=0; i<n; ++i)
  233.         {
  234.           if(distance[sort_list[i+1]] > distance[sort_list[i]])
  235.           {
  236.         j = sort_list[i+1] ;
  237.         sort_list[i+1] = sort_list[i] ;
  238.         sort_list[i] = j ;
  239.         next_n = i ;
  240.           }
  241.         }
  242.         n = next_n ;
  243.       }
  244.     }
  245.  
  246.  
  247.     /* draw enemy ships */
  248.  
  249.     for(i=0; i<nobjects; ++i)
  250.     {
  251.       object = &objects[sort_list[i]] ;
  252.       if(object->class != OBJ_EMPTY  &&
  253.          object->status != OBJ_SLEEPING)
  254.       {
  255.         draw_object(object) ;
  256.       }
  257.       else if (object->class == OBJ_EMPTY)
  258.       {
  259. #ifdef    DEBUG
  260.         printf("internal error: sort_list contains pointer") ;
  261.         printf(" to inactive object %d, %s\n",
  262.             sort_list[i], object->name) ;
  263. #endif    DEBUG
  264.         rebuild_sort_list() ;
  265.       }
  266.     }
  267.  
  268.     xgl_object_set(Main_gfx, XGL_CTX_GLOBAL_MODEL_TRANS, WIN_MAT, 0) ;
  269. }
  270.