Any good examples of 3D vector programming?

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Art
Manic Miner
Posts: 206
Joined: Fri Jul 17, 2020 7:21 pm

Re: Any good examples of 3D vector programming?

Post by Art »

Joefish wrote: Thu Jul 15, 2021 5:48 pm As for Z-buffers and the like, if you meet the following conditions:
  • All your objects rest on the ground
  • All your objects are either vertical columns or pyramidal / tapering upward (no overhangs, tunnels or arches)
  • The viewpoint can never go below ground level
  • The view never needs to roll
Then you only need a 1-D height buffer. You draw objects from nearest to furthest, and for each X-position you record the highest pixel drawn. Then for further objects, you only draw the parts of them that appear higher up the screen than what you've already drawn. Sky Ranger does this effect, though it gets it wrong sometimes.
I rewrote my whole 3D system several times, changed objects definitions, I also used some ideas from the Elite game code and I added some additional improvements to reduce amount of calculations, so the program is shorter and faster. I also made the z=x, z=-x, z=y, z=-y clipping system of 3D lines, but I couldn't make it to compute precisely, so it didn't work well enough after projecting the coordinates to the screen. Moreover, my horizon isn't in the centre of the drawing area, so I had to multiply y and -y by constants like 0.975 etc., so finally I returned to 2D clipping.

It's all OK, but I removed the wall filling (erasing) part, because I couldn't make it work correctly when objects were partially behing the camera (z<1), it didn't fill the whole polygon. So distant objects aren't hidden anymore.

So I would like to try the above idea of a 1D height buffer, because Sky Ranger draws the city very fast, so it looks good to me. I understand the basic idea, but I don't know exactly how it works. When I draw edges of an object, how can I know which pixel on an x-position is the highest? Does it mean that when I draw a line, I record the y-position for each pixel of the line during drawing? And then I do the same thing for every other line of the object and during drawing I compare every pixel? And then I draw another object and during drawing lines, for every pixel I decide if I draw the pixel or not? Wouldn't it significantly slow line drawing?
User avatar
Joefish
Rick Dangerous
Posts: 2064
Joined: Tue Nov 14, 2017 10:26 am

Re: Any good examples of 3D vector programming?

Post by Joefish »

Yes, you'd be checking the Y-position of every pixel against the buffer, and updating the buffer when you draw a pixel. Although vertical lines could be done quicker with just one clip operation.

I don't know if there's any quicker way. There might be optimisations possible if your blocks are all on a grid system, or all the same base size for example. Then if one block is obscured by another, you know you can stop drawing when it starts to clip because there's no way that same block could emerge again on the far side of the one in front.
Art
Manic Miner
Posts: 206
Joined: Fri Jul 17, 2020 7:21 pm

Re: Any good examples of 3D vector programming?

Post by Art »

Yes, Sky Ranger uses a lot of vertical lines, so it probably speeds it up.

OK, I will experiment with the idea.
Art
Manic Miner
Posts: 206
Joined: Fri Jul 17, 2020 7:21 pm

Re: Any good examples of 3D vector programming?

Post by Art »

Finally it was quite easy. I used two height buffers, the first one to compare pixel heights between two objects, the second one to compare pixel heights between lines in an object. After an object is finished, the second buffer is copied to the first one. It works well, the only problem is when object's top lines are outside of the screen, then an object below this is drawn. You can see it on the right side. So I will try to program something else for these cases.

Image

I also think that I need to write to a height buffer only when I draw roofs, so I can speed it up.

Probably I will drop colours, because now I draw objects from the nearest to the most distant, so it doesn't work as well as before. And I can't use black PAPER inside walls anymore, because I don't fill them now. But it's OK, the game will be at night.
Art
Manic Miner
Posts: 206
Joined: Fri Jul 17, 2020 7:21 pm

Re: Any good examples of 3D vector programming?

Post by Art »

Finally it works. Still mostly in Basic, but I don't expect very high frame rate in the assembler version either, so I am thinking rather about an adventure game than an action game. Maybe in some village. Now, when the hiding algorithm works, I am thinking about adding something like mountains and stars as a background.

Image
Art
Manic Miner
Posts: 206
Joined: Fri Jul 17, 2020 7:21 pm

Re: Any good examples of 3D vector programming?

Post by Art »

