2013-07-01 35 views
0

我正在尋找使用OpenGl將4個圖像混合到屏幕上。第一張圖片包含背景,其他3張圖片包含一些帶有透明度的卡通圖片。我的目標是一次渲染這4個圖像。每次調用渲染時,我都會使用新圖像更新前3幅圖像以組成新幀。繪製多個圖像紋理的問題有些消失

我是新來的OpenGL,迄今爲止我能夠實現混合這些圖像,但我注意到一些可怕的問題,當我渲染。我看到一些前3張圖片有時會丟失,或者有些被渲染,但看起來像是他們被看不見的人所裁剪...

每行代表不同的圖像。

圖片裁剪的問題:
enter image description here

圖像裁剪和一個圖像丟失:
enter image description here

究竟應該如何樣子:
enter image description here

任何幫助搞清楚的問題,我會非常感謝!在我使用的代碼下面。

這是我用來渲染圖像的代碼。

void MoviePreview::prepareTexture (GLuint texture, GLint format, int w, int h) 
{ 
    // Bind to generated texture 
    glBindTexture(GL_TEXTURE_2D, texture); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glShadeModel(GL_FLAT); 
    //glShadeModel(GL_SMOOTH); 

    glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0, format, GL_UNSIGNED_BYTE, 0); 

    if (GL_RGBA == format) 
    { 
     //glColor4f(1.0f, 1.0f, 1.0f, 1.0f); 
    } 

    // Crop the texture rectangle 
    GLint rect[] = {0, h, w, -h}; 
    glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, rect); 
    glDrawTexiOES(0, 0, 0, w, h); 
} 

void MoviePreview::resize (int w, int h) 
{ 
    LOGI("native_gl_resize %d %d", w, h); 

    // init open gl 
    //glClearColor(1.0, 1.0, 1.0, 1.0); 
    glEnable(GL_TEXTURE_2D); 
    glEnable(GL_BLEND); 
    glDisable(GL_DITHER); 
    glDisable(GL_LIGHTING); 
    glDisable(GL_DEPTH_TEST); 
    glDisable(GL_COLOR_MATERIAL); 
    glDisable(GL_FOG); 
    glDisable(GL_CULL_FACE); 
    glDisable(GL_STENCIL_TEST); 
    glDisable(GL_COLOR_LOGIC_OP); 
    glDisable(GL_ALPHA_TEST); 

    //glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);//NEW 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);//NEW 
    //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA); 

    // Generate one texture object 
    glGenTextures(MAX_TEXTURES, mTexture); 
    check_gl_error("glGenTextures"); 

    int frameHeight = h;//image.rows; 
    int frameWidth = w;//image.cols; 

    // first texture is our background texture 
    prepareTexture(mTexture[0], GL_RGBA, mBg.cols, mBg.rows); 
    prepareTexture(mTexture[1], GL_RGBA, frameWidth, frameHeight); 
    prepareTexture(mTexture[2], GL_RGBA, frameWidth, frameHeight); 
    prepareTexture(mTexture[3], GL_RGBA, frameWidth, frameHeight); 

    mSurfaceWidth = w; 
    mSurfaceHeight = h; 
} 

void MoviePreview::render (int64_t* imageIds, const int images) 
{ 
    int i     = 0; 
    double sleepDuration = 0; 

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    // TODO try to see if we can just get away from always loading the bg 
    // since it doesn't change often might be worth loading it once and that 
    // is it... 
    //glBindTexture(GL_TEXTURE_2D, mTexture[0]); 
    //glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, mBg.cols, mBg.rows, GL_RGBA, GL_UNSIGNED_BYTE, mBg.ptr()); 
    //glDrawTexiOES(0, 0, 0, mSurfaceWidth, mSurfaceHeight); 

    // TODO pass the image batch loader 
    // load images 
    for (i=0; i<images; i++) 
    { 
     if (0 < imageIds[i]) 
     { 
      sprintf(mTempPath, "%s/f1_%lld.png",mProjectPath.c_str(), imageIds[i]); 
      mImageLoader[i].loadImage(mTempPath); 
     } 
    } 

    if (0 < mFrameDuration) 
    { 
     // here we try to control the suggested frame rate 
     // set. We calculate since the last show image time 
     // if we should sleep or not... 
     if (0 < mLastDrawTimestamp) { 
      sleepDuration = mFrameDuration - (now_ms() - mLastDrawTimestamp); 
     } 

     if (0 < sleepDuration) { 
      usleep((long)sleepDuration*NANO_IN_MS); 
     } 
    } 

    // draw images 
    i = 0; 
    for (i=0; i<images; i++) 
    { 
     if (0 < imageIds[i]) 
     { 
      cv::Mat img = mImageLoader[i].getImage(); 
      if (!img.empty()) 
      { 
       glBindTexture(GL_TEXTURE_2D, mTexture[i+1]); 
       glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, img.cols, img.rows, GL_RGBA, GL_UNSIGNED_BYTE, img.ptr()); 
       glDrawTexiOES(0, 0, 0, img.cols, img.rows); 
      } 
     } 
    } 

    mLastDrawTimestamp = now_ms(); 
} 
+0

難道是Z-戰鬥? – Simon

+0

我想這不會是Z戰鬥,因爲我使用GL_TEXTURE_2D?這是一個沒有3D的交易,但也許有我不知道的設置? – Jona

+0

OpenGL沒有2D模式。一切都是3D。要排除z戰鬥,請嘗試將精靈的z座標設置爲不同的值,並查看您是否遇到問題。 – Simon

回答

0

問題結束了我的圖像源是OpenGl的同時,試圖拉攏他們被其他線程得到改進......