我正在尝试从文件中读取shader字符串,但是我在
如果我使用
那么,我应该如何解决这个问题呢?谢谢!
平台:Windows 7,Visual Studio 2010
代码行:文件:
glShaderSource()
函数行遇到了一个问题。正如你所知,glShaderSource()
需要const char**
,而我必须声明char *
来从文件中读取。因此,我使用转换来进行类型转换。如果我使用
const_cast<const char **>
,形状就出现了;但是它的颜色不对(应该是橙色而不是白色)。
如果我使用reinterpret_cast<const char**>
,我在运行时会收到访问冲突,位置0x73726576
的错误提示。那么,我应该如何解决这个问题呢?谢谢!
平台:Windows 7,Visual Studio 2010
代码行:
文件:shader.glsl
#version 330
in vec3 vp;
void main() {
gl_Position = vec4( vp, 1.0);
}
主函数():
/* 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);
}
glShaderSource( vertexShader, 1, (const GLchar**)&vertex_shader, NULL);
? - karlphilliperror C2664: 'void (GLuint,GLsizei,const GLchar **,const GLint *)' : cannot convert parameter 3 from 'char **' to 'const GLchar **'
- ciyoconst
性引起的。 - karlphillipconst char* fragment_shader
行处,fclose(shaderFile);
未从另一个文件中读取。 - ciyo