This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Exerro's profile picture

Drawing an image onto a quad.

Started by Exerro, 02 January 2015 - 02:48 AM
Exerro #1
Posted 02 January 2015 - 03:48 AM
I am working on a 3d map display thing for turtles, and I've come to a bit of a problem at the rendering.

Transforming 3d coordinates to 2d coordinates is relatively easy, and I have that working nicely. However, trying to then draw on textures is seemingly impossible, as I have to map the 2d coordinates of the texture onto 2d coordinates of the quad I'm trying to draw to. A quad (at least my definition of it) is a quadrilateral with no bounds, i.e. any of the corners can be any coordinates, completely unrelated to eachother.

I have it at a semi-working stage… it does draw in 3d, but is a) quite laggy and B)/> glitches out frequently, particularly at long distances.

Working:

A bit further to the right:


Does anyone have any ideas on how to do the coordinate transformation from a rectangular 2d image to a quad?
Bomb Bloke #2
Posted 02 January 2015 - 10:09 PM
Both those images look less like quads and more like irregular hexagons… Unless your textures are a really weird shape (or have black bits in them), I'm not sure you're drawing the basic shapes right.

I'd figure out where I was going to draw the four corners of the polygon, then calculate the center of that shape (easy enough - draw a line from each corner to its opposite, note where they intersect), and pre-store that along with the angle between the center and each corner.

Then use something like this technique to fill it in. As you plot each point, figure out the angle between it and the center, extend a vector to the shape's side along that angle, and figure out the percentage as to how far along that vector your point lies.

You can now transform that vector to apply to your texture, based on those original angles you recorded earlier. Whichever point it lands up on indicates the colour you should plot.
Exerro #3
Posted 02 January 2015 - 11:24 PM
I wasn't really clear that that was drawing 2 quads in a 3d environment (it was a cube being looked at from slightly to the right).

I decided that using quads/textures was too slow for actual use in CC if you have 50+ textures being rendered, and switched to just regular polygons, but then after porting the gfx3d module to love2d, realised the translation of 3d to 2d coordinates was all wrong. I hate 3d so much…

As for the maths behind all that, vectors still blow my mind sadly, so that's a bit beyond me. I think I'll just accept defeat and move on. Thanks for the reply to such a difficult topic though.