我的顶点着色器末尾的垃圾字符
Garbage characters at the end of my vertex shader
我一直在尝试从 c 中的文件加载我的顶点着色器。这是我将文件中的字符加载到字符串中的代码:
char* path = "shaders/vertex_shader.glsl";
if (!fopen(path, "r")) {
printf("Could not open shader file! %s", path);
exit(1);
}
FILE* shader = fopen(path, "r");
fseek(shader, 0, SEEK_END);
long file_size = ftell(shader);
rewind(shader);
char *vertex_shader_code = malloc(sizeof(char) * file_size + 1);
fread(vertex_shader_code, sizeof(char), file_size, shader);
vertex_shader_code[file_size] = '[=10=]';
printf("%s", vertex_shader_code);
fclose(shader);
vertex_shader.glsl
文件如下所示:
#version 330 core
layout (location = 0) in vec3 positions;
void main() {
gl_Position = vec4(positions.x, positions.y, positions.z, 1.0);
}
但是,每当我打印出 vertex_shader_code
字符串时,我都会在文件底部得到一堆垃圾字符:
#version 330 core
layout (location = 0) in vec3 positions;
void main() {
gl_Position = vec4(positions.x, positions.y, positions.z, 1.0);
}
════════
另外,每当我尝试编译着色器时,我都会收到以下错误:
0(8) : error C0000: syntax error, unexpected $undefined at token "<undefined>"
编译顶点着色器的代码:
void checkShaderCompilation(int shader_type_number, GLuint shader) {
int success;
char log[512];
char* shader_type;
switch (shader_type_number) {
case 0:
shader_type = "Vertex Shader";
break;
case 1:
shader_type = "Fragment Shader";
break;
default:
printf("Invalid number has been provided for the shader_type_number (check function declaration)\n");
return;
}
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 512, NULL, log);
printf(" %s \n Compilation ERROR:\n %s\n", shader_type, log);
exit(1);
}
else {
printf("%s compiled successfully!\n", shader_type);
}
}
/* Compiling the vertex shader */
vertex_shader_object = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader_object, 1, &vertex_shader_code, NULL);
glCompileShader(vertex_shader_object);
checkShaderCompilation(0, vertex_shader_object);
您的代码中存在各种 off-by-one 错误。计算 file_size
和读取着色器的部分应该是(未测试的)...
long file_size = ftell(shader);
rewind(shader);
char *vertex_shader_code = malloc(sizeof(char) * file_size + 1);
fread(vertex_shader_code, sizeof(char), file_size, shader);
vertex_shader_code[file_size] = '[=10=]';
备注:
- 我删除了
malloc
返回值的转换。参见 here。
sizeof(char)
本质上是多余的,因为它总是计算为 1。
看来我只需要以二进制模式打开文件:
FILE* shader = fopen(path, "rb");
我一直在尝试从 c 中的文件加载我的顶点着色器。这是我将文件中的字符加载到字符串中的代码:
char* path = "shaders/vertex_shader.glsl";
if (!fopen(path, "r")) {
printf("Could not open shader file! %s", path);
exit(1);
}
FILE* shader = fopen(path, "r");
fseek(shader, 0, SEEK_END);
long file_size = ftell(shader);
rewind(shader);
char *vertex_shader_code = malloc(sizeof(char) * file_size + 1);
fread(vertex_shader_code, sizeof(char), file_size, shader);
vertex_shader_code[file_size] = '[=10=]';
printf("%s", vertex_shader_code);
fclose(shader);
vertex_shader.glsl
文件如下所示:
#version 330 core
layout (location = 0) in vec3 positions;
void main() {
gl_Position = vec4(positions.x, positions.y, positions.z, 1.0);
}
但是,每当我打印出 vertex_shader_code
字符串时,我都会在文件底部得到一堆垃圾字符:
#version 330 core
layout (location = 0) in vec3 positions;
void main() {
gl_Position = vec4(positions.x, positions.y, positions.z, 1.0);
}
════════
另外,每当我尝试编译着色器时,我都会收到以下错误:
0(8) : error C0000: syntax error, unexpected $undefined at token "<undefined>"
编译顶点着色器的代码:
void checkShaderCompilation(int shader_type_number, GLuint shader) {
int success;
char log[512];
char* shader_type;
switch (shader_type_number) {
case 0:
shader_type = "Vertex Shader";
break;
case 1:
shader_type = "Fragment Shader";
break;
default:
printf("Invalid number has been provided for the shader_type_number (check function declaration)\n");
return;
}
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 512, NULL, log);
printf(" %s \n Compilation ERROR:\n %s\n", shader_type, log);
exit(1);
}
else {
printf("%s compiled successfully!\n", shader_type);
}
}
/* Compiling the vertex shader */
vertex_shader_object = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader_object, 1, &vertex_shader_code, NULL);
glCompileShader(vertex_shader_object);
checkShaderCompilation(0, vertex_shader_object);
您的代码中存在各种 off-by-one 错误。计算 file_size
和读取着色器的部分应该是(未测试的)...
long file_size = ftell(shader);
rewind(shader);
char *vertex_shader_code = malloc(sizeof(char) * file_size + 1);
fread(vertex_shader_code, sizeof(char), file_size, shader);
vertex_shader_code[file_size] = '[=10=]';
备注:
- 我删除了
malloc
返回值的转换。参见 here。 sizeof(char)
本质上是多余的,因为它总是计算为 1。
看来我只需要以二进制模式打开文件:
FILE* shader = fopen(path, "rb");