Render to Texture not working in OpenGL ES 2.0

151 views Asked by At

I am trying to implement Render-to-Texture in OpenGLES 2.0 in C++. I have tried the reference code from

// Referred from view-source:https://webglfundamentals.org/webgl/webgl-render-to-texture.html Below is the code. As output I am only getting blue colored rotating cube. What is the error in this code?

    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, 0, GL_RGBA/* GL_LUMINANCE*/, 3, 3, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
        glTexParameteri( ... )

        glGenTextures(1, &targetTexture);
        glBindTexture(GL_TEXTURE_2D, targetTexture);
        {
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,targetTextureWidth, targetTextureHeight, 0,  GL_RGBA, GL_UNSIGNED_BYTE, 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, 0, 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, 0, targetTextureWidth, targetTextureHeight);
            glClearColor(0, 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, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
            glClearColor(1, 1, 1, 1);   // clear to white
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            float aspect = WINDOW_WIDTH / WINDOW_HEIGHT;
            drawCube(aspect);
        }
        return 1;
    }

My Output: Render-to-Texture Output

0

There are 0 answers