A new version with stars and mountains. Black colour at walls is back, I used the height buffer for this, too, so finally the height buffer idea was very useful. Objects' colours are not very good, I am still thinking about a solution since I don't draw the objects from the most distant to the nearest, so the colour priority is unfortunately reversed. Still mostly in compiled Basic, so still no double buffer, but I already make a plan for an adventure game.

http://phoenix.inf.upol.cz/~rachunl/test/3d9.mp4
catmeows
Manic Miner
Posts: 718
Joined: Tue May 28, 2019 12:02 pm
Location: Prague

Re: Any good examples of 3D vector programming?

Post by catmeows »

Art wrote: Mon Aug 09, 2021 9:10 pm Objects' colours are not very good, I am still thinking about a solution since I don't draw the objects from the most distant to the nearest, so the colour priority is unfortunately reversed.
When you start implementing your draw line routine you could z-buffer colors. It would be little tricky but in general, you would need to check/rewrite attribute byte only when you cross char boundary. That may sound complicated but it shouldn't be, since your pixel is a single bit, you will do check for crossing byte boundary in horizontal axis anyway. Checking cross in vertical axis is more complicated, depends on your buffer layout but it is also doable.
Proud owner of Didaktik M
Art
Manic Miner
Posts: 206
Joined: Fri Jul 17, 2020 7:21 pm

Re: Any good examples of 3D vector programming?

Post by Art »

It is an interesting idea. I don't know yet how it would work, but I'll think about it.
User avatar
Joefish
Rick Dangerous
Posts: 2064
Joined: Tue Nov 14, 2017 10:26 am

Re: Any good examples of 3D vector programming?

Post by Joefish »

Looks really good. To reverse the colour order, start with all 0 attributes, then only assign a colour if the attribute is still 0.
Art
Manic Miner
Posts: 206
Joined: Fri Jul 17, 2020 7:21 pm

Re: Any good examples of 3D vector programming?

Post by Art »

Yes, it could work, too. I would need to make some other changes to try it, but I'll think about it, too.
catmeows
Manic Miner
Posts: 718
Joined: Tue May 28, 2019 12:02 pm
Location: Prague

Re: Any good examples of 3D vector programming?

Post by catmeows »

Funny thing - I just realized you share your videos from cz domain. Are you Czech or Slovak ?
Proud owner of Didaktik M
Art
Manic Miner
Posts: 206
Joined: Fri Jul 17, 2020 7:21 pm

Re: Any good examples of 3D vector programming?

Post by Art »

Czech. And I see that probably you too.
User avatar
Einar Saukas
Bugaboo
Posts: 3167
Joined: Wed Nov 15, 2017 2:48 pm

Re: Any good examples of 3D vector programming?

Post by Einar Saukas »

I really like this version (except for the bug on the right):

Image

It reminds me of Mercenary.

Although seeing the horizon through buildings is not realistic, it still looks better than this:

Image

and certainly much better than this:

Image
Art
Manic Miner
Posts: 206
Joined: Fri Jul 17, 2020 7:21 pm

Re: Any good examples of 3D vector programming?

Post by Art »

Yes, the horizon was the main reason why I tried to fill the buildings. But I will try some possibilities and see which one is the best.
Art
Manic Miner
Posts: 206
Joined: Fri Jul 17, 2020 7:21 pm

Re: Any good examples of 3D vector programming?

Post by Art »

What about this version? Black squares are more hidden inside walls.

Image
User avatar
Einar Saukas
Bugaboo
Posts: 3167
Joined: Wed Nov 15, 2017 2:48 pm

Re: Any good examples of 3D vector programming?

Post by Einar Saukas »

Sorry but I don't think it looks good either.

IMHO seeing the horizon through a transparent building looks somewhat stylish (nobody ever complained about this in Mercenary reviews), but painting a wireframe building with square attributes looks buggy to me.

But that's just my opinion. Perhaps others have a different point of view?
Art
Manic Miner
Posts: 206
Joined: Fri Jul 17, 2020 7:21 pm

Re: Any good examples of 3D vector programming?

Post by Art »

I see your point and it certainly looks strange. I just don't know yet how to do it better. Maybe I will figure out another solution.

About Mercenary, I agree, but buildings on the Mercenary's planet's surface are completely transparent. I my system, there are hidden lines on objects and hidden parts of objects behind other objects, so I think the scene should look completely non-transparent. But maybe I didn't choose a right approach. I could do it for example like in Driller, but I wanted to use colours and it wouldn't work in that system.

