vrml 读取和写入

来源:互联网 发布:知行论坛pt 编辑:程序博客网 时间:2024/04/27 17:22

本学期计算机图形学作业的一部分。


还是采用vrml读取为obj, 和obj写入为vrml。


只能读取最简单的点和面, 其他纹理没写。


#ifndef VRML_H_#define VRML_H_#include <stdlib.h>#include <stdio.h>#include <string.h>int vrml_point(FILE *fp, float (*point)[3]){char tmp[256];int point_count = 0;while(!feof(fp)){fscanf(fp, "%s", tmp);if (!strcmp(tmp, "[")){fscanf(fp, "%s", tmp);while(strcmp(tmp, "]")){point[point_count][0] = atof(tmp);fscanf(fp, "%s", tmp);point[point_count][1] = atof(tmp);fscanf(fp, "%s", tmp);point[point_count][2] = atof(tmp);point_count++;fscanf(fp, "%s", tmp);}return point_count;}}}int vrml_face(FILE *fp, int (*face)[3]){char tmp[256];int face_count = 0;while(!feof(fp)){fscanf(fp, "%s", tmp);if (!strcmp(tmp, "[")){fscanf(fp, "%s", tmp);while(strcmp(tmp, "]")){face[face_count][0] = atoi(tmp);fscanf(fp, "%s", tmp);face[face_count][1] = atoi(tmp);fscanf(fp, "%s", tmp);face[face_count][2] = atoi(tmp);fscanf(fp, "%s", tmp);face_count++;fscanf(fp, "%s", tmp);}return face_count;}}}// 默认三角面片// 只读取point和coordIndex// 点的索引从0开始// 逆时针void vrml2obj(char *filename, float (*point)[3], int (*face)[3], int &point_count, int &face_count){FILE *fp = fopen(filename, "r");char tmp[256];while(!feof(fp)){fscanf(fp, "%s", tmp);if (!strcmp(tmp, "point")){point_count = vrml_point(fp, point);}else if (!strcmp(tmp, "coordIndex")){face_count = vrml_face(fp, face);}elsefgets(tmp, 256, fp);}}void obj2vrml(char *filename, float (*point)[3], int (*face)[3], int point_count, int face_count){FILE *fp = fopen(filename, "w");fprintf(fp, "%s\n", "#VRML V2.0");fprintf(fp, "#points: %d\tfaces: %d\n\n", point_count, face_count);fprintf(fp, "Shape {\n");fprintf(fp, "\tgeometry IndexedFaceSet {\n");fprintf(fp, "\t\tcoord Coordinate {\n");fprintf(fp, "\t\t\tpoint [\n");for (int i = 0; i< point_count; i++){fprintf(fp, "\t\t\t%f %f %f\n", point[i][0], point[i][1], point[i][2]);}fprintf(fp, "\t\t\t]\n");fprintf(fp, "\t\t}\n");fprintf(fp, "\t\tcoordIndex [\n");for (int i = 0; i< face_count; i++){fprintf(fp, "\t\t\t%d %d %d -1\n", face[i][0], face[i][1], face[i][2]);}fprintf(fp, "\t\t]\n");fprintf(fp, "\t}\n");fprintf(fp, "}\n");fclose(fp);}#endif

over。


0 0