Jump to content
  • Advertisement
Sign in to follow this  
VanKurt

GLSL - Loading from file?

This topic is 4526 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

Hi there! I'm just getting into GLSL and having some trouble. In all resources I can find "glShaderSourceARB" is used (of course). The third parameter seems to be a pointer to some shader-code-text. Isn't there a way to pass a pointer to the file containing the source? Or do I have to load the whole file into memory before I can call glShaderSourceARB?? And how would I read afile into memory if I don't know the file's size? Thank's for your help!

Share this post


Link to post
Share on other sites
Advertisement
are you using c++? if so just use a std::string.


bool Shader::LoadSrc(string filename)
{
ifstream instream;
instream.open(filename.c_str(),ios::in);
if (instream.is_open())
{
//using the null character as a delimeter should get the whole file
getline(instream,src,'\0');
//close the file
instream.close();
return true;
}

return false;
}


src is just a member std::string

Share this post


Link to post
Share on other sites
There's a more generic (doesn't depend on a delimiter) c++ file to memory example Here. Note that the example has a memory leak, but it's just to show how to copy a file into memory.

glShaderSource can take source code that has been allocated in pieces. The third parameter is an array of pointers to strings that make up the source. So you can read and allocate it in chunks, if you want.

Share this post


Link to post
Share on other sites
Well, since I pass null as the length parameter to glShaderSource it expects null terminated strings, which is what string::c_str() provides. In that case the delimiter is implicit, since if the string contains a null character, glShaderSource will ignore everything after it anyway.

It just seemed the simplest way to do it to me, and it avoids having to seek through the file to find the length yourself.

Cocalus is absolutely right though.

Share this post


Link to post
Share on other sites
I've been slowly working on a tutorial explaining basic GLSL loading stuff... I might have it finished in a week or two. In the mean time, you might look at this source, it shows a simple way to load the text from a file and load/compile/link it.

http://www.leecbaker.com/coding/glsl/main.cpp

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!