1

I've written my own 3D Game Engine in Java by using OpenGL (LWJGL). I'd like to write a Raytracer that works on the CPU, not on the GPU because the memory for my GPU is limited and the amount of data that I use might be bigger than the memory available on my graphics card.

I did some ray-triangle intersection stuff and so on but I need to know how to interpolate values in a triangle like OpenGL does.

What I know is the x,y and z coordinate of each vertex.

enter image description here

What algorithm is usually used in OpenGL for doing Interpolation like I described.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Luecx
  • 160
  • 1
  • 12
  • 1
    Why would this be specific to OpenGL? – Androbin Aug 07 '17 at 11:39
  • I haven't seen other examples where this is used. – Luecx Aug 07 '17 at 11:40
  • Just take a weighted average of the vertices. – Androbin Aug 07 '17 at 11:40
  • This wouldn't work because the left blue point in my graphics would also be influenced by value_2 – Luecx Aug 07 '17 at 11:40
  • Really? 0.6 * value_1 + 0.0 * value_2 + 0.4 * value_3 = ... – Androbin Aug 07 '17 at 11:43
  • how do you get the 0.0? did you take the distance from the line(value_1, value_3) to value_2 ? – Luecx Aug 07 '17 at 11:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/151269/discussion-between-androbin-and-luecx). – Androbin Aug 07 '17 at 11:44
  • @Luecx see [Reflection and refraction impossible without recursive ray tracing?](https://stackoverflow.com/a/45140313/2521214) I know you want CPU side ... but look at the fragment where I do the ray triangle intersection. It computes barycentric `u,v` and then interpolate the color from vertex colors which is exactly what you want ... – Spektre Aug 07 '17 at 11:55
  • but everything can be done on cpu, or not? – Luecx Aug 07 '17 at 11:57
  • @Luecx yes I got also CPU side SW raytracer but that code I linked was available directly ... For the SW render I would need to dig into my archives and I am lazy for that. – Spektre Aug 07 '17 at 11:59

1 Answers1

2

Borrowed from gamedev.stackexchange.com

You simply need to project vector AP onto vector AB, then add the resulting vector to point A.

Here is one way to compute it:

A + dot(AP,AB) / dot(AB,AB) * AB This formula will work in 2D and in 3D. In fact it works in all dimensions.

In your case, A = value_1, AB = value_3 - value_1 and AP = value - value_1

Androbin
  • 991
  • 10
  • 27