• Create Account

## Trapezoid uvs preservation

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

22 replies to this topic

### #1masterbubu  Members

Posted 22 May 2012 - 03:34 AM

Hi All,

I am trying to create this effect on Opengl ES, without any success.

basically I have created a quad which is built up from two tri's.

my goal is to distort the vertices as I wise, but the uv's needs to react accordingly ( as the link shows ).

I have read lots on information online, but not managed to preform the task.

Any help would be great...

tnx

### #2szecs  Members

Posted 22 May 2012 - 08:04 AM

Um, the source code on the linked site is not helping? I don't know about openGL ES, but that link would have been the first link I would have posted. I'd be surprised if ES didn't support r,q coordinates.

### #3masterbubu  Members

Posted 23 May 2012 - 02:44 AM

Hi,

OpenGL ES does not work with the fixed pipe line. So I can't use glTexCoord4f.
Also, ES does not supports Quads ( Only Tri's ).
Is there a way to achieve this effect on OGL ES ?

I have read about the algorithm "Direct Linear Transform" but I'm not sure it is possible for a quad that built up from 2 triangles.

### #4Hodgman  Moderators

Posted 23 May 2012 - 03:00 AM

What effect? That page is explaining how texture mapping works, also it's doing so using a quad made out of 2 triangles.

Edited by Hodgman, 23 May 2012 - 03:13 AM.

### #5Brother Bob  Moderators

Posted 23 May 2012 - 03:03 AM

The issues you brought up are not issues at all. A texture coordinate is a texture coordinate, so even if you cannot use glTexCoord, you still have access yo vertex arrays, and whether or not you draw quads or triangles, the effect is still the same given the same coordinates.

### #6masterbubu  Members

Posted 23 May 2012 - 06:42 AM

I'm sorry, I'm confused... I have read to many resources, and I think I mixed them all.

this is the quad I defined:

<code>

SetPoints( vec3<float>(-20.0f , -10.0f, 0.0f),
vec3<float>(-10.0f , 10.0f, 0.0f),
vec3<float>( 10.0f , 10.0f, 0.0f),
vec3<float>( 20.0f , -10.0f, 0.0f) );

vTxCoords_.push_back(vec4<float>( 0.0f , 0.0f,1.0f,1.0f));
vTxCoords_.push_back(vec4<float>( 0.0f , 1.0f,1.0f,1.0f));
vTxCoords_.push_back(vec4<float>( 1.0f , 1.0f,1.0f,1.0f));
vTxCoords_.push_back(vec4<float>( 1.0f , 0.0f,1.0f,1.0f));

// Populate the indices for 2 triangles
Face s;

s.uIndex[0] = 0; s.uIndex[1] = 1; s.uIndex[2] = 3;
pGeo->vFaces.push_back(s);

s.uIndex[0] = 3; s.uIndex[1] = 2; s.uIndex[2] = 1; // also try s.uIndex[0] = 3; s.uIndex[1] = 1; s.uIndex[2] = 2;
pGeo->vFaces.push_back(s);
</code>

the shader is a simple shader that uses texture2D function (texture2D( DiffuseColor, vOutTxCoords.xy ).rgb;)

I have tried the set the textCoord manually, but failed.

### #7Brother Bob  Moderators

Posted 23 May 2012 - 06:55 AM

You're not setting the q-value of the texture coordinate as described in the link. I would say the value for the narrow part of the quad should be 0.5, not 1.0, since that part is half the size of the opposite side of the quad.

edit: The s and t coordinates need to be 0.5 as well, not only the q-coordinate.

Edited by Brother Bob, 23 May 2012 - 06:57 AM.

### #8masterbubu  Members

Posted 23 May 2012 - 08:16 AM

I tried a brute force method, I denied a key for each coord of the texture ( for each vertex ), and tried to set them manually.

every combination that I do, I get a broken texture. looks like the texture is not interpolated as one quad.

(btw: how can I upload a photo to this forum ? )

### #9Brother Bob  Moderators

Posted 23 May 2012 - 08:30 AM

If you go to the full text editor (not the one on the bottom of the thread-page; click "More Reply Options") you have the option to attach files to your post.

### #10masterbubu  Members

Posted 23 May 2012 - 08:57 AM

Well. you can easily spot the two triangles.

I have played with the textCoord of each, but I can't make them to fit right.

#### Attached Thumbnails

Edited by masterbubu, 23 May 2012 - 08:57 AM.

### #11Brother Bob  Moderators

Posted 23 May 2012 - 09:01 AM

So what have you tried to to se the texture coordinates to? It looks like the texture coordinates you posted previously, but as I said, and as the link explained, you need to adjust the coordinates for the perspective effect.

### #12masterbubu  Members

Posted 23 May 2012 - 09:13 AM

I have tried to modified the coords as you suggested, but it did not help.

so then I decided to try the brute force, but did not make any progress on that way

### #13Brother Bob  Moderators

Posted 23 May 2012 - 09:24 AM

Show the complete code which has the modifications I suggested.

### #14masterbubu  Members

Posted 23 May 2012 - 10:12 AM

SetPoints( vec3<float>(-20.0f , -10.0f, 0.0f), // BottomLeft,
vec3<float>(-10.0f ,  10.0f, 0.0f), // TopLeft,
vec3<float>( 10.0f ,  10.0f, 0.0f), // TopRight,
vec3<float>( 20.0f , -10.0f, 0.0f) );// BottomRight

vTxCoords_.push_back(vec4<float>(  0.0f , 0.0f,0.0f,1.0f));
vTxCoords_.push_back(vec4<float>(  0.0f , 0.5f,0.0f,0.5f));
vTxCoords_.push_back(vec4<float>(  0.5f , 0.5f,0.0f,0.5f));
vTxCoords_.push_back(vec4<float>(  1.0f , 0.0f,0.0f,1.0f));


I changed the r component, to 0.

### #15Brother Bob  Moderators

Posted 23 May 2012 - 10:50 AM

That's not the complete code. You must draw your vertex array somewhere at least.

### #16masterbubu  Members

Posted 23 May 2012 - 11:42 PM

//Setup
SetPoints( vec3<float>(-20.0f , -10.0f, 0.0f), // BottomLeft,
vec3<float>(-10.0f ,  10.0f, 0.0f), // TopLeft,
vec3<float>( 10.0f ,  10.0f, 0.0f), // TopRight,
vec3<float>( 20.0f , -10.0f, 0.0f) );// BottomRight

vTxCoords_.push_back(vec4<float>(  0.0f , 0.0f,0.0f,1.0f));
vTxCoords_.push_back(vec4<float>(  0.0f , 0.5f,0.0f,0.5f));
vTxCoords_.push_back(vec4<float>(  0.5f , 0.5f,0.0f,0.5f));
vTxCoords_.push_back(vec4<float>(  1.0f , 0.0f,0.0f,1.0f));
Face s;
s.uIndex[0] = 0; s.uIndex[1] = 1; s.uIndex[2] = 3;
pGeo->vFaces.push_back(s);
s.uIndex[0] = 3; s.uIndex[1] = 2; s.uIndex[2] = 1;
pGeo->vFaces.push_back(s);
uiDrawFacesStartPos = 0;
uiDrawFacesEndPos = pGeo->vFaces.size();
//Draw
... Get/Set the attributes
unsigned int *ptr = &pGeo->vFaces[ uiDrawFacesStartPos ].uIndex[ 0 ];
unsigned int  sz = (unsigned int)( uiDrawFacesEndPos - uiDrawFacesStartPos ) *3;
glDrawElements( GL_TRIANGLES, sz, GL_UNSIGNED_INT, ptr );


The shader is very basic, just use the varying textCoord in the function texture2D

diffuse = texture2D( DiffuseColor, vOutTxCoords.xy ).rgb;

tnx

### #17szecs  Members

Posted 23 May 2012 - 11:58 PM

Well, you haven't looked at the source code, or you haven't studied it in any depth.

The code clearly suggests (without reading the article) that the texture coordinates depend on the vertex coordinates. You just use independent values for the coordinates. I don't really understand why can't you just copy the coordinates and calculations from the source code (I know it's not a good way to learn, but et least you had something that you can experiment with).

Edited by szecs, 24 May 2012 - 12:01 AM.

### #18masterbubu  Members

Posted 24 May 2012 - 12:44 AM

// Setup
// Globals
float top = .3;
float scale_texcoord = 1.f;

vTxCoords_.push_back(vec4<float>(  -1.0f , -1.0f ,0.0f,1.0f));
vTxCoords_.push_back(vec4<float>(  -1.0f ,  1.0f ,0.0f,1.0f));
vTxCoords_.push_back(vec4<float>(   1.0f ,  1.0f ,0.0f,1.0f));
vTxCoords_.push_back(vec4<float>(   1.0f , -1.0f ,0.0f,1.0f));
SetPoints( vec3<float>( -1.0f , -1.0f, 0.0f), // BottomLeft,
vec3<float>( -1.0f ,  1.0f, 0.0f), // TopLeft,
vec3<float>(  1.0f ,  1.0f, 0.0f), // TopRight,
vec3<float>(  1.0f , -1.0f, 0.0f) );// BottomRight
Face s;
s.uIndex[0] = 0; s.uIndex[1] = 1; s.uIndex[2] = 3;
pGeo->vFaces.push_back(s);
s.uIndex[0] = 3; s.uIndex[1] = 2; s.uIndex[2] = 1;
pGeo->vFaces.push_back(s);
uiDrawFacesStartPos = 0;
uiDrawFacesEndPos = pGeo->vFaces.size();

//Draw
... Get/Set the attributes
unsigned int *ptr = &pGeo->vFaces[ uiDrawFacesStartPos ].uIndex[ 0 ];
unsigned int  sz = (unsigned int)( uiDrawFacesEndPos - uiDrawFacesStartPos ) *3;
glDrawElements( GL_TRIANGLES, sz, GL_UNSIGNED_INT, ptr );

//  Update Func
unsigned char keys[256];
GetKeyboardState( keys );
if( keys['1'] & 0x80 )
{
top += .05;
}
if( keys['2'] & 0x80 )
{
top -= .05;
}
float tx = scale_texcoord * top;
// Frist Triangle
pGeo->vGSVerts[0] = vec3<f32>(-1.0f , -1.0f, 0.0f);
pGeo->vGSVerts[1] = vec3<f32>(-top,1,0);
pGeo->vGSVerts[2] = vec3<f32>( top,1,0);
pGeo->vGSVerts[3] = vec3<f32>(1.0f , -1.0f, 0.0f);

vTxCoords_[1].x = -tx;
vTxCoords_[1].y = tx;
vTxCoords_[1].z = 0;
vTxCoords_[1].w = tx;
vTxCoords_[2].x = tx;
vTxCoords_[2].y = tx;
vTxCoords_[2].z = 0;
vTxCoords_[2].w = tx;


the above code does exactly the same as the tutorial.

you can see on the attachment the result I got.

I have noticed that GL_PERSPECTIVE_CORRECTION_HINT is not available at ES.

maybe its the way I'm drawing may tri's ?

### #19szecs  Members

Posted 24 May 2012 - 01:11 AM

Hmmmm. Is your texture a 8x8 checked texture?
If you, we should see a 16x16 checked texture stretched onto the trapezoid. But the texture is totally off, but the coordinates you specify look okay.

I don't see the problem but I don't think GL_PERSPECTIVE_CORRECTION_HINT has to do anything with that.

### #20masterbubu  Members

Posted 24 May 2012 - 01:18 AM

Yes, it was 16x16.

I replaced it to 8x8.

hmmm... all looks the same ( beside the GL_QUAD thing ), I'm very lost in here

#### Attached Thumbnails

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.