1
$\begingroup$

I used Blender for my project using a real photo and CG. So, I made my custom projection matrix for Blender, using it's camera source code.

Here is my custom projection code for blender scene:

public override Vector2 Project(Vector3 vertex)
    {
        double pixelSize = SensorSize * Near / FocalLength;
        pixelSize /= Width;
        pixelSize *= 2;

        double xmin = -0.5 * Width;
        double ymin = -0.5 * Height;
        double xmax = 0.5 * Width;
        double ymax = 0.5 * Height;

        xmin *= pixelSize;
        ymin *= pixelSize;
        xmax *= pixelSize;
        ymax *= pixelSize;

        Matrix44 iw = Matrix44.Invert(this.Transform.WorldWithoutScale);
        Matrix44 k = Perspective(xmin, xmax, ymax, ymin, Near, Far);
        Vector3 v = Vector3.Transform(k * iw, vertex);
        Vector2 result = new Vector2(v.X / v.Z, v.Y / v.Z);

        result.X *= Width;
        result.Y *= -Height;
        result.X += Width * 0.5;
        result.Y += Height * 0.5;

        return result;
    }

Perspective method is complete same with blender lib math function. Sensor Size and Focal Length value is same with blender's inspector. All values are same with blender scene too. (camera position, rotation, mesh vertices ...)

But when I tested the default scene using this code, vertices position had small errors.

White points are projected vertices using my own code(ignore center vertex; That is a side effect of non-depth testing). Projected vertices do not match with cube's vertices; but it's error is too large, especially on the left-bottom side, so I think this is not a calculation error.

What is the problem with my code? How can I generate same the projection as the default scene?

$\endgroup$
2
  • 1
    $\begingroup$ Easy solution: Use Blender's code. $\endgroup$ Aug 29, 2014 at 20:39
  • 2
    $\begingroup$ This question appears to be off-topic because it is about Blender C/C++ source code. These questions are considered outside the scope of this StackExchange as defined in the help center. Development questions should be asked of the developers directly instead, see the contacting developers page of the blender wiki. $\endgroup$
    – gandalf3
    Aug 29, 2014 at 23:52

0

Browse other questions tagged .