Page 2 of 2

Re: How to represent 3d objects in 2d

Posted: Tue May 03, 2016 12:14 am
by pgimeno
adge wrote:Should I maybe use matrices? Are they faster?
That depends on how you're doing your rotations now. Otherwise no one can say "faster than what".

Anyway, a rotation matrix is usually the fastest method for rotating vectors. https://en.wikipedia.org/wiki/Quaternio ... omparisons

Re: How to represent 3d objects in 2d

Posted: Tue May 03, 2016 11:12 am
by adge
faster than common equations like this one:

Code: Select all

function rotateZ3D(alpha)
	rad = alpha * math.pi/180
	sin = math.sin(rad)
	cos = math.cos(rad)
	for a = 1, #nodes, 1 do
		node = nodes[a]
		x = node.x
		y = node.y
		node.x = (x * cos - y * sin)
		node.y = (y * cos + x * sin)
	end
end
Moreover I would like to recreate this effect:
http://www.lexaloffle.com/bbs/?tid=3181

How would this be done? Using a shader? I thought of maid64 and the faces drawing function would draw lines. But this sounds resources hungry.

Re: How to represent 3d objects in 2d

Posted: Tue May 03, 2016 11:42 am
by vrld
That bit of code

Code: Select all

      node.x = (x * cos - y * sin)
      node.y = (y * cos + x * sin)
is a matrix multiplication of the matrix \[\begin{pmatrix}
\cos\alpha & -\sin\alpha\\
\sin\alpha & \cos\alpha
\end{pmatrix}\] with the vector \[\begin{pmatrix}node.x \\ node.y\end{pmatrix}\].

Matrices (or rather matrix-vector-products) are just a way to write multiple equations using less characters (mathematicians are lazy people). There is nothing magical about them.

Re: How to represent 3d objects in 2d

Posted: Tue May 03, 2016 12:15 pm
by Tanner
Since this seems to be the direction you're headed in, here's a guide on how to write a software renderer from scratch. https://github.com/ssloy/tinyrenderer/wiki

Re: How to represent 3d objects in 2d

Posted: Tue May 03, 2016 12:24 pm
by pgimeno
vrld is right, that's actually 2D matrix rotation. But since that's a rotation over a single 3D axis and you need several of these to obtain any 3D rotation, it's likely that you can obtain faster results by doing the whole 3D rotation in one go, using a 3D rotation matrix rather than a composition of 2D rotations, because you do the sines and cosines just once and apply the result to all vectors.

Re: How to represent 3d objects in 2d

Posted: Tue May 03, 2016 12:30 pm
by Davidobot
adge wrote:Should I maybe use matrices? Are they faster?
Like pointed out by the rest, the functions present are basically identical to what you would see using matrices.
adge wrote:Add lighting. That would be cool.
I haven't worked on this project in well over 6 months, maybe I'll pick it up again if I get inspired.
adge wrote:How are faces done? Just draw a polygon or rectangle with the nodes positions?
That's right, a polygon is drawn between each set of nodes.

Re: How to represent 3d objects in 2d

Posted: Tue May 03, 2016 2:19 pm
by adge
Tanner wrote:Since this seems to be the direction you're headed in, here's a guide on how to write a software renderer from scratch. https://github.com/ssloy/tinyrenderer/wiki
Wow that sounds extremely cool! Thank you!