使用EGL像素缓冲区

时间:2013-02-25 11:11:42

标签: c++ opengl-es opengl-es-2.0 egl

我在嵌入式平台Mali400GPU,Ubuntu上。我正在尝试使用EGL像素缓冲区,将使用片段着色器渲染的纹理复制到像素缓冲区,以便我可以在CPU上对该缓冲区进行一些处理。我已经完成了以下代码,但是当尝试使用缓冲区对象并在四边形上绘制时,四边形有一个扭曲的纹理,我不知道为什么。

这是在Init函数

// EGL variables
EGLDisplay eglDisplay = 0;
EGLConfig eglConfigWindow = 0;
EGLConfig eglConfigPbuffer = 0;
EGLSurface eglSurfaceWindow = 0;
EGLSurface eglSurfacePbuffer = 0;
EGLContext eglContext = 0;


const EGLint attribListWindow[] =
{
    EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
    EGL_RED_SIZE, 5,
    EGL_GREEN_SIZE, 6,
    EGL_BLUE_SIZE, 5,
    EGL_ALPHA_SIZE, 0,
    EGL_DEPTH_SIZE, 16,
    EGL_STENCIL_SIZE, 0,
    EGL_NONE
};
const EGLint attribListPbuffer[] =
{
    EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
    EGL_RED_SIZE, 5,
    EGL_GREEN_SIZE, 6,
    EGL_BLUE_SIZE, 5,
    EGL_ALPHA_SIZE, 0,
    EGL_DEPTH_SIZE, 16,
    EGL_STENCIL_SIZE, 0,
    EGL_NONE
};
const EGLint srfPbufferAttr[] =
{
    EGL_WIDTH, 1024,
    EGL_HEIGHT, 1024,
    EGL_COLORSPACE, GL_RGB,
    EGL_TEXTURE_FORMAT, EGL_TEXTURE_RGB,
    EGL_TEXTURE_TARGET, EGL_TEXTURE_2D,
    EGL_LARGEST_PBUFFER, EGL_TRUE,
    EGL_NONE
};

    EGLint iMajorVersion, iMinorVersion;
    int iConfigs;
    eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    eglInitialize(eglDisplay, &iMajorVersion, &iMinorVersion);
    eglChooseConfig(eglDisplay, attribListWindow,
                    &eglConfigWindow, 1, &iConfigs);
    eglContext = eglCreateContext(eglDisplay,
                                  eglConfigWindow, NULL, NULL);
    eglSurfaceWindow = eglGetCurrentSurface(EGL_DRAW);
    eglSurfacePbuffer = eglCreatePbufferSurface(eglDisplay,
                        eglConfigPbuffer,srfPbufferAttr);

    eglMakeCurrent(eglDisplay, eglSurfacePbuffer, eglSurfacePbuffer, eglContext);

    glGenTextures(1, &theSource);
    glBindTexture(GL_TEXTURE_2D, theSource);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024,
                 1024, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
    glTexParameterf(GL_TEXTURE_2D,
                    GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D,
                    GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D,
                    GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D,
                    GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

在绘图中:

  UserData *userData = (UserData*)esContext->userData;
    GLfloat vVertices[] = { -1.0f,  -1.0f, 0.0f,  // Position 0
                            0.0f,  0.0f,        // TexCoord 0
                            -1.0f,  1.0f, 0.0f,  // Position 1
                            0.0f,  1.0f,        // TexCoord 1
                            1.0f,  1.0f, 0.0f,  // Position 2
                            1.0f,  1.0f,        // TexCoord 2
                            1.0f, -1.0f, 0.0f,  // Position 3
                            1.0f,  0.0f         // TexCoord 3
                          };
    GLushort indices[] = { 0, 1, 2, 0, 2, 3 };

    // Set the viewport
    glViewport ( 0, 0, esContext->width, esContext->height );
    // Clear the color buffer
    glClear ( GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
     // Use the program object
    glUseProgram ( userData->programObject );

    // Load the vertex position
    glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT,
                            GL_FALSE, 5 * sizeof(GLfloat), vVertices );
    // Load the texture coordinate
    glVertexAttribPointer ( userData->texCoordLoc, 2, GL_FLOAT,
                            GL_FALSE, 5 * sizeof(GLfloat), &vVertices[3] );

    glEnableVertexAttribArray ( userData->positionLoc );
    glEnableVertexAttribArray ( userData->texCoordLoc );

    // Bind the base map
    glUniform1i ( userData->baseMapLoc, 0 );
    glActiveTexture ( GL_TEXTURE0 );
    glBindTexture ( GL_TEXTURE_2D, userData->baseMapTexId );
    glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    //glCopyTexImage2D(GL_TEXTURE_2D,0,0,0,0,0, 1024, 1024);
    glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );

    eglMakeCurrent(eglDisplay, eglSurfacePbuffer, eglSurfacePbuffer, eglContext);

    eglBindTexImage(eglDisplay, eglSurfacePbuffer, EGL_BACK_BUFFER);

    glBindTexture(GL_TEXTURE_2D, theSource);

    glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 1024, 1024, 0);
    glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );

0 个答案:

没有答案