Esempio n. 1
0
            /////////////////////////////////////////////////////////////////////////////////////////////////
            // Public methods
            /////////////////////////////////////////////////////////////////////////////////////////////////

            /// <summary>
            /// Takes a Mesh as input and will build a new list of faces and vertices. The vertex list will
            /// have no vertices sharing position in 3D space. The input mesh may have them, since often
            /// a vertex will have different mapping coordinates for each of the faces that share it.
            /// </summary>
            /// <param name="sourceMesh"></param>
            /// <param name="av3VerticesWorld"</param>
            public void BuildData(Mesh sourceMesh, Vector3[] av3VerticesWorld)
            {
                Vector3[]    av3Vertices  = sourceMesh.vertices;
                BoneWeight[] aBoneWeights = sourceMesh.boneWeights;

                Dictionary <UniqueVertex, RepeatedVertexList> dicUniqueVertex2RepeatedVertexList = new Dictionary <UniqueVertex, RepeatedVertexList>();

                m_listVertices      = new List <Vector3>();
                m_listVerticesWorld = new List <Vector3>();
                m_listBoneWeights   = new List <SerializableBoneWeight>();
                m_aFaceList         = new ListIndices[sourceMesh.subMeshCount];

                for (int nSubMesh = 0; nSubMesh < sourceMesh.subMeshCount; nSubMesh++)
                {
                    m_aFaceList[nSubMesh] = new ListIndices();
                    int[] anFaces = sourceMesh.GetTriangles(nSubMesh);

                    for (int i = 0; i < anFaces.Length; i++)
                    {
                        UniqueVertex vertex = new UniqueVertex(av3Vertices[anFaces[i]]);

                        if (dicUniqueVertex2RepeatedVertexList.ContainsKey(vertex))
                        {
                            dicUniqueVertex2RepeatedVertexList[vertex].Add(new RepeatedVertex(i / 3, anFaces[i]));
                            m_aFaceList[nSubMesh].m_listIndices.Add(dicUniqueVertex2RepeatedVertexList[vertex].UniqueIndex);
                        }
                        else
                        {
                            int nNewUniqueIndex = m_listVertices.Count;
                            dicUniqueVertex2RepeatedVertexList.Add(vertex, new RepeatedVertexList(nNewUniqueIndex, new RepeatedVertex(i / 3, anFaces[i])));
                            m_listVertices.Add(av3Vertices[anFaces[i]]);
                            m_listVerticesWorld.Add(av3VerticesWorld[anFaces[i]]);
                            m_aFaceList[nSubMesh].m_listIndices.Add(nNewUniqueIndex);

                            if (aBoneWeights != null && aBoneWeights.Length > 0)
                            {
                                m_listBoneWeights.Add(new SerializableBoneWeight(aBoneWeights[anFaces[i]]));
                            }
                        }
                    }
                }

                //Debug.Log("In: " + av3Vertices.Length + " vertices. Out: " + m_listVertices.Count + " vertices.");
            }
        public SimpleObjRenderer(Obj file)
        {
            m_vertexVBO   = GL.GenBuffer();
            m_indexVBO    = GL.GenBuffer();
            m_texcoordVBO = file.TexCoords.Count > 0 ? GL.GenBuffer() : -1;
            m_normalVBO   = file.Normals.Count > 0 ? GL.GenBuffer() : -1;
            m_textureVBO  = file.Material.DiffuseTexture != null?GL.GenTexture() : -1;

            m_unhighlightedShader = new Shader("UnlitTexture");
            m_unhighlightedShader.CompileSource(File.ReadAllText("resources/shaders/UnlitTexture.vert"), ShaderType.VertexShader);
            m_unhighlightedShader.CompileSource(File.ReadAllText("resources/shaders/UnlitTexture.frag"), ShaderType.FragmentShader);
            m_unhighlightedShader.LinkShader();

            m_highlightedShader = new Shader("TransformGizmoHighlight");
            m_highlightedShader.CompileSource(File.ReadAllText("resources/shaders/UnlitTexture.vert"), ShaderType.VertexShader);
            m_highlightedShader.CompileSource(File.ReadAllText("resources/shaders/TransformGizmoHighlight.frag"), ShaderType.FragmentShader);
            m_highlightedShader.LinkShader();

            // Generate an array of all vertices instead of the compact form OBJ comes as.
            Vector3[] positions = null;
            Vector2[] texcoords = null;
            Vector3[] normals   = null;
            int[]     triangles = new int[file.Faces.Count * 3];
            m_triangleCount = file.Faces.Count;

            List <UniqueVertex> uniqueVerts = new List <UniqueVertex>();

            for (int i = 0; i < file.Faces.Count; i++)
            {
                Obj.ObjFace face = file.Faces[i];
                for (int k = 0; k < 3; k++)
                {
                    var vertex = new UniqueVertex();
                    vertex.Position = file.Vertices[face.Positions[k]];
                    if (face.TexCoords != null)
                    {
                        vertex.TexCoord = file.TexCoords[face.TexCoords[k]];
                    }
                    if (face.Normals != null)
                    {
                        vertex.Normal = file.Normals[face.Normals[k]];
                    }


                    int vertIndex = uniqueVerts.IndexOf(vertex);
                    if (vertIndex < 0)
                    {
                        uniqueVerts.Add(vertex);
                        vertIndex = uniqueVerts.Count - 1;
                    }

                    triangles[(i * 3) + k] = vertIndex;
                }
            }

            // Copy the data out of the interlaced buffers.
            positions = new Vector3[uniqueVerts.Count];
            texcoords = file.TexCoords.Count > 0 ? new Vector2[uniqueVerts.Count] : null;
            normals   = file.Normals.Count > 0 ? new Vector3[uniqueVerts.Count] : null;

            m_boundingBox = new FAABox();
            for (int i = 0; i < uniqueVerts.Count; i++)
            {
                positions[i] = uniqueVerts[i].Position;
                m_boundingBox.Encapsulate(positions[i]);

                if (texcoords != null)
                {
                    texcoords[i] = new Vector2(uniqueVerts[i].TexCoord.X, 1 - uniqueVerts[i].TexCoord.Y);
                }
                if (normals != null)
                {
                    normals[i] = uniqueVerts[i].Normal;
                }
            }

            // Positions
            GL.BindBuffer(BufferTarget.ArrayBuffer, m_vertexVBO);
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(12 * positions.Length), positions, BufferUsageHint.StaticDraw);

            // Upload Indexes
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, m_indexVBO);
            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(4 * triangles.Length), triangles, BufferUsageHint.StaticDraw);

            // Texcoords
            if (m_texcoordVBO >= 0)
            {
                GL.BindBuffer(BufferTarget.ArrayBuffer, m_texcoordVBO);
                GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(8 * texcoords.Length), texcoords, BufferUsageHint.StaticDraw);
            }

            // Normals
            if (m_normalVBO >= 0)
            {
                GL.BindBuffer(BufferTarget.ArrayBuffer, m_normalVBO);
                GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(12 * normals.Length), normals, BufferUsageHint.StaticDraw);
            }

            // Texture
            if (m_textureVBO >= 0)
            {
                Obj.ObjMaterial mat = file.Material;

                GL.BindTexture(TextureTarget.Texture2D, m_textureVBO);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);

                // Black/white checkerboard
                float[] pixels = new[]
                {
                    0.0f, 0.0f, 0.0f, 255.0f, 255.0f, 255.0f,
                    255.0f, 255.0f, 255.0f, 0.0f, 0.0f, 0.0f
                };
                //GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, 2, 2, 0, PixelFormat.Rgb, PixelType.Float, pixels);

                System.Drawing.Imaging.BitmapData bmpData = mat.DiffuseTexture.LockBits(new System.Drawing.Rectangle(0, 0, mat.DiffuseTexture.Width, mat.DiffuseTexture.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, mat.DiffuseTexture.Width, mat.DiffuseTexture.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, bmpData.Scan0);
                mat.DiffuseTexture.UnlockBits(bmpData);
            }
        }
