# 3d stuff

So, a while ago i was trying to write some simple

3d functions (project, fill, translate/rotate)

in c++, and i got to the point where i had a triangle on the

screen, but after that i hit a strange wall. Here's what

would happen:

- whenever i moved a point or polygon in the Z or Y direction, it

would also spontaneously move in the X direction.

- Z-movement worked properly only for points in the plane

y = 0.

- Y-movement worked properly only for points in some positive, even

z plane (Z = 10 or 100 or something like that. I don't

remember exactly)

- X-movement always worked properly

I was performing all translations with statements like

point.y++ or point.x--. I have since been more succesfull

using asm, but i never figured out what caused that. Anyone

have any ideas? Oh yeah, and i was in 320x200x8 mode.

• : So, a while ago i was trying to write some simple

: 3d functions (project, fill, translate/rotate)

: in c++, and i got to the point where i had a triangle on the

: screen, but after that i hit a strange wall. Here's what

: would happen:

: - whenever i moved a point or polygon in the Z or Y direction, it

: would also spontaneously move in the X direction.

: - Z-movement worked properly only for points in the plane

: y = 0.

: - Y-movement worked properly only for points in some positive, even

: z plane (Z = 10 or 100 or something like that. I don't

: remember exactly)

: - X-movement always worked properly

: I was performing all translations with statements like

: point.y++ or point.x--. I have since been more succesfull

: using asm, but i never figured out what caused that. Anyone

: have any ideas? Oh yeah, and i was in 320x200x8 mode.

:

You shouldn't require going to assembly to do something like a matrix transformation on points. I've written code to do that a couple of times in C. I also don't see what the graphics mode has to do with the problem. Provided you have a functioning line and point drawing facility, all you're doing is moving mathematical points around.

Hmmm... I can't think off the top of my head something that would cause the phenomena you encountered. I'm guessing you already know all the next bit. The 4x4 matrices for rotation are as follows, for angle t:

X axis

[ 1 0 0 0 ]

[ 0 cos(t) sin(t) 0 ]

[ 0 -sin(t) cos(t) 0 ]

[ 0 0 0 1 ]

Y axis

[ cos(t) 0 -sin(t) 0 ]

[ 0 1 0 0 ]

[ sin(t) 0 cos(t) 0 ]

[ 0 0 0 1 ]

Z axis

[ cos(t) sin(t) 0 0 ]

[ sin(t) cos(t) 0 0 ]

[ 0 0 1 0 ]

[ 0 0 0 1 ]

For a given point represented as a 4-vector:

[x]

[y]

[z]

[1]

rotation is as simple as a vector-matrix multiply.

Pnew = (T)(Pold).

You can actually represent translations, shearing, and some forms of perspective in this format, but if you just want rotations and translations, you can get away with 3x3 matrices for rotation, and simple vector additions or subtractions on the point vector.

No reason this can't be done in C...

• Hi,

I saw that Michael Jones give you some matrix

for 3D rotation but maybe you have also some

problems for prsentation your 3D Object

on the screen.

One of your last way is to convert the

3D Object from 3d to 2d and fit them to the screen

'The simplest way is, that you forgot your

'z-coordinate. But than, you have no deep feeling.

To get the deep feeling effect, you may

divide the x and y coordinate with the z coordinate

x2d = x3d/z3d

y2d = y3d/z3d

Be carefull with the z3d coordinate, that you not

divide with 0.

The second one is that

the horzont is at the screen position (0,0) so

you have to do some transformation.

The last one is to think, that if z = 1 you have

an object with the real width

If z = 2 the width of your object is an half of

the origin.

So you have to do some scalation with the

z3d coordinate.

After converting you may add on an Z-Buffer like

this one:

if(Z-Buffer(x2d,y2d) > z3d)

{

SetPixel(x2d,y2d);

Z-Buffer(x2d,y2d) = z3d;

}