Sign in to follow this  
Followers 0
masterbubu

OpenGL
Trapezoid uvs preservation

22 posts in this topic

Hi All,

I am trying to create [url="http://www.xyzw.us/~cass/qcoord/"]this [/url]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 :)
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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 "[background=rgb(247, 247, 247)][font="verdana, helvetica, sans-serif"][size="2"][color="#000000"]Direct Linear Transform" but I'm not sure it is possible for a quad that built up from 2 triangles.[/color][/size][/font][/background]
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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 ? )
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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. Edited by masterbubu
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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 :(
0

Share this post


Link to post
Share on other sites
Show the complete code which has the modifications I suggested.
0

Share this post


Link to post
Share on other sites
[CODE]
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));
[/CODE]

I changed the r component, to 0.
0

Share this post


Link to post
Share on other sites
That's not the complete code. You must draw your vertex array somewhere at least.
0

Share this post


Link to post
Share on other sites
[CODE]
//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 );
[/CODE]

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

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

if more information is needed, please update me.

tnx :)
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
Well I already tried that:

[CODE]
// 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;
[/CODE]

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 ?
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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 [img]http://public.gamedev.net//public/style_emoticons/default/wacko.png[/img]
0

Share this post


Link to post
Share on other sites
This turned out to be a debugging question.

I'd try to only control the z and w texture coordinates (with the keyboard, shit I can't speak English today) to see they have any effect at all.
And I'd use a simpler to observe, non symmetric texture too, to see if it's flipped or upside down, etc. This may also be helpful to find the bug. Edited by szecs
0

Share this post


Link to post
Share on other sites
You are still only posting small fragments of your your code and there are still important things still missing. But since you at least posted some images with some additional clues (good observation szecs) that one half of the quad is only textured with half the texture, I get a feeling that you're not specifying four-component texture coordinates. The result seems consistent with your texture coordinates if you only take the first two components of them.
0

Share this post


Link to post
Share on other sites
Hi, sorry for the late response.

Anyway, my boss decided to drop this thing, so I did not had the time to continue investigating.

tnx you all for the help.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • By recp
      Hi,
      I'm working on new asset importer (https://github.com/recp/assetkit) based on COLLADA specs, the question is not about COLLADA directly
      also I'm working on a new renderer to render (https://github.com/recp/libgk) imported document.
      In the future I'll spend more time on this renderer of course, currently rendering imported (implemented parts) is enough for me
      assetkit imports COLLADA document (it will support glTF too),
      importing scene, geometries, effects/materials, 2d textures and rendering them seems working
      My actual confusion is about shaders. COLLADA has COMMON profile and GLSL... profiles,
      GLSL profile provides shaders for effects so I don't need to wory about them just compile, link, group them before render

      The problem occours in COMMON profile because I need to write shaders,
      Actually I wrote them for basic matrials and another version for 2d texture
      I would like to create multiple program but I am not sure how to split this this shader into smaller ones,

      Basic material version (only colors):
      https://github.com/recp/libgk/blob/master/src/default/shader/gk_default.frag
      Texture version:
      https://gist.github.com/recp/b0368c74c35d9d6912f524624bfbf5a3
      I used subroutines to bind materials, actually I liked it,
      In scene graph every node can have different program, and it switches between them if parentNode->program != node->program
      (I'll do scene graph optimizations e.g.  view frustum culling, grouping shaders... later)

      I'm going to implement transparency but I'm considering to create separate shaders,
      because default shader is going to be branching hell
      I can't generate shader for every node because I don't know how many node can be exist, there is no limit.
      I don't know how to write a good uber-shader for different cases:

      Here material struct:
      struct Material { ColorOrTexture emission; ColorOrTexture ambient; ColorOrTexture specular; ColorOrTexture reflective; ColorOrTexture transparent; ColorOrTexture diffuse; float shininess; float reflectivEyety; float transparency; float indexOfRefraction; }; ColorOrTexture could be color or 2d texture, if there would be single colorOrTex then I could split into two programs,
      Also I'm going to implement transparency, I am not sure how many program that I needed

      I'm considering to maintain a few default shaders for COMMON profile,
      1-no-texture, 2-one of colorOrTexture contains texture, 3-........

      Any advices in general or about how to optimize/split (if I need) these shaders which I provied as link?
      What do you think the shaders I wrote, I would like to write them without branching if posible,
      I hope I don't need to write 50+ or 100+ shaders, and 100+ default programs

      PS: These default shaders should render any document, they are not specific, they are general purpose...
             I'm compiling and linking default shaders when app launched

      Thanks
    • By CircleOfLight97
      Hi guys,
      I would like to contribute to a game project as a developer (open source possibly). I have some experiences in C/C++ in game development (perso projects). I don't know either unreal or unity but I have some knowledges in opengl, glsl and shading theory as I had some courses at university regarding to that. I have some knowledges in maths and basic in physics. I know a little how to use blender to do modelling, texturing and simple game assets (no characters, no animation no skinning/rigging). I have no game preferences but I like aventure game, dungeon crawler, platformers, randomly generated things. I know these kind of projects involve a lot of time and I'd be really to work on it but if there are no cleary defined specific design goals/stories/gameplay mechanics I would like to not be part of it x) and I would rather prefer a smaller but well defined project to work on that a huge and not 'finishable' one.
      CircleOfLight97
    • By gamesthatcouldbeworse
      Hi, I finally released KILL COMMANDO on gamejolt for free. It is a retro-funsplatter-shooter with C64 style. Give it a try.
    • By phil67rpg

      void TimerFunction(int value) {  glutPostRedisplay();  glutTimerFunc(1000, TimerFunction, 1); } void drawScene() {  glClear(GL_COLOR_BUFFER_BIT);      drawScene_bug();  TimerFunction(1);  eraseScene_bug(); // drawScene_bug_two(); // eraseScene_bug_two(); drawScene_ship(); drawScene_bullet();  glutSwapBuffers(); }