Exemple #1
0
        public StaticModel buildMeshes()
        {
            List <V3N3T2> verts = new List <V3N3T2>();
            List <ushort> index = new List <ushort>();

            StaticModel sm = new StaticModel();

            int indexCount  = 0;
            int indexOffset = indexCount;

            foreach (Material mat in myTris.Keys)
            {
                List <Tri> tris;
                myTris.TryGetValue(mat, out tris);

                //build the vertex list
                foreach (Tri t in tris)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        V3N3T2 v = new V3N3T2();
                        v.Position = myVerts[t.i[j]];
                        if (t.uv[j] != -1)
                        {
                            v.TexCoord = myUvs[t.uv[j]];
                        }
                        if (t.n[j] != -1)
                        {
                            v.Normal = myNorms[t.n[j]];
                        }

                        //check if this exact vertex is already in the list
                        if (!verts.Contains(v))
                        {
                            verts.Add(v);
                        }

                        index.Add((ushort)verts.IndexOf(v));
                        indexCount++;
                    }
                }

                Mesh m = new Mesh();
                m.primativeType = PrimitiveType.Triangles;
                m.indexBase     = indexOffset;
                m.indexCount    = indexCount;
                m.material      = mat;
                sm.myMeshes.Add(m);

                //update index base for next pass
                indexOffset += indexCount;
                indexCount   = 0;
            }

            sm.myVbo.setData(verts);
            sm.myIbo.setData(index);

            return(sm);
        }
Exemple #2
0
        public void loadVerts(BobModelChunk bmc)
        {
            for (int i = 0; i < bmc.vertexCount; i++)
            {
                V3N3T2 v = new V3N3T2();
                v.Position = bmc.verts[i];
                v.Normal   = bmc.normals[i];
                v.TexCoord = bmc.uvs[i];
                myVerts.Add(v);
            }

            myModel.myVbo.setData(myVerts);
        }
Exemple #3
0
        void loadVerts(Model m, Bob.ModelChunk bmc)
        {
            VertexBufferObject vbo = new VertexBufferObject(BufferUsageHint.StaticDraw);

            if (bmc.animated == false)
            {
                List <V3N3T2> verts = new List <V3N3T2>();
                for (int i = 0; i < bmc.vertexCount; i++)
                {
                    V3N3T2 v = new V3N3T2();
                    v.Position = bmc.verts[i];
                    v.Normal   = bmc.normals[i];
                    v.TexCoord = bmc.uvs[i];
                    verts.Add(v);
                }

                m.myBindings = V3N3T2.bindings();
                vbo.setData(verts);
            }
            else
            {
                List <V3N3T2B4W4> verts = new List <V3N3T2B4W4>();
                for (int i = 0; i < bmc.vertexCount; i++)
                {
                    V3N3T2B4W4 v = new V3N3T2B4W4();
                    v.Position   = bmc.verts[i];
                    v.Normal     = bmc.normals[i];
                    v.TexCoord   = bmc.uvs[i];
                    v.BoneId     = bmc.boneIdx[i];
                    v.BoneWeight = bmc.boneWeights[i];
                    verts.Add(v);
                }

                m.myBindings = V3N3T2B4W4.bindings();
                vbo.setData(verts);
            }

            m.myVbos.Add(vbo);
        }
Exemple #4
0
        public Model loadFromFile(string filename)
        {
            Info.print("Loading Assimp model {0}", filename);

            if (File.Exists(filename) == false)
            {
                Warn.print("Cannot find file {0}", filename);
                return(null);
            }

            myRootPath = Path.GetDirectoryName(filename);
            try
            {
                myScene = theAssimpContext.ImportFile(filename, PostProcessSteps.Triangulate | PostProcessSteps.GenerateNormals);
                Node rootNode = myScene.RootNode;

                //load static meshes
                createMeshes(rootNode);
            }
            catch
            {
                Warn.print("Failed to load model {0}", filename);
                return(null);
            }

            myModel.myBindings = V3N3T2.bindings();
            VertexBufferObject vbo = new VertexBufferObject(BufferUsageHint.StaticDraw);

            vbo.setData(myVerts);
            myModel.myVbos.Add(vbo);
            myModel.myIbo.setData(index);

            //should probably build a bounding box
            myModel.size = (findMax() - findMin()).Length / 2.0f;

            return(myModel);
        }
