我使用了一个来自互联网的例子,作者说它有效,对我来说它看起来是合法的。
因此,我下载了SDL2并在调试中构建了框架。我创建了一个常规的Opengl 2.1应用程序来检查SDL是否正确构建,并且我可以调试它
然后我创建了一个OpenGL 3.2核心上下文,我检查了主要版本是3,次要版本是2(调用GlGetIntegev)。
我也用过这句话:
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
我仔细检查了一下固定管道调用现在是无用的。到目前为止,一切都很好。
问题是当我尝试为glsl 1.50使用着色器时。它无法编译,并给出了一些类似的错误(我很抱歉,我现在没有前面的错误):ERROR0:2语法错误语法错误
着色器加载代码如下所示:
char* filetobuf(char *file)
{
FILE *fptr;
long length;
char *buf;
fptr = fopen(file, "rb"); /* Open file for reading */
if (!fptr) /* Return NULL on failure */
return NULL;
fseek(fptr, 0, SEEK_END); /* Seek to the end of the file */
length = ftell(fptr); /* Find out how many bytes into the file we are */
buf = (char*)malloc(length+1); /* Allocate a buffer for the entire length of the file and a null terminator */
fseek(fptr, 0, SEEK_SET); /* Go back to the beginning of the file */
fread(buf, length, 1, fptr); /* Read the contents of the file in to the buffer */
fclose(fptr); /* Close the file */
buf[length] = 0; /* Null terminator */
return buf; /* Return the buffer */
}
和着色器:
vertex shader
#version 150 core
uniform mat4 viewMatrix, projMatrix;
in vec4 position;
in vec3 color;
out vec3 Color;
void main()
{
Color = color;
gl_Position = projMatrix * viewMatrix * position ;
}
fragment shader:
#version 150 core
in vec3 Color;
out vec4 outputF;
void main()
{
outputF = vec4(Color,1.0);
}
如果我不使用3.2核心上下文,我会得到"不支持的版本"。但现在不行,所以这个错误一定是别的原因。
有线索吗?
更新事实上,在读取着色器文件时出现了一些问题,因为我刚刚创建了一个const char*,并在其中写入了整个着色器,并将引用传递给glShaderSource(),现在它就可以工作了。有趣的是,我仍然不明白filetobuf()有什么问题(我应用了Armin修复程序)。
您使用fread()不正确。
代替
fread(buf, length, 1, fptr);
应该是
fread(buf, 1, length, fptr);
这可能不是问题所在,据我所见,您的read函数没有任何错误,但最好按照预期使用库函数。
不过,我不确定您代码的第二部分。发布一条更具描述性的错误消息会有所帮助。