我正在使用 NDK 为 android 编写一个纯 c++ 应用程序。我正在尝试编写一个使用 OpenGL 进行 2D 绘图的简单应用程序,因此没有纹理或类似的东西。
我应用了此处发布的所有建议,但仍然得到
调用未实现的 OpenGL ES API
logcat 中的消息
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<!-- BEGIN_INCLUDE(manifest) -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.native_activity"
android:versionCode="1"
android:versionName="1.0">
<uses-feature android:glEsVersion="0x00020000"></uses-feature>
<!-- This is the platform API where NativeActivity was introduced. -->
<uses-sdk android:minSdkVersion="9" />
<!-- This .apk has no Java code itself, so set hasCode to false. -->
<application android:label="@string/app_name" android:hasCode="false">
<!-- Our activity is the built-in NativeActivity framework class.
This will take care of integrating with our NDK code. -->
<activity android:name="android.app.NativeActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden">
<!-- Tell NativeActivity the name of or .so -->
<meta-data android:name="android.app.lib_name"
android:value="native-activity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是代码:
EGLDisplay dpy;
EGLSurface surface;
EGLContext context;
static void android_handle_cmd( struct android_app * app, int32_t cmd ) {
switch (cmd) {
case APP_CMD_INIT_WINDOW:
// The window is being shown, get it ready.
if ( NULL != app->window ) {
Init( app );
drawscreen();
}
break;
}
}
void pixel( int x, int y ) {
if ( NULL == GLapp.surface ) {
return;
}
glColor4f( 1.0, 1.0, 1.0, 0 );
glVertexAttrib2f( 0, x + 0.5, y + 0.5 );
}
void drawscreen( void ) {
if ( NULL == GLapp.surface ) {
return;
}
eglSwapBuffers( GLapp.dpy, GLapp.surface );
}
int Init( struct android_app * app ) {
dpy = eglGetDisplay( EGL_DEFAULT_DISPLAY );
eglInitialize( dpy, 0, 0 );
const EGLint attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, // <--- OpenGL ES 2.0
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_NONE
};
EGLConfig config;
EGLint numConfigs;
eglChooseConfig( dpy, attribs, &config, 1, &numConfigs );
EGLint format;
eglGetConfigAttrib( dpy, config, EGL_NATIVE_VISUAL_ID, &format );
ANativeWindow_setBuffersGeometry( app->window, 0, 0, format );
surface = eglCreateWindowSurface( dpy, config, app->window, NULL );
const EGLint ContextAttribList[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
context = eglCreateContext( GLapp.dpy, config, EGL_NO_CONTEXT, ContextAttribList );
eglMakeCurrent( dpy, surface, surface, context );
int XSize;
eglQuerySurface( GLapp.dpy, GLapp.surface, EGL_WIDTH, &XSize );
int YSize;
eglQuerySurface( GLapp.dpy, GLapp.surface, EGL_HEIGHT, &YSize );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrthof( 0, XSize, YSize, 0, 0, 1 );
glMatrixMode( GL_MODELVIEW );
/* Displacement trick for exact pixelization */
glTranslatef( 0.375, 0.375, 0 );
glDisable( GL_DEPTH_TEST );
glClear( GL_COLOR_BUFFER_BIT );
return 0;
}
我删除了所有错误检查代码以使代码更易于阅读。Init 函数执行时未检测到任何错误,只有当我调用像素或绘图屏幕(不确定是哪个)函数时,日志才会填满“未实现的 OpenGL ES API”错误
任何人都可以帮忙吗?