public MyObject(Lib3dsMesh _node, Lib3dsFile _model) { this.node = _node; this.model = _model; if (_node.faces == null || _node.faces.Count == 0) { this.Flag = false; } else { this.Flag = true; this.Normalizes = new Lib3dsVertex[_node.faces.Count]; for (int i = 0; i < _node.faces.Count; i++) { Lib3dsFace face = _node.faces[i]; Lib3dsVertex Point1 = _node.vertices[face.index[0]]; Lib3dsVertex Point2 = _node.vertices[face.index[1]]; Lib3dsVertex Point3 = _node.vertices[face.index[2]]; this.Normalizes[i] = CreateNormalize(Point1, Point2, Point3); } } }
public static void AddMeshTo3DS(Lib3dsFile res, PSKFile f, Matrix m) { Lib3dsMesh mesh = new Lib3dsMesh(); string name = "Box00" + res.meshes.Count; mesh.name = name; mesh.matrix = Matrix2FA(Matrix.Identity); mesh.vertices = new List <Lib3dsVertex>(); foreach (PSKFile.PSKPoint p in f.psk.points) { Vector3 v = p.ToVector3(); v = Vector3.TransformCoordinate(v, m); mesh.vertices.Add(new Lib3dsVertex(v.X, -v.Y, v.Z)); } mesh.texcos = new List <Lib3dsTexturecoordinate>(); for (int i = 0; i < f.psk.points.Count; i++) { foreach (PSKFile.PSKEdge e in f.psk.edges) { if (e.index == i) { mesh.texcos.Add(new Lib3dsTexturecoordinate(e.U, e.V)); } } } mesh.faces = new List <Lib3dsFace>(); foreach (PSKFile.PSKFace face in f.psk.faces) { Lib3dsFace ff = new Lib3dsFace(); ff.flags = 6; ff.index = new ushort[3]; ff.index[0] = (ushort)f.psk.edges[face.v0].index; ff.index[1] = (ushort)f.psk.edges[face.v2].index; ff.index[2] = (ushort)f.psk.edges[face.v1].index; mesh.faces.Add(ff); } mesh.nfaces = (ushort)mesh.faces.Count; mesh.nvertices = (ushort)mesh.vertices.Count; mesh.map_type = Lib3dsMapType.LIB3DS_MAP_NONE; mesh.object_flags = 0; mesh.color = 128; res.meshes.Add(mesh); Lib3dsNode node = new Lib3dsMeshInstanceNode(); node.matrixNode = Matrix2FA(Matrix.Identity); node.parent = null; node.parent_id = 0xffff; node.hasNodeID = true; node.type = Lib3dsNodeType.LIB3DS_NODE_MESH_INSTANCE; node.flags = res.nodes[0].flags; node.node_id = (ushort)(res.meshes.Count() - 1); node.name = name; res.nodes.Add(node); }
/// <summary> /// Draw a mesh. /// </summary> /// <param name="gl">OpenGL handler.</param> /// <param name="buildingObj">The mesh.</param>BuildingObjectLib3DS _object, private void DrawMesh(OpenGL gl, Lib3dsMesh thisMesh, Hashtable textures, List <Lib3dsMaterial> matrials, DrawType type) { if (thisMesh == null || thisMesh.nfaces == 0) { return; } // Draw all the faces in this mesh. for (int j = 0; j < thisMesh.faces.Count; j++) { Lib3dsFace thisFace = thisMesh.faces[j]; float transparency = matrials[thisFace.material].transparency; //float[] fogColor = new float[4] { 0.5f, 0.5f, 0.5f, 1.0f }; //float[] LightAmbient = new float[4] { 0.5f, 0.5f, 0.5f, 1.0f }; //float[] LightDiffuse = new float[4] { 1.0f, 1.0f, 1.0f, 1.0f }; //float[] LightPosition = new float[4] { 0.0f, 0.0f, 2.0f, 1.0f }; switch (type) { case DrawType.WireFrame: gl.PolygonMode(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_LINE); IsDrawTexture = false; gl.Color(0.5f, 0.5f, 0.5f, 0.5f); gl.LineWidth(1.5f); break; case DrawType.Full: IsDrawTexture = BindTexture(gl, textures, matrials[thisFace.material].name); gl.PolygonMode(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_FILL); break; case DrawType.Face: gl.PolygonMode(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_FILL); IsDrawTexture = false; break; case DrawType.Translucent: IsDrawTexture = BindTexture(gl, textures, matrials[thisFace.material].name); gl.PolygonMode(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_FILL); gl.Enable(OpenGL.GL_BLEND); gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA); //gl.Enable(OpenGL.GL_TEXTURE_2D); // Enable Texture Mapping //gl.ShadeModel(OpenGL.GL_SMOOTH); // Enable Smooth Shading //gl.ClearColor(0.5f,0.5f,0.5f,1.0f); // We'll Clear To The Color Of The Fog //gl.ClearDepth(1.0f); // Depth Buffer Setup //gl.Enable(OpenGL.GL_DEPTH_TEST); // Enables Depth Testing //gl.DepthFunc(OpenGL.GL_LEQUAL); // The Type Of Depth Testing To Do //gl.Hint(OpenGL.GL_PERSPECTIVE_CORRECTION_HINT, OpenGL.GL_NICEST); // Really Nice Perspective Calculations //gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_AMBIENT, LightAmbient); // Setup The Ambient Light //gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light //gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_POSITION,LightPosition); // Position The Light //gl.Enable(OpenGL.GL_LIGHT1); //gl.Fog(OpenGL.GL_FOG_COLOR, fogColor);//设置雾颜色,f是一个指定颜色的数组float f[4] //gl.Fog(OpenGL.GL_FOG_DENSITY, 0.85f); // 设置雾的密度 //gl.Hint(OpenGL.GL_FOG_HINT, OpenGL.GL_DONT_CARE); // 设置系统如何计算雾气 //gl.Fog(OpenGL.GL_FOG_START, 0.01f);//设置雾从多远开始 //gl.Fog(OpenGL.GL_FOG_END, 100.0f);//设置雾从多远结束 //gl.Fog(OpenGL.GL_FOG_MODE, OpenGL.GL_LINEAR);//设置使用哪种雾,共有三中雾化模式 //gl.Enable(OpenGL.GL_FOG);//打开雾效果 transparency = 0.2f; break; default: gl.PolygonMode(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_FILL); IsDrawTexture = false; break; } if (type != DrawType.WireFrame) { gl.Color(matrials[thisFace.material].diffuse[0], matrials[thisFace.material].diffuse[1], matrials[thisFace.material].diffuse[2], matrials[thisFace.material].transparency); } gl.Begin(OpenGL.GL_TRIANGLES); for (int k = 0; k != 3; ++k) { int index = thisFace.index[k]; if (IsDrawTexture) { gl.TexCoord(thisMesh.texcos[index].s, thisMesh.texcos[index].t); } gl.Vertex(thisMesh.vertices[index].x / 20, thisMesh.vertices[index].z / 20, -thisMesh.vertices[index].y / 20); } gl.End(); if (type == DrawType.Translucent) { gl.Disable(OpenGL.GL_BLEND); } } }