Vectors?
Pages: 1 2
Rick Murray (539) 13840 posts |
I have some code that defines a C++ vector as follows: std::vector<Uint32> texture[8]; for(int i = 0; i < 8; i++) texture[i].resize(texWidth * texHeight); sets it up with code like: texture[6][texWidth * y + x] = 65536 * ycolor; //red gradient and then uses it later on as: Uint32 color = texture[texNum][texHeight * texY + texX]; This appears to be some sort of array. Is this correct? Could I just replace the first two parts with |
Rick Murray (539) 13840 posts |
I’m playing around with a ray caster to see how it works. The current code (a flat version) renders fairly quickly to 800×600 in 16m colours (1,920,000 bytes); however the wheels completely fall off when trying 1280×720 (3,686,400 bytes). Oddly, even with triple buffering it flickers crazily (25-33fps redraw). The code for this was copied from Mamie, so I’m not really sure what’s going wrong there… I suspect 256 colours might be better (480,000 bytes and 921,600 respectively), but I wonder also how much impact the fairly extensive use of floating point makes, given that it’ll all be emulated. Sadly my grasp of maths isn’t really good enough to work out what’s actually happening – which is sort of the point of the exercise. It would be nice to convert this to use integers, and a lookup table for stuff like the sin and cos (used for turning). But, again, without plugging in a hell of a lot of DaDebug, I don’t actually know what sorts of values are in use….yet. Can’t help but think that I’m going to end up with a ridiculous pile of numbers and no idea what they actually relate to. It seems to be tracing a line from the camera position outwards until it hits something (defined in a two dimensional map), and how far it went determines how big to draw the object, based upon the centre of the screen as a mid point. Repeat for each vertical line across the screen.
|
Chris Mahoney (1684) 2165 posts |
I can’t see it at all, whether opening it in a new tab (Safari on Mac) or even trying to use curl to download it. Is your server OK? curl: (7) Failed to connect to heyrick.ddns.net port 80 after 7316 ms: Network is unreachable Interestingly, ping works fine! |
Rick Murray (539) 13840 posts |
In a word, no. I upgraded to StackBeta5 and it completely broke everything. Well, not everything, but the Resolver appeared to be dead which wasn’t at all useful. Plus, DHCP (when tried) managed to allocate an address like 192.254.0.0 (.0? is that even a real address?). Back with the RISC OS stack right now. I’m going to copy across the setup stuff from the Harddisc4 archive and try again. |
Rick Murray (539) 13840 posts |
Right. Reset all the networking. Reconfigured 192.168.1.10 (static), with 192.168.1.1 as the gateway and DNS server. Let’s try the ROD stack again. |
Chris Mahoney (1684) 2165 posts |
It was working with the ROOL stack but is now failing again. Still, good to know that it’s not something at my end. At first I wondered whether you might be blocking all traffic from outside the EU/UK or something :) |
Rick Murray (539) 13840 posts |
Right. StackBeta3 (the one I was using previously) works. StackBeta5 is buggered. Resolver may or may not be broken. It just won’t connect to anything, and given that commands such as I’ll go report this in the proper place now… Edit: It’s a known problem. Seems that something was using instructions that aren’t present on “older processors” (a Pi3B+? that’s a newish ARMv7!), and a fixed release will be rolled out soon. Phew – it’s not just my setup then. ;-) |
Rick Murray (539) 13840 posts |
Right – back to the original question – is a vector some sort of array? And why is it a “vector”? That’s something that has both a value and a direction…? |
Chris Mahoney (1684) 2165 posts |
I think it’s similar to a List in C#, where you can do something like this:
Hopefully that’s self-explanatory even if you don’t know the language. You probably guessed that it prints 1, 2 and 3 in sequence. You can also do stuff like I don’t know how the C++ syntax works :) |
Jeffrey Lee (213) 6048 posts |
Yes, vectors are just user-friendly wrappers around arrays. I find that https://www.cplusplus.com/reference/vector/vector/ is a useful site for C++ & C knowledge.
Correctly naming things is hard, I guess. |
Rick Murray (539) 13840 posts |
Ah, so it’s an array that dynamically allocates its own memory. I see. Nothing charges size or is added/removed after the first allocation, so it should be easy enough to convert to regular C. Thanks for the link, it’s a good explanation. |
Paolo Fabio Zaino (28) 1882 posts |
In case you may need a Vector library for ANSI C99, I wrote one that should work fine on RISC OS and I am making it matching one-to-one all the features of the C++ Vector library: https://github.com/pzaino/zvector It being used already by many people (according to GitHub) and no one has reported any issue, it’s being tested by a lot of code testing tools, plus comes with a bunch of tests of its own. |
GavinWraith (26) 1563 posts |
The Latin word vector can mean either a carrier or a person who is carried, e.g. a traveller. William Rowan Hamilton was the first, I believe, to use this word in English: his quaternions have a vector part and a scalar part. As for its uses in computer science, there I must remain silent. |
Jean-Michel BRUCK (3009) 359 posts |
@Rick C++ vectors can be used using DDE with the help of CathLibCPP.
In this case you are right. |
Rick Murray (539) 13840 posts |
Not really the plan… I just wanted to have an idea about how raycasting (like Wolfenstein and AmCog’s Island of Zombies) actually works. The beauty of the raycaster that I’m working with is that it’s literally thirty lines of code. It’s very, very, simple. So I’m hoping I can get my head around what it is actually doing. |
Theo Markettos (89) 919 posts |
A scalar is a one-dimensional quantity. A vector is an N-dimensional quantity (where N>1). A vector in physics ‘having both magnitude and direction’ typically refers to a 2 or 3 (or 4?) dimensional quantity, where the magnitude is sqrt(x^2+y^2+z^2) – and the direction via trig. This a particular case of the general idea. Vectors in computer science (eg VFP) typically mean ‘array of N elements’, the array itself defined as having 1 row with N columns (or maybe N rows x 1 cols). ie a 2×2 array is not a vector, but could be converted to a 1×4 array which is. |
Jean-Michel BRUCK (3009) 359 posts |
For clarification and advantage of C++
texture is an array of 8 unint32 vectors |
Steffen Huber (91) 1953 posts |
The Java guys had the same idea, but changed their opinion later when designing their collection API, where the interface is called “List” and implementations are called “ArrayList” or “LinkedList” which is obviously a lot better. Apart from the LISP guys who apparently expect something very special when someone promises a “list”. |
Kuemmel (439) 384 posts |
Hi Rick, yes that’s all arrays in memory, but don’t do 65536*ycolor, it’s a #16 shift ;-) Can you post your whole code then we can spot some optimizations for sure ? I don’t know what’s the origin of your raycaster, but generally you should look at the early stuff, like you mentioned Wolfenstein and so on. They used plenty neat tricks to limit computation power. I guess mostly integer and of course any kind of turning with cos/sin should be in a look up table, or maybe it’s just needed once per frame for all pixels, then it doesn’t matter… |
Jeffrey Lee (213) 6048 posts |
There’s a lot of scope for optimisation for simple environments like this. The naive way (fire individual rays for each column of the screen) is pretty inefficient, I think for best performance you need a BSP/portal-based renderer like Doom uses. Wolf 3D is a bit of a mixed bag – I believe that some of the ports used good algorithms, others didn’t (I have a memory that in the 20th anniversary commentary Carmack said something along the lines of “the Archimedes port could have ran better if it had come out a bit later and had been able to incorporate a better algorithm which we started using for some of the other ports”, but that video doesn’t mention that. Maybe I’m thinking of another video/article, or I’m misremembering completely?). Optimisation get even more complicated when you want to add texture mapping, something I spent far too long messing around with in my port of NBlood. But, if you’re still learning the basics, it’s probably best to ignore optimisation for now. I’m not sure if it’ll help you, but a long time ago I did write a simple Wolfenstein-style renderer myself (one ray per screen column, fixed-point math, grid-based levels): http://www.phlamethrower.co.uk/riscos/tpf.zip |
Jean-Michel BRUCK (3009) 359 posts |
Thank Jeffrey, |
Rick Murray (539) 13840 posts |
Jean-Michel: A quick tutorial about screen banking: http://www.riscos.com/support/developers/agrm/chap04.htm#l0053 Note that you’ll need to allocate sufficient screen memory. I ought to write my own tutorial… Kuemmel: I’m working my way through this. Jeffrey: Thanks for the references. A brief look at the code of tpf (on my phone), you’re poking the screen data directly? Mmm, that would probably be quicker than OS_Plot. ;) |
Kuemmel (439) 384 posts |
I just had a brief look, but it seems he’s relying on floats and doubles in the inner x and y loops. So that’s not really beneficial for a RiscOS setting. Though as you said the description is nice. But I really would get rid of OS_PLot in the first place. Just reading the current screen buffer address and writing pixels directly to memory should be easy to implement I’d think and then see how much that helps. Also like Jeffrey says, the usual approach is only one ray per column. Didn’t check the code if he’s doing that or not… |
Rick Murray (539) 13840 posts |
Jean-Michel, as it was a lovely day (and I hurt in places I didn’t know I had after yesterday’s marathon in the garden), I decided today to take it easier and instead sit outside and write a few words about screen banking… Hope it helps. |
Rick Murray (539) 13840 posts |
Jean-Michel, on the RISC OS FR mailing list pointed me at Acorn User in the warmer months of 1997. So I’ve downloaded all of that and thrown the “Virtuality” articles at the laser printer to study. Only took me a quarter century to catch up. ;) |
Pages: 1 2