Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8279453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:21:04+00:00 2026-06-08T09:21:04+00:00

I’m trying to show a simple texture in OpenGL ES 2.0 (Android 4.0.4, Galaxy

  • 0

I’m trying to show a simple texture in OpenGL ES 2.0 (Android 4.0.4, Galaxy Nexus) as fullscreen background using a vertex and a fragment shader. The final result should be to display the camera image there, but for a start, simple texture from a file would be sufficient. I tested many things and for a short while it worked as I used OpenGL ES 1.x but as I plan to use shader for YUV->RGB conversion, I have to go with 2.0.

I have the following vertex shader:

attribute vec4 position;
attribute vec4 inputTextureCoordinate;

varying vec2 textureCoordinate;

void main() {
    gl_Position = position;
    textureCoordinate = inputTextureCoordinate.xy
}

and this fragment shader:

varying highp vec2 textureCoordinate;

uniform sampler2D videoFrame;

void main() {
    gl_FragColor = texture2D(videoFrame, textureCoordinate);
}

Shader creation as such should be ok, because when I write gl_FragColor=vec4(1,0,0,0) it works perfectly well and shows me a red background.

OnSurfaceCreated I do the following:

 a = makeFloatBuffer(squareVertices);
 b = makeFloatBuffer(textureVertices);

with squareVertices and textureVertices as follows:

static float squareVertices[] = { -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f,
        1.0f, 1.0f, };

static float textureVertices[] = { 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
        0.0f, 0.0f, };

OnSurfaceChanged I do the following:

GLES20.glViewport(0, 0, width, height);
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.raw.htc);
GLES20.glGenTextures(1, textures, 0);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

R.raw.htc is 128×128 pixels png.

And OnDrawFrame:

    GLES20.glUseProgram(program);

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);

    GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "videoFrame"),0);

    GLES20.glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
    GLES20.glClearDepthf(1.0f);
    GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);

    int x = GLES20.glGetAttribLocation(program, "position");
    int y = GLES20.glGetAttribLocation(program, "inputTextureCoordinate");

    GLES20.glEnableVertexAttribArray(x);        
    GLES20.glVertexAttribPointer(x, 2, GLES20.GL_FLOAT, false, 0, a);
    GLES20.glEnableVertexAttribArray(y);
    GLES20.glVertexAttribPointer(y, 4, GLES20.GL_FLOAT, false, 0, b);

    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);

The only thing I see is a completely black screen and I have no idea why. I saw some other posts here with similar problems but none of the proposed solution helped me out or no solution was given at all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:21:06+00:00Added an answer on June 8, 2026 at 9:21 am

    As said above, we could find a solution for the issue although we don’t know exactly the reason why it is working now and was not before (we changed several things and suddenly it worked). Nonetheless I want to share our working version:

    The shader are now completed to also do the YUV->RGB conversion:
    The vertex shader:

    attribute vec4 position;
    attribute vec4 inputTextureCoordinate;
    
    varying vec2 textureCoordinate;
    
     void main() {
          gl_Position = position;
          textureCoordinate = inputTextureCoordinate.xy;
     };
    

    The fragment shader:

    varying highp vec2 textureCoordinate;
    uniform sampler2D videoFrame;
    uniform sampler2D videoFrame2;
    
    void main(void) {
        highp float nx,ny,r,g,b,y,u,v;
        highp vec2 uv;
        highp vec4 txl,ux,vx;"
        nx=(textureCoordinate.x)/1024.0*720.0;
        ny=(textureCoordinate.y)/1024.0*480.0;
        y=texture2D(videoFrame,vec2(nx,ny)).r;
        uv=texture2D(videoFrame2,vec2(nx,ny)).ag;
        y=1.1643*(y-0.0625);
        u=uv[0]-0.5;
        v=uv[1]-0.5;
        r=y+1.5958*v;
        g=y-0.39173*u-0.81290*v;
        b=y+2.017*u;
    
        gl_FragColor=vec4(r,g,b,1);
    };
    

    OnSurfaceCreated is still the same but the definition of the vertices changed a bit:

    static float squareVertices[] = { -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f,
            1.0f, 1.0f, };
    
    private float textureVertices[] = { 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
            1.0f, 0.0f, };
    

    OnSurfaceChanged:

        buffer = new int[2];
        GLES20.glGenTextures(2, buffer, 0);
        for (int i = 0; i < 2; i++) {
            int texture = buffer[i];
            Log.v("Texture", "Texture is at " + texture);
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
            GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLES20.GL_TRUE);
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
                    GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
                    GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
                    GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
                    GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
        }
    
        buf = new byte[1024 * 1024];
        for (int i = 0; i < 1024 * 1024; i++) {
            buf[i] = 0;
        }
        buf2 = new byte[1024 * 1024];
        for (int i = 0; i < 1024 * 1024; i++) {
            buf2[i] = -128;
        }
    
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, buffer[0]);
    
        GLES20.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_LUMINANCE, 1024,
                1024, 0, GL10.GL_LUMINANCE, GL10.GL_UNSIGNED_BYTE,
                ByteBuffer.wrap(buf));
    
        GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, buffer[1]);
    
        GLES20.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_LUMINANCE_ALPHA,
                512, 512, 0, GL10.GL_LUMINANCE_ALPHA, GL10.GL_UNSIGNED_BYTE,
                ByteBuffer.wrap(buf2));
    

    OnDrawFrame:

        GLES20.glViewport(0, 0, mWidth, mHeight);
    
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, buffer[0]);
    
        GLES20.glTexSubImage2D(GL10.GL_TEXTURE_2D, 0, 0, 0, Util.CAMWIDTH,
                Util.CAMHEIGHT, GL10.GL_LUMINANCE, GL10.GL_UNSIGNED_BYTE,
                ByteBuffer.wrap(mBuffer, 0, Util.CAMWIDTH * Util.CAMHEIGHT));
    
        GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, buffer[1]);
    
        GLES20.glTexSubImage2D(
                GL10.GL_TEXTURE_2D,
                0,
                0,
                0,
                Util.CAMWIDTH / 2,
                Util.CAMHEIGHT / 2,
                GL10.GL_LUMINANCE_ALPHA,
                GL10.GL_UNSIGNED_BYTE,
                ByteBuffer.wrap(mBuffer, Util.CAMWIDTH * Util.CAMHEIGHT,
                        Util.CAMWIDTH / 2 * Util.CAMHEIGHT).slice());
    
        GLES20.glClearColor(1.0f, .0f, .0f, .0f);
    
        GLES20.glUseProgram(program);
    
        GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "videoFrame"),
                0);
        GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "videoFrame2"),
                1);
    
        GLES20.glClearDepthf(1.0f);
        GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
    
        int x = GLES20.glGetAttribLocation(program, "position");
        int y = GLES20.glGetAttribLocation(program, "inputTextureCoordinate");
    
        GLES20.glEnableVertexAttribArray(x);
        GLES20.glVertexAttribPointer(x, 2, GLES20.GL_FLOAT, false, 0, a);
        GLES20.glEnableVertexAttribArray(y);
        GLES20.glVertexAttribPointer(y, 2, GLES20.GL_FLOAT, false, 0, b);
    
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.