我正在尝试在 Android Native-activity 中使用 opengles 绘制对象,但我什么也没看到 [英] I'm trying to draw objects using opengles in Android Native-activity, but I don't see anything
问题描述
我正在使用本机活动中的 opengles1.0 开发我的游戏.
然而,过去运行良好的源代码现在不起作用了.
屏幕上没有任何内容,但我想知道原因.
这是我的源代码.
static int engine_init_display(struct engine* engine) {//初始化 OpenGL ES 和 EGL/** 此处指定所需配置的属性.* 下面,我们选择每个颜色至少 8 位的 EGLConfig* 与屏幕窗口兼容的组件*/const EGLint attribs[] = {EGL_SURFACE_TYPE、EGL_WINDOW_BIT、EGL_BLUE_SIZE, 8,EGL_GREEN_SIZE, 8,EGL_RED_SIZE, 8,EGL_DEPTH_SIZE, 24,EGL_NONE};EGLint w, h, 格式;EGLint numConfigs;EGLConfig 配置;EGLS表面;EGLContext 上下文;EGLDisplay 显示 = eglGetDisplay(EGL_DEFAULT_DISPLAY);[eglInitialize(display, 0, 0);/* 在这里,应用程序选择它想要的配置.在这* 样本,我们有一个非常简化的选择过程,我们从中挑选* 第一个符合我们标准的 EGLConfig */eglChooseConfig(display, attribs, &config, 1, &numConfigs);/* EGL_NATIVE_VISUAL_ID 是 EGLConfig 的一个属性,它是* 保证被 ANativeWindow_setBuffersGeometry() 接受.* 一旦我们选择了 EGLConfig,我们就可以安全地重新配置* ANativeWindow 缓冲区匹配,使用 EGL_NATIVE_VISUAL_ID.*/eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format);表面 = eglCreateWindowSurface(display, config, engine->app->window, NULL);上下文 = eglCreateContext(display, config, NULL, NULL);如果(eglMakeCurrent(显示,表面,表面,上下文)== EGL_FALSE){LOGW(无法使用 eglMakeCurrent");返回-1;}eglQuerySurface(显示,表面,EGL_WIDTH,&w);eglQuerySurface(显示,表面,EGL_HEIGHT,&h);引擎->显示=显示;引擎->上下文=上下文;引擎->表面=表面;引擎->宽度= w;发动机->高度= h;引擎-> state.angle = 0;renderModule = RenderModule::getInstance();renderModule->setupViewPort(engine->width, engine->height);GLManager* GLManager;glManager = GLManager::getInstance();glManager->引擎=引擎;glManager->setAssetManager(engine->app->activity->assetManager);ScreenManager* screenManager = glManager->screen();screenManager->scrWidth = engine->width;screenManager->scrHeight = 引擎->高度;screenManager->引擎=引擎;renderModule->setGLManager(glManager);renderModule->rendererSetting();...返回0;}
draw_frame 函数
static void engine_draw_frame(struct engine* engine) {如果(引擎->显示== NULL){//不显示.返回;}renderModule->setupViewPort(engine->width, engine->height);GLManager* glManager = GLManager::getInstance();ScreenManager* screenManager = glManager->screen();GLfloat x = screenManager->getOGLX(engine->state.x[0]);GLfloat y = screenManager->getOGLY(engine->state.y[0]);//GLuint scrX = glManager->screen()->getScrX(x);//GLuint scrY = glManager->screen()->getScrY(y);如果(屏幕触摸){renderModule->setFakeMode(true);}renderModule->render();如果(屏幕触摸){screenTouched = false;glReadPixels(engine->state.x[pointerIndex],screenManager->scrHeight - engine->state.y[pointerIndex],1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 像素);renderModule->setFakeMode(false);renderModule->render();}glClear(GL_DEPTH_BUFFER_BIT);eglSwapBuffers(引擎->显示,引擎->表面);}
rendererSetting 函数
void RenderModule::rendererSetting() {glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);glEnable(GL_DEPTH_TEST);glDepthFunc(GL_LEQUAL);glDepthMask(GL_TRUE);/*glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);glEnable(GL_CULL_FACE);glShadeModel(GL_SMOOTH);*/...}
和 setupViewPort 函数
void RenderModule::setupViewPort(GLuint width, GLuint height) {glViewport(0, 0, 宽度, 高度);glMatrixMode(GL_PROJECTION);glLoadIdentity();GLfloat 比率 = (GLfloat)((GLfloat)width * 1.0f)/((GLfloat)height * 1.0f);glFrustumf(-ratio, ratio, -1.0f, 1.0f, 1.5f, 1000.0f);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(0, 0, 200,//eyeXYZ0.0f, 0.0f, 0.0f,//centerXYZ0.0f、1.0f、0.0f);viewportSetUp = true;}
终于
gluLookAt 函数
void gluLookAt(GLfloat eyeX, GLfloat eyeY, GLfloat eyeZ,GLfloat centerX, GLfloat centerY, GLfloat centerZ, GLfloat upX, GLfloat upY, GLfloat upZ){GLfloat f[3] ={中心X - 眼睛X,中心Y - 眼睛Y,centerZ - 眼睛Z,};//使 f 和向上向量具有单位长度GLfloat lengthOfF = sqrt(f[0] * f[0] + f[1] * f[1] + f[2] * f[2]);f[0]/= lengthOfF;f[1]/= lengthOfF;f[2]/= lengthOfF;GLfloat lengthOfUp = sqrt(upX*upX + upY*upY + upZ*upZ);upX/= lengthOfUp;upY/= lengthOfUp;upZ/= lengthOfUp;//使用 f 和 Up 的叉积得到 s,//和 s 和 f 的叉积得到 uGLfloat s[3] ={f[1] * upZ - upY*f[2],f[2] * upX - upZ*f[0],f[0] * upY - upX*f[1]};GLfloat u[3] ={s[1] * f[2] - f[1] * s[2],s[2] * f[0] - f[2] * s[0],s[0] * f[1] - f[0] * s[1]};//按规定填充矩阵.//注意:OpenGL 是列专业";GLfloat M[16] ={s[0], u[0], -f[0], 0.0f,s[1], u[1], -f[1], 0.0f,s[2], u[2], -f[2], 0.0f,0.0f、0.0f、0.0f、1.0f};glMultMatrixf(M);glTranslatef(-eyeX, -eyeY, -eyeZ);}
我认为是renderSeting函数和setupViewPort函数有问题.我想寻求帮助以解决此问题.
如果离物体中心的距离是 200,但是物体半径的大小是 1000,那么你就在物体内部.
到物体背面的距离为 1200 (1000+200).由于到近平面的距离为 1000,
glFrustumf(-ratio, ratio, -1.0f, 1.0f, 1.5f, 1000.0f);
视锥远平面旁的物体.
I'm developing my game using opengles1.0 in Native Activity.
However, source code, which used to work well, doesn't work.
Nothing is drawing on the screen, but I want to know the cause.
This is my source code.
static int engine_init_display(struct engine* engine) {
// initialize OpenGL ES and EGL
/*
* Here specify the attributes of the desired configuration.
* Below, we select an EGLConfig with at least 8 bits per color
* component compatible with on-screen windows
*/
const EGLint attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_DEPTH_SIZE, 24,
EGL_NONE
};
EGLint w, h, format;
EGLint numConfigs;
EGLConfig config;
EGLSurface surface;
EGLContext context;
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);[
eglInitialize(display, 0, 0);
/* Here, the application chooses the configuration it desires. In this
* sample, we have a very simplified selection process, where we pick
* the first EGLConfig that matches our criteria */
eglChooseConfig(display, attribs, &config, 1, &numConfigs);
/* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
* guaranteed to be accepted by ANativeWindow_setBuffersGeometry().
* As soon as we picked a EGLConfig, we can safely reconfigure the
* ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */
eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format);
surface = eglCreateWindowSurface(display, config, engine->app->window, NULL);
context = eglCreateContext(display, config, NULL, NULL);
if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
LOGW("Unable to eglMakeCurrent");
return -1;
}
eglQuerySurface(display, surface, EGL_WIDTH, &w);
eglQuerySurface(display, surface, EGL_HEIGHT, &h);
engine->display = display;
engine->context = context;
engine->surface = surface;
engine->width = w;
engine->height = h;
engine->state.angle = 0;
renderModule = RenderModule::getInstance();
renderModule->setupViewPort(engine->width, engine->height);
GLManager* glManager;
glManager = GLManager::getInstance();
glManager->engin = engine;
glManager->setAssetManager(engine->app->activity->assetManager);
ScreenManager* screenManager = glManager->screen();
screenManager->scrWidth = engine->width;
screenManager->scrHeight = engine->height;
screenManager->engine = engine;
renderModule->setGLManager(glManager);
renderModule->rendererSetting();
...
return 0;
}
draw_frame function
static void engine_draw_frame(struct engine* engine) {
if (engine->display == NULL) {
// No display.
return;
}
renderModule->setupViewPort(engine->width, engine->height);
GLManager* glManager = GLManager::getInstance();
ScreenManager* screenManager = glManager->screen();
GLfloat x = screenManager->getOGLX(engine->state.x[0]);
GLfloat y = screenManager->getOGLY(engine->state.y[0]);
//GLuint scrX = glManager->screen()->getScrX(x);
//GLuint scrY = glManager->screen()->getScrY(y);
if (screenTouched) {
renderModule->setFakeMode(true);
}
renderModule->render();
if (screenTouched) {
screenTouched = false;
glReadPixels(engine->state.x[pointerIndex],
screenManager->scrHeight - engine->state.y[pointerIndex],
1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
renderModule->setFakeMode(false);
renderModule->render();
}
glClear(GL_DEPTH_BUFFER_BIT);
eglSwapBuffers(engine->display, engine->surface);
}
rendererSetting function
void RenderModule::rendererSetting() {
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_TRUE);
/*
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glEnable(GL_CULL_FACE);
glShadeModel(GL_SMOOTH);
*/
...
}
and setupViewPort function
void RenderModule::setupViewPort(GLuint width, GLuint height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat ratio = (GLfloat)((GLfloat)width * 1.0f) / ((GLfloat)height * 1.0f);
glFrustumf(-ratio, ratio, -1.0f, 1.0f, 1.5f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 200, //eyeXYZ
0.0f, 0.0f, 0.0f, //centerXYZ
0.0f, 1.0f, 0.0f);
viewportSetUp = true;
}
finally
gluLookAt function
void gluLookAt(GLfloat eyeX, GLfloat eyeY, GLfloat eyeZ,
GLfloat centreX, GLfloat centreY, GLfloat centreZ, GLfloat upX, GLfloat upY, GLfloat upZ)
{
GLfloat f[3] =
{
centreX - eyeX,
centreY - eyeY,
centreZ - eyeZ,
};
// make f and the up vector have unit length
GLfloat lengthOfF = sqrt(f[0] * f[0] + f[1] * f[1] + f[2] * f[2]);
f[0] /= lengthOfF;
f[1] /= lengthOfF;
f[2] /= lengthOfF;
GLfloat lengthOfUp = sqrt(upX*upX + upY*upY + upZ*upZ);
upX /= lengthOfUp;
upY /= lengthOfUp;
upZ /= lengthOfUp;
// use the cross product of f and Up to get s,
// and the cross product of s and f to get u
GLfloat s[3] =
{
f[1] * upZ - upY*f[2],
f[2] * upX - upZ*f[0],
f[0] * upY - upX*f[1]
};
GLfloat u[3] =
{
s[1] * f[2] - f[1] * s[2],
s[2] * f[0] - f[2] * s[0],
s[0] * f[1] - f[0] * s[1]
};
// Fill the matrix as prescribed.
// Note: OpenGL is "column major"
GLfloat M[16] =
{
s[0], u[0], -f[0], 0.0f,
s[1], u[1], -f[1], 0.0f,
s[2], u[2], -f[2], 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
glMultMatrixf(M);
glTranslatef(-eyeX, -eyeY, -eyeZ);
}
In my opinion, there is a problem with renderSeting function and setupViewPort function. I would like to ask for help to resolve this issue.
If the distance from the center of the object is 200, but the size of the radius of the object is 1000, you are inside the object.
The distance to the back of the object is 1200 (1000+200). Since the distance to the near plane is 1000,
glFrustumf(-ratio, ratio, -1.0f, 1.0f, 1.5f, 1000.0f);
the object by the far plane of the viewing frustum.
这篇关于我正在尝试在 Android Native-activity 中使用 opengles 绘制对象,但我什么也没看到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!