示例#1
0
 public Vertex(BasicVec3 pos, BasicVec3 nrm, BasicVec2 tex, BasicVec4 col)
 {
     this.Pos = pos;
     this.Nrm = nrm;
     this.Tex = tex;
     this.Col = col;
 }
示例#2
0
        public void LoadScene(RMDScene rmdScene)
        {

            _camera = new Camera();
            // set up shader program
            _shaderProg = new ShaderProgram("shader");
            _shaderProg.AddUniform("proj");
            _shaderProg.AddUniform("view");
            _shaderProg.AddUniform("tran");
            _shaderProg.AddUniform("diffuse");
            _shaderProg.AddUniform("diffuseColor");
            _shaderProg.AddUniform("isTextured"); //used like a bool but is actually an int because of glsl design limitations ¬~¬
            _shaderProg.Bind();

            Console.WriteLine("loading scene");
            Console.WriteLine("num textures: {0} ", rmdScene.TextureDictionary != null ? rmdScene.TextureDictionary.TextureCount : 0);

            if (rmdScene.TextureDictionary != null)
            {
                int textureIdx = 0;
                foreach (RWTextureNative texture in rmdScene.TextureDictionary.Textures)
                {
                    Console.WriteLine("processing texture: {0}", textureIdx++);

                    // get the pixel array
                    BasicCol4[] pixels = new BasicCol4[texture.Width * texture.Height];
                    for (int i = 0; i < texture.Width * texture.Height; i++)
                        pixels[i] = texture.IsIndexed ? new BasicCol4(texture.Palette[texture.PixelIndices[i]]) : new BasicCol4(texture.Pixels[i]);

                    // create the texture
                    int tex = 0;
                    GL.CreateTextures(TextureTarget.Texture2D, 1, out tex);

                    // set up the params
                    GL.TextureParameter(tex, TextureParameterName.TextureWrapS, RWToGLConversionHelper.WrapDictionary[texture.HorrizontalAddressingMode]);
                    GL.TextureParameter(tex, TextureParameterName.TextureWrapT, RWToGLConversionHelper.WrapDictionary[texture.VerticalAddressingMode]);
                    GL.TextureParameter(tex, TextureParameterName.TextureMagFilter, RWToGLConversionHelper.FilterDictionary[texture.FilterMode]);
                    GL.TextureParameter(tex, TextureParameterName.TextureMinFilter, RWToGLConversionHelper.FilterDictionary[texture.FilterMode]);

                    // set up the bitmap data
                    GL.TextureStorage2D(tex, 1, SizedInternalFormat.Rgba8, texture.Width, texture.Height);
                    GL.TextureSubImage2D(tex, 0, 0, 0, texture.Width, texture.Height, PixelFormat.Rgba, PixelType.UnsignedByte, pixels);

                    // add a texture lookup for the processed texture
                    _texLookup.Add(texture.Name, tex);
                }
            }

            float min_x = 0, min_y = 0, min_z = 0;
            float max_x = 0, max_y = 0, max_z = 0;
            int clumpIdx = 0;
            foreach (RWScene rwScene in rmdScene.Scenes)
            {
                Console.WriteLine("geometry count: {0}", rwScene.MeshCount);
                Console.WriteLine("processing clump: {0}", clumpIdx++);

                int drawCallIdx = 0;
                foreach (RWDrawCall drawCall in rwScene.DrawCalls)
                {
                    Console.WriteLine("processing draw call: {0}", drawCallIdx);
                    Console.WriteLine("geo:{0}\t frame:{1}\t flag1:{2}\t flag2{3}\t", drawCall.MeshIndex, drawCall.NodeIndex, drawCall.Flag1, drawCall.Flag2);

                    var geom = rwScene.Meshes[drawCall.MeshIndex];
                    var frame = rwScene.Nodes[drawCall.NodeIndex];

                    Console.WriteLine(geom.MaterialSplitData.MaterialSplitCount);
                    Console.WriteLine(geom.MaterialSplitData.PrimitiveType);

                    Vertex[] vertices = new Vertex[geom.VertexCount];
                    int[][] allIndices = new int[geom.MaterialSplitData.MaterialSplitCount][];
                    int[] allIndicesCount = new int[geom.MaterialSplitData.MaterialSplitCount];
                    int[] fullIndices = new int[geom.TriangleCount*3];

                    for(int i = 0; i < geom.TriangleCount; i++)
                    {
                        fullIndices[i * 3 + 0] = geom.Triangles[i].A;
                        fullIndices[i * 3 + 1] = geom.Triangles[i].B;
                        fullIndices[i * 3 + 2] = geom.Triangles[i].C;
                    }

                    // remap the vertices
                    for (int i = 0; i < geom.VertexCount; i++)
                    {
                        // get the vertex info
                        //var oldPos = geom.HasVertices ? Vector3.Transform(new Vector4(geom.Vertices[i], 1.0f), frame.WorldTransform).Xyz : new Vector3(0, 0, 0);
                        var oldPos = geom.HasVertices ? SN.Vector3.Transform(geom.Vertices[i], frame.WorldTransform) : new SN.Vector3(0, 0, 0);
                        var oldNrm = geom.HasNormals ? SN.Vector3.TransformNormal(geom.Normals[i], frame.WorldTransform) : new SN.Vector3(0, 0, 0);
                        var oldTcd = geom.HasTexCoords ? geom.TextureCoordinateChannels[0][i] : new SN.Vector2(0, 0);
                        var oldCol = geom.HasColors ? geom.Colors[i] : Color.White;

                        if (oldPos.X < min_x) min_x = oldPos.X;
                        if (oldPos.X > max_x) max_x = oldPos.X;
                        if (oldPos.Y < min_y) min_y = oldPos.Y;
                        if (oldPos.Y > max_y) max_y = oldPos.Y;
                        if (oldPos.Z < min_z) min_z = oldPos.Z;
                        if (oldPos.Z > max_z) max_z = oldPos.Z;

                        // create basic vecs
                        var pos = new BasicVec3(oldPos);
                        var nrm = new BasicVec3(oldNrm);
                        var tcd = new BasicVec2(oldTcd);
                        var col = new BasicVec4(oldCol);

                        // set the new interleaved vertex
                        vertices[i] = new Vertex(pos, nrm, tcd, col);
                    }

                    for (int i = 0; i < geom.MaterialSplitData.MaterialSplitCount; i++)
                    {
                        allIndices[i] = geom.MaterialSplitData.MaterialSplits[i].Indices;
                        allIndicesCount[i] = geom.MaterialSplitData.MaterialSplits[i].IndexCount;
                    }
                    Console.WriteLine("tex channels: " + geom.TextureCoordinateChannelCount);
                    Console.WriteLine("num materials: " + geom.MaterialCount);

                    for (int m = 0; m < geom.MaterialCount; m++)
                        Console.WriteLine("material: {0}", geom.Materials[m]);

                    // setup the vbo
                    int vbo = GL.GenBuffer();
                    GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
                    GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * 12 * vertices.Length, vertices, BufferUsageHint.StaticDraw);

                    // setup the ibo
                    int[] ibo = new int[allIndices.Length];
                    GL.GenBuffers(allIndices.Length, ibo);
                    for (int i = 0; i < ibo.Length; i++)
                    {
                        GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo[i]);
                        GL.BufferData(BufferTarget.ElementArrayBuffer, sizeof(int) * allIndices[i].Length, allIndices[i], BufferUsageHint.StaticDraw);
                    }
                    // setup the vertex attribs
                    GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, sizeof(float) * 12, 0);
                    GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, sizeof(float) * 12, (sizeof(float) * 3));
                    GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, sizeof(float) * 12, (sizeof(float) * 6));
                    GL.VertexAttribPointer(3, 4, VertexAttribPointerType.Float, false, sizeof(float) * 12, (sizeof(float) * 8));

                    // get the mesh color from the material
                    // get the texture name if there's a texture assigned
                    string[] texname = new string[geom.MaterialSplitData.MaterialSplitCount];
                    Color[] colors = new Color[geom.MaterialSplitData.MaterialSplitCount];
                    for (int i = 0; i < texname.Length; i++)
                    {
                        texname[i] = geom.MaterialCount > 0 && geom.Materials[geom.MaterialSplitData.MaterialSplits[i].MaterialIndex].IsTextured ? geom.Materials[geom.MaterialSplitData.MaterialSplits[i].MaterialIndex].TextureReference.ReferencedTextureName : string.Empty;
                        colors[i] = geom.MaterialCount > 0 ? geom.Materials[geom.MaterialSplitData.MaterialSplits[i].MaterialIndex].Color : Color.White;
                    }
                    // add the render model to the list
                    _models.Add(new GPUModel(vbo, ibo, geom.VertexCount, allIndicesCount, texname, colors, geom.MaterialSplitData.PrimitiveType == RWPrimitiveType.TriangleStrip));
                }
            }
            _cameraTarget = new Vector3((min_x + max_x) / 2, (min_y + max_y) / 2, (min_z + max_z) / 2);
            Console.WriteLine(_cameraTarget);
            
            // everything's processed, the scene is ready to be rendered

            _sceneready = true;
            Program.LoopFunctions.Add(Input);
        }
示例#3
0
 public Vertex(BasicVec3 pos, BasicVec3 nrm, BasicVec2 tex, BasicVec4 col)
 {
     this.pos = pos;
     this.nrm = nrm;
     this.tex = tex;
     this.col = col;
 }