问题
I am trying to read shader strings from a file; however, I faced with a problem at glShaderSource() function line. As you know, glShaderSource() takes const char**, and I have to declare char * for reading from the file. So, I am using casting to convert types.
If I use const_cast<const char **>, the shape appears; however, it has wrong color (It should be orange not white).
If I use reinterpret_cast<const char**>, I get a Access violation reading location 0x73726576 error in running time.
So, how can I solve this problem? Thank you!
Platform: Windows 7, Visual Studio 2010
Code Lines:
File: shader.glsl
#version 330
in vec3 vp;
void main() {
gl_Position = vec4( vp, 1.0);
}
main():
/* FILE READING */
FILE* shaderFile = fopen( "shader.glsl ", "r");
int fileSize = 0;
char* vertex_shader = NULL;
//Getting File Size
fseek( shaderFile, 0, SEEK_END );
fileSize = ftell( shaderFile );
rewind( shaderFile );
//Reading From File
vertex_shader = (char*)malloc( sizeof( char) * (fileSize+1) );
fread( vertex_shader, sizeof( char ), fileSize, shaderFile );
vertex_shader[ fileSize] = '\0';
fclose( shaderFile );
//Shader definition - If I used this format, it works.
/*const char* vertex_shader = "#version 330\n"
"in vec3 vp;"
"void main(){"
"gl_Position = vec4( vp, 1.0);"
"}";*/
//If I use const char* vertex_shader above, it appears orange.
const char* fragment_shader = "#version 330\n"
"out vec4 frag_colour;"
"void main () {"
" frag_colour = vec4(0.7, 0.4, 0.2, 1.0);"
"}";
//Shader compiling
unsigned int vertexShader = glCreateShader( GL_VERTEX_SHADER );
//The shape appears but not orange
glShaderSource( vertexShader, 1, const_cast<const char **>(&vertex_shader) , NULL );
//glShaderSource( vertexShader, 1, reinterpret_cast<const char**>(vertex_shader) , NULL ); //Gives error
glCompileShader( vertexShader );
unsigned int fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
glShaderSource( fragmentShader, 1, &fragment_shader, NULL);
glCompileShader( fragmentShader );
//Shader program
unsigned int shaderProgram = glCreateProgram();
glAttachShader( shaderProgram, fragmentShader );
glAttachShader( shaderProgram, vertexShader );
glLinkProgram( shaderProgram );
//Drawing
while( !glfwWindowShouldClose( window ) )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glUseProgram( shaderProgram );
glBindVertexArray( vao );
glDrawArrays( GL_TRIANGLES, 0, 3);
glfwPollEvents();
glfwSwapBuffers( window);
}
回答1:
Pass the address of the array to the function:
glShaderSource( vertexShader, 1, (const GLchar**)&vertex_shader, NULL);
EDIT:
Thanks for updating the code in the question. If your program compiles, doesn't crash but the shaders still don't work, it's time to investigate if the GLSL compiler returned any errors! After each call to glCompileShader() write something like the following to display any problems that occurred during compilation:
// This checks for errors upon compiling the Vertex Shader
GLint _compiled = 0;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &_compiled);
if (!_compiled)
{
GLint length;
GLchar* log;
glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &length);
log = new GLchar[length];
glGetShaderInfoLog(vertexShader, length, &length, log);
std::cerr << "!!! Compile log = " << log << std::endl;
delete log;
return;
}
回答2:
The shaderSource functiont takes a char ** so take your char * and just use the & operator on it instead of trying to just cast it.
来源:https://stackoverflow.com/questions/20753942/reading-opengl-shader-language-from-glsl-file