我目前正在使用原生活动和GLES 2.0,并且我正在使用ndk从资源中加载GLES的着色器。
这是简单着色器的源代码:
顶点着色器: simple.vsh
attribute vec4 vPosition;
void main()
{
gl_Position = vPosition;
}片段着色器: simple.fsh
precision mediump float;
void main()
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}这是我用来加载着色器文件的代码
AAsset *shaderAsset= AAssetManager_open(app->activity->assetManager, path, AASSET_MODE_BUFFER);
size_t length = AAsset_getLength(shaderAsset);
LOGI("Shader source size: %d\n", length);
char* buffer = (char*) malloc(length);
AAsset_read(shaderAsset, buffer, length);
LOGI("Shader source : %s\n", buffer);
AAsset_close(shaderAsset);
free(buffer);当我运行应用程序时,我在android logcat中看到以下内容:
Shader source size: 71
07-22 13:23:52.683 11135 11146 I native-activity: Shader source : attribute vec4 vPosition;
07-22 13:23:52.683 11135 11146 I native-activity: void main()
07-22 13:23:52.683 11135 11146 I native-activity: {
07-22 13:23:52.683 11135 11146 I native-activity: gl_Position = vPosition;
07-22 13:23:52.683 11135 11146 I native-activity: }
07-22 13:23:52.683 11135 11146 I native-activity: sP
07-22 13:23:52.683 11135 11146 I native-activity: Shader source size: 86
07-22 13:23:52.683 11135 11146 I native-activity: Shader source : precision mediump float;
07-22 13:23:52.683 11135 11146 I native-activity: void main()
07-22 13:23:52.683 11135 11146 I native-activity: {
07-22 13:23:52.683 11135 11146 I native-activity: gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
07-22 13:23:52.683 11135 11146 I native-activity: }
07-22 13:23:52.683 11135 11146 I native-activity: fs`注意文件末尾的"sP“和"fs'”。
我还为我的构建xml文件中的文件扩展名添加了“无压缩”,以防这是问题所在,但因此问题仍然存在。
有没有人知道这是什么原因?
发布于 2012-07-22 19:43:17
不知何故,尾随的零没有进入资产。
尝试使用
// one extra char for the trailing zero
char* buffer = (char*) malloc(length + 1);
AAsset_read(shaderAsset, buffer, length);
// fix the string to be zero-terminated
buffer[length] = 0;https://stackoverflow.com/questions/11599608
复制相似问题