Concerning colours, I've just finished the colour priority of nearer objects, so the colours look better now (for example the near tree from the previous picture is green).
Art
Manic Miner
Posts: 206
Joined: Fri Jul 17, 2020 7:21 pm

Re: Any good examples of 3D vector programming?

Post by Art »

A new version with correct colours, better mountains and dotted roofs (a simulated lighting from the top). I agree that it still looks a big buggy and that the non-filled version looks cleaner. Maybe more dots on the roofs to hide colour squares.

Image
User avatar
Joefish
Rick Dangerous
Posts: 2064
Joined: Tue Nov 14, 2017 10:26 am

Re: Any good examples of 3D vector programming?

Post by Joefish »

I think that looks quite good; I worry that it might look pretty bad if it was moving; the lines moving smoothly but the colours jumping.
Have you tried rounding off the X-positions of the lines to whole character positions. It would distort the view but I wonder by how much..?
dfzx
Manic Miner
Posts: 685
Joined: Mon Nov 13, 2017 6:55 pm
Location: New Forest, UK
Contact:

Re: Any good examples of 3D vector programming?

Post by dfzx »

How long does it take to render that scene?
Derek Fountain, author of the ZX Spectrum C Programmer's Getting Started Guide and various open source games, hardware and other projects, including an IF1 and ZX Microdrive emulator.
Art
Manic Miner
Posts: 206
Joined: Fri Jul 17, 2020 7:21 pm

Re: Any good examples of 3D vector programming?

Post by Art »

Joefish wrote: Thu Aug 12, 2021 2:03 pm I think that looks quite good; I worry that it might look pretty bad if it was moving; the lines moving smoothly but the colours jumping.
Have you tried rounding off the X-positions of the lines to whole character positions. It would distort the view but I wonder by how much..?
Yes, colours can be off, 4 pixels at the most from vertical contour lines. Non-vertical contour lines are only on roofs, which can be partially masked using dots. It's an interesting idea about rounding off the x-positions. I can try it.
Art
Manic Miner
Posts: 206
Joined: Fri Jul 17, 2020 7:21 pm

Re: Any good examples of 3D vector programming?

Post by Art »

dfzx wrote: Thu Aug 12, 2021 3:09 pm How long does it take to render that scene?
4 seconds in compiled Basic. Probably mainly because I draw every line dot by dot in Basic.
Art
Manic Miner
Posts: 206
Joined: Fri Jul 17, 2020 7:21 pm

Re: Any good examples of 3D vector programming?

Post by Art »

Art wrote: Thu Aug 12, 2021 3:13 pm
Joefish wrote: Thu Aug 12, 2021 2:03 pm I think that looks quite good; I worry that it might look pretty bad if it was moving; the lines moving smoothly but the colours jumping.
Have you tried rounding off the X-positions of the lines to whole character positions. It would distort the view but I wonder by how much..?
Yes, colours can be off, 4 pixels at the most from vertical contour lines. Non-vertical contour lines are only on roofs, which can be partially masked using dots. It's an interesting idea about rounding off the x-positions. I can try it.
Rounding off to the nearest character position looked strange, so now I am moving each x-position one pixel towards the left or right edge of its character position. Background colours can be off 2 pixels at the most from vertical lines now.
Art
Manic Miner
Posts: 206
Joined: Fri Jul 17, 2020 7:21 pm

Re: Any good examples of 3D vector programming?

Post by Art »

Added more roof dots, slightly modified the filling routine, shifting only vertices which belong to contour vertical lines. On the picture, you can see the worst case which can happen: On the left side of the near right building, the black colour is shifted 2 pixels to the right. On the left side of the far right building, the vertical line is shifted to the left, so the building's base isn't exactly parallel with the near right building. The far pyramid isn't filled very well. But in most cases, it looks better and pyramids probably will be only on the top of some buildings in the final game.

Image
User avatar
Sol_HSA
Microbot
Posts: 162
Joined: Thu Feb 04, 2021 11:03 am
Location: .fi
Contact:

Re: Any good examples of 3D vector programming?

Post by Sol_HSA »

That's pretty! Needs to be quite bit faster to be playable, though.. =)
Post Reply