Jump to content
  • Advertisement
Sign in to follow this  
SteveDeFacto

How to load a GLSL shader?

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

I need to load a GLSL shader from a file and compile it but I'm not entirely sure how? Here is my best guess:




GLenum VertexShader;
GLenum FragmentShader;

VertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
FragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);

FILE* f = NULL;
fopen_s( &f, file.c_str(), "rb" );

fseek( f, 0, SEEK_END );
int filesize = ftell(f);
fseek( f, 0, SEEK_SET );

char* data = new char[filesize];
fread( &data, filesize, 1, f );

glShaderSourceARB( VertexShader, 1, &data, NULL );
glShaderSourceARB( FragmentShader, 1, &data, NULL );

glCompileShader( VertexShader );
glCompileShader( FragmentShader );

fclose( f );



Is that right? Can I load both a Fragment shader and a Vertex shader from the same file?

Share this post


Link to post
Share on other sites
Advertisement

Read: http://www.lighthous...om/opengl/glsl/

You really ought to be able to find this stuff for yourself, that's what search engines are for.


Currently I am working two jobs and I goto school full time. You could have simply said yes or no and saved me 2 hours instead of posting a tutorial that I could find in 30 seconds. This type of question is exactly what these forums are for and your reply didn't help me in any way...

Share this post


Link to post
Share on other sites
You ask how to load a shader, I link you to a site that explicitly explains how to load glsl shaders, and you say that it doesn't help you at all.


This type of question is exactly what these forums are for
[/quote]

No, these forums are not for "how do I do X I have no time to research anything myself".

Really sorry I bothered to answer your question, it won't happen again.

Share this post


Link to post
Share on other sites
Yes, you can load multiple shaders from the single file, but no, you cannot do it on the way you did.

glShaderSource() requires source code in a string, or an array of strings. So if you want to have multiple shaders in the same file, write your own parser that will divide sources of each shader into separate string after loading from the file, and post each of them to an adequate shader.

Share this post


Link to post
Share on other sites

Yes, you can load multiple shaders from the single file, but no, you cannot do it on the way you did.

glShaderSource() requires source code in a string, or an array of strings. So if you want to have multiple shaders in the same file, write your own parser that will divide sources of each shader into separate string after loading from the file, and post each of them to an adequate shader.




Thank you! That's what I needed to know!

Share this post


Link to post
Share on other sites
1. Don't use ARB functions like glShaderSourceARB with normal functions.
2. When you read a file like that, it won't be NULL terminated.
3. And don't forget to check for errors. f could be NULL after calling fopen_s.
The shaders might fail to compile or link.
Error checking is fully shown here
http://www.opengl.org/wiki/Tutorial1:_Rendering_shapes_with_glDrawRangeElements,_VAO,_VBO,_shaders_%28C%2B%2B_/_freeGLUT%29

It also has an alternative with "ifstream" for reading a file.

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.

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!