Beispiel #1
0
        public static ObjProperties GetImportedObjectProperties(
            string fileName,
            double scale)
        {
            ObjImporter importer = new ObjImporter();

            ObjImporter.meshStruct mesh = importer.ImportFile(fileName);

            Vector3d[] vertexPoint = new Vector3d[mesh.vertices.Length];

            for (int i = 0; i < mesh.vertices.Length; i++)
            {
                vertexPoint[i] = mesh.vertices[i];
            }

            OpenGLUtilities.UnitizeObject(ref vertexPoint);
            OpenGLUtilities.ScaleObject(ref vertexPoint, scale);

            int nTriangle = mesh.faceData.Length / 3;

            int[][] triangleIndex = new int[nTriangle][];

            for (int i = 0; i < nTriangle; i++)
            {
                triangleIndex[i] = new int[] {
                    (int)mesh.faceData[i * 3].x - 1,
                    (int)mesh.faceData[(i * 3) + 1].x - 1,
                    (int)mesh.faceData[(i * 3) + 2].x - 1
                };
            }

            return(new ObjProperties {
                vertexPoint = vertexPoint, triangleIndex = triangleIndex
            });
        }
Beispiel #2
0
        /// <summary>
        /// GL draw solid.
        /// </summary>
        /// <param name="solid">Solid.</param>
        /// <param name="GLM_TEXTURE">If set to <c>true</c> GL add texture coordinates.</param>
        /// <param name="GLM_FLAT">If set to <c>true</c> GL add plane normal.</param>
        private static void GLDrawSolid(
            ObjImporter.meshStruct solid,
            Vector3d translate,
            bool GLM_TEXTURE,
            bool GLM_FLAT,
            bool GLM_SMOOTH)
        {
            int[] indicedata  = new int[solid.faceData.Length];
            int[] textureData = new int[solid.faceData.Length];
            int[] normalData  = new int[solid.faceData.Length];

            GL.Begin(PrimitiveType.Triangles);

            int nTriangle = solid.faceData.Length / 3;

            for (int i = 0; i < nTriangle; i++)
            {
                int index = i * 3;
                indicedata [index]     = (int)solid.faceData[i * 3].x - 1;
                indicedata [index + 1] = (int)solid.faceData[(i * 3) + 1].x - 1;
                indicedata [index + 2] = (int)solid.faceData[(i * 3) + 2].x - 1;


                textureData[index]     = (int)solid.faceData[i * 3].y - 1;
                textureData[index + 1] = (int)solid.faceData[(i * 3) + 1].y - 1;
                textureData[index + 2] = (int)solid.faceData[(i * 3) + 2].y - 1;

                normalData [index]     = (int)solid.faceData[i * 3].z - 1;
                normalData [index + 1] = (int)solid.faceData[(i * 3) + 1].z - 1;
                normalData [index + 2] = (int)solid.faceData[(i * 3) + 2].z - 1;

                if (GLM_FLAT)
                {
                    var a = new Vector3d(
                        solid.vertices[indicedata[index]].x,
                        solid.vertices[indicedata[index]].y,
                        solid.vertices[indicedata[index]].z);

                    var b = new Vector3d(
                        solid.vertices[indicedata [index + 1]].x,
                        solid.vertices[indicedata [index + 1]].y,
                        solid.vertices[indicedata [index + 1]].z);

                    var c = new Vector3d(
                        solid.vertices[indicedata [index + 2]].x,
                        solid.vertices[indicedata [index + 2]].y,
                        solid.vertices[indicedata [index + 2]].z);

                    Vector3d normal =
                        GeometryUtils.CalculateTriangleNormal(a, b, c);

                    GL.Normal3(normal.x, normal.y, normal.z);
                }

                if (GLM_SMOOTH)
                {
                    GL.Normal3(
                        solid.normals[normalData[index]].x,
                        solid.normals[normalData[index]].y,
                        solid.normals[normalData[index]].z);
                }

                if (GLM_TEXTURE && solid.uv.Length > 0)
                {
                    GL.TexCoord2(
                        solid.uv [textureData [index]].x,
                        solid.uv [textureData [index]].y);
                }

                GL.Vertex3(
                    solid.vertices[indicedata [index]].x - translate.x,
                    solid.vertices[indicedata [index]].y - translate.y,
                    solid.vertices[indicedata [index]].z - translate.z);

                if (GLM_SMOOTH)
                {
                    GL.Normal3(
                        solid.normals[normalData[index + 1]].x,
                        solid.normals[normalData[index + 1]].y,
                        solid.normals[normalData[index + 1]].z);
                }

                if (GLM_TEXTURE && solid.uv.Length > 0)
                {
                    GL.TexCoord2(
                        solid.uv [textureData [index + 1]].x,
                        solid.uv [textureData [index + 1]].y);
                }

                GL.Vertex3(
                    solid.vertices[indicedata [index + 1]].x - translate.x,
                    solid.vertices[indicedata [index + 1]].y - translate.y,
                    solid.vertices[indicedata [index + 1]].z - translate.z);

                if (GLM_SMOOTH)
                {
                    GL.Normal3(
                        solid.normals[normalData[index + 2]].x,
                        solid.normals[normalData[index + 2]].y,
                        solid.normals[normalData[index + 2]].z);
                }

                if (GLM_TEXTURE && solid.uv.Length > 0)
                {
                    GL.TexCoord2(
                        solid.uv [textureData [index + 2]].x,
                        solid.uv [textureData [index + 2]].y);
                }

                GL.Vertex3(
                    solid.vertices[indicedata [index + 2]].x - translate.x,
                    solid.vertices[indicedata [index + 2]].y - translate.y,
                    solid.vertices[indicedata [index + 2]].z - translate.z);
            }

            GL.End();
        }