渲染到纹理在 OpenGL ES 2.0 中不起作用

问题描述

我正在尝试使用 C++ 在 OpenGLES 2.0 中实现渲染到纹理。 我已经尝试了来自

的参考代码

// 从视图源引用:https://webglfundamentals.org/webgl/webgl-render-to-texture.html 下面是代码。 作为输出,我只得到蓝色旋转立方体。 这段代码有什么错误?

    float cube_positions[36 * 3] =      { ....      };  
    unsigned int texcoords[36 * 2] =    { ....      };
    GLsizei targetTextureWidth = 256;       GLsizei targetTextureHeight = 256;
    int InitCube()
    {
        hProgram = load_shaders(VS_TEXTURE,FS_TEXTURE);
        positionLocation = glGetAttribLocation(hProgram,"position");
        texcoordLocation = glGetAttribLocation(hProgram,"inputtexcoord");
        int matrixLocation = glGetUniformLocation(hProgram,"mvp");
        int textureLocation = glGetUniformLocation(hProgram,"basetexture");
        glGenBuffers(1,&positionBuffer); glGenBuffers(1,&texcoordBuffer);

        glBindBuffer(GL_ARRAY_BUFFER,positionBuffer);
        glBufferData(GL_ARRAY_BUFFER,36 * 3 * sizeof(GLfloat),cube_positions,GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER,texcoordBuffer);
        glBufferData(GL_ARRAY_BUFFER,36 * 2 * sizeof(GLfloat),texcoords,GL_STATIC_DRAW);
    
        glGenTextures(1,&texture);     glBindTexture(GL_TEXTURE_2D,texture);
        // fill texture with 3x3 pixels 
        float data[] =  {...}; // 3 * 3 * 4
      //glPixelStorei(GL_UNPACK_ALIGNMENT,1);
        glTexImage2D(GL_TEXTURE_2D,GL_RGBA/* GL_LUMINANCE*/,3,GL_RGBA,GL_UNSIGNED_BYTE,data);
        glTexParameteri( ... )

        glGenTextures(1,&targetTexture);
        glBindTexture(GL_TEXTURE_2D,targetTexture);
        {
            glTexImage2D(GL_TEXTURE_2D,targetTextureWidth,targetTextureHeight,NULL);
            glTexParameteri(...);
        }
        ///////////////////////////////////////////////////////////////////////
        glGenFramebuffers(1,&fb);
        glBindFramebuffer(GL_FRAMEBUFFER,fb); 
        glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,targetTexture,0);
        return 1;
    }

    #define WINDOW_WIDTH 1200
    #define WINDOW_HEIGHT 600
    int drawCube(float aspect)
    {   
        ....
        glDrawArrays(GL_TRIANGLES,6 * 6);
        return 1;
    }

    int  drawScene()
    {
    //    glFrontFace(GL_CW);
    //    glEnable(GL_CULL_FACE);
        glEnable(GL_DEPTH_TEST);
        {
            glBindFramebuffer(GL_FRAMEBUFFER,fb);
            glBindTexture(GL_TEXTURE_2D,texture);
            glViewport(0,targetTextureHeight);
            glClearColor(0,1,1);   // clear to blue
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            float aspect = (float)targetTextureWidth / targetTextureHeight;
            drawCube(aspect);
        }

        {
            // render to the canvas
            glBindFramebuffer(GL_FRAMEBUFFER,NULL);
            glBindTexture(GL_TEXTURE_2D,targetTexture);
            glViewport(0,WINDOW_WIDTH,WINDOW_HEIGHT);
            glClearColor(1,1);   // clear to white
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            float aspect = WINDOW_WIDTH / WINDOW_HEIGHT;
            drawCube(aspect);
        }
        return 1;
    }

我的输出:

Render-to-Texture Output

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)