// 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; //定义法失绑定方式