home *** CD-ROM | disk | FTP | other *** search
- #ifndef lint
- static char sccsid[] = "@(#)draw_objects_xgl.c 1.1 92/05/28 SMI" ;
- /* from draw_objects_xgl.c 1.1 90/07/23 SMI */
- #endif
-
- /*
- * Copyright (c) 1986 by Sun Microsystems, Inc.
- */
-
- /*
- * this file goes through the object list and draws all of the objects
- * on the screen
- */
-
-
- #define MAX_TMP 100 /* how many points to buffer */
-
-
- #include <stdio.h>
- #include <math.h>
- #include "graphics.h"
- #include "dstar.h"
- #include "line_object.h"
-
- extern int debug_level ;
-
-
-
-
- static
- rebuild_sort_list()
- {
- register Object *object = &objects[0] ;
- register int i ;
-
-
- for(i=0; i<nobjects; ++i)
- object = &objects[sort_list[i]] ;
-
- nobjects = 0 ;
- for( i=0; i<MAX_OBJECTS; ++i)
- {
- if( object->class != OBJ_EMPTY )
- sort_list[nobjects++] = i ;
- ++object ;
- }
- }
-
-
-
- static int
- draw_object_lines(desc)
- Object_Desc *desc ;
- {
- XYZF *xyzf ;
- int color ;
- Xgl_pt_flag_f3d tmp_pts[MAX_TMP], *op ;
- Xgl_pt_list tmp_pt_list ;
-
- xyzf = (XYZF *) desc->first ;
-
- color = -1 ;
- op = tmp_pts ;
- tmp_pt_list.pt_type = XGL_PT_FLAG_F3D ;
- tmp_pt_list.bbox = NULL ;
- tmp_pt_list.num_pts = 0 ;
- tmp_pt_list.pts.flag_f3d = tmp_pts ;
- while(xyzf->color != LINE_END)
- {
- if(xyzf->color != color || tmp_pt_list.num_pts >= MAX_TMP)
- {
- if( tmp_pt_list.num_pts > 1 )
- {
- xgl_multipolyline(Main_gfx, NULL, 1, &tmp_pt_list) ;
- tmp_pts[0] = tmp_pts[tmp_pt_list.num_pts-1] ;
- tmp_pt_list.num_pts = 1 ;
- op = &tmp_pts[1] ;
- }
- Set_color(Main_gfx, color=xyzf->color) ;
- }
- op->x = xyzf->p.x ;
- op->y = xyzf->p.y ;
- op->z = xyzf->p.z ;
- op->flag = xyzf->color != MOVE ;
- ++op ;
- ++tmp_pt_list.num_pts ;
- ++xyzf ;
- }
- if( tmp_pt_list.num_pts > 1 )
- xgl_multipolyline(Main_gfx, NULL, 1, &tmp_pt_list) ;
- }
-
-
-
- static int
- draw_laser_beams(object)
- Object *object ;
- {
- Pt3d beam[2] ;
- Xgl_pt_list tmp_pt_list ;
-
- Set_color(Main_gfx,RED) ;
- beam[0] = beam[1] = object->Posn ;
- beam[1].x += object->Pointing.x * LASER_RANGE ;
- beam[1].y += object->Pointing.y * LASER_RANGE ;
- beam[1].z += object->Pointing.z * LASER_RANGE ;
- tmp_pt_list.pt_type = XGL_PT_F3D ;
- tmp_pt_list.bbox = NULL ;
- tmp_pt_list.num_pts = 2 ;
- tmp_pt_list.pts.f3d = beam ;
- xgl_multipolyline(Main_gfx, NULL, 1, &tmp_pt_list) ;
- }
-
-
-
-
-
-
-
-
- draw_object(object)
- Object *object ;
- {
- Mat3d WtoM, MtoW ;
- Object_Desc *desc ;
- float Dist ;
- register float dx, dy, dz ;
- extern int draw_object_bsp() ;
-
- /* build transformation matrix (model space to world) */
- /* and at the same time build (world to model space) */
-
- ident_mat(MtoW) ;
- ident_mat(WtoM) ;
-
- WtoM[0][2] = (MtoW[2][0] = object->Forward.x) ;
- WtoM[1][2] = (MtoW[2][1] = object->Forward.y) ;
- WtoM[2][2] = (MtoW[2][2] = object->Forward.z) ;
- WtoM[0][1] = (MtoW[1][0] = object->Up.x) ;
- WtoM[1][1] = (MtoW[1][1] = object->Up.y) ;
- WtoM[2][1] = (MtoW[1][2] = object->Up.z) ;
- WtoM[0][0] = (MtoW[0][0] = object->Right.x) ;
- WtoM[1][0] = (MtoW[0][1] = object->Right.y) ;
- WtoM[2][0] = (MtoW[0][2] = object->Right.z) ;
- MtoW[3][0] = object->Posn.x ;
- MtoW[3][1] = object->Posn.y ;
- MtoW[3][2] = object->Posn.z ;
-
- xgl_transform_write( TM1, MtoW) ;
- xgl_transform_multiply( TM2, TM1, WIN_MAT) ;
- xgl_object_set(Main_gfx, XGL_CTX_GLOBAL_MODEL_TRANS, TM2, 0) ;
-
- dx = object->Posn.x - Me->Posn.x ;
- dy = object->Posn.y - Me->Posn.y ;
- dz = object->Posn.z - Me->Posn.z ;
- Dist = dx*dx + dy*dy + dz*dz ;
-
- desc = descriptions[object->description] ;
- while(Dist < desc->thresh && desc->next)
- desc = desc->next ;
-
- if(desc->type == LINE_DRAWING)
- draw_object_lines(desc) ;
-
- /* Transform the user's position (Me->Posn) into
- the object's model space coordinates */
- else if(desc->type == BSP_TREE) {
- Pt3d viewer;
- Xgl_pt Xfpt ;
-
- xgl_transform_write( TM1, WtoM);
-
- Xfpt.pt_type = XGL_PT_F3D ;
- Xfpt.pt.f3d = &viewer ;
- viewer.x = -object->Posn.x ;
- viewer.y = -object->Posn.y ;
- viewer.z = -object->Posn.z ;
- xgl_transform_translate(TM1, &Xfpt, XGL_TRANS_PRECONCAT) ;
-
- viewer = Me->Posn ;
- xgl_transform_point(TM1, &Xfpt) ;
- draw_object_bsp(desc, &viewer) ;
- }
-
- else
- printf("undefined object type %d\n",desc->type) ;
-
- if(object->flags & LASER_FLAG)
- {
- xgl_object_set(Main_gfx, XGL_CTX_GLOBAL_MODEL_TRANS, WIN_MAT, 0) ;
- draw_laser_beams(object) ;
- xgl_object_set(Main_gfx, XGL_CTX_GLOBAL_MODEL_TRANS, TM2, 0) ;
- }
- }
-
-
-
- draw_objects()
- {
- register int i ;
- register Object *object ;
-
-
- /* sort all objects by distance along Me->Forward */
- /* a simple bubble sort will do very well with the
- frame-to-frame coherence that exists */
-
- {
- int n, next_n ;
- float distance[MAX_OBJECTS] ;
- register int j ;
- register Object **optr ;
-
- object = &objects[0] ;
- for(i=0; i<MAX_OBJECTS; ++i)
- {
- if(object->class != OBJ_EMPTY &&
- object->status != OBJ_SLEEPING)
- {
- distance[i] = Me->Forward.x*(object->Posn.x - Me->Posn.x) +
- Me->Forward.y*(object->Posn.y - Me->Posn.y) +
- Me->Forward.z*(object->Posn.z - Me->Posn.z) ;
- }
- ++object ;
- }
-
- n = nobjects-1 ;
-
- while(n > 0)
- {
- next_n = 0 ;
- for(i=0; i<n; ++i)
- {
- if(distance[sort_list[i+1]] > distance[sort_list[i]])
- {
- j = sort_list[i+1] ;
- sort_list[i+1] = sort_list[i] ;
- sort_list[i] = j ;
- next_n = i ;
- }
- }
- n = next_n ;
- }
- }
-
-
- /* draw enemy ships */
-
- for(i=0; i<nobjects; ++i)
- {
- object = &objects[sort_list[i]] ;
- if(object->class != OBJ_EMPTY &&
- object->status != OBJ_SLEEPING)
- {
- draw_object(object) ;
- }
- else if (object->class == OBJ_EMPTY)
- {
- #ifdef DEBUG
- printf("internal error: sort_list contains pointer") ;
- printf(" to inactive object %d, %s\n",
- sort_list[i], object->name) ;
- #endif DEBUG
- rebuild_sort_list() ;
- }
- }
-
- xgl_object_set(Main_gfx, XGL_CTX_GLOBAL_MODEL_TRANS, WIN_MAT, 0) ;
- }
-