设为首页 加入收藏

TOP

VRML文件操作(一)
2014-11-23 22:08:31 来源: 作者: 【 】 浏览:0
Tags:VRML 文件 操作

// Leaf.cpp : Defines the entry point for the console application.

//

#include "stdAfx.h"

#define graphfile "t3.WRL" //读入的VRML文件名称

#define texturefile "bigLeaf1.JPG" //读入的纹理图像名称

int numOfCoord; //储存顶点坐标的个数

int numOfTexCoord; //存储纹理坐标个数

int numOfIndex; //储存索引信息个数

float (*tempCoord)[3];

int *tempIndex;

float (*tempTexCoord)[2];

//读取VRML文件中的场景数据

void readFile(const char *filename)

{

//创建读取外部文件

SoInput myGraphInput;

if (!myGraphInput.openFile(filename))

{

fprintf(stderr, "Cannot open file %s ",filename);

exit(1);

}

SoVRMLGroup *myGraph = SoDB::readAllVRML(&myGraphInput);

if (myGraph == NULL)

{

fprintf(stderr, "Problem reading file ");

exit(1);

}

myGraphInput.closeFile();

//读取VRML文件

//搜索并取出SoVRMLShape节点

myGraph->ref();

SoVRMLShape *Shape = new SoVRMLShape;

SoSearchAction search;

search.setType(SoVRMLShape::getClassTypeId());

search.setInterest(SoSearchAction::FIRST);

search.apply(myGraph);

if(search.getPath() == 0)

{

printf("error! ");

exit(1);

}

else

{

printf("right ");

Shape = (SoVRMLShape *)search.getPath()->getTail();

Shape->setOverride(TRUE);

}

//得到geometry的信息,其是指向SoVRMLIndexedFaceSet节点的指针的容器

SoVRMLIndexedFaceSet *indexedFaceSet = new SoVRMLIndexedFaceSet;

indexedFaceSet = (SoVRMLIndexedFaceSet *)Shape->geometry.getValue();

//得到其中的点的坐标信息并保存到全局变量数组

SoVRMLCoordinate *coordInate = new SoVRMLCoordinate;

coordInate = (SoVRMLCoordinate *)indexedFaceSet->coord.getValue();

numOfCoord = coordInate->point.getNum();

tempCoord=new float[numOfCoord][3];

for(int i=0; i

{

for(int j=0; j<3; j++)

{

tempCoord[i][j] =coordInate->point[i][j];

// printf("%f ",);

}

}

//得到其中的点的坐标索引信息并保存到全局变量数组

numOfIndex = indexedFaceSet->coordIndex.getNum();

tempIndex = new int[numOfIndex];

for(int k=0; k

{

tempIndex[k] = indexedFaceSet->coordIndex[k];

}

//得到其中的纹理的坐标信息并保存到全局变量数组

SoTextureCoordinate2 *textureCoord = new SoTextureCoordinate2;

textureCoord = (SoTextureCoordinate2 *)indexedFaceSet->texCoord.getValue();

numOfTexCoord = textureCoord->point.getNum();

tempTexCoord=new float[numOfTexCoord][2];

for(int n=0; n

{

for(int p=0; p<2; p++)

{

tempTexCoord[n][p] = textureCoord->point[n][p];

}

}

}

//场景数据初始化

void drawFace(SoIndexedFaceSet *myIndexedFaceSet)

{

SoMFVec3f coordVec3f;

SoMFInt32 coordIndex;

SoMFVec2f textureCoordVec2f;

for(int j=0; j

{

coordVec3f.set1Value(j,tempCoord[j]);

}

for(int k=0;k

{

coordIndex.set1Value(k,tempIndex[k]);

}

for(int i=0; i

{

textureCoordVec2f.set1Value(i,tempTexCoord[i]);

}

SoVertexProperty *myVertexProperty = new SoVertexProperty;

//设置顶点的坐标

myVertexProperty->vertex = coordVec3f;

//设定平面法失

myVertexProperty->normal.set1Value(0, SbVec3f(0, 1, 0));

//设置纹理坐标

myVertexProperty->texCoord = textureCoordVec2f;

//定义法失绑定方式

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇OpenGL的矩阵 下一篇C语言学习趣事_经典面试题系列_2

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: