// 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<numOfCoord; 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<numOfIndex; 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<numOfTexCoord; 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<numOfCoord; j++)
{
coordVec3f.set1Value(j,tempCoord[j]);
}
for(int k=0;k<numOfIndex;k++)
{
coordIndex.set1Value(k,tempIndex[k]);
}
for(int i=0; i<numOfTexCoord; i++)
{
textureCoordVec2f.set1Value(i,tempTexCoord[i]);
}
SoVertexProperty *myVertexProperty = new SoVertexProperty;
//設置頂點的坐標
myVertexProperty->vertex = coordVec3f;
//設定平面法失
myVertexProperty->normal.set1Value(0, SbVec3f(0, 1, 0));
//設置紋理坐標
myVertexProperty->texCoord = textureCoordVec2f;
//定義法失綁定方式
myVertexProperty->normalBinding = SoNormalBinding::OVERALL;
//定義面集
myIndexedFaceSet->coordIndex = coordIndex;
myIndexedFaceSet->vertexProperty.setValue(myVertexProperty);
}
//定時器傳感器回調函數
static void timertra(void * data, SoSensor *)
{
static SbBool direction = FALSE;
SoIndexedFaceSet * myIndexedFaceSet = (SoIndexedFaceSet*) data;
SoMFVec3f coordVec3f;
SoMFInt32 coordIndex;
SoMFVec2f textureCoordVec2f;
//修改頂點坐標,實現波動