⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objfileread.cpp

📁 用VC++实现OBJ文件读取,实现了强大的功能.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		{
	    
		}
		else if (inputLine[0] == 'u') 
		{
			if (strncmp("usemtl", inputLine, 6) != 0)
				continue;		
			if (materials == NULL)
				materials = readMaterials(FileName);

			sscanf(&inputLine[7], "%s", materialName);
			matPtr = materials;
			while ((strcmp(materialName, matPtr->name) != 0) &&
				(matPtr->name[0] != 0))
				matPtr++;
			if (matPtr->name[0] == 0)
				fprintf(stderr, "Can't find %s\n", materialName);
	  
			model->colorList[model->colorCount].index = model->vertexCount;
			model->colorList[model->colorCount].ra = matPtr->ra;
			model->colorList[model->colorCount].ga = matPtr->ga;
			model->colorList[model->colorCount].ba = matPtr->ba;
			model->colorList[model->colorCount].rd = matPtr->rd;
			model->colorList[model->colorCount].gd = matPtr->gd;
			model->colorList[model->colorCount].bd = matPtr->bd;
			model->colorList[model->colorCount].ad = matPtr->ad;
			model->colorList[model->colorCount].rs = matPtr->rs;
			model->colorList[model->colorCount].gs = matPtr->gs;
			model->colorList[model->colorCount].bs = matPtr->bs;
			model->colorList[model->colorCount].spec = matPtr->spec;
			model->colorCount++;
		}
    }

    model->vertexList[model->vertexCount].draw = 0;

    model->colorList[model->colorCount].index = model->vertexCount + 1;

     model->lineCount = 0;
    for (i = 0; i < model->edgeCount; i++) 
	{
		//寻找该边界的颜色
		model->lineList[model->lineCount].colorIndex = 0;
		for (j = 0; j <= model->colorCount; j++) 
		{
			if (model->objVertexList[model->edgeList[i][0]].vertexIndex <
				model->colorList[j].index) 
			{
				model->lineList[model->lineCount].colorIndex = j-1;
				break;
			}
		}

		model->lineList[model->lineCount].draw = 0;
		model->lineList[model->lineCount].x = 
			model->objVertexList[model->edgeList[i][0]].x;
		model->lineList[model->lineCount].y = 
			model->objVertexList[model->edgeList[i][0]].y;
		model->lineList[model->lineCount].z = 
			model->objVertexList[model->edgeList[i][0]].z;
		model->lineCount++;

		model->lineList[model->lineCount].draw = 1;
		model->lineList[model->lineCount].x = 
			model->objVertexList[model->edgeList[i][1]].x;
		model->lineList[model->lineCount].y = 
			model->objVertexList[model->edgeList[i][1]].y;
		model->lineList[model->lineCount].z = 
			model->objVertexList[model->edgeList[i][1]].z;
		model->lineCount++;
    }
    model->lineList[model->lineCount].draw = 0;

    int edgeBase;			
    model->lineStripCount = 0;
    edgeBase = 0;
    do 
	{
		while (model->edgeList[edgeBase][0] == -1)
			edgeBase++;
		if (edgeBase >= model->edgeCount)
			break;			/* Exit here */

		i = edgeBase;

		v0 = model->edgeList[i][0];
		v1 = model->edgeList[i][1];

		//寻找该边界的颜色
		model->lineStripList[model->lineStripCount].colorIndex = 0;
		for (j = 0; j <= model->colorCount; j++) 
		{
			if (model->objVertexList[v0].vertexIndex <=
				model->colorList[j].index) 
			{
				model->lineStripList[model->lineStripCount].colorIndex= j-1;
				break;
			}
		}

		model->lineStripList[model->lineStripCount].draw = 0;
		model->lineStripList[model->lineStripCount].x = 
			model->objVertexList[v0].x;
		model->lineStripList[model->lineStripCount].y = 
			model->objVertexList[v0].y;
		model->lineStripList[model->lineStripCount].z = 
			model->objVertexList[v0].z;
        model->lineStripCount++;

		model->lineStripList[model->lineStripCount].draw = 1;
		model->lineStripList[model->lineStripCount].x =
            model->objVertexList[v1].x;
		model->lineStripList[model->lineStripCount].y = 
			model->objVertexList[v1].y;
		model->lineStripList[model->lineStripCount].z = 
			model->objVertexList[v1].z;
		model->lineStripCount++;

		for (j=0; j<model->objVertexList[v0].edgesNum; j++) 
		{
			if (model->objVertexList[v0].edges[j] == edgeBase) 
			{
	    		model->objVertexList[v0].edges[j] = -1;
				break;
			}
		}
		for (j=0; j<model->objVertexList[v1].edgesNum; j++) 
		{
			if (model->objVertexList[v1].edges[j] == edgeBase) 
			{
	    		model->objVertexList[v1].edges[j] = -1;
				break;
			}
		}

		model->edgeList[i][0] = -1;			

		do 
		{
			foundMatch = 0;
			for (j=0; j<model->objVertexList[v1].edgesNum; j++) 
			{
				//寻找没有使用的边界
				if (model->objVertexList[v1].edges[j] != -1) 
				{ 
					if (model->edgeList[model->objVertexList[v1].edges[j]][0] 
						!= -1) 
					{
						foundMatch = 1;
						k = model->objVertexList[v1].edges[j];
						model->objVertexList[v1].edges[j] = -1;
						if (model->edgeList[k][0] == v1) 
						{
							//查找该边界的颜色
							model->lineStripList[
								model->lineStripCount].colorIndex = 0;
							for (m = 0; m <= model->colorCount; m++) 
							{
								if (model->objVertexList[
									model->edgeList[k][1]].vertexIndex <=
									model->colorList[m].index) 
								{
									model->lineStripList[
										model->lineStripCount].colorIndex= m-1;
									break;
								}
							}

  		 					model->lineStripList[model->lineStripCount].draw = 1;
							model->lineStripList[model->lineStripCount].x = 
								model->objVertexList[model->edgeList[k][1]].x;
							model->lineStripList[model->lineStripCount].y = 
								model->objVertexList[model->edgeList[k][1]].y;
							model->lineStripList[model->lineStripCount].z = 
								model->objVertexList[model->edgeList[k][1]].z;
							model->lineStripCount++;
							v1 = model->edgeList[k][1];
						}
						else 
						{
							//查找该边界的颜色
							model->lineStripList[
								model->lineStripCount].colorIndex = 0;
							for (m = 0; m <= model->colorCount; m++) 
							{
								if (model->objVertexList[
									model->edgeList[k][0]].vertexIndex <=
									model->colorList[m].index) 
								{
									model->lineStripList[
										model->lineStripCount].colorIndex= m-1;
									break;
								}
							}

		  					model->lineStripList[model->lineStripCount].draw = 1;
							model->lineStripList[model->lineStripCount].x = 
								model->objVertexList[model->edgeList[k][0]].x;
							model->lineStripList[model->lineStripCount].y = 
								model->objVertexList[model->edgeList[k][0]].y;
							model->lineStripList[model->lineStripCount].z = 
								model->objVertexList[model->edgeList[k][0]].z;
							model->lineStripCount++;
		  					v1 = model->edgeList[k][0];
						}
						model->edgeList[k][0] = -1; 
						break;
					}
				} 
            }  
		} while (foundMatch != 0);
    } while (edgeBase < model->edgeCount);
    model->lineStripList[model->lineStripCount].draw = 0;
    fclose(dataFile);
    free(materials);
} /* End of readObjData */


