コード例 #1
0
        public TransientBuffer Alloc(uint size)
        {
            var tb = new TransientBuffer
            {
                size = size
            };

            for (int i = 0; i < buffers.Count; i++)
            {
                ref TransientBufferDesc tbc = ref buffers.At(i);
                if (tbc.size + size < tbc.buffer.Size)
                {
                    tb.offset = tbc.size;
                    tb.buffer = tbc.buffer;
                    tbc.size += MathUtil.Align(size, (uint)alignment);
                    return(tb);
                }
            }
コード例 #2
0
ファイル: ObjModelReader.cs プロジェクト: Gaiaxis/SharpGame
        /*
         * public static void CalculateMeshTangents(FastList<VertexPosTexNorm> vertices,
         *  FastList<VertexPosTexNTB> tangentVertices, List<uint[]> trianglesList)
         * {
         *  //speed up math by copying the mesh arrays
         *  //variable definitions
         *  int vertexCount = vertices.Count;
         *
         *  vec3[] tan1 = new vec3[vertexCount];
         *  vec3[] tan2 = new vec3[vertexCount];
         *
         *  vec4[] tangents = new vec4[vertexCount];
         *
         *  foreach (var triangles in trianglesList)
         *  {
         *      int triangleCount = triangles.Length;
         *      for (long a = 0; a < triangleCount; a += 3)
         *      {
         *          int i1 = (int)triangles[a + 0];
         *          int i2 = (int)triangles[a + 1];
         *          int i3 = (int)triangles[a + 2];
         *
         *          vec3 v1 = vertices[i1].position;
         *          vec3 v2 = vertices[i2].position;
         *          vec3 v3 = vertices[i3].position;
         *
         *          vec2 w1 = vertices[i1].texcoord;
         *          vec2 w2 = vertices[i2].texcoord;
         *          vec2 w3 = vertices[i3].texcoord;
         *
         *          float x1 = v2.x - v1.x;
         *          float x2 = v3.x - v1.x;
         *          float y1 = v2.y - v1.y;
         *          float y2 = v3.y - v1.y;
         *          float z1 = v2.z - v1.z;
         *          float z2 = v3.z - v1.z;
         *
         *          float s1 = w2.x - w1.x;
         *          float s2 = w3.x - w1.x;
         *          float t1 = w2.y - w1.y;
         *          float t2 = w3.y - w1.y;
         *
         *          float r = 1.0f / (s1 * t2 - s2 * t1);
         *
         *          vec3 sdir = new vec3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
         *          vec3 tdir = new vec3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
         *
         *          tan1[i1] += sdir;
         *          tan1[i2] += sdir;
         *          tan1[i3] += sdir;
         *
         *          tan2[i1] += tdir;
         *          tan2[i2] += tdir;
         *          tan2[i3] += tdir;
         *      }
         *  }
         *
         *  for (int a = 0; a < vertexCount; ++a)
         *  {
         *      vec3 n = vertices[a].normal;
         *      vec3 t = tan1[a];
         *      if (t == vec3.Zero)
         *      {
         *          continue;
         *      }
         *
         *      ref var newVertex = ref tangentVertices.At(a);
         *      newVertex.position = vertices[a].position;
         *      newVertex.texcoord = vertices[a].texcoord;
         *      newVertex.normal = vertices[a].normal;
         *
         *      vec3 tmp = glm.normalize(t - n * vec3.Dot(n, t));
         *
         *      var tangent = new vec4(tmp.x, tmp.y, tmp.z, 0);
         *      tangent.w = (vec3.Dot(vec3.Cross(t, n), tan2[a]) < 0.0f) ? -1.0f : 1.0f;
         *      newVertex.tangent = tangent;
         *  }
         *
         * }*/

        void CreateTangentSpaceTangents(FastList <VertexPosTexNorm> vertices,
                                        FastList <VertexPosTexNTB> tangentVertices, List <uint[]> trianglesList)
        {
            vec3 v0, v1, v2;
            vec3 p0, p1, p2;
            vec3 d1, d2;

            float[,] uv = new float[3, 2];
            float det, u, v, l1, l2;

            int vertexCount = vertices.Count;

            vec3[] sVector = new vec3[vertexCount];
            vec3[] tVector = new vec3[vertexCount];

            foreach (var indices in trianglesList)
            {
                int faceCount = indices.Length;
                for (int k = 0; k < faceCount; k += 3)
                {
                    v0 = vertices[(int)indices[k]].position;
                    v1 = vertices[(int)indices[k + 1]].position - v0;
                    v2 = vertices[(int)indices[k + 2]].position - v0;

                    uv[0, 0] = vertices[(int)indices[k]].texcoord.x;
                    uv[0, 1] = vertices[(int)indices[k]].texcoord.y;
                    uv[1, 0] = vertices[(int)indices[k + 1]].texcoord.x - uv[0, 0];
                    uv[1, 1] = vertices[(int)indices[k + 1]].texcoord.y - uv[0, 1];
                    uv[2, 0] = vertices[(int)indices[k + 2]].texcoord.x - uv[0, 0];
                    uv[2, 1] = vertices[(int)indices[k + 2]].texcoord.y - uv[0, 1];

                    det = (uv[1, 0] * uv[2, 1]) - (uv[2, 0] * uv[1, 1]);

                    if (Math.Abs(det) < 0.000001f)
                    {
                        continue;
                    }

                    u  = 0; v = 0;
                    u -= uv[0, 0];
                    v -= uv[0, 1];
                    p0 = v0 + v1 * ((u * uv[2, 1] - uv[2, 0] * v) / det) + v2 * ((uv[1, 0] * v - u * uv[1, 1]) / det);

                    u  = 1; v = 0;
                    u -= uv[0, 0];
                    v -= uv[0, 1];

                    p1 = v0 + v1 * ((u * uv[2, 1] - uv[2, 0] * v) / det) + v2 * ((uv[1, 0] * v - u * uv[1, 1]) / det);

                    u   = 0; v = 1;
                    u  -= uv[0, 0]; v -= uv[0, 1];
                    p2  = v0 + v1 * ((u * uv[2, 1] - uv[2, 0] * v) / det) + v2 * ((uv[1, 0] * v - u * uv[1, 1]) / det);
                    d1  = p2 - p0;
                    d2  = p1 - p0;
                    l1  = glm.length(d1);
                    l2  = glm.length(d2);
                    d1 *= 1.0f / l1;
                    d2 *= 1.0f / l2;
                    int j = (int)indices[k];

                    sVector[j]   += d1;
                    tVector[j].x += d2.x; tVector[j].y += d2.y; tVector[j].z += d2.z;

                    j = (int)indices[k + 1];

                    sVector[j].x += d1.x; sVector[j].y += d1.y; sVector[j].z += d1.z;
                    tVector[j].x += d2.x; tVector[j].y += d2.y; tVector[j].z += d2.z;

                    j = (int)indices[k + 2];

                    sVector[j].x += d1.x; sVector[j].y += d1.y; sVector[j].z += d1.z;
                    tVector[j].x += d2.x; tVector[j].y += d2.y; tVector[j].z += d2.z;
                }
            }

            for (int i = 0; i < vertexCount; i++)
            {
                v0 = sVector[i];
                v0 = NormalizeRobust(v0);
                v1 = tVector[i];

                vec3 n = glm.vec3(vertices[i].normal.x, vertices[i].normal.y, vertices[i].normal.z);
                if (glm.length2(v1) < 0.0001f)
                {
                    v1 = glm.cross(v0, n);
                }

                v1           = NormalizeRobust(v1);
                sVector[i]   = v0;
                tVector[i].x = v1.x;
                tVector[i].y = v1.y;
                tVector[i].z = v1.z;

                ref var newVertex = ref tangentVertices.At(i);
                newVertex.position = vertices[i].position;
                newVertex.texcoord = vertices[i].texcoord;

                newVertex.normal    = n;
                newVertex.tangent   = tVector[i];
                newVertex.bitangent = sVector[i];
            }