Author Topic: [3D Math]Correct perspective  (Read 4569 times)

0 Members and 1 Guest are viewing this topic.

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
[3D Math]Correct perspective
« on: December 29, 2012, 08:21:08 am »
I don't know if it fits in here, but I can't find a better place..

Here I have a cube:

With rotation matrices I can rotate it on a specified axis and I can translate it:

But this doesn't look right, I don't want to have orthogonal projection, I want it to look like:


I already tried projection matrices but they're only displaying:

That's way to extreme and still in the wrong direction..

How can I achieve this?

BTW: The bottleneck of this seems to be the RAM write speed, on the emulator it runs > 200 FPS
On a real calulator ~90 FPS.
Would double buffering be faster than memcpy every frame?

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [3D Math]Correct perspective
« Reply #1 on: January 04, 2013, 12:13:26 pm »
My way to do 3D, at least in nCraft, can be sumed up by this :


The camera doesn't move, the vertices can be translated or rotated.

Let's see how we calculate the coordinates of the projection of a vertex on the screen.
The ? represents the horizontal coordinate of the vertex on the screen (from the center). It is calculated with (10/x)*y, with Thales theorem.
The vertical coordinate of the vertex projection is calculated with (10/x)*z.

You calculate this with all the vertices defining your cube, and then, you draw rectangular faces with the coordinates you got. (to draw rectangles, use two triangles)


The result should look like what's on your mind...

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: [3D Math]Correct perspective
« Reply #2 on: January 04, 2013, 04:57:11 pm »
So you're linear interpolating to get screen x and y.
Are your vertex coordinates in [0;1] for x,y,z?

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [3D Math]Correct perspective
« Reply #3 on: January 06, 2013, 10:07:51 am »
No, not necessarily.

Actually, they can be as far as you want, if they are in the field of view cone. (The only restriction is that the x-coordinate can't be negative)
That's why if you want to keep a good speed, you should check if vertices are not too far before drawing them.

Here is the function that I use in nCraft :
-vertices* is an 1D array with 3D coordinates of used vertices. (example : {5,0,0,  5,1,0,  5,1,1  5,0,1})
-pos* is an 1D array when I will store the 2D coordinates of projections (example : {50,50  100,50   50,100  100,100})
-size is the number of vertices

Code: [Select]
void computeVertices(float *vertices,int *pos,int size)
{
  int i=0;
  float temp=0.0;
  for(i=0;i<size;i++)
  {
    if(vertices[i*3+1]>0)
    {
      temp=OFFSET*vertices[i*3]/vertices[i*3+1]*50.0+160.0;  //offset is an arbitrary value. I use 10.
      pos[i*2]=(int)temp; //screen coordinates have to be integers
      temp=-OFFSET*vertices[i*3+2]/vertices[i*3+1]*50.0+120.0;
      pos[i*2+1]=(int)temp;
    }
    else
    {
      //if vertices are behind the camera, I store arbitrary values to remember not to draw them
      pos[i*2]=-10000;
      pos[i*2+1]=-10000;
    }
  }
}

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: [3D Math]Correct perspective
« Reply #4 on: January 06, 2013, 10:23:24 am »
Ok.
Do you clip triangles or test every pixel if it's on screen?
BTW: I wouldn't use i*3 every time. I'd do uint32_t size3 = size*3 and for(i =0; i < size3; i+=3)
« Last Edit: January 06, 2013, 10:23:58 am by Vogtinator »

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [3D Math]Correct perspective
« Reply #5 on: January 08, 2013, 12:17:36 pm »
I made a simple clipped triangle routine. I can share it, if you're interested.
Yeah i*3 is a small but good optimization.