Jump to content
  • Advertisement
Sign in to follow this  

OpenGL GLSL modulo strange behavior with negative number

This topic is 1001 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, I have been having an issue with the modulo implementation in GLSL


I'm running version 4.5 Core and have the following problem:


Even though mod is defined as 

x - y * floor(x/y)

(source: https://www.opengl.org/sdk/docs/man4/html/mod.xhtml) which, when you plot it for y = 3, looks like this:




I am running into an issue where the result of -1 % 3 is apparently not within the domain [0, 2].


To be precise, the reduced code causing the error message is this:

vec3 barycentric;
vec3 barycentric_verts_sequence[3] = vec3[](vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0));
barycentric = barycentric_verts_sequence[-1 % 3];

The error reads: error C1068: array index out of bounds.


If shaders were easily debuggable, I probably wouldn't be asking this question, but since there's no way to step through the shader code while it's running, I was hoping that someone more experienced with GLSL could help me out!


Thanks in advance,



Share this post

Link to post
Share on other sites

Looking at the GLSL spec, it doesn't seem to specify how operator% should be implemented... which means that behavior for negative inputs could well be implementation defined -- either using the floor definition, or the truncation definition which rounds towards zero instead of rounding towards negative infinity.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!