FILE * COBJFileRead::fileOpen(const char *filename,
    const char *mode) 
{
    FILE *retVal;
	retVal = fopen(filename, mode);
        if (retVal != NULL)    return retVal;
	return NULL;
}


MaterialColor * COBJFileRead::readMaterials(char *FileName)
{
    FILE *mtlFile;				//需要读入的材质文件
    char inputLine[256];		//输入线
    MaterialColor *matPtr;		//分配空间
    int i;						//数组索引
    int count;					//读入的数量
    GLfloat r, g, b;			//读入的颜色
    GLfloat spec;				//读入的高光值

	char MaterialName[256];
	strcpy(MaterialName,FileName);
    int index=strlen(FileName);
	strncpy(MaterialName+(index-3),"mtl",3);

    mtlFile = fileOpen(MaterialName, "r");
    if (mtlFile == NULL) {
	fprintf(stderr, "Error, could not open 'materials.mtl', exiting.\n");
	exit(1);
    }

    matPtr = (MaterialColor *)malloc(100 * sizeof(MaterialColor));

    i = -1;
    for(;;) {
	if (fgets(inputLine, 250, mtlFile) == NULL)
	    break;			//文件结尾

	if (strncmp("newmtl", inputLine, 6) == 0) {
	    i++;
	    sscanf(&inputLine[7], "%s", matPtr[i].name);
	}
	else if (strncmp("Ka", inputLine, 2) == 0) {
	    count = sscanf(inputLine, "Ka %f %f %f", &r, &g, &b);
	    if (count != 3)
		continue;
	    matPtr[i].ra = r;
	    matPtr[i].ga = g;
	    matPtr[i].ba = b;
	}
	else if (strncmp("Kd", inputLine, 2) == 0) {
	    count = sscanf(inputLine, "Kd %f %f %f", &r, &g, &b);
	    if (count != 3)
		continue;
	    matPtr[i].rd = r;
	    matPtr[i].gd = g;
	    matPtr[i].bd = b;
	    matPtr[i].ad = 1.0;
	}
	else if (strncmp("Ks", inputLine, 2) == 0) {
	    count = sscanf(inputLine, "Ks %f %f %f", &r, &g, &b);
	    if (count != 3)
		continue;
	    matPtr[i].rs = r;
	    matPtr[i].gs = g;
	    matPtr[i].bs = b;
	}
	else if (strncmp("Ns", inputLine, 2) == 0) {
	    count = sscanf(inputLine, "Ns %f", &spec);
	    if (count != 1)
		continue;
	    matPtr[i].spec = spec;
	}
    }
    i++;

    matPtr[i].name[0] = 0;		
    matPtr[i].ra = 0.0f;
    matPtr[i].ga = 0.1f;
    matPtr[i].ba = 0.0f;
    matPtr[i].rd = 0.2f;
    matPtr[i].gd = 1.0f;
    matPtr[i].bd = 0.0f;
    matPtr[i].ad = 1.0f;
    matPtr[i].rs = 1.0f;
    matPtr[i].gs = 0.8f;
    matPtr[i].bs = 0.0f;
    matPtr[i].spec = 25.0f;

    return matPtr;
} /* End of readMaterials */




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -