Perspective 3d Rendering in Flash
I decided to post on here I cool project I made a couple of months ago to test my understanding of 3d rendering and, well, maybe test my maths a little too 🙂
This is a 3d rendering app and pipeline which I made myself using nothing but Flash and Actionscript 3.0. I used no other pre made code or libraries.
Click here to see it in action, rendering some simple shapes. (There’s more at the end of this post)
The FPS counter shows the number of frames per second. The max is 125, and the higher the better. As you can see, it’s very fast, and looks perfectly accurate. This is because I made this while learning OpenGL. The dudes on Elysian Shadows IRC were an awesome help on this project.
What I did was make a simple API for those using the 3d renderer, in which you pass 3d points to functions I make such as drawTriangle.
My drawTriangle then takes these 3d points, and passes them through a function which converts from 3d space into 2d screenspace. I do this using some heavy matrix multiplication; first, the 3d points are passed through the world transformation matrix; this basically controls the camera position and allows easy translation and rotation of objects for the developer. Next, it is passed through the projection matrix. This is a matrix which converts it into a nice proportioned point. With this, I then divide the x and y coordinates by the Z for perspective.
By now I have a 2d point for each point in 3d space, between -1 and 1. So I simply multiply and translate these to fit between the width and height of the flash, which gives me the final 2d point on the screen.
Then I use the Flash API to draw a shape between the 3 points of the triangle, and add the triangle to my display list.
At the end of each frame, the developer calls the update function, which sorts each triangle in the display list by the depth, using my sorting algorithm. This means that triangles further away will be drawn BEHIND triangles that are closer.
This results in the nice system which you can see 🙂
However, I didn’t stop there. In order to make this more useful, I then opened up my C environment to produce a .MD2 model file loader. This is a program I made which parses a Quake 2 format file, and produces a .as (actionscript) file with the commands to draw the Quake 2 model in my Flash application. This means that anyone can use a standard 3d modelling package such as Blender or 3DS Max, and render their models inside my 3d rendering API.
This is pretty awesome, and I show you the following example below, in which I used my C program to render the Berserker enemy from Quake 2. Beware that this will run alot slower, as there are a good few hundred triangles being rendered;
Click here to see it rendering a Quake 2 Model using my C++ app.
I might release the source code in the near future, but if I do, I want to make something myself with it first. So stay tuned.
Thanks for reading!
Categorised as: My Project Showcase