Esempio n. 3
0
                // Overrides from Object

                public override bool Equals(object obj)
                {
                    UniqueVertex uniqueVertex = obj as UniqueVertex;

                    return((uniqueVertex.m_nFixedX == m_nFixedX) && (uniqueVertex.m_nFixedY == m_nFixedY) && (uniqueVertex.m_nFixedZ == m_nFixedZ));
                }
        public SimpleObjRenderer(Obj file)
        {
            m_vertexVBO = GL.GenBuffer();
            m_indexVBO = GL.GenBuffer();
            m_texcoordVBO = file.TexCoords.Count > 0 ? GL.GenBuffer() : -1;
            m_normalVBO = file.Normals.Count > 0 ? GL.GenBuffer() : -1;
            m_textureVBO = file.Material.DiffuseTexture != null ? GL.GenTexture() : -1;

            m_unhighlightedShader = new Shader("UnlitTexture");
            m_unhighlightedShader.CompileSource(File.ReadAllText("resources/shaders/UnlitTexture.vert"), ShaderType.VertexShader);
            m_unhighlightedShader.CompileSource(File.ReadAllText("resources/shaders/UnlitTexture.frag"), ShaderType.FragmentShader);
            m_unhighlightedShader.LinkShader();

            m_highlightedShader = new Shader("TransformGizmoHighlight");
            m_highlightedShader.CompileSource(File.ReadAllText("resources/shaders/UnlitTexture.vert"), ShaderType.VertexShader);
            m_highlightedShader.CompileSource(File.ReadAllText("resources/shaders/TransformGizmoHighlight.frag"), ShaderType.FragmentShader);
            m_highlightedShader.LinkShader();

            // Generate an array of all vertices instead of the compact form OBJ comes as.
            Vector3[] positions = null;
            Vector2[] texcoords = null;
            Vector3[] normals = null;
            int[] triangles = new int[file.Faces.Count * 3];
            m_triangleCount = file.Faces.Count;

            List<UniqueVertex> uniqueVerts = new List<UniqueVertex>();

            for (int i = 0; i < file.Faces.Count; i++)
            {
                Obj.ObjFace face = file.Faces[i];
                for (int k = 0; k < 3; k++)
                {
                    var vertex = new UniqueVertex();
                    vertex.Position = file.Vertices[face.Positions[k]];
                    if (face.TexCoords != null) vertex.TexCoord = file.TexCoords[face.TexCoords[k]];
                    if (face.Normals != null) vertex.Normal = file.Normals[face.Normals[k]];


                    int vertIndex = uniqueVerts.IndexOf(vertex);
                    if (vertIndex < 0)
                    {
                        uniqueVerts.Add(vertex);
                        vertIndex = uniqueVerts.Count - 1;
                    }

                    triangles[(i * 3) + k] = vertIndex;
                }
            }

            // Copy the data out of the interlaced buffers.
            positions = new Vector3[uniqueVerts.Count];
            texcoords = file.TexCoords.Count > 0 ? new Vector2[uniqueVerts.Count] : null;
            normals = file.Normals.Count > 0 ? new Vector3[uniqueVerts.Count] : null;

            m_boundingBox = new FAABox();
            for (int i = 0; i < uniqueVerts.Count; i++)
            {
                positions[i] = uniqueVerts[i].Position;
                m_boundingBox.Encapsulate(positions[i]);

                if (texcoords != null) texcoords[i] = new Vector2(uniqueVerts[i].TexCoord.X, 1 - uniqueVerts[i].TexCoord.Y);
                if (normals != null) normals[i] = uniqueVerts[i].Normal;
            }

            // Positions
            GL.BindBuffer(BufferTarget.ArrayBuffer, m_vertexVBO);
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(12 * positions.Length), positions, BufferUsageHint.StaticDraw);

            // Upload Indexes
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, m_indexVBO);
            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(4 * triangles.Length), triangles, BufferUsageHint.StaticDraw);

            // Texcoords
            if (m_texcoordVBO >= 0)
            {
                GL.BindBuffer(BufferTarget.ArrayBuffer, m_texcoordVBO);
                GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(8 * texcoords.Length), texcoords, BufferUsageHint.StaticDraw);
            }

            // Normals
            if (m_normalVBO >= 0)
            {
                GL.BindBuffer(BufferTarget.ArrayBuffer, m_normalVBO);
                GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(12 * normals.Length), normals, BufferUsageHint.StaticDraw);
            }

            // Texture
            if (m_textureVBO >= 0)
            {
                Obj.ObjMaterial mat = file.Material;

                GL.BindTexture(TextureTarget.Texture2D, m_textureVBO);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);

                // Black/white checkerboard
                float[] pixels = new[]
                {
                    0.0f, 0.0f, 0.0f,   255.0f, 255.0f, 255.0f,
                    255.0f, 255.0f, 255.0f,   0.0f, 0.0f, 0.0f
                };
                //GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, 2, 2, 0, PixelFormat.Rgb, PixelType.Float, pixels);

                System.Drawing.Imaging.BitmapData bmpData = mat.DiffuseTexture.LockBits(new System.Drawing.Rectangle(0, 0, mat.DiffuseTexture.Width, mat.DiffuseTexture.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, mat.DiffuseTexture.Width, mat.DiffuseTexture.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, bmpData.Scan0);
                mat.DiffuseTexture.UnlockBits(bmpData);
            }
        }