Zalo DS Blog

Wednesday, June 18, 2008

Painting in 2D using 3D (III)

It's been a while. Again. LOL XDDDD

Ok, let't finish with this. When I wrote the previous blog I was still doing something bad. And I am going to expose it here so that nobody else has the same problem.

The problem is that in libnds glOrtho is defined like this

void glOrtho(float left, float right, float bottom, float top, float zNear, float zFar) {
glOrthof32(floattof32(left), floattof32(right), floattof32(bottom), floattof32(top), floattof32(zNear), floattof32(zFar));
}


if we take a look at floattof32

#define floattof32(n) ((int32)((n) * (1 << 12)))

Then we see basically that the params that glOrthof32 receive are 24:12 fixed points. Now I am going to show you what I was doing wrong. I called glOrthof32 like this

glOrthof32(0 << 12, 256 << 12, 0 << 12, 192 << 12, -1 << 12, 1 << 12)


Ok, the problem here is that as I say in a previous post, vertex coordinates in DS must be between [-8, 7] range. So, it was going to be impossible to draw on my current perspective (wich extends from 0 to 256 and 0 to 192). I was only going to be able to draw in a very low part of the screen. So... what did I do? I used a glScale for my vertex... and... well, I think it is better if I tell you how to do it right, because it is possible to do it like that, but it is more complicated.

So. I was curious about how other people did this. It couldn't be that complicated. And I see a portion of code when someone did something like this...

glOrthof32(0, 256, 0, 192, -1, 1)


Yes, as simple as that. Now you must be thinking that I am stupid or something similar XDDD. The problem is that I has a Fixed point class that manages all my conversion. So I don't really called glOrtho with the params displaced 12. My class did. Id you take a look at the previous call and you think about its meaning, really what it os doing in floats world is this:

glOrthof32(0 / 4096, 256 / 4096, 0 / 4096, 192 / 4096, -1 / 4096, 1 / 4096)

which is

glOrthof32(0, 0.0625, 0, 0.046875, -0.000244140625, 0.000244140625)


This projection is perfect for 2D painting!!
Ok, so the conclussion basically is... I was trying to scale each vertex when I could have just scaled the perspective!

Thank you very much.

By the way. I am going to write this week. I have something to upload ^_^ I have almost finished a new simple game.

1 Comments:

Post a Comment

<< Home