Live data from Hacker News

Rendering my (billiard) balls in a fragment shader

getlazarus.org

41–50 of 88 posts

Re: Rendering my (billiard) balls in a fragment shader

#41

I happened to notice that PI is #define'd to 3.1415926538, which isn't the correctly rounded off value of 3.14159265358... Seems like they missed a '5'.

Interesting point if it had been apparent it would have been an issue, at what point does a poor Pi become an issue?

What would a Pi of 3.0 do?

Re: Rendering my (billiard) balls in a fragment shader

#42

Good timing - I was procrastinating on HN instead of trying to fix my phucked-up phong; you've helped me do both.

With a good phong it's all about the dot products and specular highlight (if you using highlights). Make sure you clamp the dot products and give yourself a minimum ambient light value of around 0.15 to 0.2.

Re: Rendering my (billiard) balls in a fragment shader

#43
post #36

I happened to notice that PI is #define'd to 3.1415926538, which isn't the correctly rounded off value of 3.14159265358... Seems like they missed a '5'.

I'm surprised you'd have to define pi in a fragment shader at all. Surely such a commonly used constant would be defined in the language/headers already?

You should always define Pi in your code, in case the value ever changes.

Re: Rendering my (billiard) balls in a fragment shader

#44
post #20

What's the benefit of rendering using this technique over a regular sphere rendered with the usual pipeline?

Well first of you get infinite smoothness and better lighting (and reflections if you add them) because there is no underlying geometry. It's just math. It's also much faster, as you're only giving the GPU four vertices defining the square. This can even be sped up further by using point sprites which consume only one vertex.

"It's just math." But math can be expensive, and in the fragment shader you are calculating everything per-pixel, whereas if you provide your geometry to the vertex shader, you only have to calculate something once per vertex, which is usually much less.

Furthermore, depending on the GPU and the exact workload, it can be that vertex and fragment shading can run in parallel. In that case, it is better to spread the load over both, instead of letting the fragment shaders do everything.

The same goes for texturing. Instead of calculating most of the ball's color pattern in the fragment shader, and only using texture lookups for the number itself, if you just have a texture covering the whole ball, you basically offload a lot of computation to the texture units.

That said, for a pool game where the balls are one of the most important items on the screen, it's probably worth it to get the perfect smoothness and lighting, as you mentioned.

Re: Rendering my (billiard) balls in a fragment shader

#45
post #7

From the HN guidelines: please use the original title, unless it is misleading or linkbait; don't editorialize. The correct title is Rendering Pool Balls , not "Rendering my balls in a fragment shader".

Fixed.

A new HN meme is born.

Re: Rendering my (billiard) balls in a fragment shader

#47
post #30

I'm just getting my feet wet with shaders myself. Could this fragment shader be used in a "real" 3D game? At the moment, the sphere is orthographicly projected onto the screen so the sphere will always appear as a circle rather than an ellipse. How easy would it be to use this shader in place of a spherical mesh as you might find in a typical geometry based 3D render? I.e. could you use the same technique and render…

It can be incorporated into a 3D scene as long as you can make a square billboard . You may have problems with clipping that' you need to address manually, but it might be worth it as you can get better visuals, assuming you are trying to render spheres.

As far as I know there shouldn't be any distortion issues. A sphere will always look like a circle from any perspective. Well that is unless it's really really big or your are really really tiny and you give the size difference you are relatively close to its surface. At that perspective it just look like a flat plane.

Re: Rendering my (billiard) balls in a fragment shader

#48
post #36

I happened to notice that PI is #define'd to 3.1415926538, which isn't the correctly rounded off value of 3.14159265358... Seems like they missed a '5'.

I'm surprised you'd have to define pi in a fragment shader at all. Surely such a commonly used constant would be defined in the language/headers already?

I've always though about that fact myself. Can somone find the answer to why didn't the GLSL designers / video card driver people make PI built into their language?

Re: Rendering my (billiard) balls in a fragment shader

#49

I happened to notice that PI is #define'd to 3.1415926538, which isn't the correctly rounded off value of 3.14159265358... Seems like they missed a '5'.

That’s a little bit more accuracy than what is needed for this post, but good catch :-) https://www.jpl.nasa.gov/edu/news/2016/3/16/how-many-decimal...

Thats an interestring post, thank you! Do you think that's why for example JavaScripts Math.PI also comes out to 3.141592653589793?

Re: Rendering my (billiard) balls in a fragment shader

#50
post #30

I'm just getting my feet wet with shaders myself. Could this fragment shader be used in a "real" 3D game? At the moment, the sphere is orthographicly projected onto the screen so the sphere will always appear as a circle rather than an ellipse. How easy would it be to use this shader in place of a spherical mesh as you might find in a typical geometry based 3D render? I.e. could you use the same technique and render…

I'm also new to shaders, so I might be wrong: Camera distortion (/perspective) is done in the vertex shader, this post here presents a fragment shader. So if you take a quad, in the vertex shader then rotate it so it'll face the camera (i.e. all vertices have same z/depth value in clip space), you can then probably use this fragment shader to render the ball.

You have to rotate the quad to face the camera, since it has to cover the area where the ball may render (imagine the extreme case: if the quad is perpendicular to the camera, it looks like a line to the camera, you could only render the ball within that line, it wouldn't protrude to the left or right).

Post reply on HN