Log in or register to post comments

Render background texture on a rectangle

March 24, 2017 - 5:01am #1

I am using vuforia android sdk and want to render the background frame on a rectangle. what I want is each pixel should have exact same color as the background frame color. I am using the gl_Fragcord in fragment shader to get the pixel position. I have passed the background image as texture to the shader. When I try to find the color of pixel, there is a shift between the rectangle color and background texture color. I am using Samsung galaxy S7.

Please have a look at pictures. As you can see, there is a shift in both x and y direction though x is quite accurate compared to y.

Following is the code


    <pre class="brush: csharp"> double[] TEAPOT_VERTS = { -1.0,  -1.0, 0.0, 1.0, -1.0, 0.0, 1.0, 1.0, 0.0, -1.0, -1.0, 0.0, 1.0, 1.0, 0.0, -1.0, 1.0, 0.0};</pre>

Texture Coordinates

   <pre class="brush: csharp"> double[] TEAPOT_TEX_COORDS = { 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0};</pre>

In Fragment shader. Here I have to invert the x, y coordinates because of difference in opengl and device x,y coordinate. Also 1440 and 2560 are the screen resolutions. The background image size is 1440 and 2560. I have hardcoded this in `public void configureVideoBackground()` function of `SampleAppRenderer` class. Opengl viewport size is also 1440 and 2560.

<pre class="brush: csharp">

    uniform sampler2D backgroundSampler2D;   

    vec2 screenpos = vec2((1.0 - gl_FragCoord.y/2560.0), (1.0 - gl_FragCoord.x/1440.0));

    color = texture2D(backgroundSampler2D, screenpos);

    gl_FragColor = vec4((color.xyz), 1.0);


I think there should be a one-one mapping of pixels and the rectangle pixel colors should match exactly with the background texture color. I think there is some issue with the coordinates returned from gl_Fragcoord or the background texture dimensions. Any help?

Image icon y_50.png1.67 MB
Image icon x_50.png1.72 MB
Log in or register to post comments