Jump to content
  • Advertisement

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. 

Parameters

shader
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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!