2
\$\begingroup\$

I am using SunBurn for lights and rendering, when i create sceneobject in code, effect is like this enter image description here

see in this light is in the middle so sceneobject in middle is well lit and as we go to corner light fades

I want to make it like this enter image description here

see in this light spreads gradually, this i created with block imported in editor but i want to do it with blocks created with code.

This is how i am adding sceneobjects :

var vertexBuffer = new VertexBuffer(GraphicsDevice, 
    VertexPositionNormalTextureBump.VertexDeclaration, 
    tile.quad.Vertices.Length, BufferUsage.None);

VertexPositionNormalTextureBump.BuildTangentSpaceDataForTriangleList(
    tile.quad.Indices, tile.quad.Vertices);

vertexBuffer.SetData(tile.quad.Vertices);

 BoundingSphere bSphere = 
     new BoundingSphere(tile.quad.Origin, 
     tile.quad.Height > tile.quad.Width ? tile.quad.Height : tile.quad.Width);

 SceneObject obj = new SceneObject(tile.Effect,bSphere,
    BoundingBox.CreateFromSphere(bSphere),
    Matrix.CreateWorld(tile.Position, Vector3.Forward, Vector3.Up),
    vertexBuffer,PrimitiveType.TriangleStrip,2, 0, 0);

 obj.Visibility = ObjectVisibility.Rendered;

 tile.SceneObjectId = obj.UniqueId;

 sceneInterface.ObjectManager.Submit(obj);

I think this is because tangent vectors are not correct, this is my code to add position, tangent, uvs, normals and binormals

public Quad(Vector3 origin, Vector3 normal, Vector3 up,
  float width, float height,float u=1, float v=1)
{
    Vertices = new VertexPositionNormalTextureBump[4];
    Indices = new short[6];
    Origin = origin;
    Normal = normal;
    Up = up;

    Width = width;
    Height = height;

    // Calculate the quad corners
    Left = Vector3.Cross(normal, Up);
    Vector3 uppercenter = (Up * height / 2) + origin;
    UpperLeft = uppercenter + (Left * width / 2);
    UpperRight = uppercenter - (Left * width / 2);
    LowerLeft = UpperLeft - (Up * height);
    LowerRight = UpperRight - (Up * height);


    if (u != 1 || v != 1)
        FillVertices(u, v);
    else
    FillVertices();
}
private void BuildTangentSpaceDataForTriangleList(short[] indices,
    Vector3[] positions, Vector2[] uvs, Vector3[] tangents, Vector3[] binormals)
{
    for (int i = 0; i < indices.Length; i += 3)
    {
        int index_vert0 = indices[i];
        int index_vert1 = indices[i + 1];
        int index_vert2 = indices[i + 2];

        Vector2 uv0 = uvs[index_vert0];
        Vector2 uv1 = uvs[index_vert1];
        Vector2 uv2 = uvs[index_vert2];

        float s1 = uv1.X - uv0.X;
        float s2 = uv2.X - uv0.X;
        float t1 = uv1.Y - uv0.Y;
        float t2 = uv2.Y - uv0.Y;

        float r = s1 * t2 - s2 * t1;
        if (r != 0.0f)
        {
            r = 1.0f / r;

            Vector3 position0 = positions[index_vert0];
            Vector3 position1 = positions[index_vert1];
            Vector3 position2 = positions[index_vert2];

            float x1 = position1.X - position0.X;
            float x2 = position2.X - position0.X;
            float y1 = position1.Y - position0.Y;
            float y2 = position2.Y - position0.Y;
            float z1 = position1.Z - position0.Z;
            float z2 = position2.Z - position0.Z;

            Vector3 tangent = new Vector3(
                (t2 * x1 - t1 * x2) * r,
                (t2 * y1 - t1 * y2) * r,
                (t2 * z1 - t1 * z2) * r);

            Vector3 binormal = new Vector3(
                (s1 * x2 - s2 * x1) * r,
                (s1 * y2 - s2 * y1) * r,
                (s1 * z2 - s2 * z1) * r);

            tangents[index_vert0] += tangent;
            tangents[index_vert1] += tangent;
            tangents[index_vert2] += tangent;

            binormals[index_vert0] += binormal;
            binormals[index_vert1] += binormal;
            binormals[index_vert2] += binormal;
        }
    }

    for (int i = 0; i < tangents.Length; i++)
    {
        tangents[i].Normalize();
        binormals[i].Normalize();
    }
}
private void FillVertices()
{
    // Fill in texture coordinates to display full texture
    // on quad, its rotated as planes are rotated while applying
    Vector2 textureUpperLeft = new Vector2(1.0f, 1.0f);
    Vector2 textureUpperRight = new Vector2(0.0f, 1.0f);
    Vector2 textureLowerLeft =new Vector2(1.0f, 0.0f);
    Vector2 textureLowerRight = new Vector2(0.0f, 0.0f); 

    // Provide a normal for each vertex
    for (int i = 0; i < Vertices.Length; i++)
    {
        Vertices[i].Normal = Normal;
    }



    // Set the position and texture coordinate for each
    // vertex
    Vertices[0].Position = LowerLeft;
    Vertices[0].TextureCoordinate = textureLowerLeft;
    Vertices[1].Position = UpperLeft;
    Vertices[1].TextureCoordinate = textureUpperLeft;
    Vertices[2].Position = LowerRight;
    Vertices[2].TextureCoordinate = textureLowerRight;
    Vertices[3].Position = UpperRight;
    Vertices[3].TextureCoordinate = textureUpperRight;

    // Set the index buffer for each vertex, using
    // clockwise winding
    Indices[0] = 0;
    Indices[1] = 1;
    Indices[2] = 2;
    Indices[3] = 2;
    Indices[4] = 1;
    Indices[5] = 3;

    var tangents = new Vector3[4];
    var binormals = new Vector3[4];
    var uvs = new Vector2[4];
    var positions = new Vector3[4];

    for (int i = 0; i < 4; i++)
    {
        positions[i] = Vertices[i].Position;
        uvs[i] = Vertices[i].TextureCoordinate;
    }
    BuildTangentSpaceDataForTriangleList(Indices, positions, uvs, tangents, binormals);
    for (int i = 0; i < 4; i++)
    {
        Vertices[i].Tangent = tangents[i];
        Vertices[i].Binormal = binormals[i];
    }

}

i ll be glad if somebody could help me, thank you

\$\endgroup\$
1
  • \$\begingroup\$ It looks like if the lighting values at each vertex were interpolated across the face of poly, then you'd have the result you want. I don't know much about the tech you're working with, but is there a way for you to check if you are using a flat shading model? \$\endgroup\$ Sep 20 '13 at 21:35
1
\$\begingroup\$

Unless you are leveraging the tan/bitan for normal mapping, and you have some really strange normal maps, that is unlikely to cause the issue.

It looks to me like you are performing per vertex lighting calculations in the top frame, and the bottom is per pixel. Or, the position data you are using to perform attenuation calculations is a bit odd. Notice each block seems to have a flat shading, as if every pixel for each block is being attenuated using the same distance calculation.

\$\endgroup\$
1
  • \$\begingroup\$ I am using sunburn for lighting, and it uses tangent and binormals to calculate light. I know something is wrong in vertexbuffer, i think tangents and binormal values are wrong. because if i import a plane through sunburn content pipeline and get the vertexbuffer from that plane, light is spreading evenly. Don't know how to correct this code \$\endgroup\$
    – Shivam cv
    Sep 21 '13 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.