Results 1 to 7 of 7

Thread: Filling a trapezoid with a texture

  1. #1
    Junior Member Newbie
    Join Date
    May 2011
    Posts
    4

    Filling a trapezoid with a texture

    I am trying to take a camera preview image and display it in a trapezoid. The image displays fine in a rectangle but when I change one or more corners to make the shape a trapezoid the camera preview image now "bends" where the triangles meet.

    Here are the relevant code excerpts.

    final static float leftPanCamObjCoords[] = new float[] {
    -2.0f, 6.0f, -5.0f, // top-right
    -2.0f, 0.0f, -5.0f, // bottom-right
    -8.0f, 6.0f, -5.0f, // top-left
    -8.0f, -4.0f, -5.0f // bottom-left
    };

    leftPanCamBuff = makeFloatBuffer(leftPanCamObjCoords);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, leftPanCamBuff);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuff);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY );
    gl.glEnable(GL10.GL_TEXTURE_2D);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    GLU.gluLookAt(gl, 0, 0, 5.5f, 0, 0, 0, 0, 1, 0);
    gl.glNormal3f(0, 0, 1);

    gl.glGenTextures(1, cameraTexture, 0);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, cameraTexture[0]);

    gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_LUMINANCE,
    frameWidth, frameHeight, 0,
    GL10.GL_LUMINANCE, GL10.GL_UNSIGNED_BYTE,
    ByteBuffer.wrap(dispBuffer));

    gl.glTexParameterf(GL10.GL_TEXTURE_2D,
    GL10.GL_TEXTURE_MIN_FILTER,
    GL10.GL_LINEAR);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D,
    GL10.GL_TEXTURE_MAG_FILTER,
    GL10.GL_LINEAR);

    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
    GL10.GL_NICEST);

    gl.glDrawElements(GL10.GL_TRIANGLES, 1,
    GL10.GL_UNSIGNED_BYTE, rearPanCamBuff);

    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);


    Any hints would be appreciated.


    -Dale

  2. #2
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,671

    Re: Filling a trapezoid with a texture

    The image displays fine in a rectangle but when I change one or more corners to make the shape a trapezoid the camera preview image now "bends" where the triangles meet.
    Of course it does. You're not drawing a trapezoid; you're drawing two triangles. Trapezoids are a fairly distorted shape; if you draw them as two triangles, you will see distortion in the interpolated texture coordinates.

    Generally, what you need to do is break the trapezoid into smaller pieces.

  3. #3
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Filling a trapezoid with a texture

    Use the q coordinate as given here http://www.r3.nu/~cass/qcoord/

    The above link is dead but this link should work.
    http://replay.web.archive.org/200802.../~cass/qcoord/

    Let me know if this helps.
    Regards,
    Mobeen

  4. #4
    Junior Member Newbie
    Join Date
    May 2011
    Posts
    4

    Re: Filling a trapezoid with a texture

    Thanks for the suggestion. The problem with that solution is that in Java under Android the GL_QUADS option is not supported.

  5. #5
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,671

    Re: Filling a trapezoid with a texture

    http://replay.web.archive.org/20080209130648/http://www.r3.nu/~cass/qcoord/
    That doesn't work (the technique, not the link). It causes projection, which is not the same thing as what he's trying to do.

    Granted, it looks similar. But the forced-perspective distortion that it causes is very different from what you would want out of flat mapping to a trapezoid. And the link points this out.

    The problem with that solution is that in Java under Android the GL_QUADS option is not supported.
    Is there some reason you can't just make it use two triangles instead? The technique doesn't care about whether you happen to be rendering with GL_QUADS, GL_TRIANGLES, or a geometry shader that turns GL_LINES_ADJACENCY into GL_TRIANGLE_STRIP.

  6. #6
    Junior Member Newbie
    Join Date
    May 2011
    Posts
    4

    Re: Filling a trapezoid with a texture

    Quote Originally Posted by Alfonse Reinheart
    Generally, what you need to do is break the trapezoid into smaller pieces.
    Yes, I have experimented with variations on this but the texture being a camera image it always looks fractured at triangle boundaries. I am trying to display a completely smooth camera image in a trapezoid.

  7. #7
    Junior Member Newbie
    Join Date
    May 2011
    Posts
    4

    Re: Filling a trapezoid with a texture

    [quote=Alfonse Reinheart]
    Is there some reason you can't just make it use two triangles instead? The technique doesn't care about whether you happen to be rendering with GL_QUADS, GL_TRIANGLES, or a geometry shader that turns GL_LINES_ADJACENCY into GL_TRIANGLE_STRIP.
    The coordinates I listed in my original message:

    -2.0f, 6.0f, -5.0f, // top-right
    -2.0f, 0.0f, -5.0f, // bottom-right
    -8.0f, 6.0f, -5.0f, // top-left
    -8.0f, -4.0f, -5.0f // bottom-left

    are two triangles that form a trapezoid. If I change them to form a rectangle:

    -2.0f, 6.0f, -5.0f, // top-right
    -2.0f, 0.0f, -5.0f, // bottom-right
    -8.0f, 6.0f, -5.0f, // top-left
    -8.0f, 0.0f, -5.0f // bottom-left

    the texture draws just fine (no angle change where the two triangles meet). But if I move one point lower to make two triangles that form a trapezoid the image changes its angle where the two triangles meet.

Similar Threads

  1. Filling texture with glTexStorage2D
    By Asatnin in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 04-27-2016, 09:31 AM
  2. Texture not filling on complete Object in Android
    By infoshore in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 08-15-2013, 11:02 AM
  3. RGBA filling in texture is not working using gles
    By anilec in forum OpenGL: Linux
    Replies: 1
    Last Post: 05-10-2013, 06:00 AM
  4. Filling a polygon with a repeating texture
    By transparent in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 01-11-2011, 09:07 AM
  5. mapping texture to trapezoid
    By kohn in forum OpenGL: Advanced Coding
    Replies: 18
    Last Post: 10-29-2004, 12:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean