Polygon Rasterization

BlitzMax Forums/BlitzMax Programming/Polygon Rasterization

Endive(Posted 2016) [#1]
I'm on a quest to really understand graphics programming. I can do some opengl but I want to go into raytracing and shaders and to do that there is really a need to understand the technology from the very early days on forward.

So I'm writing a polygon rasterizing 3D engine.

This will use a Z-buffer for hidden surface elimination.

My plan, per polygon, is to go as follows:
* trace polygon outline using bresenham algorithm. Build list of points in the outline. Sort the points by the Y coordinate. There should either be one or two points per Y coordinate.
* For each two points, draw a line between them, but only plot the points in the line when the Z coordinate of a point is less than the Z coordinate currently in the Z buffer. If it is, plot the point and update the new value for that point to the Z buffer.

After the first iteration I will save RGB information for pixels at the polygon edges and interpolate between those per-raster to allow lighting.

Any other thoughts about this? Has anyone here written a polygon rasterizer? Am I going about it wrong?


Floyd(Posted 2016) [#2]
No thoughts, but somebody did this back in the early days of Blitz Basic.

I remember seeing a rendered image of the Utah teapot, in bright red.

I seem to recall an animated dolphin too, although I may be mixing that up with the dolphin demo included with Blitz3D.


Yasha(Posted 2016) [#3]
Learn from the best: Quake source analysis, Quake 2 source analysis

I think this might be what Floyd is referring to.


Endive(Posted 2016) [#4]
Good calls.

When I try to run samples out of that VGL (in BlitzPlus) I get an error "Function Vgliapirtlmovememory not found."

Is this part of this guy's other library? Who wrote this?


Floyd(Posted 2016) [#5]
You need to put VirtualGL.decls in your userlibs folder.

I don't remember the old teapot demo using any sort of userlib. The name of the author escapes me. He left ages ago to concentrate on real life. I think he had a Scandanavian sounding name.


Kryzon(Posted 2016) [#6]
These might be interesting for you:

- https://fgiesen.wordpress.com/2011/07/09/a-trip-through-the-graphics-pipeline-2011-index/
- http://simonschreibt.de/gat/renderhell-book2/


Endive(Posted 2016) [#7]
Thank you.

I'm going to try a run at this. I should be able to get flat shading on polys no problem and that's good enough to get a really nice lighting effect, I actually like flat better than gouraud.


dw817(Posted 2016) [#8]
Endive, feel free to post some of the work you've done. Not only will you likely WOW us with your skill, but some of us may even be able to help you with complex technical issues.


Endive(Posted 2016) [#9]
Floyd, Yasha, are you able to get the VGL examples to compile? I get a compiler error, "BlitzCC has stopped working" and the compile crashes (on the Doom sample), and on Sample.bb I get "invalid image handle."

I think I'm just going to write my own polygon rasterizer. Is it necessary or faster to subdivide polygons into triangles and then rasterize those? If so I know how to do that.


Yasha(Posted 2016) [#10]
Seems that despite being a software renderer, VirtualGL can only be built with Blitz3D, not BlitzPlus. Haven't examined the details.

Is it necessary or faster to subdivide polygons into triangles and then rasterize those?


Most engines don't assume the existence of non-triangle polygons at all (they won't even have data structures that admit a variable number of vertices per-poly; flat arrays give better performance). Breaking the polygons up into triangles usually happens at the model-editing stage; the game itself never sees these. Only editor applications will bother to support this feature, and only on a logical level (they'll have a triangle-only representation of your mesh hidden in the background that they use for the drawing).

So, yes, but you shouldn't have to do it yourself.


Endive(Posted 2016) [#11]
Seems that despite being a software renderer, VirtualGL can only be built with Blitz3D, not BlitzPlus. Haven't examined the details.

That explains it.

What do you think of my fill strategy? Building up an x,y array of pixels on the edges of the tri, then for each y value, interpolate between the two x values to plot, and only plotting and writing to Z buffer if the Z value is closer to the camera than the value currently in the Z buffer?

There's also the issue with non-coplanar vertices that crop up in polygons of orders above triangle :)_


Yasha(Posted 2016) [#12]
That fill strategy sounds familiar, if I'm imagining it correctly. You shouldn't need to store any explicit pixel coordinates in an actual array except for the vertices though.

IIRC this strategy requires splitting most triangles into two parts, above and below a horizontal line from the middle vertex (arranged vertically)? So each tri is essentially drawn as two. (Some of them might end up being free.)


Endive(Posted 2016) [#13]
How would you raytrace/march from a z field?

How would you produce a normal map from such a texture, assuming black is low and white is raised?


Yasha(Posted 2016) [#14]
Nvidia have an introduction to heightmap raymarching here: http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter08.html

It's much less efficient than using isosurfaces, because you generally have to use a fixed step size. (There are some ways to introduce variable step sizes, but they require building extra objects in memory and it's not clear that they're actually any faster.) The same basic principle applies to voxel objects.

You don't need raymarching to produce a normal map, though. Normal maps are used for polygon rendering, and usually aren't needed for raytracing (where you have pixel-perfect normal information anyway!). A normal map is just the map of the derivative of the height. See also Batch Height to Normal Converter


Endive(Posted 2016) [#15]
Wow, that converter is just what I was looking for. You are really on the ball.

Is there any sort of shader-based bumpmapping solution in Blitzmax similar to what Mojo2 does?


Endive(Posted 2016) [#16]
Here's a good page on rasterizing tris:

Triangle Rasterization


Endive(Posted 2016) [#17]
Ok, here's my triangle rasterizer:


Now I have to modify it so that it can take Z values at the vertices and interpolate between those for each scanline.


dw817(Posted 2016) [#18]
That's some beautiful code, Endive ! I'm very impressed !

I'm curious though, what is the purpose of "SortVertices" ?

Running the program without it I still get the same results, nicely drawn triangles.