// check a polymodel for it's color and return it int g3_poly_get_color(void *model_ptr) { ubyte *p = model_ptr; int color = 0; while (w(p) != OP_EOF) switch (w(p)) { case OP_DEFPOINTS: p += w(p+2)*sizeof(struct vms_vector) + 4; break; case OP_DEFP_START: p += w(p+2)*sizeof(struct vms_vector) + 8; break; case OP_FLATPOLY: { int nv = w(p+2); if (g3_check_normal_facing(vp(p+4),vp(p+16)) > 0) color = (w(p+28)); p += 30 + ((nv&~1)+1)*2; break; } case OP_TMAPPOLY: { int nv = w(p+2); p += 30 + ((nv&~1)+1)*2 + nv*12; break; } case OP_SORTNORM: if (g3_check_normal_facing(vp(p+16),vp(p+4)) > 0) //facing color = g3_poly_get_color(p+w(p+28)); else //not facing color = g3_poly_get_color(p+w(p+30)); p += 32; break; case OP_RODBM: p+=36; break; case OP_SUBCALL: color = g3_poly_get_color(p+w(p+16)); p += 20; break; case OP_GLOW: p += 4; break; default: ; } return color; }
bool do_facing_check(vms_vector *norm,g3s_point **vertlist,vms_vector *p) { if (norm) { //have normal Assert(norm->x || norm->y || norm->z); return g3_check_normal_facing(p,norm); } else { //normal not specified, so must compute vms_vector tempv; //get three points (rotated) and compute normal vm_vec_perp(&tempv,&vertlist[0]->p3_vec,&vertlist[1]->p3_vec,&vertlist[2]->p3_vec); return (vm_vec_dot(&tempv,&vertlist[1]->p3_vec) < 0); } }
//alternate interpreter for morphing object bool g3_draw_morphing_model(ubyte *p,grs_bitmap **model_bitmaps,vms_angvec *anim_angles,g3s_lrgb model_light,vms_vector *new_points) { fix *glow_values = NULL; glow_num = -1; //glow off by default while (w(p) != OP_EOF) switch (w(p)) { case OP_DEFPOINTS: { int n = w(p+2); rotate_point_list(Interp_point_list,new_points,n); p += n*sizeof(struct vms_vector) + 4; break; } case OP_DEFP_START: { int n = w(p+2); int s = w(p+4); rotate_point_list(&Interp_point_list[s],new_points,n); p += n*sizeof(struct vms_vector) + 8; break; } case OP_FLATPOLY: { int nv = w(p+2); int i,ntris; gr_setcolor(w(p+28)); for (i=0;i<2;i++) point_list[i] = Interp_point_list + wp(p+30)[i]; for (ntris=nv-2;ntris;ntris--) { point_list[2] = Interp_point_list + wp(p+30)[i++]; g3_check_and_draw_poly(3,point_list,NULL,NULL); point_list[1] = point_list[2]; } p += 30 + ((nv&~1)+1)*2; break; } case OP_TMAPPOLY: { int nv = w(p+2); g3s_uvl *uvl_list; g3s_lrgb light, *lrgb_list; g3s_uvl morph_uvls[3]; int i,ntris; MALLOC(lrgb_list, g3s_lrgb, nv); //calculate light from surface normal if (glow_num < 0) //no glow { light.r = light.g = light.b = -vm_vec_dot(&View_matrix.fvec,vp(p+16)); light.r = f1_0/4 + (light.r*3)/4; light.r = fixmul(light.r,model_light.r); light.g = f1_0/4 + (light.g*3)/4; light.g = fixmul(light.g,model_light.g); light.b = f1_0/4 + (light.b*3)/4; light.b = fixmul(light.b,model_light.b); } else //yes glow { light.r = light.g = light.b = glow_values[glow_num]; glow_num = -1; } //now poke light into l values uvl_list = (g3s_uvl *) (p+30+((nv&~1)+1)*2); for (i=0;i<nv;i++) { lrgb_list[i].r = light.r; lrgb_list[i].g = light.g; lrgb_list[i].b = light.b; } for (i=0;i<3;i++) morph_uvls[i].l = (light.r+light.g+light.b)/3; for (i=0;i<2;i++) { point_list[i] = Interp_point_list + wp(p+30)[i]; morph_uvls[i].u = uvl_list[i].u; morph_uvls[i].v = uvl_list[i].v; } for (ntris=nv-2;ntris;ntris--) { point_list[2] = Interp_point_list + wp(p+30)[i]; morph_uvls[2].u = uvl_list[i].u; morph_uvls[2].v = uvl_list[i].v; i++; g3_check_and_draw_tmap(3,point_list,uvl_list,lrgb_list,model_bitmaps[w(p+28)],NULL,NULL); point_list[1] = point_list[2]; morph_uvls[1].u = morph_uvls[2].u; morph_uvls[1].v = morph_uvls[2].v; } p += 30 + ((nv&~1)+1)*2 + nv*12; d_free(lrgb_list); break; } case OP_SORTNORM: if (g3_check_normal_facing(vp(p+16),vp(p+4)) > 0) { //facing //draw back then front g3_draw_morphing_model(p+w(p+30),model_bitmaps,anim_angles,model_light,new_points); g3_draw_morphing_model(p+w(p+28),model_bitmaps,anim_angles,model_light,new_points); } else { //not facing. draw front then back g3_draw_morphing_model(p+w(p+28),model_bitmaps,anim_angles,model_light,new_points); g3_draw_morphing_model(p+w(p+30),model_bitmaps,anim_angles,model_light,new_points); } p += 32; break; case OP_RODBM: { g3s_point rod_bot_p,rod_top_p; g3s_lrgb rodbm_light = { f1_0, f1_0, f1_0 }; g3_rotate_point(&rod_bot_p,vp(p+20)); g3_rotate_point(&rod_top_p,vp(p+4)); g3_draw_rod_tmap(model_bitmaps[w(p+2)],&rod_bot_p,w(p+16),&rod_top_p,w(p+32),rodbm_light); p+=36; break; } case OP_SUBCALL: { vms_angvec *a; if (anim_angles) a = &anim_angles[w(p+2)]; else a = &zero_angles; g3_start_instance_angles(vp(p+4),a); g3_draw_polygon_model(p+w(p+16),model_bitmaps,anim_angles,model_light,glow_values); g3_done_instance(); p += 20; break; } case OP_GLOW: if (glow_values) glow_num = w(p+2); p += 4; break; } return 1; }
//calls the object interpreter to render an object. The object renderer //is really a seperate pipeline. returns true if drew bool g3_draw_polygon_model(ubyte *p,grs_bitmap **model_bitmaps,vms_angvec *anim_angles,g3s_lrgb model_light,fix *glow_values) { glow_num = -1; //glow off by default while (w(p) != OP_EOF) switch (w(p)) { case OP_DEFPOINTS: { int n = w(p+2); rotate_point_list(Interp_point_list,vp(p+4),n); p += n*sizeof(struct vms_vector) + 4; break; } case OP_DEFP_START: { int n = w(p+2); int s = w(p+4); rotate_point_list(&Interp_point_list[s],vp(p+8),n); p += n*sizeof(struct vms_vector) + 8; break; } case OP_FLATPOLY: { int nv = w(p+2); Assert( nv < MAX_POINTS_PER_POLY ); if (g3_check_normal_facing(vp(p+4),vp(p+16)) > 0) { int i; #ifdef FADE_FLATPOLY short c; unsigned char cc; int l; #endif // DPH: Now we treat this color as 15bpp // gr_setcolor(w(p+28)); #ifndef FADE_FLATPOLY gr_setcolor(gr_find_closest_color_15bpp(w(p + 28))); #else //l = (32 * model_light) >> 16; l = f2i(fixmul(i2f(32), (model_light.r+model_light.g+model_light.b)/3)); if (l<0) l = 0; else if (l>32) l = 32; cc = gr_find_closest_color_15bpp(w(p+28)); c = gr_fade_table[(l<<8)|cc]; gr_setcolor(c); #endif for (i=0;i<nv;i++) point_list[i] = Interp_point_list + wp(p+30)[i]; g3_draw_poly(nv,point_list); } p += 30 + ((nv&~1)+1)*2; break; } case OP_TMAPPOLY: { int nv = w(p+2); g3s_uvl *uvl_list; Assert( nv < MAX_POINTS_PER_POLY ); if (g3_check_normal_facing(vp(p+4),vp(p+16)) > 0) { int i; g3s_lrgb light, *lrgb_list; MALLOC(lrgb_list, g3s_lrgb, nv); //calculate light from surface normal if (glow_num < 0) //no glow { light.r = light.g = light.b = -vm_vec_dot(&View_matrix.fvec,vp(p+16)); light.r = f1_0/4 + (light.r*3)/4; light.r = fixmul(light.r,model_light.r); light.g = f1_0/4 + (light.g*3)/4; light.g = fixmul(light.g,model_light.g); light.b = f1_0/4 + (light.b*3)/4; light.b = fixmul(light.b,model_light.b); } else //yes glow { light.r = light.g = light.b = glow_values[glow_num]; glow_num = -1; } //now poke light into l values uvl_list = (g3s_uvl *) (p+30+((nv&~1)+1)*2); for (i=0;i<nv;i++) { uvl_list[i].l = (light.r+light.g+light.b)/3; lrgb_list[i].r = light.r; lrgb_list[i].g = light.g; lrgb_list[i].b = light.b; } for (i=0;i<nv;i++) point_list[i] = Interp_point_list + wp(p+30)[i]; g3_draw_tmap(nv,point_list,uvl_list,lrgb_list,model_bitmaps[w(p+28)]); d_free(lrgb_list); } p += 30 + ((nv&~1)+1)*2 + nv*12; break; } case OP_SORTNORM: if (g3_check_normal_facing(vp(p+16),vp(p+4)) > 0) { //facing //draw back then front g3_draw_polygon_model(p+w(p+30),model_bitmaps,anim_angles,model_light,glow_values); g3_draw_polygon_model(p+w(p+28),model_bitmaps,anim_angles,model_light,glow_values); } else { //not facing. draw front then back g3_draw_polygon_model(p+w(p+28),model_bitmaps,anim_angles,model_light,glow_values); g3_draw_polygon_model(p+w(p+30),model_bitmaps,anim_angles,model_light,glow_values); } p += 32; break; case OP_RODBM: { g3s_point rod_bot_p,rod_top_p; g3s_lrgb rodbm_light = { f1_0, f1_0, f1_0 }; g3_rotate_point(&rod_bot_p,vp(p+20)); g3_rotate_point(&rod_top_p,vp(p+4)); g3_draw_rod_tmap(model_bitmaps[w(p+2)],&rod_bot_p,w(p+16),&rod_top_p,w(p+32),rodbm_light); p+=36; break; } case OP_SUBCALL: { vms_angvec *a; if (anim_angles) a = &anim_angles[w(p+2)]; else a = &zero_angles; g3_start_instance_angles(vp(p+4),a); g3_draw_polygon_model(p+w(p+16),model_bitmaps,anim_angles,model_light,glow_values); g3_done_instance(); p += 20; break; } case OP_GLOW: if (glow_values) glow_num = w(p+2); p += 4; break; default: Error("invalid polygon model\n"); } return 1; }
// check a polymodel for it's color and return it int g3_poly_get_color(ubyte *p) { int color = 0; while (w(p) != OP_EOF) switch (w(p)) { case OP_DEFPOINTS: p += w(p+2)*sizeof(struct vms_vector) + 4; break; case OP_DEFP_START: p += w(p+2)*sizeof(struct vms_vector) + 8; break; case OP_FLATPOLY: { int nv = w(p+2); Assert( nv < MAX_POINTS_PER_POLY ); if (g3_check_normal_facing(vp(p+4),vp(p+16)) > 0) { #ifdef FADE_FLATPOLY short c; unsigned char cc; int l; #endif #ifndef FADE_FLATPOLY color = gr_find_closest_color_15bpp(w(p + 28)); #else //l = (32 * model_light) >> 16; l = f2i(fixmul(i2f(32), (model_light.r+model_light.g+model_light.b)/3)); if (l<0) l = 0; else if (l>32) l = 32; cc = gr_find_closest_color_15bpp(w(p+28)); color = gr_fade_table[(l<<8)|cc]; #endif } p += 30 + ((nv&~1)+1)*2; break; } case OP_TMAPPOLY: { int nv = w(p+2); p += 30 + ((nv&~1)+1)*2 + nv*12; break; } case OP_SORTNORM: if (g3_check_normal_facing(vp(p+16),vp(p+4)) > 0) //facing color = g3_poly_get_color(p+w(p+28)); else //not facing color = g3_poly_get_color(p+w(p+30)); p += 32; break; case OP_RODBM: p+=36; break; case OP_SUBCALL: p += 20; break; case OP_GLOW: p += 4; break; default: Error("invalid polygon model\n"); } return color; }
//adds a segment's edges to the edge list void add_edges(segment *seg) { short *svp; int nv; g3s_codes cc; med_get_vertex_list(seg,&nv,&svp); // set nv = number of vertices, svp = pointer to vertex indices cc=rotate_list(nv,svp); if (! cc.and) { //all off screen? int i,sn,fn,vn; int flag; ubyte edge_flags[N_EDGES_PER_SEGMENT]; for (i=0;i<N_NORMAL_EDGES;i++) edge_flags[i]=ET_NOTUSED; for (;i<N_EDGES_PER_SEGMENT;i++) edge_flags[i]=ET_NOTEXTANT; for (sn=0;sn<MAX_SIDES_PER_SEGMENT;sn++) { side *sidep = &seg->sides[sn]; int num_faces, num_vertices; int vertex_list[6]; create_all_vertex_lists(&num_faces, vertex_list, seg-Segments, sn); if (num_faces == 1) num_vertices = 4; else num_vertices = 3; for (fn=0; fn<num_faces; fn++) { int en; int *v0; //Note: normal check appears to be the wrong way since the normals points in, but we're looking from the outside if (g3_check_normal_facing(&Vertices[seg->verts[vertex_list[fn*3]]],&sidep->normals[fn])) flag = ET_NOTFACING; else flag = ET_FACING; v0 = &vertex_list[fn*3]; for (vn=0; vn<num_vertices-1; vn++) { // en = find_edge_num(vertex_list[fn*3 + vn], vertex_list[fn*3 + (vn+1)%num_vertices]); en = find_edge_num(*v0, *(v0+1)); if (en!=-1) if (flag < edge_flags[en]) edge_flags[en] = flag; v0++; } en = find_edge_num(*v0, vertex_list[fn*3]); if (en!=-1) if (flag < edge_flags[en]) edge_flags[en] = flag; } } for (i=0; i<N_EDGES_PER_SEGMENT; i++) if (i<N_NORMAL_EDGES || (edge_flags[i]!=ET_NOTEXTANT && Show_triangulations)) add_edge(seg->verts[edges[i]/8],seg->verts[edges[i]&7],edge_flags[i]); } }
//calls the object interpreter to render an object. The object renderer //is really a seperate pipeline. returns true if drew bool g3_draw_polygon_model(void *model_ptr, grs_bitmap **model_bitmaps, vms_angvec *anim_angles, fix model_light, fix *glow_values) { ubyte *p = (ubyte*)model_ptr; int current_poly = 0; glow_num = -1; //glow off by default while (w(p) != OP_EOF) switch (w(p)) { case OP_DEFPOINTS: { int n = w(p + 2); rotate_point_list(Interp_point_list, vp(p + 4), n); p += n*sizeof(struct vms_vector) + 4; break; } case OP_DEFP_START: { int n = w(p + 2); int s = w(p + 4); rotate_point_list(&Interp_point_list[s], vp(p + 8), n); p += n*sizeof(struct vms_vector) + 8; break; } case OP_FLATPOLY: { int nv = w(p + 2); Assert(nv < MAX_POINTS_PER_POLY); if (g3_check_normal_facing(vp(p + 4), vp(p + 16)) > 0) { int i; gr_setcolor(w(p + 28)); for (i = 0; i<nv; i++) point_list[i] = Interp_point_list + wp(p + 30)[i]; g3_draw_poly(nv, point_list); } p += 30 + ((nv&~1) + 1) * 2; break; } case OP_TMAPPOLY: { int nv = w(p + 2); g3s_uvl *uvl_list; Assert(nv < MAX_POINTS_PER_POLY); if (g3_check_normal_facing(vp(p + 4), vp(p + 16)) > 0) { int i; fix light; //calculate light from surface normal if (glow_num < 0) { //no glow light = -vm_vec_dot(&View_matrix.fvec, vp(p + 16)); light = f1_0 / 4 + (light * 3) / 4; light = fixmul(light, model_light); } else { //yes glow light = glow_values[glow_num]; glow_num = -1; } //now poke light into l values uvl_list = (g3s_uvl *)(p + 30 + ((nv&~1) + 1) * 2); for (i = 0; i<nv; i++) uvl_list[i].l = light; for (i = 0; i<nv; i++) point_list[i] = Interp_point_list + wp(p + 30)[i]; g3_draw_tmap(nv, point_list, uvl_list, model_bitmaps[w(p + 28)]); } p += 30 + ((nv&~1) + 1) * 2 + nv * 12; break; } case OP_SORTNORM: if (g3_check_normal_facing(vp(p + 16), vp(p + 4)) > 0) { //facing //draw back then front g3_draw_polygon_model(p + w(p + 30), model_bitmaps, anim_angles, model_light, glow_values); g3_draw_polygon_model(p + w(p + 28), model_bitmaps, anim_angles, model_light, glow_values); } else { //not facing. draw front then back g3_draw_polygon_model(p + w(p + 28), model_bitmaps, anim_angles, model_light, glow_values); g3_draw_polygon_model(p + w(p + 30), model_bitmaps, anim_angles, model_light, glow_values); } p += 32; break; case OP_RODBM: { g3s_point rod_bot_p, rod_top_p; g3_rotate_point(&rod_bot_p, vp(p + 20)); g3_rotate_point(&rod_top_p, vp(p + 4)); g3_draw_rod_tmap(model_bitmaps[w(p + 2)], &rod_bot_p, w(p + 16), &rod_top_p, w(p + 32), f1_0); p += 36; break; } case OP_SUBCALL: { vms_angvec *a; if (anim_angles) a = &anim_angles[w(p + 2)]; else a = &zero_angles; g3_start_instance_angles(vp(p + 4), a); g3_draw_polygon_model(p + w(p + 16), model_bitmaps, anim_angles, model_light, glow_values); g3_done_instance(); p += 20; break; } case OP_GLOW: if (glow_values) glow_num = w(p + 2); p += 4; break; default: Int3(); } return 1; }
void draw_all_edges(automap *am) { g3s_codes cc; int i,j,nbright; ubyte nfacing,nnfacing; Edge_info *e; vms_vector *tv1; fix distance; fix min_distance = 0x7fffffff; g3s_point *p1, *p2; nbright=0; for (i=0; i<=am->highest_edge_index; i++ ) { //e = &am->edges[Edge_used_list[i]]; e = &am->edges[i]; if (!(e->flags & EF_USED)) continue; if ( e->flags & EF_TOO_FAR) continue; if (e->flags&EF_FRONTIER) { // A line that is between what we have seen and what we haven't if ( (!(e->flags&EF_SECRET))&&(e->color==am->wall_normal_color)) continue; // If a line isn't secret and is normal color, then don't draw it } cc=rotate_list(2,e->verts); distance = Segment_points[e->verts[1]].p3_z; if (min_distance>distance ) min_distance = distance; if (!cc.uand) { //all off screen? nfacing = nnfacing = 0; tv1 = &Vertices[e->verts[0]]; j = 0; while( j<e->num_faces && (nfacing==0 || nnfacing==0) ) { #ifdef COMPACT_SEGS vms_vector temp_v; get_side_normal(&Segments[e->segnum[j]], e->sides[j], 0, &temp_v ); if (!g3_check_normal_facing( tv1, &temp_v ) ) #else if (!g3_check_normal_facing( tv1, &Segments[e->segnum[j]].sides[e->sides[j]].normals[0] ) ) #endif nfacing++; else nnfacing++; j++; } if ( nfacing && nnfacing ) { // a contour line am->drawingListBright[nbright++] = e-am->edges; } else if ( e->flags&(EF_DEFINING|EF_GRATE) ) { if ( nfacing == 0 ) { if ( e->flags & EF_NO_FADE ) gr_setcolor( e->color ); else gr_setcolor( gr_fade_table[e->color+256*8] ); g3_draw_line( &Segment_points[e->verts[0]], &Segment_points[e->verts[1]] ); } else { am->drawingListBright[nbright++] = e-am->edges; } } } } if ( min_distance < 0 ) min_distance = 0; // Sort the bright ones using a shell sort { int t; int i, j, incr, v1, v2; incr = nbright / 2; while( incr > 0 ) { for (i=incr; i<nbright; i++ ) { j = i - incr; while (j>=0 ) { // compare element j and j+incr v1 = am->edges[am->drawingListBright[j]].verts[0]; v2 = am->edges[am->drawingListBright[j+incr]].verts[0]; if (Segment_points[v1].p3_z < Segment_points[v2].p3_z) { // If not in correct order, them swap 'em t=am->drawingListBright[j+incr]; am->drawingListBright[j+incr]=am->drawingListBright[j]; am->drawingListBright[j]=t; j -= incr; } else break; } } incr = incr / 2; } } // Draw the bright ones for (i=0; i<nbright; i++ ) { int color; fix dist; e = &am->edges[am->drawingListBright[i]]; p1 = &Segment_points[e->verts[0]]; p2 = &Segment_points[e->verts[1]]; dist = p1->p3_z - min_distance; // Make distance be 1.0 to 0.0, where 0.0 is 10 segments away; if ( dist < 0 ) dist=0; if ( dist >= am->farthest_dist ) continue; if ( e->flags & EF_NO_FADE ) { gr_setcolor( e->color ); } else { dist = F1_0 - fixdiv( dist, am->farthest_dist ); color = f2i( dist*31 ); gr_setcolor( gr_fade_table[e->color+color*256] ); } g3_draw_line( p1, p2 ); } }
void draw_model(polymodel *pm,int submodel_num,vms_angvec *anim_angles,fix light,morph_data *md) { int i,mn; int facing; int sort_list[MAX_SUBMODELS],sort_n; //first, sort the submodels sort_list[0] = submodel_num; sort_n = 1; for (i=0;i<pm->n_models;i++) if (md->submodel_active[i] && pm->submodel_parents[i]==submodel_num) { facing = g3_check_normal_facing(&pm->submodel_pnts[i],&pm->submodel_norms[i]); if (!facing) sort_list[sort_n++] = i; else { //put at start int t; for (t=sort_n;t>0;t--) sort_list[t] = sort_list[t-1]; sort_list[0] = i; sort_n++; } } //now draw everything for (i=0;i<sort_n;i++) { mn = sort_list[i]; if (mn == submodel_num) { int i; for (i=0;i<pm->n_textures;i++) { texture_list_index[i] = ObjBitmaps[ObjBitmapPtrs[pm->first_texture+i]]; texture_list[i] = &GameBitmaps[ObjBitmaps[ObjBitmapPtrs[pm->first_texture+i]].index]; } #ifdef PIGGY_USE_PAGING // Make sure the textures for this object are paged in... piggy_page_flushed = 0; for (i=0;i<pm->n_textures;i++) PIGGY_PAGE_IN( texture_list_index[i] ); // Hmmm... cache got flushed in the middle of paging all these in, // so we need to reread them all in. if (piggy_page_flushed) { piggy_page_flushed = 0; for (i=0;i<pm->n_textures;i++) PIGGY_PAGE_IN( texture_list_index[i] ); } // Make sure that they can all fit in memory. Assert( piggy_page_flushed == 0 ); #endif g3_draw_morphing_model(&pm->model_data[pm->submodel_ptrs[submodel_num]],texture_list,anim_angles,light,&md->morph_vecs[md->submodel_startpoints[submodel_num]]); } else { vms_matrix orient; vm_angles_2_matrix(&orient,&anim_angles[mn]); g3_start_instance_matrix(&pm->submodel_offsets[mn],&orient); draw_model(pm,mn,anim_angles,light,md); g3_done_instance(); } } }