gpt4 book ai didi

java - 如何单独纹理化(使用不同的纹理文件)3D 模型?

转载 作者:太空宇宙 更新时间:2023-11-04 14:31:45 25 4
gpt4 key购买 nike

我有一个在 Blender 中制作的 3D 文件并导出到 java(.OBJ 文件),该文件将其纹理分成了一些文件。在这个 .obj 文件中,它有一些名为 USEMTL 的字段及其各部分的名称(纹理文件)。但是,当我在屏幕上绘制它时,他只显示最后一个 USEMTL 名称。我的问题是:我怎样才能让他以正确的方式阅读和纹理化?不研磨其他纹理?

这是具有 .obj 加载器的类

    public MLoader(String path, Model m) throws IOException {
@SuppressWarnings("resource")
BufferedReader reader = new BufferedReader(new FileReader(new File(path)));
String line;

while ((line = reader.readLine()) != null) {
if (line.startsWith("v ")) {
float
v1 = Float.valueOf(line.split(" ")[1]),
v2 = Float.valueOf(line.split(" ")[2]),
v3 = Float.valueOf(line.split(" ")[3]);

Vector3f v = new Vector3f (v1, v2, v3);

m.vertex.add(v);
} if (line.startsWith("usemtl ")){
String name = String.valueOf(line.split(" ")[1]);
m.nameTexture.add(name);
continue;
} if (line.startsWith("f ")) {
float
v1 = Float.valueOf(line.split(" ")[1].split("/")[0]),
v2 = Float.valueOf(line.split(" ")[2].split("/")[0]),
v3 = Float.valueOf(line.split(" ")[3].split("/")[0]),

n1 = Float.valueOf(line.split(" ")[1].split("/")[1]),
n2 = Float.valueOf(line.split(" ")[2].split("/")[1]),
n3 = Float.valueOf(line.split(" ")[3].split("/")[1]);

Vector3f
v = new Vector3f (v1, v2, v3),
n = new Vector3f (n1, n2, n3);

m.face.add(new Faces(v, n));
}if (line.startsWith("vt ")) {
float
vt1 = Float.valueOf(line.split(" ")[1]),
vt2 = Float.valueOf(line.split(" ")[2]);

Vector2f vt = new Vector2f (vt1, vt2);

m.vertexTexture.add(vt);
}
}
}

如您所见,我创建了一个 IF 语句,只是为了获取 usemtl 内容(位于 .obj 文件内)- 纹理名称(位于单独的文件中),只是为了看看是否可以单独绑定(bind)它们。但我很难做到这一点(也许,逻辑不在我这边)。如何进行?

其他类:

纹理类别

public class Textures {
public Texture[] tx;

public void setNumTex(int i) {
tx = new Texture[i];
}

public void setTexture(String format, String name, int i) {
try {
tx[i] = TextureLoader.getTexture(format, new FileInputStream(new File("res/Textures/" + name + format)));
} catch (IOException e) {
e.printStackTrace();
}
}

public void texturing(Vector2f ft1, Vector2f ft2, int indexx) {
for (int i=0; i<indexx; i++) {
tx[i].bind();
}
glTexCoord2f(ft1.x, ft1.y);
glTexCoord2f(ft2.x, ft2.y);
}
}

渲染类

public class Renderer {
Model m;

public void loadContent(String objPath) {
//Everithing that is loading is been putting here
m = Model.getModel("res/Models/" + objPath);

m.loadTex();
}

public void render() {
glEnable(GL_SMOOTH);

m.renderModel();
}
}

模型类

public class Model {
Textures tx;

List<Vector3f> vertex, norms;
List<Vector2f> vertexTexture;
List<Faces> face;
List<String> nameTexture;
String name[];

private int numTex = 0;

Vector2f
t1 = new Vector2f(), t2 = new Vector2f();

Vector3f
v1 = new Vector3f(), v2 = new Vector3f(), v3 = new Vector3f(),
n1 = new Vector3f(), n2 = new Vector3f(), n3 = new Vector3f(),

public Model(String path)throws
LWJGLException, FileNotFoundException, IOException {
vertex = new ArrayList<Vector3f>();
norms = new ArrayList<Vector3f>();
vertexTexture = new ArrayList<Vector2f>();
face = new ArrayList<Faces>();
nameTexture = new ArrayList<String>();

tx = new Textures();

new MLoader(path, this);
}

public static Model getModel(String path, Vector3f position, Vector3f rotation) {
try {
return new Model(path, position, rotation);
} catch (LWJGLException | IOException e) {
e.printStackTrace();
}
return null;
}

public void loadTex() {
name = new String[nameTexture.toArray().length];
tx.setNumTex(nameTexture.toArray().length);

for (int i=0; i<name.length; i++) {
name[i] = nameTexture.get(i);
numTex += 1;

tx.setTexture(".TGA", name[i], i);
}
}

public void renderModel() {
glPushMatrix();
glPolygonMode(GL_FRONT_AND_BACK, GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glBegin(GL_TRIANGLES);
{
for (Faces f : face) {
v1 = vertex.get((int) f.v.x - 1);
v2 = vertex.get((int) f.v.y - 1);
v3 = vertex.get((int) f.v.z - 1);
n1 = vertex.get((int) f.n.x - 1);
n2 = vertex.get((int) f.n.y - 1);
n3 = vertex.get((int) f.n.z - 1);
t1 = vertexTexture.get((int) f.n.x - 1); //
t2 = vertexTexture.get((int) f.n.y - 1); //

//Vertexes
glVertex3f(v1.x, v1.y-3.4f, v1.z-0.53f);
glVertex3f(v2.x, v2.y-3.4f, v2.z-0.53f);
glVertex3f(v3.x, v3.y-3.4f, v3.z-0.53f);

//Normals
glNormal3f(n1.x, n1.y-3.4f, n1.z-0.53f);
glNormal3f(n2.x, n2.y-3.4f, n2.z-0.53f);
glNormal3f(n3.x, n3.y-3.4f, n3.z-0.53f);

//Texture
tx.texturing(t1, t2, numTex);
//tx.texturing(n1, n2, n3);
}
}
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
}

最佳答案

我不会放置任何代码,因为您的问题对于 2 行解决方案来说非常复杂。但是这里它通常在简单的 3D 引擎中工作。网格的多重纹理意味着网格被分成 2 个或更多子网格-meshes。我不是 blender 专家,但我确信它能够像 Autodesk 3D studio max 一样导出带有子网格树的网格。现在,当您将这样的网格导入到程序中时,您应该能够解析所有网格这些子网格体分成单独的实体。每个这样的子网格体都有自己的顶点、纹理坐标和法线。发出绘制调用时,您将逐一迭代所有子网格,并在其自己的绘制调用中绘制每个子网格。因此,在每个绘制调用中,您还应该能够附加用于该特定的唯一纹理子网格。仅此而已。现在由您决定如何设计这样的系统。希望对您有所帮助。

关于java - 如何单独纹理化(使用不同的纹理文件)3D 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26066806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com