openFrameworks•3D部分3D部分结构其中一部分由ofNode继承,为scenegraphic结构ofCameraofNodeofEasyCamofMeshof3dUtils•从ofNode中继承•具备基本的GLUT相机功能•坐标转换相机位置移动操作和相关矩阵操作相机操作ofCamerasetFov(float)Y轴方向上的视场角°,indegrees(45-60)setNearClip(float)指定观察者到视景体的最近的裁剪面的距离setFarClip(float)指定观察者到视景体的最远的裁剪面的距离ofCamera视点(观察点)设置FrustumsetupgetProjectionMatrix()正交透视/透视Ortho/PerspectivegetModelViewMatrix()位置/方向Position/OrientationgetModelViewProjectionMatrix()模型变换矩阵*投影矩阵Modelview*ProjectionofCamera透视及模型视图矩阵PerspectiveandmodelviewmatricesworldToScreen()转化opengl世界坐标至屏幕坐标ConvertopenGLworldtoscreenscreenToWorld()转化屏幕坐标至opengl坐标ConvertscreentoGLworldworldToCamera()转化世界坐标至相机坐标ConvertworldtocameracameraToWorld()转化相机坐标至世界坐标ConvertcameratoworldofCamera坐标系转换CoordinateconversionclasstestApp:publicofBaseApp{public:...ofCameracam;};ofCameratestApp.hvoidtestApp::setup(){ofBackground(33,33,33);cam.setNearClip(0.1);cam.setFarClip(100);//1metertotheright,5metersawayfromobjectcam.setPosition(ofVec3f(1,0,5));}voidtestApp::draw(){cam.begin();glPointSize(10);glBegin(GL_POINTS);glVertex3f(0,0,0);glEnd();cam.end();}ofCameratestApp.cppofNodeofNode•包含标准3d场景项目•3d物体位置、大小、朝向的管理•有很多内置的非常棒的帮助方法•ofCamera是从这里继承过来的3d物体容器/3Dobjectcontainertruck()从左到右移动x轴向上/Movelefttoright(x-axis)boom()Y轴上下移动/Moveupanddown(y-axis)dolly()Z轴前后移动/Moveforwardandbackward(z-axis)setPosition()直接指定相机位置/Setpositiondirectly相机运动/MovementofNodetilt()围绕X轴向上旋转/Rotatearoundx-axispan()围绕y轴向上旋转/Rotatearoundy-axisdolly()围绕z轴向上旋转/Rotatearoundz-axisrotate(a,axis)围绕任意轴向上旋转/Rotatearoundarbitraryaxis相机选择RotationofNodetransformGL()应用ofNode中的transformation功能(做这个用途glPushMatrix/glMultMatrixf())restoreTransformGL()做glPopMatrix,在调用transformGL和drawing后.resetTransform()重置朝向和位置运动位置及朝向测试/Testingmovement/orientationofNodeofNodevoidsetScale(floats)voidsetScale(floatx,floaty,floatz)voidsetScale(constofVec3f&p)voidsetPosition(floatx,floaty,floatz)voidsetPosition(constofVec3f&p)voidsetGlobalPosition(floatx,floaty,floatz)voidsetGlobalPosition(constofVec3f&p)voidsetOrientation(constofQuaternion&q)voidsetOrientation(constofVec3f&eulerAngles)voidsetGlobalOrientation(constofQuaternion&q)ofNode•自己创建“类”从ofNode继承•实现虚函数virtualcustomDraw(void)方法.•绘制的话,只需要在实例中调用draw()即可自定义3D节点类型/Custom3DnodesclassMyCustom3DNode:publicofNode{public:virtualvoidcustomDraw(){ofSphere(0,0,0,0.7);ofDrawAxis(2);}};ofNode自定义3d节点例子,ExtendofNodebegin()/end()把你绘制的东西draw()放在这个之间setTarget(ofNode)追随或查看目标和相机的交互/CamerainteractionmadeeasyofEasyCamclassMyCustom3DNode:publicofNode{public:virtualvoidcustomDraw(){ofSphere(0,0,0,0.7);ofDrawAxis(2);}};classtestApp:publicofBaseApp{public:ofEasyCameasy_cam;vectorMyCustom3DNode*custom_nodes;MyCustom3DNode*mover;};ofEasyCam自定义3d节点类型-exampleofEasyCamofEasyCamvoidtestApp::setup(){ofBackground(33,33,33);//setdistanceofcamera5metersawayeasy_cam.setDistance(5);//createabunchofcustom3Dnodesfloatr=4;for(inti=0;i10;++i){MyCustom3DNode*node=newMyCustom3DNode();node-setPosition(cos((float)i/10*TWO_PI)*r,sin((float)i/10*TWO_PI)*r,-5);custom_nodes.push_back(node);mover=node;}//settargetforcameraeasy_cam.setTarget(*mover);}CustomofNode-exampleofEasyCamvoidtestApp::draw(){easy_cam.begin();//drawcustomnodes.for(inti=0;icustom_nodes.size();++i){custom_nodes[i]-draw();}//moveoverx-axis.floattruck_amount=sin(ofGetElapsedTimef()*0.5)*0.001;mover-truck(truck_amount);easy_cam.end();}ofEasyCamCustomofNode-exampleofEasyCamofMesh介绍包含所有定点数据相关的容器/Containerforallvertexrelateddata•当使用3d模型或3d数据时使用者,它会利用opengl绘制和创建3d模型•面向更加现代的写法(如,可以直接使用最新写法方式的opengl)•清晰的api方法:add[Vertex,Color,TexCoord]()•良好的接口方式可以导出3d模型(如:OBJ,PLY)•可以直接和顶点缓冲数据工作(VBO)addVertex(ofVec3f)AddonevertexaddVertices(vectorofVec3f&)ReferencetoofVec3faddVertices(ofVec3f*,intnum)PointertoverticessetVertex(intindex)RemovebyindexofMesh添加顶点数据/AddingverticesremoveVertex(intindex)RemovebyindexclearVertices()Removeallvertices移除顶点数据RemoveverticesgetVertex(int)Getonevertex得到顶点数据GetvertexaddNormal(ofVec3f&)AddanormaladdNormals(vectorofVec3f)AddvectorofnormalsaddNormals(ofVec3f*,int)AddarrayofnormalssetNormal(int,ofVec3f&)SetonenormalbyindexofMesh添加法线AddingnormalsremoveNormal(int)RemovebyindexclearNormals()Removeallnormals移除法线Removingnormals得到法线RetrievingnormalsgetNormal(int)GetnormalbyindexaddIndex(ofIndexType)AddaindexaddIndices(constofIndextype&)AddabunchofindicesaddIndices(ofIndexType*,int)AddbunchofindicessetIndex(int,ofIndexType)SetaspecificindexofMesh添加索引AddingindicesremoveIndex(inti)RemovebyindexclearIndices()Removeallindices移除缩影Removingindices得到索引RetrievingindicesgetIndex(int)Getindexbyindex;-)addColor(constofFloatColor&c)AddaindexaddColors(constofFloatColor&cols)AddabunchofindicesaddColors(constofFloatColor*cols,intamt)AddbunchofindicessetColor(intindex,constofFloatColor&c)SetaspecificindexofMesh添加颜色AddingcolorsremoveColor(intindex)RemovebyindexclearColors()Removeallindices移除颜色Removingcolors得到颜色RetrievingcolorsofFloatColorgetColor(inti)Getindexbyindex;-)getNumVertices()TotalnumberofverticesgetNumColors()TotalnumberofcolorsgetNumTexCoords()TotalnumberoftexturecoordsgetNumIndices()TotalnumberofindicesofMeshVbo中有