News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu

3D Graphics

Started by kelp7, 16:04, 10 November 16

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

kelp7

Hi,


Does anyone know of any guides for beginners in explaining how to make a 3D object (such as a cube) and display it on the screen (in pixels) ? I presume there's a fair amount of maths involved. Have always wondered how this is achieved and how 3D spaced is modelled into a 2D screen.


kelp

fano

This is "just" 3 dimensions points and projection matrix (4*4) , there are not so complicated math (i was able to understand that  :laugh: ) , you can find a lot of tutorial about this around the internet.


Specific to CPC, there is a tutorial on Quasar CPC but it is written in French language: http://quasar.cpcscene.net/doku.php?id=dossier:3d
"NOP" is the perfect program : short , fast and (known) bug free

Follow Easter Egg products on Facebook !

arnoldemu

3D models are defined by "vertices". These are 3d points with an x,y and z component.

If you think back to maths at school, x and y were left->right and down->up on the paper. Now think of extending out of the paper going "up" and call that z.

(There are some "complications" with coordinate systems. Some people like to make Z up, some like Y up. There is also the complication with "left handed" and "right handed", "row" or "column" vectors, order of multiplication for matrices and is positive rotation clockwise or anti-clockwise - I'm going to skip that for the moment just so it is a little easier to understand.).

For a box you need 8 vertices for each of the corners. Some people like to put the centre of the box at 0,0,0 in "object" space. They do this because rotations are easier to understand that way.

In 3d you have "object" space - the space in which you define your objects vertices. If you make something in max, maya or blender you can export that and consider it to be in object space.

To define the cube there are choices:
- if wireframe, you need a list of start/end vertices for each line. e.g. vertex 1 to vertex 2, vertex 2 to vertex 3, vertex 3 to vertex 1

- for faces, you can define triangles, and define the vertices in clockwise or anti-clockwise order (this fact is important if you want to remove faces that are visually behind.

For a beginner go with wireframe, then advance onto triangles.
For wireframe you can use the native line draw of your computer to do the work once you have the coordinates in "camera space".

Now there is a space called "world space". Here you define the position and rotation of your objects.

To convert from "object space" to "world space" you use a "object-to-world space" matrix. This involves taking a copy of the object space vertex, applying a rotation to it, adding the position and then you have them in world space.

To view the objects you need a camera. The camera has a position in the world AND a rotation.

You take the points and transform them from "world space" to "camera space".
The result you get is the points relative to the camera. There are matrices that do the whole lot "object->world->camera".

Often in camera space, z is the distance away from the camera. You can use this fact to know if the camera is too far away to be seen (it would be drawn as a point or small and you can then reject or "clip" it.). Equally you can know if it's behind the camera and would not be visible and again clip it. You can also do other tests to check if it's off the screen to the left, right, top or bottom.

Once in camera space you apply the perspective transformation. This uses the camera x,y and z coordinates to compute a pixel position. This pixel position has x,y screen coordinates.The coordinates can be fully outside or partially outside the screen rectangle.

Then to draw you need to clip the coordinates against the screen dimensions.

And now you have your object on the screen from 3d to 2d.

That is the general flow.


So look up "vector", "position", "matrix", "object space", "screen space", "camera space" to get an idea of what is going on.



My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

SRS


kelp7

Thank you folks, very helpful as always!

roudoudou

On a machine like Amstrad i suggest you to cheat all the mathematical stuff which cost a lot of CPU. And BTW completly forget the frustum related code.

You keep coordinates, rotations/translation stuff (with a matrix or not) and a cheated projection which reduce X/Y values according to Z value
My pronouns are RASM and ACE

arnoldemu

I agree with @roudoudou and I did try to post a message last night but my broadband failed.

My suggestion is to learn the "slow" way first. Understand the full sequence of steps to go from 3d to 2d.
Then when you understand it you can remove, optimise or approximate the parts you don't need.

For example:
- use fixed point instead of floating point (faster but then you need to choose your number ranges carefully. Multiplying and dividing fixed point numbers can make them go outside their min/max range). In addition to keep accuracy high you will need more than one representation with different ranges.
floats are generally easier to use because this is not so much of a problem - but floating point "emulation" on an 8-bit is going to be slow. Remember pcs, macs and modern consoles have it all in h/w.
In addition modern cpus can do multiple adds at the same time, or you could offload it to the gpu to do a lot of the work.

- you could avoid a projection matrix multiply (converting 3d camera space to 2d screen space) and do it much more simply using sx=x/z and sy=y/z or even use a lookup table. If you choose your camera field of view (angle that the camera sees) then you can make a good approximation using those equations.

- if your just moving forwards through space then you could avoid the world space to camera space matrix completely and just translate the points.

The main problem with internet examples and things is that they don't always tell you if it's left or right handed or which order the multiplies go and that kind of thing, and if you mix and match it just causes confusion and extra work to convert the numbers back and forth.



My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

Powered by SMFPacks Menu Editor Mod