Ejemplo n.º 1
0
        public static void readOBJ(string filename, MaterialManager materialManager, ref List <OBJFileReader.OBJFace> faces, ref List <Vector3> positions, ref List <Vector3> normals, ref List <Vector2> texCoords, ref Material material)
        {
            // Sections
            // . normals
            // . texcoords
            // . verts
            // . faces


            StreamReader sourceFile = new StreamReader(filename);

            string line;

            do
            {
                line = sourceFile.ReadLine();
                if (line == null)
                {
                    break;
                }

                if (line.Contains("#"))
                {
                    // comment
                }
                else if (line.Contains("mtllib"))
                {
                    // material
                    // Read material properties from file
                    OBJFileReader.readMaterialFile(line, materialManager, ref material);
                }
                else if (line.Contains("usemtl"))
                {
                    // material, what material to use.
                }
                else if (line.Contains("vn"))
                {
                    normals.Add(readNormal(line));
                }
                else if (line.Contains("vt"))
                {
                    texCoords.Add(readVector2(line));
                }
                else if (line.Contains("v"))
                {
                    positions.Add(readPos(line));
                }
                else if (line.Contains("f"))
                {
                    OBJFileReader.readFaces(line, ref faces);
                }
            } while (line != null);


            sourceFile.Close();
        }
Ejemplo n.º 2
0
		// Reads on .obj file
		static public MeshData CreateFromFile(string filename, MaterialManager materialManager)
		{
			MeshData newData = new MeshData();

			List<Vector3> positions = new List<Vector3>();
			List<Vector3> normals = new List<Vector3>();
			List<Vector2> texCoords = new List<Vector2>();

			List<OBJFileReader.OBJFace> faces = new List<OBJFileReader.OBJFace>();

			Material meshMaterial = new Material("");

			OBJFileReader.readOBJ(filename, materialManager, ref faces, ref positions, ref normals, ref texCoords, ref meshMaterial);

			// Create positions 

			newData.hasPositionData = true;
			newData.hasTexCoordData = true;
			newData.hasNormalData = true;

			bool useIndices = true;

			List<OBJFileReader.OBJFace> uniqueFaces = null;
			Dictionary<OBJFileReader.OBJFace, int> uniqueFacesDict = null;
			

			if (useIndices)
			{
				uniqueFaces = new List<OBJFileReader.OBJFace>();
				uniqueFacesDict = new Dictionary<OBJFileReader.OBJFace, int>();
				newData.indices = new List<int>();
				newData.hasIndexData = true;
			}
			
			newData.positions = new List<Vector3>();
			newData.texCoords = new List<Vector2>();
			newData.normals = new List<Vector3>();

			bool addFace = true;

			foreach (OBJFileReader.OBJFace face in faces)
			{
				if (useIndices)
				{
					//OBJFileReader.OBJFinder finder = new OBJFileReader.OBJFinder(face);
					bool alreadyFound = uniqueFacesDict.ContainsKey(face);
					int faceIndex = -1;
					if (alreadyFound)
					{
						uniqueFacesDict.TryGetValue(face, out faceIndex);
						addFace = false;
					}
					else
					{
						faceIndex = uniqueFacesDict.Count;
						uniqueFacesDict.Add(face, faceIndex);

						// Add face info to arrays
						addFace = true;
					}
					newData.indices.Add(faceIndex);
				}
				
				// This is always true when not using indices
				if (addFace)
				{
					newData.positions.Add(positions[(int)face.positionIndex - 1]);
					newData.texCoords.Add(texCoords[(int)face.texCoordIndex - 1]);
					newData.normals.Add(normals[(int)face.normalIndex - 1]);
				}
			}

			Logger.LogInfo("Mesh Data read from " + filename);

			newData.drawType = MeshData.DataDrawType.Triangles;

			newData.GenerateBufferHandles();

			Error.checkGLError("Mesh Data created from file: " + filename);

			return newData;
		}
Ejemplo n.º 3
0
        private bool loadSceneByFile(string sceneFile, ref Scene scene)
        {
            StreamReader sourceFile   = new StreamReader(sceneFile);
            AssetManager assetManager = AssetManager.GetSingleton();

            bool    nameFound     = false;
            bool    positionFound = false;
            string  modelName     = null;
            Vector3 position      = new Vector3(0, 0, 0);
            float   scale         = 1.0f;
            bool    scaleFound    = false;

            bool          vsFound     = false;
            bool          fsFound     = false;
            ShaderProgram modelShader = null;

            List <PosAndDir> cameraFrames = new List <PosAndDir>();

            string vsName = null;
            string fsName = null;

            char[] space = { ' ' };
            string line;

            do
            {
                line = sourceFile.ReadLine();
                if (line == null)
                {
                    break;
                }

                if (line.Contains("#"))
                {
                    // comment
                }

                if (line.Contains("scene"))
                {
                    string[] sceneLines = line.Split(space);
                    string   sceneName  = sceneLines[1];
                    Logger.LogInfo("Loading scene " + sceneName + " from file.");
                }

                if (line.Contains("model"))
                {
                    string[] modelLines = line.Split(space);
                    modelName = modelLines[1];
                    nameFound = true;
                }

                if (line.Contains("position"))
                {
                    position      = OBJFileReader.readVector3(line);
                    positionFound = true;
                }

                if (line.Contains("scale"))
                {
                    scale      = OBJFileReader.readFloat(line);
                    scaleFound = true;
                }

                if (line.Contains("shadervs"))
                {
                    string[] tokens = line.Split(space);
                    vsFound = true;
                    vsName  = tokens[1];
                }

                if (line.Contains("shaderfs"))
                {
                    string[] tokens = line.Split(space);
                    fsFound = true;
                    fsName  = tokens[1];
                }

                if (nameFound && positionFound && scaleFound)
                {
                    Logger.LogInfo("Found model settings: " + modelName + ", P: " + position.ToString() + " S: " + scale);

                    int    fileTypeStart  = modelName.LastIndexOf('.');
                    int    nameLength     = modelName.Length - (modelName.Length - fileTypeStart);
                    string modelNameNoOBJ = modelName.Substring(0, nameLength);

                    scene.AddDrawable(assetManager.GetMesh(modelNameNoOBJ, modelName, assetManager.GetMaterial(modelNameNoOBJ).materialName, modelShader, position));

                    Logger.LogInfo("Model: " + modelName + " added to scene ");

                    nameFound     = false;
                    positionFound = false;
                    scaleFound    = false;
                }

                if (vsFound && fsFound)
                {
                    Logger.LogInfo("Found shader setting: VS: " + vsName + ", FS: " + fsName);

                    Logger.LogInfo("Shader: " + vsName + " added to scene ");

                    vsFound = false;
                    fsFound = false;
                }
            } while (line != null);

            sourceFile.Close();

            return(true);
        }
Ejemplo n.º 4
0
 static Vector3 readNormal(string normalLine)
 {
     // vn # # #
     return(OBJFileReader.readVector3(normalLine));
 }
Ejemplo n.º 5
0
 static Vector3 readPos(string posLine)
 {
     return(OBJFileReader.readVector3(posLine));
 }