Loading GLSL shaders

Started by
3 comments, last by Ezbez 17 years, 11 months ago
I'm currently trying to load and compile a simple GLSL shader in C++. My code suffers from the copy-and-paste syndrom, but I'm really just trying to learn shaders, not how to load them, so I don't mind too much. Well, here's the code:
	//Load shaders
	GLenum my_program;
	GLcharARB * my_fragment_shader_source;
	GLcharARB * my_vertex_shader_source;

	// Get Vertex And Fragment Shader Sources
	FILE *fp;
	GLubyte *buf;
	int length;
	bool ret = true;

	if (!(fp = fopen("test.vert","rb")))
		printf("Error opening shader.");

	fseek(fp, 0, SEEK_END);
	length = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	my_vertex_shader_source = new GLcharARB[length+1];
	fread( my_vertex_shader_source, 1, length, fp);< -- First error

	if (!(fp = fopen("test.frag","rb")))
		printf("Error opening shader.");

	fseek(fp, 0, SEEK_END);
	length = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	my_fragment_shader_source = new GLcharARB[length+1];
	fread( my_fragment_shader_source, 1, length, fp);< -- Second error 
And Here's the error messages: error C2664: 'void (GLhandleARB,GLsizei,const GLcharARB **,const GLint *)' : cannot convert parameter 3 from 'GLcharARB **' to 'const GLcharARB **' error C2664: 'void (GLhandleARB,GLsizei,const GLcharARB **,const GLint *)' : cannot convert parameter 3 from 'GLcharARB **' to 'const GLcharARB **' The lines that the errors take place on are marked above. Does anyone know how to fix this? I didn't think that converting from a non const to a const variable would cause an error, so I'd geuss that it is something else.
Advertisement
Quote:Original post by Ezbez
I'm currently trying to load and compile a simple GLSL shader in C++. My code suffers from the copy-and-paste syndrom, but I'm really just trying to learn shaders, not how to load them, so I don't mind too much.

Well, here's the code:
*** Source Snippet Removed ***

And Here's the error messages:

error C2664: 'void (GLhandleARB,GLsizei,const GLcharARB **,const GLint *)' : cannot convert parameter 3 from 'GLcharARB **' to 'const GLcharARB **'

error C2664: 'void (GLhandleARB,GLsizei,const GLcharARB **,const GLint *)' : cannot convert parameter 3 from 'GLcharARB **' to 'const GLcharARB **'


The lines that the errors take place on are marked above. Does anyone know how to fix this? I didn't think that converting from a non const to a const variable would cause an error, so I'd geuss that it is something else.

Are you sure that the compile errors are from the lines you marked? THey look like they are from glShaderSource (or whatever the corresponding ARB extension function is called).

To solve, you can just use const_cast, or with C-style casting (const GLcharARB**) pointer_to_string.
Oops, yeah I did give the wrong eror. They are on glShaderSourceARB().

Here's the bit of code that I left out:
	// Create Shader And Program Objects	my_program = glCreateProgramObjectARB();	vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);	fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);	// Load Shader Sources	// Load Shader Sources	glShaderSourceARB(vertexShader, 1, const_cast<GLcharARB**>(&my_vertex_shader_source), NULL);//First error	delete my_vertex_shader_source;	glShaderSourceARB(fragmentShader, 1, const_cast<GLcharARB**>(&my_fragment_shader_source), NULL);//SecondError	delete my_vertex_shader_source;

with the added const_cast. However, I get the exact same errors as before pointing to the same lines.

Any other ideas?

Quote:Original post by Ezbez
Any other ideas?

Sure! [grin]

Try const_cast<const GLcharARB**>(&my_fragment_shader_source)
*wacks self on the head*

Next time I will take the time to fully read the tutorial and make sure that I'm not using the code to get rid of const-ness! Thanks a bunch, it compiles now!

This topic is closed to new replies.

Advertisement