From: Shawn Hargreaves Newsgroups: comp.os.msdos.djgpp Subject: Allegro 3D examples needed (long) Date: Fri, 28 Feb 1997 23:54:06 +0000 Organization: None Distribution: world Message-ID: References: NNTP-Posting-Host: talula.demon.co.uk MIME-Version: 1.0 Lines: 81 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp David Jenkins writes: >I've tried following the 3D examples in the Allegro folder and I'm >stuck. Especially with the matrix stuff. Ok. I knew the brevity of my docs was going to cause problems :-) Unfortunately I don't have time to write a full 3d tutorial at the moment: any takers for that? But I'll have a go at a brief overview... You have a 3d object, built out of a set of polygons, right? And each polygon (let's say we are using triangles, for simplicity, but your polys could have any number of sides you like), has a set of vertices. Each vertex is defined by three coordinates, X, Y, and Z. To draw this 3d object on a 2d screen, the basic problem is how to transform each of these vertices from 3d space into a 2d x/y position. This operation is called the perspective projection, and fortunately for us that's all we need to do: given a 3d triangle defined by three vertices, if you transform those vertices into 2d coordinates and then draw a triangle betewen those three 2d points, you'll get the right result... With Allegro, call the set_projection_viewport() function to specifiy how big your screen is, and then use the persp_project() function to transform 3d points into 2d. And that's all you need to do: out of this you'll get a set of points which you can connect with regular lines or triangles, or texture map or gouraud shade them with the triangle3d() routine. Things get a bit more interesting if you want to rotate the object, or to view it from a different position (the basic perspective projection has the effect of a camera positioned at the origin, facing along the Z axis). You can move 3d points around just by adding values to them, and rotate them with some fairly basic trig, but that can be inefficient if you need to apply a long sequence of transformations to a great many points. This is where matrices come in. A matrix is just a 2d array of numbers, which can be used to encode various geometrical transformations like scaling, rotation, and translation. Allegro provides functions that construct matrices to do all of these operations: for example if you wanted to rotate the point x, y, z by an angle of 10 around the X axis, you could use the code: MATRIX mtx; get_x_rotate_matrix(&mtx, itofix(10)); apply_matrix(&mtx, x, y, z, &x, &y, &z); The beauty of this approach is that once you've constructed the matrix, you can apply it to hundreds of points, in order to transform an entire object rather than just one vertex. Also, you can build matrices that perform extremely complicated transformations by constructing several simpler matrices and then multiplying them together. For example, if you took this matrix (which rotates points around the X axis), and another which shifted points +10 units along the Z axis, you could multiply them together to get a third matrix M. When you applied M to a point, it would be both rotated around the X axis and shifted 10 units along Z, in a single operation. Very handy! I hope that makes sense. Whatever you still don't understand, just ask... >Could somebody email me 2 source codes one for a cube and one for a >pyramid?? Just drawing a plain object onto the screen and a function to >make it rotate. That's pretty close to what my bouncing balls example does (ex22), or the 3D bit in test.exe (look at the polygon3d_proc() function). I know this code isn't the clearest possible, so any suggestions on how to improve it would be very welcome... >Unless someone can point me to a 3D tutorial that would be of some help >to me with Allegro. The PCGPE contains a bit of basic 3d maths info, but the best I've seen is zed3d (this is on x2ftp). A decent book would be much better, though... /* * Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/ * Beauty is a French phonetic corruption of a short cloth neck ornament. */