问题描述
我正在尝试加载在LWJGL网上找到的枪的3D模型。我一直在关注它的教程,但似乎无法使其正常工作。该模型可以加载,但顶点和纹理不正确。
代码加载了.obj文件,然后它将针对每个顶点,纹理和法线坐标遍历文件中的每一行数据。但是我在其余方面遇到了麻烦。
OBJLoader文件:
import RenderEngine.Loader;
import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.util.vector.Vector3f;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class OBJLoader {
private static List<Vector3f> vertexdata = new ArrayList<Vector3f>();
private static List<Vector2f> texturedata = new ArrayList<Vector2f>();
private static List<Integer> indexdata = new ArrayList<Integer>();
private static List<Vector3f> realvertex = new ArrayList<Vector3f>();
private static float[] vertexArray = null;
private static float[] textureArray = null;
private static int[] indexArray = null;
private static float[] realvertexArray = null;
public static RawModel loadOBJFile(String filename,Loader loader){
FileReader fr = null;
try {
fr = new FileReader("res/Models/Gun/"+filename+".obj");
} catch (FileNotFoundException e) {
e.printStackTrace();
System.err.println("Couldn't load "+filename+".obj");
}
BufferedReader reader = new BufferedReader(fr);
try{
String line;
while(true){
line = reader.readLine();
if(line == null){
break;
}
String[] currentline = line.split(" ");
if(line.startsWith("v ")){
Vector3f vertex = new Vector3f(Float.parseFloat(currentline[1]),Float.parseFloat(currentline[2]),Float.parseFloat(currentline[3]));
vertexdata.add(vertex);
}else if(line.startsWith("vt ")){
Vector2f vertex = new Vector2f(Float.parseFloat(currentline[1]),Float.parseFloat(currentline[2]));
texturedata.add(vertex);
} else if(line.startsWith("vn ")){
Vector3f vertex = new Vector3f(Float.parseFloat(currentline[1]),Float.parseFloat(currentline[3]));
realvertex.add(vertex);
}else if(line.startsWith("f ")){
vertexArray = new float[vertexdata.size()*3];
textureArray = new float[texturedata.size()*2];
realvertexArray = new float[texturedata.size()*3];
break;
}
}
while(line != null){
try{
if(line.startsWith("f")){
String[] currentline = line.split(" ");
String[] rawdata1 = currentline[1].split("/");
String[] rawdata2 = currentline[2].split("/");
String[] rawdata3 = currentline[3].split("/");
processData(rawdata1);
processData(rawdata2);
processData(rawdata3);
}
}catch(Exception e){ }
line = reader.readLine();
}
reader.close();
}catch(Exception e){
System.err.println("Error while parsing OBJ data");
e.printStackTrace();
}
vertexArray = new float[vertexdata.size()*3];
indexArray = new int[indexdata.size()];
int vertexPointer = 0;
for(Vector3f pos: vertexdata){
vertexArray[vertexPointer++] = pos.x;
vertexArray[vertexPointer++] = pos.y;
vertexArray[vertexPointer++] = pos.z;
}
// int texturepointer = 0;
// for(Vector2f pos: texturedata){
// textureArray[texturepointer++] = pos.x;
// textureArray[texturepointer++] = pos.y;
// }
for(int i = 0;i < indexdata.size();i++){
indexArray[i] = indexdata.get(i);
}
System.out.println("VERTEX: "+Arrays.toString(vertexArray));
System.out.println("TEXTURE: "+Arrays.toString(textureArray));
System.out.println("NORMAL: "+realvertex);
// System.out.println("INDEX: "+Arrays.toString(indexArray));
return loader.loadToVAO(vertexArray,textureArray,indexArray);
}
private static void processData(String[] vertexData){
int currentVertexPointer = Integer.parseInt(vertexData[0]);
indexdata.add(currentVertexPointer);
Vector2f currentTexture = texturedata.get(Integer.parseInt(vertexData[1]) - 1);
textureArray[currentVertexPointer*2] = currentTexture.x;
textureArray[currentVertexPointer*2+1] = currentTexture.y;
Vector3f currentNorm = realvertex.get(Integer.parseInt(vertexData[2]) - 1);
realvertexArray[currentVertexPointer*3] = currentNorm.x;
realvertexArray[currentVertexPointer*3+1] = currentNorm.y;
realvertexArray[currentVertexPointer*3+2] = currentNorm.z;
}
}
The result of the code is here
The tutorial i followed is here
**编辑:我已经设法修复了顶点,这只是我现在需要帮助的纹理坐标
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)