使用WIDTH和HEIGHT或使用(WIDTH - 1)和(HEIGHT - 1)进行位移映射?

时间:2016-04-16 12:03:12

标签: opengl webgl texture-mapping

我会向第{4}页http://page.mi.fu-berlin.de/block/htw-lehre/wise2012_2013/bel_und_rend/skripte/szirmay2006.pdf提出问题。

而不是dhdu和dhdv中的WIDTH和HEIGHT:

float2 du=float2(1/WIDTH, 0);
float2 dv=float2(0, 1/HEIGHT);
float dhdu = SCALE/(2/WIDTH) * (tex2D(hMap, uv+du).a - tex2D(hMap, uv-du).a);
float dhdv = SCALE/(2/HEIGHT) * (tex2D(hMap, uv+dv).a - tex2D(hMap, uv-dv).a);
// get model space normal vector
float3 mNormal = normalize(N+t*dhdu+b*dhdv);

我在我的小javascript测试程序中使用(WIDTH - 1)和(HEIGHT - 1)dhdu和dhdv:

float2 du=float2(1/WIDTH, 0);
float2 dv=float2(0, 1/HEIGHT);
float dhdu = SCALE/(2/(WIDTH - 1)) * (tex2D(hMap, uv+du).a - tex2D(hMap, uv-du).a);
float dhdv = SCALE/(2/(HEIGHT - 1)) * (tex2D(hMap, uv+dv).a - tex2D(hMap, uv-dv).a);
// get model space normal vector
float3 mNormal = normalize(N+t*dhdu+b*dhdv);

否则我会得到错误的结果。我将结果与:

进行比较
displacedP0 = displace(p0, normal, height0)
displacedP1 = displace(p1, normal, height1)
displacedP2 = displace(p2, normal, height2)
v0 = displacedP1.minus(displacedP0);
v1 = displacedP2.minus(displacedP0);
displacedSurfaceNormal = v0.cross(v1).normalize();

我使用一个简单的数组进行测试,而不是纹理。

//  v
//    +-------+-------+-------+-------+-------+-------+-------+-------+
//  3 |       |  255  |  170  |       |       |       |       |       |
//    +-------+-------+-------+-------+-------+-------+-------+-------+
//  2 |  255  |h0=170 | h2=85 |   0   |       |       |       |       |
//    +-------+-------+-------+-------+-------+-------+-------+-------+
//  1 |  170  | h1=85 |   0   |   0   |       |       |       |       |
//    +-------+-------+-------+-------+-------+-------+-------+-------+
//  0 |       |   0   |   0   |       |       |       |       |       |
//    +-------+-------+-------+-------+-------+-------+-------+-------+
//        0       1       2       3       4       5       6       7      u
let texture = [];
texture[0] = [];
texture[1] = [];
texture[2] = [];
texture[3] = [];

texture[0][0] = 'x';
texture[1][0] = 0;
texture[2][0] = 0;
texture[3][0] = 'x';

texture[0][1] = 170 / 255;
texture[1][1] = 85 / 255;
texture[2][1] = 0;
texture[3][1] = 0;

texture[0][2] = 255 / 255;
texture[1][2] = 170 / 255;
texture[2][2] = 85 / 255;
texture[3][2] = 0;

texture[0][3] = 'x';
texture[1][3] = 255 / 255;
texture[2][3] = 170 / 255;
texture[3][3] = 'x';

纹理的大小:

let WIDTH = 8;
let HEIGHT = 4;

这里我使用(WDITH - 1)和(HEIGHT - 1)来定义顶点p0,p1和p2的u坐标和v坐标:

let p0 = new Vertex(0, 0, 0);
p0.texture(1 / (WIDTH - 1), 2 / (HEIGHT - 1));

let p1 = new Vertex(0, 0, 1);
p1.texture(1 / (WIDTH - 1), 1 / (HEIGHT - 1));

let p2 = new Vertex(1, 0, 0);
p2.texture(2 / (WIDTH - 1), 2 / (HEIGHT - 1));

这是错的吗?这是原因,为什么我总是要编程-1?

我的纹理坐标系似乎没问题。如果u的范围是0到6,那么宽度是7.示例请参阅https://msdn.microsoft.com/en-us/library/windows/desktop/bb206245%28v=vs.85%29.aspx另请参阅http://bpeers.com/articles/glpixel/:"观察宽度为N的纹理,即纹素中心的X坐标我(我从0..N-1算起),将是1 /(2.N)+ i / N"。

enter image description here

这张图片是否正确?

0 个答案:

没有答案