home *** CD-ROM | disk | FTP | other *** search
- #ifndef lint
- static char sccsid[] = "@(#)draw_image_xgl.c 1.2 92/05/28 SMI" ;
- /* from draw_image_xgl.c 1.1 90/07/23 SMI */
- #endif
-
- /*
- * Copyright (c) 1986 by Sun Microsystems, Inc.
- */
-
- /*
- * this file draws the image seen on the screen. It is the top-level
- * graphics routine
- */
-
-
- #define STAT_LEFT 200 /* x-posn of status lines */
- #define LOCK_STAT_LEFT 400 /* x-posn of status lines */
-
-
- #include <stdio.h>
- #ifdef XV
- #include <xview/window_hs.h>
- #else
- #include <sunwindow/window_hs.h>
- #endif
- #include <math.h>
- #include "graphics.h"
- #include "dstar.h"
-
- extern int debug_level ;
-
- #ifndef XV
- extern struct pixrect dstar_pr ;
- #endif
-
- Xgl_trans Idtrans = NULL ;
- Xgl_bounds_f3d Idwin = {0.,1.,0.,1.,0.,1.} ;
- Xgl_bounds_f3d Bigwin = {-2000.,2000.,-2000.,2000.,-2000.,2000.} ;
- static Xgl_pt_f3d cube1[] = { {-.5,-.5,-.5}, { .5,-.5,-.5},
- { .5, .5,-.5}, {-.5, .5,-.5}, {-.5,-.5,-.5},
- {-.5,-.5, .5}, { .5,-.5, .5}, { .5, .5, .5},
- {-.5, .5, .5}, {-.5,-.5, .5}} ;
- static Xgl_pt_f3d cube2[] = {{ .5,-.5,-.5},{ .5,-.5,.5}} ;
- static Xgl_pt_f3d cube3[] = {{ .5, .5,-.5},{ .5, .5,.5}} ;
- static Xgl_pt_f3d cube4[] = {{-.5, .5,-.5},{-.5, .5,.5}} ;
- static Xgl_pt_list cube[4] ;
-
- static Xgl_pt_flag_f3d decoration_pts[] = {
- {-100.0, 0.0, 0.0, 0}, /* center */
- { 100.0, 0.0, 0.0, 1},
- {0.0, -100.0, 0.0, 0},
- {0.0, 100.0, 0.0, 1},
- {0.0, 0.0, -100.0, 0},
- {0.0, 0.0, 100.0, 1},
-
- {900.0, -100.0, 0.0, 0}, /* X */
- {1100.0, 100.0, 0.0, 1},
- {900.0, 100.0, 0.0, 0},
- {1100.0, -100.0, 0.0, 1},
-
- {-100.0, 1100.0, 0.0, 0}, /* Y */
- {0.0, 1000.0, 0.0, 1},
- {0.0, 900.0, 0.0, 1},
- {100.0, 1100.0, 0.0, 0},
- {0.0, 1000.0, 0.0, 1},
-
- {-100.0, 100.0, 1000.0, 0}, /* Z */
- { 100.0, 100.0, 1000.0, 1},
- {-100.0,-100.0, 1000.0, 1},
- { 100.0,-100.0, 1000.0, 1}} ;
- static Xgl_pt_list decorations ;
-
- static Xgl_pt_flag_f3d center_pts[] = {
- { 0.45, 0.45, 0.0, 0}, { 0.55, 0.45, 0.0, 1},
- { 0.55, 0.55, 0.0, 1}, { 0.45, 0.55, 0.0, 1},
- { 0.45, 0.45, 0.0, 1},
- { 0.45, 0.5, 0.0, 0}, { 0.47, 0.5, 0.0, 1},
- { 0.53, 0.5, 0.0, 0}, { 0.55, 0.5, 0.0, 1},
- { 0.5, 0.45, 0.0, 0}, { 0.5, 0.47, 0.0, 1},
- { 0.5, 0.53, 0.0, 0}, { 0.5, 0.55, 0.0, 1}} ;
- static Xgl_pt_list center ;
-
- static Xgl_pt_f3d laserbeam_pts[] = {
- { 0.0, 0.0, 0.0}, { 0., 0., 0.0}, { 1.0, 0.0, 0.0}} ;
- static Xgl_pt_list laserbeam ;
-
- static Xgl_matrix_f3d hsi_mat = { /* viewport matrix for HSI */
- {.1,0.,0.,0.},
- {0.,.3,0.,0.},
- {0.,0.,1.,0.},
- {.15,.5,0.,1.}} ;
-
- static int dbvalue = 2 ;
-
- draw_image()
- {
- int i,j,k,iret ;
- Mat3d matrix, R_matrix ;
- Pt3d p, q ;
- Pt3d p1,p2,p3,p4,p5 ;
- float dx ;
- Xgl_bounds_f2d bounds ;
- static Rect onepix = {0,0,1,1} ;
- Rect full_rect ;
- char line[80] ;
- register Object *object ;
- int color ;
- Xgl_pt offset ;
- Xgl_pt_f3d offset_p ;
- #ifdef XV
- static Xgl_pt_f2d radar_pos = {0.05, 0.25} ,
- armed_pos = {0.05, 0.50} ,
- remain_pos = {0.05, 0.75} ,
- score_pos = {0.05, 0.05} ,
- lock_pos = {0.75, 0.15} ;
-
- /* Added score_text_npl - ds */
- Xgl_color text_color ;
- #else
- static int red_rop=0, yellow_rop, white_rop ;
- #endif XV
-
-
- #ifdef DEBUG
- if(debug_level && missiles[0] != NULL && missiles[0]->target != NULL)
- object = missiles[0] ;
- else
- #endif DEBUG
- object = Me ;
-
- /* set db control and clear back buffer */
-
- dbuf_control(Main_gfx, dbvalue) ;
- xgl_object_get(Main_gfx, XGL_CTX_DC_VIEWPORT, &bounds) ;
- full_rect.r_left = 0 ;
- full_rect.r_top = 0 ;
- full_rect.r_width = bounds.xmax - bounds.xmin ;
- full_rect.r_height = bounds.ymax - bounds.ymin ;
- #ifndef XV
- if( red_rop == 0 ) {
- red_rop = PIX_SRC | PIX_COLOR(dbmap[RED]) ;
- yellow_rop = PIX_SRC | PIX_COLOR(dbmap[YELLOW]);
- white_rop = PIX_SRC | PIX_COLOR(dbmap[WHITE]);
- }
- #endif
-
- pw_lock(Main_pw, &full_rect ) ;
- xgl_context_new_frame(Main_gfx) ;
- pw_unlock(Main_pw) ;
-
- pw_lock(Main_pw, &onepix ) ;
-
- /* turn on all clipping except back, set 1:1 view transformation */
-
- Set_color(Main_gfx,WHITE) ;
- xgl_object_set(Main_gfx, XGL_CTX_CLIP_PLANES, 0x3e, 0) ;
-
-
- if( Idtrans == NULL ) {
- Idtrans = xgl_transform_create(XGL_TRANS_DATA_TYPE, XGL_DATA_FLT,
- XGL_TRANS_DIMENSION, XGL_TRANS_3D, 0) ;
- xgl_transform_identity(Idtrans) ;
- }
-
- /* build orientation matrix */
-
- ident_mat(R_matrix) ;
- R_matrix[2][0] = object->Forward.x ;
- R_matrix[2][1] = object->Forward.y ;
- R_matrix[2][2] = object->Forward.z ;
- R_matrix[1][0] = object->Up.x ;
- R_matrix[1][1] = object->Up.y ;
- R_matrix[1][2] = object->Up.z ;
- R_matrix[0][0] = object->Right.x ;
- R_matrix[0][1] = object->Right.y ;
- R_matrix[0][2] = object->Right.z ;
-
- R_matrix[3][0] = object->Posn.x ;
- R_matrix[3][1] = object->Posn.y ;
- R_matrix[3][2] = object->Posn.z ;
- /* OBJ_MAT = users (Me) position and orientation in World Space */
- /* OBJ_MAT = ROTATION * TRANSLATION */
- xgl_transform_write(OBJ_MAT, R_matrix) ;
- R_matrix[3][0] = 0; R_matrix[3][1] = 0; R_matrix[3][2] = 0;
-
- xgl_transform_write(ROT_MAT, R_matrix) ;
- xgl_transform_transpose(ROT_MAT, ROT_MAT) ;
-
- /* build perspective & windowing matrices, draw stars */
-
- xgl_transform_write(TM1, P_matrix) ;
- xgl_transform_multiply(WIN_MAT, ROT_MAT, TM1) ;
- xgl_object_set(Main_gfx, XGL_CTX_GLOBAL_MODEL_TRANS, WIN_MAT, 0) ;
-
- Set_color(Main_gfx,WHITE) ;
- draw_starfield() ;
-
- /* build inverse to position matrix and multiply with ROT_MAT
- to form xform mat for viewed objects */
-
- offset.pt_type = XGL_PT_F3D ;
- offset.pt.f3d = &offset_p ;
- offset_p.x = -object->Posn.x ;
- offset_p.y = -object->Posn.y ;
- offset_p.z = -object->Posn.z ;
- xgl_transform_translate(TM1, &offset, XGL_TRANS_REPLACE) ;
- xgl_transform_multiply(TM3, TM1, ROT_MAT) ;
- xgl_transform_write(TM1, P_matrix) ;
- xgl_transform_multiply(TM2, TM3, TM1) ;
- window_mat(-1.0,1.0, -0.75,0.75, -1.0,1.0, matrix) ;
- xgl_transform_write(TM1, matrix) ;
- xgl_transform_multiply(WIN_MAT, TM2, TM1) ;
-
- /* draw test object */
-
- #ifdef COMMENT
- draw_starfield() ;
- #endif COMMENT
-
- decorations.pt_type = XGL_PT_FLAG_F3D ;
- decorations.bbox = NULL ; /* TODO */
- decorations.num_pts =
- (sizeof(decoration_pts)/sizeof(Xgl_pt_flag_f3d)) ;
- decorations.pts.flag_f3d = decoration_pts ;
- xgl_multipolyline(Main_gfx, NULL, 1, &decorations) ;
-
- /* draw enemy space ships and other objects */
-
- draw_objects() ;
-
-
- /* set 1:1 view transformation */
-
- window_mat(0.0, 1.0, 0.0, 1.0, 0.0, 1.0, matrix) ;
- xgl_transform_write(WIN_MAT, matrix) ;
-
-
- /* mark screen center */
-
- if( (Me->flags & RADAR_FLAG) == 0)
- {
- Set_color(Main_gfx,BLUE) ;
- }
- else if(Me->target == NULL)
- {
- Set_color(Main_gfx, YELLOW) ;
- }
- else
- {
- Set_color(Main_gfx, RED) ;
- }
- center.pt_type = XGL_PT_FLAG_F3D ;
- center.bbox = NULL ; /* TODO */
- center.num_pts =
- (sizeof(center_pts)/sizeof(Xgl_pt_flag_f3d)) ;
- center.pts.flag_f3d = center_pts ;
- xgl_multipolyline(Main_gfx, NULL, 1, ¢er) ;
-
- if(auto_pilot || debug_level)
- {
- Mouse_ax = object->Delta.x * (1.0/VIEWSCALE) + .5 ;
- Mouse_ay = object->Delta.y * (1.0/VIEWSCALE) + .5 ;
- Mouse_x = Mouse_ax * full_rect.r_width ;
- Mouse_y = (1.0 - Mouse_ay) * full_rect.r_height ;
- }
-
- /* draw laser beams if appropriate */
-
- if(Me->flags & LASER_FLAG)
- {
- Set_color(Main_gfx,RED) ;
- laserbeam_pts[1].x = Mouse_ax ;
- laserbeam_pts[1].y = Mouse_ay ;
- laserbeam.pt_type = XGL_PT_F3D ;
- laserbeam.bbox = NULL ; /* TODO */
- laserbeam.num_pts = (sizeof(laserbeam_pts)/sizeof(Xgl_pt_f3d)) ;
- laserbeam.pts.f3d = laserbeam_pts ;
- xgl_multipolyline(Main_gfx, NULL, 1, &laserbeam) ;
- }
-
-
-
- /* draw cursor if appropriate */
-
- if(auto_pilot || debug_level)
- {
- #ifndef XV
- pw_rop(Main_pw, Mouse_x-8, Mouse_y-8, 16, 16,
- PIX_SRC | PIX_DST | PIX_COLOR(0xff), &dstar_pr, 0,0) ;
- #else
- Xgl_pt_i2d pos;
- Xgl_color rop_color ;
- rop_color.index = dbmap[YELLOW] ;
-
- pos.x = Mouse_x-8;
- pos.y = Mouse_y-8;
- xgl_object_set(Main_gfx, XGL_CTX_SURF_FRONT_COLOR, &rop_color, 0) ;
- xgl_context_copy_raster(Main_gfx, (Xgl_bounds_i2d *)NULL,
- &pos, Cursor_ras);
- #endif XV
- }
-
-
-
- pw_unlock(Main_pw) ;
-
- /* control panel: set db control and clear back buffer */
-
- dbuf_control(Control_gfx, dbvalue) ;
- #ifndef XV
- xgl_context_new_frame(Control_gfx) ;
- #endif
-
- pw_lock(Control_pw, &onepix ) ;
-
-
- xgl_transform_write(Ctrl_view_mat, hsi_mat) ;
- draw_hsi(Control_gfx) ;
-
-
- /* radar status */
-
- if( (Me->flags & RADAR_FLAG) != 0)
- {
- if( Me->target == NULL )
- {
- #ifndef XV
- pw_text(Control_pw, STAT_LEFT, 10, yellow_rop, NULL, "radar on") ;
- #else
- text_color.index = dbmap[YELLOW] ;
- xgl_object_set(Text_gfx, XGL_CTX_SFONT_TEXT_COLOR, &text_color, 0) ;
- xgl_stroke_text(Text_gfx, "radar on", &radar_pos, NULL);
- #endif
- }
- else
- {
- register float dx,dy,dz ;
-
- dx = Me->target->Posn.x - Me->Posn.x ;
- dy = Me->target->Posn.y - Me->Posn.y ;
- dz = Me->target->Posn.z - Me->Posn.z ;
- dx = sqrt(dx*dx+dy*dy+dz*dz) ;
- sprintf(line, "radar locked, range =%7.1f %-20.20s",
- dx, Me->target->name) ;
- #ifndef XV
- pw_text(Control_pw, STAT_LEFT, 10, red_rop, NULL, line) ;
- #else
- text_color.index = dbmap[RED] ;
- xgl_object_set(Text_gfx, XGL_CTX_SFONT_TEXT_COLOR, &text_color, 0) ;
- xgl_stroke_text(Text_gfx, line, &radar_pos, NULL);
- #endif
- }
- }
-
- /* score - added by dennis simpson */
-
- sprintf(line,"Score: %d", Me->score);
- #ifndef XV
- pw_text(Control_pw, STAT_LEFT, 90, white_rop, NULL, line);
- #else
- text_color.index = dbmap[WHITE] ;
- xgl_object_set(Text_gfx, XGL_CTX_SFONT_TEXT_COLOR, &text_color, 0) ;
- xgl_stroke_text(Text_gfx, line, &score_pos, NULL);
- #endif
-
- /* radar lock added by ds */
-
- {Object *ob;int i;
- ob=&objects[0];
- line[0] = '\0' ;
- #ifdef XV
- lock_pos.y = .75 ;
- text_color.index = dbmap[YELLOW] ;
- xgl_object_set(Text_gfx, XGL_CTX_SFONT_TEXT_COLOR, &text_color, 0) ;
- for(i=0;i<MAX_OBJECTS && lock_pos.y >= 0.;i++)
- #else
- j = 50 ;
- for(i=0;i<MAX_OBJECTS && j <= 110;i++)
- #endif XV
- {
- if(ob->class == OBJ_PLAYER &&
- ob->status == OBJ_ACTIVE &&
- ob->target == Me)
- {
- sprintf(line,"Locked by %s score: %d", ob->name,ob->score);
- #ifdef XV
- xgl_stroke_text(Text_gfx, line, &lock_pos, NULL) ;
- lock_pos.y -= .15 ;
- #else
- pw_text(Control_pw, LOCK_STAT_LEFT, j,
- yellow_rop, NULL, line);
- j += 20 ;
- #endif XV
- }
- ob++;
- }
-
-
- /* missile armed */
-
- #ifdef XV
- text_color.index = dbmap[WHITE] ;
- xgl_object_set(Text_gfx, XGL_CTX_SFONT_TEXT_COLOR, &text_color, 0) ;
- #endif /* XV */
-
- if( armed_missiles > 0 )
- {
- #ifndef XV
- pw_text(Control_pw, STAT_LEFT, 30, white_rop, NULL, "missile armed") ;
- #else
- xgl_stroke_text(Text_gfx, "missile armed", &armed_pos, NULL);
- }
- #endif XV
- }
-
- /* missiles remaining */
-
- sprintf(line,"missiles remaining: %-2d",missiles_remaining) ;
- #ifndef XV
- pw_text(Control_pw, STAT_LEFT, 50, white_rop, NULL, line) ;
- #else
- xgl_stroke_text(Text_gfx, line, &remain_pos, NULL);
- #endif XV
-
- pw_unlock(Control_pw) ;
-
- dbuf_swap() ;
- }
-
-
-
- set_line_color(ctx, i)
- Xgl_3d_ctx ctx ;
- int i ;
- {
- Xgl_color color ;
-
- color.index = dbmap[i] ;
- xgl_object_set(ctx, XGL_CTX_LINE_COLOR, &color, 0) ;
- }
-
-
-
- set_pgon_color(ctx, i)
- Xgl_3d_ctx ctx ;
- int i ;
- {
- Xgl_color color ;
-
- color.index = dbmap[i] ;
- xgl_object_set(ctx, XGL_CTX_SURF_FRONT_COLOR, &color, 0) ;
- }
-
-
-
- static Mat3d Identity = {{1.0,0.0,0.0,0.0},
- {0.0,1.0,0.0,0.0},
- {0.0,0.0,1.0,0.0},
- {0.0,0.0,0.0,1.0}} ;
-
- static Mat2d Identity2 = {{1.0,0.0},
- {0.0,1.0},
- {0.0,0.0}} ;
-
-
-
-
-
- ident_mat(matrix)
- Mat3d matrix ;
- {
- bcopy(Identity, matrix, sizeof(Mat3d)) ;
- }
-
-
- /*
- * viewplane model of perspective. calling routine specifies the
- * z-coordinate from which the model is being viewed (ze) and the
- * z-coordinate of the viewing projection plane (zp, typically 0.0)
- */
-
- void
- persp(ze,zp,matrix)
- float ze,zp ;
- Mat3d matrix ;
- {
- bcopy(Identity, matrix, sizeof(Mat3d)) ;
-
- if(ze!=zp)
- {
- matrix[2][3] = -1.0/(zp-ze) ;
- matrix[3][3] = -ze * matrix[2][3] ;
- matrix[2][2] = matrix[2][3] + matrix[3][3] ;
- }
- }
-
-
- /*
- * build a matrix that maps the region defined by [x0,y0,z0 x1,y1,z1]
- * to the gp's viewport
- */
-
-
- window_mat2(x0,x1,y0,y1,matrix)
- float x0,x1,y0,y1 ;
- register Mat2d matrix ;
- {
- bcopy(Identity2, matrix, sizeof(Mat2d)) ;
-
- matrix[0][0] = 2.0/(x1-x0) ;
- matrix[1][1] = 2.0/(y1-y0) ;
- matrix[2][0] = -1.0 - x0 * matrix[0][0] ;
- matrix[2][1] = -1.0 - y0 * matrix[1][1] ;
- }
-
-
- window_mat(x0,x1,y0,y1,z0,z1,matrix)
- float x0,x1,y0,y1,z0,z1 ;
- register Mat3d matrix ;
- {
- bcopy(Identity, matrix, sizeof(Mat3d)) ;
-
- matrix[0][0] = 2.0/(x1-x0) ;
- matrix[1][1] = 2.0/(y1-y0) ;
- matrix[2][2] = 1.0/(z1-z0) ;
- matrix[3][0] = -1.0 - x0 * matrix[0][0] ;
- matrix[3][1] = -1.0 - y0 * matrix[1][1] ;
- matrix[3][2] = - z0 * matrix[2][2] ;
- }
-
-
-
-
- persp_window_mat(ze,zp,x0,x1,y0,y1,z0,z1,matrix)
- float ze,zp,x0,x1,y0,y1,z0,z1 ;
- register Mat3d matrix ;
- {
- register float dist ;
-
- bcopy(Identity, matrix, sizeof(Mat3d)) ;
-
- dist = x1-x0 ;
- matrix[0][0] = 2.0/dist ;
- matrix[3][0] = -(x1+x0)*.5*matrix[0][0] ;
-
- dist = y1-y0 ;
- matrix[1][1] = 2.0/dist ;
- matrix[3][1] = -(y1+y0)*.5*matrix[1][1] ;
-
- dist = zp-ze ;
- matrix[2][3] = 1.0/dist ;
- matrix[3][3] = -ze * matrix[2][3] ;
-
- dist = z1-z0 ;
- matrix[2][2] = (z1*matrix[2][3] + matrix[3][3]) / dist ;
- matrix[3][2] = -z0 * matrix[2][2] ;
- }
-