I'm trying to draw objects using opengles in Android Native-activity, but I don't see anything

笑着哭i 提交于 2021-02-05 08:22:49

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/65695372/im-trying-to-draw-objects-using-opengles-in-android-native-activity-but-i-don

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!