View Full Version : Modifying Texture Coordinates to represent visible area
05-04-2012, 02:25 PM
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  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.
PS I miss GLSLEditorSample. :sorrow: Best Mac replacement? OpenGL Shader Builder is crap by comparison. (Or does anyone have a Lion GLSLEditorSample version?)
05-06-2012, 10:12 AM
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?
05-06-2012, 11:11 AM
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 ?
05-06-2012, 11:26 AM
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).
05-06-2012, 11:47 AM
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.
05-06-2012, 10:21 PM
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,
Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.