我是 Android NDK 和 Native Activity 的新手,我喜欢在屏幕中间创建一个三角形,但无论我如何尝试,我都不会出现!
这是我的初始化方法:
void Engine::initialize() {
LOGI("Engine::initialize fired!");
const EGLint attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_NONE
};
EGLint w, h, dummy, format;
EGLint numConfigs;
EGLConfig config;
EGLSurface surface;
EGLContext context;
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
eglInitialize(display, 0, 0);
eglChooseConfig(display, attribs, &config, 1, &numConfigs);
eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
ANativeWindow_setBuffersGeometry(this->app->window, 0, 0, format);
surface = eglCreateWindowSurface(display, config, this->app->window, NULL);
context = eglCreateContext(display, config, NULL, NULL);
if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
LOGW("Unable to eglMakeCurrent");
return;
}
eglQuerySurface(display, surface, EGL_WIDTH, &w);
eglQuerySurface(display, surface, EGL_HEIGHT, &h);
this->display = display;
this->context = context;
this->surface = surface;
this->width = w;
this->height = h;
// Initialize GL state.
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glEnable(GL_CULL_FACE);
glShadeModel(GL_SMOOTH);
glDisable(GL_DEPTH_TEST);
this->animating = true;
}
这是我的渲染方法:
void Engine::onRender() {
glClearColor(0.7, 0.1, 0.5, 1);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, this->width, this->height);
//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//glFrustumf(-this->width / 2, this->width / 2, -this->height / 2, this->height / 2, 1, 3);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, 0);
GLfloat triangle[] = {
0, 0, 0,
0, 100, 0,
100, -100, 0
};
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTranslatef(0, 0, 0);
glColor4f(1.0f, 0.3f, 0.0f, .5f);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, triangle);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
glDisableClientState(GL_VERTEX_ARRAY);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -10);
eglSwapBuffers(this->display, this->surface);
}
任何人都可以帮忙吗?
我只能看到粉红色/紫色背景,但知道任何其他像素:| 控制台没有错误。