Here's a 3d renderer I've been making.
I made most everything pretty modular, so it's easy to add in custom meshes and draw them at any position. I set it up to work similarly to OpenGL3
Version 1.2 Update:
- Added Matrix scaling
- Optimized vertex projecting
- Added triangle rasterization and sorting. Rasterization is QUITE slow... If anyone could help speed it up, that'd be awesome! :)
Version 1.2b Update:
- Improved Rasterization functionality
Version 1.3 Update:
- Adopted NuSan's triangle rasterization.
- Wrote a billboard system (requires user-defined texture regions)
a few tips:
For flat/uniformely coloured triangles use rectfill or rect. Memset could possibly be faster but the packink/unpacking of pixels in 4 bits may ruin the perf.
Moreover for those triangles you dont need barycentric coordinates if you have nothing to interpolate: Split your triangles in 2 and just draw line by line between edges.
Do backface culling! (maybe you do already).
If you need interpolation (for non flat shading or texturing) you need pset and barycentric coords but need to optimize a lot to get decent perf.
As your cubes are convex shapes you can sort only the objects and not all the faces (if you have backface culling)
A zbuffer can be set up also but it can be too expensive for real time. I have implemented a 8bit one in my terrain renderer demo but it is not really real time.
Dont expect to be able to make a fully feature 3D engine in pico with decent perf and functionalities. You ll need trade of.
You can take a look at my demo if you're interested: https://www.lexaloffle.com/bbs/?tid=2688
Hope this helps
Cool renderer, your code is very clean and organized !
I am working on a renderer too, so here are my tips :
Avoid using too much struct for constants (like Math.Pi or even Camera.width) because accessing a struct in LUA is like searching in a tree, its slow. Try with global when you can.
Drawing a general triangle can be separated in 2 simpler triangle, each with one horizontal side. That way, you can compute the first and last pixel of each line and only draw that instead of iterating over the all bounding square.
You can check the game of J-Fry for more details.
As J-Fry said, drawing rectfill is super fast (faster than rect ^^), if you dont need per pixel changes (like texture, zbuffer ...)
You can probably speed up your sort by doing it only partialy each frame, like one or two steps of a buble sort. As your triangle dont change order very fast, some frames of delay is ok.
Keep up the good work
Are you using 4x4 matrices or something simpler to cut corners and get some extra speed? x/w and y/w are your screen coordinates, z/w is really just needed for sorting since you aren't z-buffering. The 1/w is the screenspace scale of a point. You should be able to use that directly to calculate your billboard scale.
[Please log in to post a comment]