# How to work around the pointer limitations of GLSL?

(I am new to Computer Graphics in general)

I am learning how to ray trace from a book called ''Peter Shirley - Ray Tracing in One Weekend''. In the book, the code is written in C++. I have intermediate-advanced level understanding of good ol' C, but I've never written even simple programs in C++. I don't pretend to understand everything the code says in the book. But, that is the fun part -- figuring it out.

But, for ray tracing, even C is slow. And writing GLSL fragment shaders is fast, even on my potato PC. And GLSL is closer to C than to C++. So, I'm trying to follow through the book using GLSL. But, GLSL has many limitations and I was hoping if you could teach me how to get around those limitations.

1. Pointers. GLSL doesn't have pointers. In the book, there is a function bool hit_sphere(const vec3& center, float radius, const ray& r) I believe that ''address of'' operator just points to the variable that is passed when the function is called. Without pointers, how to call a function by reference in GLSL?

2. Late binding. In the book, every object is a class which exposes an interface function called hit which returns if a ray hits the object. How does one implement that in GLSL structs?

• In 1) those a references, in glsl you'll just pass things as they are. For 2) you don't have polymorphism, so you can split your spheres, triangles, etc. in separate arrays and deal with those in separate parts of the code. Here's something you can check out: github.com/GraphicsProgramming/RVPT – lightxbulb Aug 8 '20 at 11:43

Similarly, any form of "late binding" can just be a switch statement based on some value in a data structure. The different casees define which function to call.