Pregunta

I am drawing a textured trapezoid in OpenGL and affine problem occurs:

http://upload.wikimedia.org/wikipedia/commons/5/57/Perspective_correct_texture_mapping.jpg

I want my texture this in perspective-correct.

I have to interpolate in the image space (sw tw w) and I don't know how to do it:

http://i.stack.imgur.com/O0AnC.png

I paste my current code project:

  • c++:

ttps://gist.github.com/danicomas/a1f5a0e6849b3ac8b51c169c2c030e37 (Add http)

  • vertex:

ttps://gist.github.com/danicomas/fee77cf48fc5085f61a2fcf7a2c6d5de (Add http)

  • fragment:

ttps://gist.github.com/danicomas/0bbd679d2d7da18bc61ee23b36096a16 (Add http)

How can I do this? Some example code?

¿Fue útil?

Solución

Finally. I found it a simple solution!

  • c++

glPushMatrix();
glBegin(GL_QUADS);

float scale_texcoord = 0.7;
float top = 0.7;

float tx = scale_texcoord * top;

glTexCoord2f(-1 / 2, -1);
glVertex2f(-1.4, -1);

glTexCoord4f(0,  0, 0, tx);
glVertex2f(-top,  1);

glTexCoord4f( tx,  0, 0, tx);
glVertex2f( top,  1);

glTexCoord2f( 1, -1);
glVertex2f( 1.4, -1);

glEnd();
glPopMatrix();

  • fragment:

uniform sampler2D sampler;

void main()
{
 vec2 interpolated = vec2(gl_TexCoord[0].x / gl_TexCoord[0].w, gl_TexCoord[0].y);

 gl_FragColor = texture2D(sampler, vec2(interpolated.x, interpolated.y));
}

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top