Live data from Hacker News

Rendering my (billiard) balls in a fragment shader

getlazarus.org

21–30 of 88 posts

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

#22
post #2

Here is a summary I wrote on my attempt to render balls using only a fragment shader. I am pretty happy with the result, and I've included a breakdown of the creation process.

Cool, I recently started with GLSL, and it’s interesting there are many roads that lead to a similar solution (how I draw circles or make a center is tad different)

Where or how did you learn GLSL?

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

#23
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.

I've been advised not to use point sprites. Apparently, most GPU inplementations merely convert it into a quad in the backend, and gl_PointSize seems to have a soft maximum that depends on implementation.

I've found a mechanism that uses the "interleaved attribs" feature of transform feedbacks to spit out 4 verts at a time (as six to draw two GL_TRIANGLES) into a buffer that a quad-drawing shader program consumes.

Using GL_POINTS/gl_PointCoord is probably better though just for teaching children fundamentals, I just wish that it actually worked properly in practice.

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

#24
post #2

Here is a summary I wrote on my attempt to render balls using only a fragment shader. I am pretty happy with the result, and I've included a breakdown of the creation process.

Great write up, that was a fun read.

In understanding how the rotation was implemented, it took me longer than it probably should have to realize that the normal map (which I think of in terms of lighting) is actually a radial position on the sphere.

I had to go back and forth with the final and original code a few times, and finally noted that "n" gets renamed to "point" when it becomes an argument to colorLookup.

This dual usage of the normal for lighting brightness calculation and as a radial position on the sphere surface for looking up the color makes sense in retrospect but is also a somewhat subtle aspect that you might want to address more explicitly in your text instead of just silently renaming the variable in the code.

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

#25

Earlier quoted context omitted.

What was it originally? Hard to get more amusing than the actual article title...

The actual article was titled "Rendering Pool Balls". He fixed it by updating the article title rather than the HN submission(which I don't think you even can).

You can. Within a certain amount of time of having submitted.

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

#27
post #22
post #2

Here is a summary I wrote on my attempt to render balls using only a fragment shader. I am pretty happy with the result, and I've included a breakdown of the creation process.

Cool, I recently started with GLSL, and it’s interesting there are many roads that lead to a similar solution (how I draw circles or make a center is tad different) Where or how did you learn GLSL?

I was around and writing OpenGL when we started getting the first programmable cards. Nvidia shot out first with their Cg language, but I held back and waited for the "Orange Book" to come up. I bought it right away and learned from it and by looking at other peoples shaders.

https://duckduckgo.com/?q=opengl+orange+book

Also the quick reference card helps

https://duckduckgo.com/?q=glsl+quick+reference&t=h_&ia=web

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

#28
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.

Based!

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

#29
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.

Malicious compliance. Love it.

I'll be referencing this in the future.

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

#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 multiple balls on the screen with positions in 3D space and rendered correctly taking into account any distortion caused by the camera?
Post reply on HN