0

I have my C# sample code posted at: opentk pitch rotation deforms the shape

As you can see in the code, I can draw and rotate the 3d object. What I want to do is: Given a 3d coordinate (x, y, z), how to get the projected 2d coordinate (x, y)?

Here is what I have so far:

    int[] viewport = new int[4];
    Matrix4 modelViewMatrix, projectionMatrix;
    GL.GetFloat(GetPName.ModelviewMatrix, out modelViewMatrix);
    GL.GetFloat(GetPName.ProjectionMatrix, out projectionMatrix);
    GL.GetInteger(GetPName.Viewport, viewport);
    // [x, y] = vec * modelViewMatrix * projectionMatrix * viewport
Community
  • 1
  • 1
max
  • 9,708
  • 15
  • 89
  • 144

1 Answers1

0

You are close. The line you commented out only contains [x,y]. Assuming you have all of your matrices properly worked out (and assuming 'vec' is a 3 dimensional vector), you do the multiplication as you have commented out, except your result will be another 3 dimensional vector where you just ignore your z-value. Your z-values are transformed by these matrix multiplications, but graphically they cannot be seen (while looking at XY-plane). That being said, depending on how you view things (moving camera VS moving object), the angle at which the object is being viewed can change the 3D to '2D' transformation.

TheBlindSpring
  • 631
  • 5
  • 22
  • Thanks. I still do not know the answer. First, I discovered that vector multipication with matrix is done using Vector4.Transform. Now, I need to know what is the forth value ('W' component) of the input (vec). I tried putting 0 and 1 which yielded wrong answers. Also, I need to know how to use viewport as a Matrix4 - in the above code, it is an array of 4 elements and it is [0,0,width,height] when all is centered. Please guide me – max Jul 01 '14 at 07:44