简体   繁体   English

从2D纹理上的位置(x,y)计算.obj文件中的vt(u,v)坐标

[英]Calculating vt (u,v) coordinates in .obj file from position (x,y) on 2D texture

I'm writing a C++ algorithm that returns an X,Y position on a 2D texture. 我正在编写一种C ++算法,该算法在2D纹理上返回X,Y位置。 Using the X,Y value I wish to find the u,v texture coordinates of a 3D object (already mapped in software). 我希望使用X,Y值找到3D对象(已在软件中映射)的u,v纹理坐标。

I have these calculations: 我有这些计算:

u = X/texture_width
v = texture_height - Y/texture_height

However the values calculated can not be found under vt in my obj file. 但是,在我的obj文件的vt下找不到计算的值。

Help would be appreciated, many thanks. 帮助将不胜感激,非常感谢。

Assuming that your (u,v) coordinates are supposed to be within the range [0,1] x [0,1], your computation is not quite right. 假设您的(u,v)坐标应在[0,1] x [0,1]范围内,则您的计算不太正确。 It should be 它应该是

u = X/texture_width
v = 1 - Y/texture_height

Given an image pixel coordinate (X,Y), this will compute the corresponding texture (u,v) coordinate. 给定图像像素坐标(X,Y),这将计算相应的纹理(u,v)坐标。 However, if you pick a random image pixel and convert its (X,Y) coordinate into a (u,v) coordinate, this coordinate will most likely not show up in the list of vt entries in the OBJ file. 但是,如果选择一个随机图像像素并将其(X,Y)坐标转换为(u,v)坐标,则该坐标很可能不会出现在OBJ文件的vt条目列表中。

The reason is that (u,v) coordinates in the OBJ file are only specified at the corners of the faces of your 3D object. 原因是OBJ文件中的(u,v)坐标仅在3D对象的面指定。 The coordinates that you compute from image pixels likely lie in the interior of the faces. 您根据图像像素计算出的坐标可能位于人脸内部

Assuming your OBJ file represents a triangle mesh with positions and texture coordinates, the entries for faces will look something like this: 假设您的OBJ文件代表具有位置和纹理坐标的三角形网格,则面的条目将如下所示:

f p1/t1 p2/t2 p3/t3

where p1 , p2 , p3 are position indices and t1 , t2 , t3 are texture coordinate indices. 其中p1p2p3是位置索引, t1t2t3是纹理坐标索引。

To find whether your computed (u,v) coordinate maps to a given triangle, you'll need to 要查找您计算的(u,v)坐标是否映射到给定的三角形,您需要

  • find the texture coordinates (u1,v1), (u2,v2), (u3,v3) of the corners by looking up the vt entries with the indices t1 , t2 , t3 , 通过查找索引为t1t2t3vt条目,找到角的纹理坐标(u1,v1),(u2,v2),(u3,v3),
  • find out whether the point (u,v) lies inside the triangle with corners (u1,v1), (u2,v2), (u3,v3). 找出点(u,v)是否位于带有角(u1,v1),(u2,v2),(u3,v3)的三角形内。 There are several ways to compute this . 有几种计算方法

If you repeat this check for all f entries of the OBJ file, you'll find the triangle(s) which the given image pixel maps to. 如果对OBJ文件的所有f个条目重复此检查,您将找到给定图像像素映射到的三角形。 If you don't find any matches then the pixel does not appear on the surface of the object. 如果找不到任何匹配项,则像素不会出现在对象的表面上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM