## Recommended Posts

Posted (edited)

** Beginner Question **

Can somebody please try to explain parameters two and four. I am trying to create a shader from source code and I'm unable to interpret from the OpenGL documentation the purpose of these parameters.

In regards to the fourth parameter, the documentation also states - "If length is NULL, each string is assumed to be null terminated. If length is a value other than NULL, it points to an array containing a string length for each of the corresponding elements of string." - can somebody please try to explain this in layman's terms.

 Parametersshader Specifies the handle of the shader object whose source code is to be replaced.count Specifies the number of elements in the string and length arrays.string Specifies an array of pointers to strings containing the source code to be loaded into the shader.length Specifies an array of string lengths.
Edited by calioranged

##### Share on other sites

string is an array of char* pointers to strings, count tells the function how large that array is, because in C and C++ arrays don't store their own length.

If you leave the length array as NULL then it will use the usual C convention of NULL-terminated strings, however if you're not null terminating your strings, you can instead pass in a second array which tells the function how long each of the strings in string are. Basically, you can ignore length for normal use.

##### Share on other sites
6 minutes ago, Irusan, son of Arusan said:

string is an array of char* pointers to strings, count tells the ﻿function how large that array is, because in C and C++ arrays don't store their own length.﻿﻿

From the few examples that I have seen, the glShaderSource code usually looks something like this:

glShaderSource(id, 1, &source, null);

Does the '1' in the second parameter just mean that there is one source code being attached?

I am still confused about the fourth parameter...

Quote

If you leave the length array as NULL then it will use the usual C convention of NULL-terminated strings, however if you're not null terminating your strings, you can instead pass in a second array which tells the function how long each of the strings in string are.

From my understanding 'null termination' just means that there must be a '\0' at the end of the shader source code. However in the example that I am learning from, there is no null termination character at the end of the source code, null/nullptr is still used as the fourth parameter, and everything works fine.

Shader source code in example I am learning from:

std::string vertexShader =
"#version 330 core\n"
"\n"
"layout(location = 0) in vec4 position;\n"
"\n"
"void main()\n"
"{\n"
"   gl_Position = position;\n"
"}\n";

Then the source code is inserted into the shader object:

glShaderSource(id, 1, &src, nullptr);

So why is it that this program compiles fine where 'nullptr' is the fourth parameter and the shader source code does not feature the null termination character?

##### Share on other sites

Yes, the 1 means that only one is being attached.

The data storage in std::string is guaranteed to be stored in a null terminated by the C++ standard from C++11 onwards. I assume there's some missing code in the bit you've posted, since its defining vertexShader and then you're passing src?

##### Share on other sites
1 minute ago, Irusan, son of Arusan said:

Yes, the 1 means that only one is being attached.

Thanks.

1 minute ago, Irusan, son of Arusan said:

The data storage in std::string is guaranteed to be stored in a null terminated by the C++ standard from C++11 onwards.

Oh alright so we don't need to explicitly state '\0' at the end of an std::string because this is implicitly guaranteed by C++?

3 minutes ago, Irusan, son of Arusan said:

I assume there's some missing code in the bit you've posted, since its defining vertexShader and then you're passing src?

Yes 'vertexShader' is an argument of a function call to a function that compiles the shader, and it becomes the 'src' parameter inside this function.

Thanks a lot for all your help!

## Create an account

Register a new account

• 10
• 10
• 12
• 9
• 33