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

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.

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/20080209130648/http://www.r3.nu/~cass/qcoord/

Let me know if this helps.

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

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.

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.

[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.