Exemple #5
0
        public static Model CreatePlane(Vector3 min, Vector3 max, Texture t)
        {
            Model model = new Model();

            ushort[] index = { 0, 1, 2,
                               0, 2, 3, //front
            };

            V3N3T2[] verts = new V3N3T2[4];

            //face
            verts[0].Position = new Vector3(max.X, min.Y, min.Z); verts[0].TexCoord = new Vector2(0, 0);
            verts[1].Position = new Vector3(min.X, min.Y, min.Z); verts[1].TexCoord = new Vector2(1, 0);
            verts[2].Position = new Vector3(min.X, max.Y, min.Z); verts[2].TexCoord = new Vector2(1, 1);
            verts[3].Position = new Vector3(max.X, max.Y, min.Z); verts[3].TexCoord = new Vector2(0, 1);

            model.myBindings = V3N3T2.bindings();
            VertexBufferObject vbo = new VertexBufferObject(BufferUsageHint.StaticDraw);

            vbo.setData(verts);
            model.myVbos.Add(vbo);
            model.myIbo.setData(index);

            Mesh mesh = new Mesh();

            mesh.primativeType = PrimitiveType.Triangles;
            mesh.indexBase     = 0;
            mesh.indexCount    = 6;
            mesh.material      = new Material("simple plane");
            mesh.material.addAttribute(new TextureAttribute("diffuseMap", t));
            mesh.material.myFeatures     |= Material.Feature.DiffuseMap;
            mesh.material.hasTransparency = t.hasAlpha;

            model.myMeshes.Add(mesh);
            return(model);
        }
Exemple #6
0
        void createMeshes(Node node)
        {
            if (node.HasMeshes)
            {
                foreach (int meshIndex in node.MeshIndices)
                {
                    Assimp.Mesh amesh = myScene.Meshes[meshIndex];

                    //create the material
                    Graphics.Material mat = createMaterial(myScene.Materials[amesh.MaterialIndex]);

                    //create the geometry
                    Graphics.Mesh mesh = new Graphics.Mesh();
                    mesh.primativeType = OpenTK.Graphics.OpenGL.PrimitiveType.Triangles;
                    mesh.indexBase     = currIndexOffset;
                    mesh.indexCount    = amesh.FaceCount * 3;
                    mesh.material      = mat;

                    //get the indices
                    foreach (Face face in amesh.Faces)
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            index.Add((UInt16)(face.Indices[i] + currVertOffset));
                            currIndexOffset++;
                        }
                    }

                    //get the verts
                    for (int i = 0; i < amesh.VertexCount; i++)
                    {
                        V3N3T2 v = new V3N3T2();
                        v.Position = toVector(amesh.Vertices[i]);

                        if (amesh.HasNormals == true)
                        {
                            v.Normal = toVector(amesh.Normals[i]);
                        }

                        if (amesh.HasTextureCoords(0) == true)
                        {
                            v.TexCoord = toVector2D(amesh.TextureCoordinateChannels[0][i]);
                        }

                        myVerts.Add(v);
                    }

                    currVertOffset += amesh.VertexCount;

                    myModel.myMeshes.Add(mesh);
                }
            }

            if (node.HasChildren)
            {
                foreach (Node child in node.Children)
                {
                    createMeshes(child);
                }
            }
        }
Exemple #7
0
        //public override void prepareFrameBegin() { }
        //public override void preparePerFrame(Renderable r) { }
        //public override void preparePerViewBegin(View v) { }
        //public override void preparePerView(Renderable r, View v) { }
        //public override void preparePerViewFinalize(View v) { }
        //public override void preparePerPassBegin(Pass p) { }

        public override void preparePerPass(Renderable r, Pass p)
        {
            StaticModelRenderable smr = r as StaticModelRenderable;

            StaticModelUniformData modelData = new StaticModelUniformData();

            modelData.modelMatrix  = smr.model.myInitialTransform * smr.modelMatrix;
            modelData.normalMatrix = (smr.model.myInitialTransform * smr.modelMatrix).ClearTranslation();
            //modelData.inverseNormalMatrix = modelData.normalMatrix.Inverted();
            modelData.activeLights = new Vector4(0, 1, 2, 3);
            myModelData.Add(modelData);

            //save the index for this model
            int modelDataIndex = myModelData.Count - 1;

            foreach (Mesh mesh in smr.model.myMeshes)
            {
                MaterialEffect effect            = getEffect(p.technique, (UInt32)mesh.material.myFeatures);
                PipelineState  pipeline          = effect.createPipeline(mesh.material);
                RenderQueue <StaticModelInfo> rq = p.findRenderQueue(pipeline.id) as RenderQueue <StaticModelInfo>;
                if (rq == null)
                {
                    rq      = Renderer.device.createRenderQueue <StaticModelInfo>(pipeline);
                    rq.name = rq.myPipeline.shaderState.shaderProgram.name + "-" + (mesh.material.hasTransparency == true ? "transparent" : "opaque");
                    rq.myPipeline.vaoState.vao = new VertexArrayObject();
                    rq.myPipeline.vaoState.vao.bindVertexFormat(rq.myPipeline.shaderState.shaderProgram, V3N3T2.bindings());
                    rq.visualizer = this;
                    p.registerQueue(rq);
                }

                StaticModelInfo info = rq.nextInfo();

                effect.updateRenderState(mesh.material, info.renderState);

                float dist = (p.view.camera.position - r.position).Length;
                info.distToCamera = dist;

                info.renderState.setUniform(new UniformData(0, Uniform.UniformType.Int, modelDataIndex));
                info.renderState.setStorageBuffer(myModelBuffer.id, 2);
                info.renderState.setVertexBuffer(smr.model.myVbos[0].id, 0, 0, V3N3T2.stride);
                info.renderState.setIndexBuffer(smr.model.myIbo.id);
                info.indexOffset = mesh.indexBase;
                info.indexCount  = mesh.indexCount;

                info.sortId = getSortId(info);
            }
        }
Exemple #8
0
        public static Model createCube(Vector3 min, Vector3 max, Texture t)
        {
            Model model = new Model();

            ushort[] index = { 0,   1,  2,
                               0,   2,  3, //front
                               4,   5,  6,
                               4,   6,  7, //left
                               8,   9, 10,
                               8,  10, 11, //right
                               12, 13, 14,
                               12, 14, 15, // top
                               16, 17, 18,
                               16, 18, 19, // bottom
                               20, 21, 22,
                               20, 22, 23  // back
            };

            V3N3T2[] verts = new V3N3T2[24];
            //front face
            verts[0].Position = new Vector3(max.X, min.Y, min.Z); verts[0].TexCoord = new Vector2(0, 0);
            verts[1].Position = new Vector3(min.X, min.Y, min.Z); verts[1].TexCoord = new Vector2(1, 0);
            verts[2].Position = new Vector3(min.X, max.Y, min.Z); verts[2].TexCoord = new Vector2(1, 1);
            verts[3].Position = new Vector3(max.X, max.Y, min.Z); verts[3].TexCoord = new Vector2(0, 1);

            //left
            verts[4].Position = new Vector3(min.X, min.Y, min.Z); verts[4].TexCoord = new Vector2(0, 0);
            verts[5].Position = new Vector3(min.X, min.Y, max.Z); verts[5].TexCoord = new Vector2(1, 0);
            verts[6].Position = new Vector3(min.X, max.Y, max.Z); verts[6].TexCoord = new Vector2(1, 1);
            verts[7].Position = new Vector3(min.X, max.Y, min.Z); verts[7].TexCoord = new Vector2(0, 1);

            //right
            verts[8].Position  = new Vector3(max.X, min.Y, max.Z); verts[8].TexCoord = new Vector2(0, 0);
            verts[9].Position  = new Vector3(max.X, min.Y, min.Z); verts[9].TexCoord = new Vector2(1, 0);
            verts[10].Position = new Vector3(max.X, max.Y, min.Z); verts[10].TexCoord = new Vector2(1, 1);
            verts[11].Position = new Vector3(max.X, max.Y, max.Z); verts[11].TexCoord = new Vector2(0, 1);

            //top
            verts[12].Position = new Vector3(min.X, max.Y, max.Z); verts[12].TexCoord = new Vector2(0, 0);
            verts[13].Position = new Vector3(max.X, max.Y, max.Z); verts[13].TexCoord = new Vector2(1, 0);
            verts[14].Position = new Vector3(max.X, max.Y, min.Z); verts[14].TexCoord = new Vector2(1, 1);
            verts[15].Position = new Vector3(min.X, max.Y, min.Z); verts[15].TexCoord = new Vector2(0, 1);

            //bottom
            verts[16].Position = new Vector3(min.X, min.Y, min.Z); verts[16].TexCoord = new Vector2(0, 0);
            verts[17].Position = new Vector3(max.X, min.Y, min.Z); verts[17].TexCoord = new Vector2(1, 0);
            verts[18].Position = new Vector3(max.X, min.Y, max.Z); verts[18].TexCoord = new Vector2(1, 1);
            verts[19].Position = new Vector3(min.X, min.Y, max.Z); verts[19].TexCoord = new Vector2(0, 1);

            //back
            verts[20].Position = new Vector3(min.X, min.Y, max.Z); verts[20].TexCoord = new Vector2(0, 0);
            verts[21].Position = new Vector3(max.X, min.Y, max.Z); verts[21].TexCoord = new Vector2(1, 0);
            verts[22].Position = new Vector3(max.X, max.Y, max.Z); verts[22].TexCoord = new Vector2(1, 1);
            verts[23].Position = new Vector3(min.X, max.Y, max.Z); verts[23].TexCoord = new Vector2(0, 1);

            model.myBindings = V3N3T2.bindings();
            VertexBufferObject vbo = new VertexBufferObject(BufferUsageHint.StaticDraw);

            vbo.setData(verts);
            model.myVbos.Add(vbo);
            model.myIbo.setData(index);

            Mesh mesh = new Mesh();

            mesh.primativeType = PrimitiveType.Triangles;
            mesh.indexBase     = 0;
            mesh.indexCount    = 36;
            mesh.material      = new Material("simple cube");
            mesh.material.addAttribute(new TextureAttribute("diffuseMap", t));
            mesh.material.myFeatures     |= Material.Feature.DiffuseMap;
            mesh.material.hasTransparency = t.hasAlpha;

            model.myMeshes.Add(mesh);
            return(model);
        }