📄 test_sphere.c
字号:
MENU_STRING, "Off", MENU_NOTIFY_PROC, zbuff_proc, 0, 0); menu_face = xv_create(XV_NULL,MENU_COMMAND_MENU, MENU_ITEM, MENU_STRING, "Off", MENU_NOTIFY_PROC, face_proc, 0, MENU_ITEM, MENU_STRING, "Front", MENU_NOTIFY_PROC, face_proc, 0, MENU_ITEM, MENU_STRING, "Back", MENU_NOTIFY_PROC, face_proc, 0, 0); menu_disting = xv_create(XV_NULL,MENU_COMMAND_MENU, MENU_ITEM, MENU_STRING, "False", MENU_NOTIFY_PROC, disting_proc, 0, MENU_ITEM, MENU_STRING, "True", MENU_NOTIFY_PROC, disting_proc, 0, 0); menu_norm_flip = xv_create(XV_NULL,MENU_COMMAND_MENU, MENU_ITEM, MENU_STRING, "False", MENU_NOTIFY_PROC, norm_flip_proc, 0, MENU_ITEM, MENU_STRING, "True", MENU_NOTIFY_PROC, norm_flip_proc, 0, 0); /* number of spheres sub-menu */ menu_spheres = xv_create(XV_NULL,MENU_COMMAND_MENU, MENU_ITEM, MENU_STRING, "0", MENU_NOTIFY_PROC, num_spheres_proc, 0, MENU_ITEM, MENU_STRING, "1", MENU_NOTIFY_PROC, num_spheres_proc, 0, MENU_ITEM, MENU_STRING, "2", MENU_NOTIFY_PROC, num_spheres_proc, 0, MENU_ITEM, MENU_STRING, "3", MENU_NOTIFY_PROC, num_spheres_proc, 0, MENU_ITEM, MENU_STRING, "4", MENU_NOTIFY_PROC, num_spheres_proc, 0, MENU_ITEM, MENU_STRING, "5", MENU_NOTIFY_PROC, num_spheres_proc, 0, MENU_ITEM, MENU_STRING, "6", MENU_NOTIFY_PROC, num_spheres_proc, 0, MENU_ITEM, MENU_STRING, "7", MENU_NOTIFY_PROC, num_spheres_proc, 0, MENU_ITEM, MENU_STRING, "8", MENU_NOTIFY_PROC, num_spheres_proc, 0, 0); /* main application menu */ menu_main = xv_create(XV_NULL,MENU_COMMAND_MENU, MENU_ITEM, MENU_STRING, "Clear", MENU_NOTIFY_PROC, clear_proc, 0, MENU_ITEM, MENU_STRING, "Redraw", MENU_NOTIFY_PROC, redraw_proc, 0, MENU_ITEM, MENU_STRING, "Front Shading", MENU_PULLRIGHT, menu_front_shading, 0, MENU_ITEM, MENU_STRING, "Back Shading", MENU_PULLRIGHT, menu_back_shading, 0, MENU_ITEM, MENU_STRING, "Edges", MENU_PULLRIGHT, menu_edges, 0, MENU_ITEM, MENU_STRING, "Z-Buffer", MENU_PULLRIGHT, menu_zbuff, 0, MENU_ITEM, MENU_STRING, "Face Culling", MENU_PULLRIGHT, menu_face, 0, MENU_ITEM, MENU_STRING, "Face Distinguish", MENU_PULLRIGHT, menu_disting, 0, MENU_ITEM, MENU_STRING, "Normal Flip", MENU_PULLRIGHT, menu_norm_flip, 0, MENU_ITEM, MENU_STRING, "Spheres", MENU_PULLRIGHT, menu_spheres, 0, MENU_ITEM, MENU_STRING, tris_count_str, 0, MENU_ITEM, MENU_STRING, timestr, 0, MENU_ITEM, MENU_STRING, tris_per_sec, 0,#ifdef TRI_PERF MENU_ITEM, MENU_STRING, tri_area, 0,#endif 0);}/********** **********//********** Data Handling Routines **********//********** **********//**** * get_data(filename) * * Retrieve triangle strip data from ASCII file and reformat data into * Xgl data structures. * * Input data must be formatted using the .tri file format. * ****/staticvoidget_data(filename) char *filename;{ char ch[4]; register int i; float f0, f1, f2, f3; int color, text_len; int facet; /* boolean; working on facet info? */ int num_tri_strip, totxgl_num_facets, totxgl_num_pts; int num_facets, num_pts; int num_plin, num_lin, num_debug; int ignore; FILE *file_pt, *fopen(); bbox_pl = (Xgl_bbox *)0; txt_index = 0; box_index = 0; pts_index = 0; pl_index = 0; fct_index = 0; if (pl) free(pl); if (fct_list) free(fct_list); if (p_ind) free(p_ind); if (f_ind) free(f_ind); if (l_bbox) free(l_bbox); if (pts_nc) free(pts_nc); if (fct_nc) free(fct_nc); if (texts) { for (i = 0; i < num_txt; i++) free(texts[i].str); free (texts); } texts = (text_info *)0; pl = (Xgl_pt_list *)0; fct_list = (Xgl_facet_list *)0; p_ind = (int *)0; f_ind = (int *)0; l_bbox = (Xgl_bbox *)0; pts_nc = (Xgl_pt_color_normal_flag_f3d *)0; fct_nc = (Xgl_color_normal_facet *)0; if (!(file_pt = fopen(filename,"r"))) { printf("ERROR: file %s not found!\n", filename); printf(" Try using the -f command line switch.\n\n"); data_ok = FALSE; exit(-1); } else { /* file found, scan it */ while((i = fscanf(file_pt,"%s",ch)) != EOF) { switch (ch[0]) { case 'A' : /* viewing info */ fscanf(file_pt,"%f %f %f", &p_eye_perspective.x, &p_eye_perspective.y, &p_eye_perspective.z); fscanf(file_pt,"%f %f %f", &p_eye_position_pt.x, &p_eye_position_pt.y, &p_eye_position_pt.z); fscanf(file_pt,"%f %f %f", &p_eye_look_at_pt.x, &p_eye_look_at_pt.y, &p_eye_look_at_pt.z); fscanf(file_pt,"%f %f %f", &p_eye_up_vector.x, &p_eye_up_vector.y, &p_eye_up_vector.z); p_eye_new_view = TRUE; break; case 'a' : /* facet or vertex RGBa color */ fscanf(file_pt,"%f %f %f %f",&f0,&f1,&f2,&f3); if (facet) { fct_nc[pl_index - 1].color.rgb.r = f0; fct_nc[pl_index - 1].color.rgb.g = f1; fct_nc[pl_index - 1].color.rgb.b = f2; } else { pts_nc[pts_index - 1].color.rgb.r = f0; pts_nc[pts_index - 1].color.rgb.g = f1; pts_nc[pts_index - 1].color.rgb.b = f2; } break; case 'B' : /* bounding box for all pts in this file */ fscanf(file_pt,"%f %f %f", &f0, &f1, &f2); pl_bbox.bbox_type = XGL_BBOX_F3D; pl_bbox.box.f3d.xmin = f0; pl_bbox.box.f3d.ymin = f1; pl_bbox.box.f3d.zmin = f2; fscanf(file_pt,"%f %f %f", &f0, &f1, &f2); pl_bbox.box.f3d.xmax = f0; pl_bbox.box.f3d.ymax = f1; pl_bbox.box.f3d.zmax = f2; bbox_pl = &pl_bbox; if (pl) pl[pl_index - 1].bbox = bbox_pl; break; case 'b' : /* facet bounding box */ fscanf(file_pt,"%f %f %f", &f0, &f1, &f2); l_bbox[box_index].bbox_type = XGL_BBOX_F3D; l_bbox[box_index].box.f3d.xmin = f0; l_bbox[box_index].box.f3d.ymin = f1; l_bbox[box_index].box.f3d.zmin = f2; fscanf(file_pt,"%f %f %f", &f0, &f1, &f2); l_bbox[box_index].box.f3d.xmax = f0; l_bbox[box_index].box.f3d.ymax = f1; l_bbox[box_index].box.f3d.zmax = f2; pl[pl_index - 1].bbox = (l_bbox + box_index); box_index += 1; break; case 'c' : /* facet or vertex RGB color */ fscanf(file_pt,"%f %f %f",&f0,&f1,&f2); if (facet) { fct_nc[fct_index - 1].color.rgb.r = f0; fct_nc[fct_index - 1].color.rgb.g = f1; fct_nc[fct_index - 1].color.rgb.b = f2; } else { pts_nc[pts_index - 1].color.rgb.r = f0; pts_nc[pts_index - 1].color.rgb.g = f1; pts_nc[pts_index - 1].color.rgb.b = f2; } break; case 'i' : /* facet or vertex index color */ fscanf(file_pt,"%d",&color); if (color == 127) color--; if (facet) { fct_nc[fct_index - 1].color.index = color; } else { pts_nc[pts_index - 1].color.index = color; } break; case 'n' : /* facet or vertex normal */ fscanf(file_pt,"%f %f %f",&f0,&f1,&f2); if (facet) { fct_nc[fct_index - 1].normal.x = f0; fct_nc[fct_index - 1].normal.y = f1; fct_nc[fct_index - 1].normal.z = f2; } else { pts_nc[pts_index - 1].normal.x = f0; pts_nc[pts_index - 1].normal.y = f1; pts_nc[pts_index - 1].normal.z = f2; } break; case 'P' : /* header line for each triangle strip in file */ fscanf(file_pt,"%d %d", &num_facets, &num_pts); pl[pl_index].num_pts = num_pts; pl[pl_index].pt_type = XGL_PT_COLOR_NORMAL_FLAG_F3D; pl[pl_index].pts.color_normal_flag_f3d = &(pts_nc[pts_index]); p_ind[pl_index] = pts_index; f_ind[pl_index] = fct_index; fct_list[pl_index].facet_type = fct_type; fct_list[pl_index].num_facets = num_facets; fct_list[pl_index].facets.color_normal_facets = &(fct_nc[fct_index]); pl_index++; break; case 'p' : /* header line for vertex data to follow */ fscanf(file_pt,"%d %d", &num_debug, &ignore); fct_index++; facet = 1; break; case 'S' : /* # of strips, total # of triangles, total # of vertices */ fscanf(file_pt,"%d %d %d", &num_tri_strip, &totxgl_num_facets, &totxgl_num_pts); num_pl = num_tri_strip; pl = (Xgl_pt_list *)(malloc(num_pl * sizeof(Xgl_pt_list))); fct_list = (Xgl_facet_list *)(malloc(num_pl * sizeof(Xgl_facet_list))); p_ind = (int *)(malloc(num_pl * sizeof(int))); f_ind = (int *)(malloc(num_pl * sizeof(int))); l_bbox = (Xgl_bbox *)(malloc(num_pl * sizeof(Xgl_bbox))); pts_nc = (Xgl_pt_color_normal_flag_f3d *) (malloc(totxgl_num_pts * sizeof(Xgl_pt_color_normal_flag_f3d))); fct_nc = (Xgl_color_normal_facet *) (malloc(totxgl_num_facets *sizeof(Xgl_color_normal_facet))); break; case 'T' : /* number of text strings in file */ fscanf(file_pt,"%d",&num_txt); texts = (text_info *)(malloc(num_txt * sizeof(text_info))); break; case 't' : /* text info */ fscanf(file_pt,"%f %f %f", &f0, &f1, &f2); texts[txt_index].pos.x = f0; texts[txt_index].pos.y = f1; texts[txt_index].pos.z = f2; fscanf(file_pt,"%f %f %f", &f0, &f1, &f2); texts[txt_index].dir[0].x = f0; texts[txt_index].dir[0].y = f1; texts[txt_index].dir[0].z = f2; fscanf(file_pt,"%f %f %f", &f0, &f1, &f2); texts[txt_index].dir[1].x = f0; texts[txt_index].dir[1].y = f1; texts[txt_index].dir[1].z = f2; fscanf(file_pt, "%d", &text_len); texts[txt_index].str = (char *)(malloc(text_len * sizeof(char))); fscanf(file_pt, "%c", ch); for (i = 0; i < text_len; i++) fscanf(file_pt, "%c",&(texts[txt_index].str[i])); texts[txt_index].str[i] = 0; txt_index++; break; case 'v' : /* vertices */ fscanf(file_pt,"%f %f %f", &pts_nc[pts_index].x, &pts_nc[pts_index].y, &pts_nc[pts_index].z); pts_nc[pts_index].flag = 3; facet = 0; pts_index++; break; default: break; } } data_ok = TRUE; fclose(file_pt); render_point_type_proc(); render_facet_type_proc(); clip_calc_proc(); }}/**** * render_point_type_proc() * * Converts input vertex data to Xgl input point-list format. * ****/staticvoidrender_point_type_proc(){ register int num_pts; num_pts = pts_index; /* # of pts = # of pts read by get_data() */ if (pts) free(pts); pts = (Xgl_pt_flag_f3d *)0; if (pts_n) free(pts_n); pts_n = (Xgl_pt_normal_flag_f3d *)0; pts_n = (Xgl_pt_normal_flag_f3d *) (malloc(num_pts * sizeof(Xgl_pt_normal_flag_f3d))); for (for_i = 0; for_i < num_pts; for_i++) { pts_n[for_i].x = pts_nc[for_i].x; pts_n[for_i].y = pts_nc[for_i].y; pts_n[for_i].z = pts_nc[for_i].z; pts_n[for_i].normal = pts_nc[for_i].normal; pts_n[for_i].flag = 3; } for (for_i = 0; for_i < num_pl; for_i++) { pl[for_i].pts.normal_flag_f3d = &(pts_n[p_ind[for_i]]); pl[for_i].pt_type = XGL_PT_NORMAL_FLAG_F3D; }}/**** * render_facet_type_proc() * * Converts input facet data to Xgl input facet-list format. * ****/staticvoidrender_facet_type_proc(){ register int num_facets; num_facets = fct_index; if (fcts_normal) free(fcts_normal); fcts_normal = (Xgl_normal_facet *)0; if (fcts_color) free(fcts_color); fcts_color = (Xgl_color_facet *)0; fcts_normal = (Xgl_normal_facet *)(malloc(num_facets * sizeof(Xgl_normal_facet))); for (for_i = 0; for_i < num_facets; for_i++) { fcts_normal[for_i].normal.x = fct_nc[for_i].normal.x; fcts_normal[for_i].normal.y = fct_nc[for_i].normal.y; fcts_normal[for_i].normal.z = fct_nc[for_i].normal.z; } for (for_i = 0; for_i < num_pl; for_i++) { fct_list[for_i].facets.normal_facets = &(fcts_normal[f_ind[for_i]]); fct_list[for_i].facet_type = fct_type; }}/**** * clip_calc_proc() * * Computes vdc window depth and informs Xgl. * ****/staticvoidclip_calc_proc(){ float dist, diag, xdiag, ydiag, zdiag; dist = sqrt(p_eye_position_pt.x * p_eye_position_pt.x + p_eye_position_pt.y * p_eye_position_pt.y + p_eye_position_pt.z * p_eye_position_pt.z); diag = 1.0; if (bbox_pl) { xdiag = bbox_pl->box.f3d.xmax - bbox_pl->box.f3d.xmin; ydiag = bbox_pl->box.f3d.ymax - bbox_pl->box.f3d.ymin; zdiag = bbox_pl->box.f3d.zmax - bbox_pl->box.f3d.zmin; diag = sqrt(xdiag * xdiag + ydiag * ydiag + zdiag * zdiag); diag *= 0.5; }/* vdc.zmin = bound.zmin = dist - diag; vdc.zmax = bound.zmax = dist + diag;*/ vdc.zmin = bound.zmin = 0; vdc.zmax = bound.zmax = 600; xgl_object_set(cctx, XGL_CTX_VDC_WINDOW, &vdc, XGL_CTX_VIEW_CLIP_BOUNDS, &bound, 0);}/********** **********//********** Routines to Handle Window/Canvas Events **********/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -