예제 #1
0
 public static bool Load(Context context, ObjMesh mesh, string fileName)
 {
     try
     {
         using (var input = context.Assets.Open(fileName))
             using (StreamReader streamReader = new StreamReader(input))
             {
                 Load(mesh, streamReader);
                 streamReader.Close();
                 return(true);
             }
     }
     catch { return(false); }
 }
예제 #2
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            viewportHeight = Height; viewportWidth = Width;

            string vertexShaderCode   = null;
            string fragmentShaderCode = null;

            try {
                vertexShaderCode   = FileTools.getContentByStream(Context.Assets.Open("Shaders/Spot/vs.glsl"));
                fragmentShaderCode = FileTools.getContentByStream(Context.Assets.Open("Shaders/Spot/fs.glsl"));
                spotShader         = new Shader(vertexShaderCode, fragmentShaderCode);

                vertexShaderCode   = FileTools.getContentByStream(Context.Assets.Open("Shaders/Depth/vs.glsl"));
                fragmentShaderCode = FileTools.getContentByStream(Context.Assets.Open("Shaders/Depth/fs.glsl"));
                depthShader        = new Shader(vertexShaderCode, fragmentShaderCode);
            }
            catch (Exception ex) {
                throw new Exception("Can't load shaders from file: {0}", ex);
            }

            //////////////////////////////////////////////////////////////////////

            ObjMesh sphere = new ObjMesh(Context, "triad_sphere.obj");

            scene     = new Scene.Scene(spotShader);
            scene.Cam = new Scene.Camera(new Vector3(0.0f, 0.0f, 10.0f));
            Vector3 lightAtt = new Vector3(1.0f, 0.00f, 0.02f);

            Light light;

            light = new Light();
            Objects.InitSpotLight(ref light);
            light.direction   = new Vector3(1.0f, 0.0f, 0.0f);
            light.color       = new Vector4(0.0f, 0.5f, 1.0f, 1.0f);
            light.attenuation = lightAtt;
            scene.appendLight(light);

            light = new Light();
            Objects.InitSpotLight(ref light);
            light.direction   = new Vector3(-1.0f, 0.0f, 0.0f);
            light.color       = new Vector4(1.0f, 0.0f, 0.0f, 1.0f);
            light.attenuation = lightAtt;
            scene.appendLight(light);

            light = new Light();
            Objects.InitSpotLight(ref light);
            light.direction   = new Vector3(0.0f, 0.0f, 1.0f);
            light.color       = new Vector4(1.0f, 0.5f, 0.0f, 1.0f);
            light.attenuation = lightAtt;
            scene.appendLight(light);

            light = new Light();
            Objects.InitPointLight(ref light);
            light.pos         = new Vector3(0.0f, 6.0f, 6.0f);
            light.color       = new Vector4(0.0f, 0.75f, 0.0f, 1.0f);
            light.attenuation = lightAtt;
            scene.appendLight(light);

            float d  = 2.0f;
            int   id = 0;

            for (int i = -1; i < 2; ++i)
            {
                for (int j = -1; j < 2; ++j)
                {
                    for (int k = -1; k < 2; ++k)
                    {
                        if (i == 0 && j == 0 && k == 0)
                        {
                            continue;
                        }
                        Scene.Object obj = new Scene.Object()
                        {
                            Position = new Transformation()
                            {
                                TranslateVector = new Vector3(d * i, d * j, d * k),
                                ScaleFactor     = 0.8f
                            },
                            Mesh = sphere,
                            Id   = id++,
                        };
                        scene.appendObject(obj);
                    }
                }
            }

            mScaleDetector = new ScaleGestureDetector(Context, this);

            //UpdateFrame += delegate(object sender, FrameEventArgs args)
            //{
            //};

            RenderFrame += delegate
            {
                Render();
            };

            scene.setViewport(Width, Height);
            scene.init();

            Run(30);
        }
예제 #3
0
        static void Load(ObjMesh mesh, TextReader textReader)
        {
            vertices  = new List <Vector3>();
            normals   = new List <Vector3>();
            texCoords = new List <Vector2>();
            objVerticesIndexDictionary = new Dictionary <ObjMesh.ObjVertex, short>();
            objVertices  = new List <ObjMesh.ObjVertex>();
            objTriangles = new List <ObjMesh.ObjTriangle>();
            objQuads     = new List <ObjMesh.ObjQuad>();

            string line;

            while ((line = textReader.ReadLine()) != null)
            {
                line = line.Trim(splitCharacters);
                line = line.Replace("  ", " ");

                string[] parameters = line.Split(splitCharacters);

                switch (parameters[0])
                {
                case "p":     // Point
                    break;

                case "g":
                    break;

                case "v":     // Vertex
                    float x = float.Parse(parameters[1], CultureInfo.InvariantCulture.NumberFormat);
                    float y = float.Parse(parameters[2], CultureInfo.InvariantCulture.NumberFormat);
                    float z = float.Parse(parameters[3], CultureInfo.InvariantCulture.NumberFormat);
                    vertices.Add(new Vector3(x, y, z));
                    break;

                case "vt":     // TexCoord
                    float u = float.Parse(parameters[1], CultureInfo.InvariantCulture.NumberFormat);
                    float v = float.Parse(parameters[2], CultureInfo.InvariantCulture.NumberFormat);
                    texCoords.Add(new Vector2(u, v));
                    break;

                case "vn":     // Normal
                    float nx = float.Parse(parameters[1], CultureInfo.InvariantCulture.NumberFormat);
                    float ny = float.Parse(parameters[2], CultureInfo.InvariantCulture.NumberFormat);
                    float nz = float.Parse(parameters[3], CultureInfo.InvariantCulture.NumberFormat);
                    normals.Add(new Vector3(nx, ny, nz));
                    break;

                case "f":
                    switch (parameters.Length)
                    {
                    case 4:
                        ObjMesh.ObjTriangle objTriangle = new ObjMesh.ObjTriangle();
                        objTriangle.Index0 = ParseFaceParameter(parameters[1]);
                        objTriangle.Index1 = ParseFaceParameter(parameters[2]);
                        objTriangle.Index2 = ParseFaceParameter(parameters[3]);
                        objTriangles.Add(objTriangle);
                        break;

                    case 5:
                        ObjMesh.ObjQuad objQuad = new ObjMesh.ObjQuad();
                        objQuad.Index0 = ParseFaceParameter(parameters[1]);
                        objQuad.Index1 = ParseFaceParameter(parameters[2]);
                        objQuad.Index2 = ParseFaceParameter(parameters[3]);
                        objQuad.Index3 = ParseFaceParameter(parameters[4]);
                        objQuads.Add(objQuad);
                        break;
                    }
                    break;
                }
            }

            mesh.Vertices  = objVertices.ToArray();
            mesh.Triangles = objTriangles.ToArray();
            mesh.Quads     = objQuads.ToArray();

            objVerticesIndexDictionary = null;
            vertices     = null;
            normals      = null;
            texCoords    = null;
            objVertices  = null;
            objTriangles = null;
            objQuads     = null;
        }