示例#1
0
        ColorValue RenderPixel(Vec2I pixelIndex)
        {
            int triangleIndex = lightmapTriangleMap[pixelIndex.Y][pixelIndex.X];

            if (triangleIndex == -1)
            {
                return(new ColorValue(-1, -1, -1, -1));
            }

            Mesh mesh = lightmapMeshObject.Mesh;

            Mat4 transform = new Mat4(lightmapMeshObject.Rotation.ToMat3() *
                                      Mat3.FromScale(lightmapMeshObject.Scale), lightmapMeshObject.Position);

            Vec3 position;
            Vec3 normal;
            {
                Vec2 texCoord = GetTexCoordByPixelIndex(pixelIndex);

                int index0 = mesh.Indices[triangleIndex * 3 + 0];
                int index1 = mesh.Indices[triangleIndex * 3 + 1];
                int index2 = mesh.Indices[triangleIndex * 3 + 2];

                Vec3 position0 = transform * mesh.Positions[index0];
                Vec3 position1 = transform * mesh.Positions[index1];
                Vec3 position2 = transform * mesh.Positions[index2];

                Vec3 normal0 = lightmapMeshObject.Rotation * mesh.Normals[index0];
                Vec3 normal1 = lightmapMeshObject.Rotation * mesh.Normals[index1];
                Vec3 normal2 = lightmapMeshObject.Rotation * mesh.Normals[index2];

                Vec2 texCoord0 = mesh.LightmapTexCoords[index0];
                Vec2 texCoord1 = mesh.LightmapTexCoords[index1];
                Vec2 texCoord2 = mesh.LightmapTexCoords[index2];

                float coef0;
                float coef1;
                GetTriangePositionCoefficients(ref texCoord0, ref texCoord1, ref texCoord2,
                                               ref texCoord, out coef0, out coef1);

                //calculate position
                {
                    Vec3 p01 = Vec3.Lerp(position0, position1, coef0);
                    Vec3 p02 = Vec3.Lerp(position0, position2, coef0);
                    position = Vec3.Lerp(p01, p02, coef1);
                }

                //calculate normal
                {
                    Vec3 p01 = Vec3.Lerp(normal0, normal1, coef0);
                    Vec3 p02 = Vec3.Lerp(normal0, normal2, coef0);
                    normal = Vec3.Lerp(p01, p02, coef1);
                    normal.Normalize();
                }
            }

            //calculate pixel color
            ColorValue resultColor = new ColorValue(0, 0, 0);

            foreach (MyLight light in lights)
            {
                //simple culling method. need use grid
                if (!light.Bounds.IsContainsPoint(position))
                {
                    continue;
                }

                //calculate illumination
                float illumination = light.GetIllumination(position, normal);
                if (illumination <= .00001f)
                {
                    continue;
                }

                //check for direct visibility
                bool shadowed = false;

                if (calculateShadows)
                {
                    Ray ray = light.GetCheckVisibilityRay(position);

                    RayCastResult result = physicsScene.RayCast(ray, contactGroup);
                    if (result.Shape != null)
                    {
                        shadowed = true;
                    }
                }

                ColorValue color = light.DiffuseColor * illumination;

                if (shadowed)
                {
                    color *= new ColorValue(1, 1, 1) - shadowColor;
                }

                resultColor += color;
            }

            resultColor.Alpha = 1;
            return(resultColor);
        }