Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Modifying Texture Coordinates to represent visible area

  1. #1
    Intern Contributor
    Join Date
    May 2008
    Location
    USA
    Posts
    99

    Modifying Texture Coordinates to represent visible area

    I have a rectangular mesh, where the height ranges from +1 to -1, and the the sides are at +Aspect Ratio, - Aspect ratio. Texture coordinates [0] range from 0,0 to 1,1

    In my vertex shader, I multiply by the modelviewprojectionmatrix to make adjustments to vertices relative to the screen (warping). But I also would like to apply a transformation to the texture coordinates (actually make the 2nd set this value) so that 0,0 to 1,1 is the visible section of my mesh.

    I tried to multiply by the whole MVP or MV matrix, but it seems to push the coordinates so that I see nothing.

    Any tips?

    Thanks,

    Bruce

    PS I miss GLSLEditorSample. Best Mac replacement? OpenGL Shader Builder is crap by comparison. (Or does anyone have a Lion GLSLEditorSample version?)

  2. #2
    Intern Contributor
    Join Date
    May 2008
    Location
    USA
    Posts
    99
    A picture expresses it better. In the diagram below, it's clear that multiplying the vertices by the MVP matrix gives me the viewport coords. But what do I transform the tex-cords by so that 0,0, - 1,1 becomes x1,y1 - x2,y2?

    Bruce

    Click image for larger version. 

Name:	Tex Coord Q.jpg 
Views:	53 
Size:	42.0 KB 
ID:	709
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	Tex Coord Q.jpg 
Views:	52 
Size:	18.6 KB 
ID:	708  

  3. #3
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    11
    I I understand you request, you need to apply to the vertex the mvp + a bias to get the texture coordinates.
    textureCoord= bias * mvp * gl_Vertex;

    the bias matrix is a 4x4 matrix:
    bias = (
    0.5, 0.0, 0.0, 0.5,
    0.0, 0.5, 0.0, 0.5,
    0.0, 0.0, 0.5, 0.5,
    0.0, 0.0, 0.0, 0.0);

    Is it what you were expecting or was I completely wrong about your though ?

  4. #4
    Intern Contributor
    Join Date
    May 2008
    Location
    USA
    Posts
    99
    I'm not sure. I have the 0,0 - 1,1 tex coords: I had thought I was going to multiply those, but you're saying use the vertex and a bias? The bias being to deal with the vertices being -1 - +1 instead of 0 - 1?

    I guess I do have to insert aspect ratio in there, since the vertices are scaled differently horizontally (texture always 0-1, vertices depending on A/R).



    Bruce

  5. #5
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    11
    Yes but your aspect ratio is held by the projection matrix, thus held by the mvp matrix. By multiplying a vertex by the mvp you get coordinates such as the values in [-1, 1] are displayed on the screen (or buffer binded). If I understand well what you need, you want to apply over the screen your texture, thus by applying the bias you convert those coordinates in [0, 1], bounds understandable for a texture. Then in your fragment shader you use vec4 color = texture2D(yourTexture, coordTexture.st/coordTexture.w) to get the good pixel in your texture.

    If you need to apply the texture over a quad which is not the screen, I'm out, I'm still new with openGL and didn't tried this yet.

  6. #6
    Intern Contributor
    Join Date
    May 2008
    Location
    USA
    Posts
    99
    I tried a few different Matrix orders, and was getting close, but in the end I did mvp * vertex, then did a multiply by a vec4 bias and added an offset. Those numbers seemed arbitrary, like they were related to my camera > model distance, but I ended up with the right result.

    Thanks for helping,

    Bruce

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •