Ran*_*nov 2 c c++ android android-ndk
我正在玩本机活动和GLES 2.0,我正在使用ndk从资产中加载GLES着色器.
这是简单着色器的来源:
顶点着色器:simple.vsh
attribute vec4 vPosition;
void main()
{
gl_Position = vPosition;
}
Run Code Online (Sandbox Code Playgroud)
片段着色器:simple.fsh
precision mediump float;
void main()
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
Run Code Online (Sandbox Code Playgroud)
这是我用来加载着色器文件的代码
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);
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,我在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`
Run Code Online (Sandbox Code Playgroud)
注意文件末尾的"sP"和"fs".
我还为我的构建xml文件中的文件扩展添加了"nocompression",以防万一是问题所在,但问题仍然存在.
有谁知道可能导致这种情况的原因是什么?
不知何故,零尾零点不会进入资产.
尝试使用
// 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;
Run Code Online (Sandbox Code Playgroud)