-1

Firstly, I'm new to WebGL, OpenGL programming in general. I think that these tools will allow me to solve a problem, but I don't really know where to start.

My problem: Given a single origin, a target (triangle or mesh) and a list of many (thousands) of direction vectors, is there a way to use my GPU to count the number of rays that hit the target? Ideally I would like to use the power of my GPU to solve this problem, as it seems better suited to it than my CPU.

I am fairly sure I could write some code to do this using my cpu, but it would take quiet a long time to go through my vectors in a for loop.

Thanks

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • Are the direction vectors of your rays randomly distributed or do they form a uniform grid of some sort? How many triangles do you have compared to the number of rays? – Quinchilion Sep 13 '17 at 17:54
  • The directions are as random as possible but restricted to one hemisphere. In general I will be dealing with a simple target with less than a dozen triangles. In some cases there would be a need to look at 3 or 4 separate targets. – Mr. Marphia Sep 13 '17 at 20:37

2 Answers2

0

You could try this, this is not a begginers thing though:

  1. Store your triangle/mesh in VBO, this is the easy part.
  2. Store your direction vectors in a texture, easy too.
  3. Use Instanced Rendering. This way each shader instance has a different identifier.
  4. Use that identifier to sample from the texture. You have a different direction vector for each shader instance.
  5. Now the tricky part: The shader increments a value (the already calculated number of hits) in some buffer. Use an image. Read at wiki Image Load Store about them, specially the "Atomic operations" part.
  6. Finally, use other shader to read the value from that image.
Ripi2
  • 7,031
  • 1
  • 17
  • 33
  • Definitely beyond my ability now, but at least it's a starting point. I can now research the concepts and hopefully figure it out. Previously didn't even know what terms to include in my searches. – Mr. Marphia Sep 13 '17 at 20:42
0

A way I see to achieve what you are attemping to do, is to think your problem in another way, in a kind of "reversed" raycast from screen.

You say you have only one origin, a target, and many directions. So it is roughly like a raytracing rendering, so you can think the problem in a point of view of what WebGL is made for: drawing 3D objects on screen, using a projection matrix.

  • Create a projection matrix corresponding to your origin as point of view, looking at your target, with the appropriate FOV

  • Render the object in flat color (white on black background for example) in a texture, usuing this projection matrix.

  • Use the same projection matrix to "project" your ray into the 2D texture coordinates.

  • Check at texture pixel in the resulting 2D coordinate of the ray direction : if pixel is white, this ray hit the object.

You even can use another shader to achieve the last two parts, by passing your ray directions in a vertex buffer, with the previously created texture in the fragment shader. the probleme will be to get the incremented value from shader.