luca-piccioni / OpenGL.Net

Modern OpenGL bindings for C#.
MIT License
568 stars 108 forks source link

[QUESTION] Why I need to pass string[] into shadersource? #100

Closed WhoseTheNerd closed 6 years ago

WhoseTheNerd commented 6 years ago

Hi, im trying to learn opengl in c#. I got to a problem when i needed to compile shaders, so I started looking up tutorials about it and when writing code. The function 'Gl.ShaderSource();' takes shader source as string array, which is annoying. Here's code I got currently: https://pastebin.com/nFU7bYZe.

luca-piccioni commented 6 years ago

Well, this is not related to OpenGL.Net itself: the public API is determined directly by the GL specification, which the project is trying to reflect closely as much as possible.

Indeed the question is: why OpenGL API designers require an array of strings?

The reason is quite simple: to make simpler the composition/generation of shader sources. The insertion, deletion, and modification of shader source lines are simpler if strings organized in an array. For example, if your shader source has preprocessor definitions, you typically will include dynamically at the very beginning (before submitting them using glShaderSource); but the #version directive must be the very first line. How do you solve this? If using a single string, you should search the first newline character and modify the single entire string, but if using an array, you simply insert array items at the right location.

In your case, nothing blocks you to pass an array of length 1, containing only a single string.

As example, reference to the OpenGL.Net.Objects source.