Nick's blog
https://blogs.warwick.ac.uk/nickforrington
en-GB(C) 2019https://blogs.law.harvard.edu/tech/rssWarwick Blogs, University of Warwick, https://blogs.warwick.ac.uk120Wine for Windows?! by
https://blogs.warwick.ac.uk/nickforrington/entry/wine_for_windows/
<p class="answer">Writing about web page <a href="http://sourceforge.net/project/showfiles.php?group_id=6241&package_id=112520" title="Related external link: http://sourceforge.net/project/showfiles.php?group_id=6241&package_id=112520">http://sourceforge.net/project/showfiles.php?group_id=6241&package_id=112520</a></p>
<p>Probably nothing new to a lot of people, but I just spotted that the <a href="http://www.winehq.org/site/download">wine website</a> has binary downloads for Windows.<br />
</p>
<p>Admittedly I can think of a couple of uses for this, but that doesn't really take away from the initial "wtf?" I got when I found out.</p>
<p>I wonder if it runs <a href="http://www.cygwin.com/">cygwin</a>... :-)<br />
</p>
<p>(For anyone wondering what I'm blathering about, wine is a compatibility layer used for running Windows programs on Unix - or in this case, running Windows programs on Windows)<br />
</p>CygwinUnixWindowsWineWtfWed, 02 Jul 2008 14:09:42 GMThttps://blogs.warwick.ac.uk/nickforrington/entry/wine_for_windows/#comments094d73991aca1531011ae41b777f0cc94Raytracing: Intersection with a sphere by
https://blogs.warwick.ac.uk/nickforrington/entry/raytracing_intersection_with_1/
<p class="answer">Follow-up to <a href="https://blogs.warwick.ac.uk/nickforrington/entry/raytracing_intersection_with/" title="Related blog entry">Raytracing: Intersection with a plane</a> from <a href="https://blogs.warwick.ac.uk/nickforrington">Nick's blog</a></p>
<p>Last time I went through intersection of a ray with a plane. Plane’s are all well and good, but you can’t have a ray tracer without spheres everywhere :-)</p>
<p>This article’s another maths-heavy one I’m afraid – more vectors and dot products, this time with a quadratic equation thrown in at the end. Enjoy.</p>
<p>Equation for our ray:</p>
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?P%7E%3D%7EO%7E%2B%7EDt" alt="P = O + Dt" border="0" />
<p>(P is the position, O is the origin of the ray, D is the direction of the ray, t is the parametric variable)</p>
<p>Equation for a sphere:</p>
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?%28x%7E-%7EC_x%29%5E2%7E%2B%7E%28y%7E-%7EC_y%29%5E2%7E%2B%7E%28z%7E-%7EC_z%29%5E2%7E%3D%7Er%5E2" alt="(x - C_x)^2 + (y - C_y)^2 + (z - C_z)^2 = r^2" border="0" />
<p>(x, y and z represent points around the surface of the sphere, C is the centre of the sphere, r is the radius of the sphere)</p>
<p>As with the plane intersection in the previous article, we solve the equations simultaneously, setting P = (x,y,z).</p>
<p>First we’ll rewrite the sphere equation in vector form. Summing the squares of the x, y and z components is the same as the dot product.</p>
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?%28P%7E-%7EC%29%7E%5Ccdot%7E%28P%7E-%7EC%29%7E%3D%7Er%5E2" alt="(P - C) \cdot (P - C) = r^2" border="0" />
<p>Now substitute in the equation for our ray:</p>
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?%28O%7E%2B%7EDt%7E-%7EC%29%7E%5Ccdot%7E%28O%7E%2B%7EDt%7E-%7EC%29%7E%3D%7Er%5E2" alt="(O + Dt - C) \cdot (O + Dt - C) = r^2" border="0" />
<p>We’re trying to solve for t, so let’s break up the above equation using the distributivity of the dot product:</p>
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?%28O%7E%2B%7EDt%7E-%7EC%29%7E%5Ccdot%7EO%7E%2B%7E%28O%7E%2B%7EDt%7E-%7EC%29%7E%5Ccdot%7EDt%7E%2B%7E%28O%7E%2B%7EDt%7E-%7EC%29%7E%5Ccdot%7E%28-C%29%3D%7Er%5E2" alt="(O + Dt - C) \cdot O + (O + Dt - C) \cdot Dt + (O + Dt - C) \cdot (-C)= r^2" border="0" />
<p>Now extract the Dt terms:</p>
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?%28O%7E-%7EC%29%7E%5Ccdot%7EO%7E%2B%7E%28O%7E-%7EC%29%7E%5Ccdot%7EDt%7E%2B%7E%28O%7E-%7EC%29%7E%5Ccdot%7E%28-C%29%7E%2B%7E%28Dt%29%7E%5Ccdot%7EO%7E%2B%7E%28Dt%29%7E%5Ccdot%7E%28Dt%29%7E%2B%7E%28Dt%29%7E%5Ccdot%28-C%29%7E%3D%7Er%5E2" alt="(O - C) \cdot O + (O - C) \cdot Dt + (O - C) \cdot (-C) + (Dt) \cdot O + (Dt) \cdot (Dt) + (Dt) \cdot(-C) = r^2" border="0" />
<p>Collect like terms:</p>
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?%28O%7E-%7EC%29%7E%5Ccdot%7EO%7E%2B%7E2%28O%7E-%7EC%29%7E%5Ccdot%7EDt%7E%2B%7E%28O%7E-%7EC%29%7E%5Ccdot%7E%28-C%29%7E%2B%7E%28Dt%29%7E%5Ccdot%7E%28Dt%29%7E%3D%7Er%5E2" alt="(O - C) \cdot O + 2(O - C) \cdot Dt + (O - C) \cdot (-C) + (Dt) \cdot (Dt) = r^2" border="0" />
<p>Now if we take the scalar t outside of the dot products, and move the r squared to the left hand side we get:</p>
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?%28O%7E-%7EC%29%7E%5Ccdot%7EO%7E%2B%7E%28O%7E-%7EC%29%7E%5Ccdot%7E%28-C%29%7E%2B%7Et%282%28O%7E-%7EC%29%7E%5Ccdot%7ED%29%7E%2B%7Et%5E2%28D%7E%5Ccdot%7ED%29%7E-%7Er%5E2%7E%3D%7E0" alt="(O - C) \cdot O + (O - C) \cdot (-C) + t(2(O - C) \cdot D) + t^2(D \cdot D) - r^2 = 0" border="0" />
<p>Which we can solve using the <a href="http://en.wikipedia.org/wiki/Quadratic_formula#Quadratic_formula">quadratic formula</a></p>
Where<br />
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?%0Aa%7E%3D%7ED%7E%5Ccdot%7ED%7E%3D%7E1%7E%5C%5C%0A%0Ab%7E%3D%7E2%28O%7E-%7EC%29%7E%5Ccdot%7ED%7E%5C%5C%0A%0Ac%7E%3D%7E%28O-C%29%7E%5Ccdot%7EO%7E%2B%7E%28O-C%29%7E%5Ccdot%7E%28-C%29%7E-%7Er%5E2%7E%3D%7E%28O-C%29%7E%5Ccdot%7E%28O-C%29%7E-%7Er%5E2%0A" alt=" a = D \cdot D = 1 \\ b = 2(O - C) \cdot D \\ c = (O-C) \cdot O + (O-C) \cdot (-C) - r^2 = (O-C) \cdot (O-C) - r^2 " border="0" />
<p>This is where I notice that I’ve reused “C” as a variable name – don’t get confused between “C” for centre” and the “c” in the quadratic formula.</p>
<p>Here I’ve also simplified A and C. A is always 1 in this case, as the direction of our ray “D” is normalised. I’ve also combined C into a single dot product.</p>
<p>Here’s the quadratic formula, which we can simplify by removing the “a” multiplicand.</p>
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?t%7E%3D%7E%5Cfrac%7B-b%7E%5Cpm%7E%5Csqrt%7Bb%5E2%7E-%7E4ac%7D%7D%7B2a%7D%7E%5C%5C%0A%0A%3D%7E%5Cfrac%7B-b%7E%5Cpm%7E%5Csqrt%7Bb%5E2%7E-%7E4c%7D%7D%7B2%7D" alt="t = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \\ = \frac{-b \pm \sqrt{b^2 - 4c}}{2}" border="0" />
<p>The rest is just a case of plugging in your values.</p>
<p>In order to solve this, you should check the value of the determinant (the bit inside the square root). If it’s < 0 then we have no real solutions, and therefore no intersections.</p>
<p>If the determinant is >= 0, then you need to check both solutions to the equation(-b + ... and -b – ...). We want the closest (lowest value) positive solution. A -ve value means the intersection is behind the ray. Also, if you have one +ve and one -ve solution, then your ray has been cast from inside the sphere.</p>
<p>One final thing worth noting is that there are special methods used to compute quadratic solutions which help to reduce the effect of rounding errors.</p>
<p>Ok, well hope that makes sense. Let me know if you have any questions or comments. Next post will probably cover lighting, so you can actually get planes and spheres in colour!</p>IntersectionRayRaytracingSphereMon, 11 Feb 2008 14:26:59 GMThttps://blogs.warwick.ac.uk/nickforrington/entry/raytracing_intersection_with_1/#comments094d739917e5f0fd0117ffd353850de39Raytracing: Intersection with a plane by
https://blogs.warwick.ac.uk/nickforrington/entry/raytracing_intersection_with/
<p class="answer">Follow-up to <a href="https://blogs.warwick.ac.uk/nickforrington/entry/raytracing_a_very/" title="Related blog entry">Raytracing: A (Very) Brief Introduction</a> from <a href="https://blogs.warwick.ac.uk/nickforrington">Nick's blog</a></p>
<p>Last time I mentioned that rays are cast from the eye point. This isn’t much good of course unless you can tell what the ray collides with. So here’s an idea of how to check for the intersection of a ray and a flat plane.</p>
<h3>Representing our objects</h3>
<p>Here’s the mathsy bit. If you’ve covered vectors before, then I think it should be reasonably easy to follow. Let me know if you have any questions or spot any mistakes.</p>
<h4>The Ray</h4>
<p>We can represent our ray with an origin, and a direction. Both of these are 3-dimensional vectors. It will come in handy later if our direction is normalised (has a length of 1).</p>
<p>We can then represent the path the ray takes as a <a href="http://en.wikipedia.org/wiki/Parametric_equation">parametric equation</a> in t.</p>
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?P%7E%3D%7EO%7E%2B%7EDt" alt="P%7E%3D%7EO%7E%2B%7EDt" border="0" />
<p>(P is the position, O is the origin, and D is the direction).</p>
<p>Because our direction vector is normalised, t is also the distance the ray has traveled from the origin.</p>
<h4>The Plane</h4>
<p>Representing a plane is a little less intuitive, but we can do it using a (3D) vector to represent the <a href="http://en.wikipedia.org/wiki/Surface_normal">normal</a> to the plane, as well as a value to represent the distance to the origin (in the direction of the normal). Note: this is the distance to the origin at (0,0,0) rather than the origin of the ray mentioned above.</p>
<p>e.g. if we look down the Z-axis from 0 in the positive direction, we could represent a wall 10 units away with a plane with a normal of (0,0,-1) and a distance of 10.</p>
<p>The general equation for a plane is:</p>
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?ax%7E%2B%7Eby%7E%2B%7Ecz%7E%2B%7Ed%7E%3D%7E0" alt="ax%7E%2B%7Eby%7E%2B%7Ecz%7E%2B%7Ed%7E%3D%7E0" border="0" />
<p>Where a, b and c are the x, y and z components of the normal, and d is the distance to the origin. To make the later equations more concise, we’ll rewrite the plane equation in vector form as:</p>
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?P%7E%5Ccdot%7EN%7E%3D%7E-d" alt="P%7E%5Ccdot%7EN%7E%3D%7E-d" border="0" />
<p>(P is the position, N is the normal, and d is the distance from the origin. Also the dot is the <a href="http://en.wikipedia.org/wiki/Dot_product">dot product</a>)</p>
<h4>The Maths</h4>
<p>As we’ve already established, t is the distance the ray has travelled from the origin. What we want to do now is find out how far the ray has travelled if/when it intersects our plane. We can do this by solving the ray and plane equations simultaneously.</p>
Starting with the plane equation:<br />
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?P%7E%5Ccdot%7EN%7E%3D%7E-d" alt="P%7E%5Ccdot%7EN%7E%3D%7E-d" border="0" />
<p>Substitute in the ray equation:</p>
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?%28O%2BDt%29%7E%5Ccdot%7EN%7E%3D%7E-d" alt="%28O%2BDt%29%7E%5Ccdot%7EN%7E%3D%7E-d" border="0" />
<p>The dot product is <a href="http://en.wikipedia.org/wiki/Distributive">distributive</a>, so we can do this:</p>
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?O%7E%5Ccdot%7EN%7E%2B%7E%28Dt%29%7E%5Ccdot%7EN%7E%3D%7E-d" alt="O%7E%5Ccdot%7EN%7E%2B%7E%28Dt%29%7E%5Ccdot%7EN%7E%3D%7E-d" border="0" />
<p>The dot product also lets us take multiplied scalar arguments outside the brackets like so:</p>
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?O%7E%5Ccdot%7EN%7E%2B%7Et%28D%7E%5Ccdot%7EN%29%7E%3D%7E-d" alt="O%7E%5Ccdot%7EN%7E%2B%7Et%28D%7E%5Ccdot%7EN%29%7E%3D%7E-d" border="0" />
<p>Now shuffle it around a bit…</p>
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?t%28D%7E%5Ccdot%7EN%29%7E%3D%7E-d%7E-%7EO%7E%5Ccdot%7EN" alt="t%28D%7E%5Ccdot%7EN%29%7E%3D%7E-d%7E-%7EO%7E%5Ccdot%7EN" border="0" />
<p>A bit more…</p>
<img class="latex" src="http://blogs.warwick.ac.uk/cgi-bin/mimetex.cgi?t%7E%3D%7E%5Cfrac%7B-d%7E-%7EO%7E%5Ccdot%7EN%7D%7BD%7E%5Ccdot%7EN%7D" alt="t%7E%3D%7E%5Cfrac%7B-d%7E-%7EO%7E%5Ccdot%7EN%7D%7BD%7E%5Ccdot%7EN%7D" border="0" />
<p>Super! Now we have the distance the ray has travelled when it hits the plane. We can plug this value back into the ray equation to get the intersection point in 3D coordinates too.</p>
<p>If the ray doesn’t hit the plane then both the numerator and denominator of the above equation are 0.</p>
<p>If you’d implemented what I’ve said so far, you would probably have a screen with half white (for the intersection hits) and half black (for the misses). So what we need now is some colour, or a slightly more interesting primitive to draw.</p>
<p>So, next time I’ll probably cover intersection with a sphere or how to colour your object with some simple lighting equations. Any preferences? I’ll probably do them both anyway.</p>RaytracingFri, 01 Feb 2008 19:49:58 GMThttps://blogs.warwick.ac.uk/nickforrington/entry/raytracing_intersection_with/#comments094d739917cb22c00117cdb7a95f01c06Raytracing: A (Very) Brief Introduction by
https://blogs.warwick.ac.uk/nickforrington/entry/raytracing_a_very/
<p>I’ve recently started writing a basic ray tracer, and thought I’d write a bit about how they work.</p>
<p>The basic idea of ray tracing is to generate a 2D image (or series of images) from a 3D scene by simulating how rays of light travel.</p>
<p>One important difference here is that instead of simulating rays cast from a light source and eventually hitting the eye/camera, a ray tracer will generally cast rays backwards from the eye, taking information from light sources at the end. This means we don’t have to simulate all the rays coming from each light source, most of which we don’t see (i.e. they don’t hit the eye).</p>
<p>Rays are cast from the eye though a plane, which represents our render target, with one ray for each pixel of the render target.</p>
<p>The ray tracer then determines the (nearest) intersection of the ray with the scene geometry. From here we can cast secondary rays to simulate reflection, refraction, etc. Also rays can be cast directly to the light sources in the scene. These rays (which may in turn spawn other rays) are all combined to give a colour for the ray. This colour is then applied to the pixel that the ray was cast through.</p>
<p>That just about covers my basic introduction. I intend to write a few more articles on this, the next of which will probably cover how to determine intersections between rays and geometry. Bring your maths hats.</p>RaytracingTue, 29 Jan 2008 13:24:22 GMThttps://blogs.warwick.ac.uk/nickforrington/entry/raytracing_a_very/#comments094d739917b6eeef0117c5b842b10dbf20