Hello Guest

Texturing problems with Blender .obj models

  • 2 Replies
  • 3610 Views
Texturing problems with Blender .obj models
« on: February 22, 2018, 16:41:33 »
Hello everyone, I'm a young developer, it's the first time I work with a 3d project;
Following a youtube tutorial I wrote an OBJLoader, but with the model I made with blender the textures are all misplaced;
I have another model, a stall that loads perfectly, I can't understand if the problem are the models or the program.
Code: [Select]
public static RawModel loadOBJModel(String filename,Loader loader)
{
FileReader fr=null;
try {
fr=new FileReader(new File("src/sources/"+filename+".obj"));
} catch (FileNotFoundException e) {
System.err.println("COuld not read");
e.printStackTrace();
}
BufferedReader reader=new BufferedReader(fr);
String line;
List<Vector3f> vertices=new ArrayList<Vector3f>();
List<Vector2f> texture=new ArrayList<Vector2f>();
List<Vector3f> normals=new ArrayList<Vector3f>();
List<Integer> indices=new ArrayList<Integer>();

float[] vertexArray=null;
float[] texturesArray=null;
float[] normalsArray=null;
int[] indicesArray=null;
try {
while(true)
{

line=reader.readLine();
String[] currentLine=line.split(" ");
if(line.startsWith("v "))
{
Vector3f vector=new Vector3f(Float.parseFloat(currentLine[1]),Float.parseFloat(currentLine[2]),Float.parseFloat(currentLine[3]));
vertices.add(vector);
}
else if(line.startsWith("vt "))
{
Vector2f tex=new Vector2f(Float.parseFloat(currentLine[1]),Float.parseFloat(currentLine[2]));
texture.add(tex);
}
else if(line.startsWith("vn "))
{
Vector3f normal=new Vector3f(Float.parseFloat(currentLine[1]),Float.parseFloat(currentLine[2]),Float.parseFloat(currentLine[3]));
normals.add(normal);
}
else if(line.startsWith("f "))
{
texturesArray=new float[vertices.size()*2];
normalsArray=new float[vertices.size()*3];
break;
}
}

while(line!=null)
{
if(!line.startsWith("f "))
{
line=reader.readLine();
continue;
}

String[] currentLine=line.split(" ");
String[] vertex1=currentLine[1].split("/");
String[] vertex2=currentLine[2].split("/");
String[] vertex3=currentLine[3].split("/");

processVertex(vertex1,indices,texture,normals,texturesArray,normalsArray);
processVertex(vertex2,indices,texture,normals,texturesArray,normalsArray);
processVertex(vertex3,indices,texture,normals,texturesArray,normalsArray);
line=reader.readLine();
}
reader.close();

}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

    vertexArray=new float[vertices.size()*3];
    indicesArray=new int[indices.size()];
   
    int vertexPointer=0;
   
    for(Vector3f vector:vertices)
    {
    vertexArray[vertexPointer++]=vector.x;
    vertexArray[vertexPointer++]=vector.y;
    vertexArray[vertexPointer++]=vector.z;
    }
    for(int i=0;i<indices.size();i++)
    {
    indicesArray[i]=indices.get(i);
    }
   
    RawModel model=loader.loadToVAO(vertexArray,texturesArray,normalsArray, indicesArray);
    return model;

}

public static void processVertex(String[] vertexData,List<Integer> indices,List<Vector2f> tex,
List<Vector3f> normals,float[] textureArray,float[] normalsArray)
{
int currentVertexPointer=Integer.parseInt(vertexData[0])-1;

indices.add(currentVertexPointer);
Vector2f currentTex=tex.get(Integer.parseInt(vertexData[1])-1);
textureArray[currentVertexPointer*2]=currentTex.x;
textureArray[currentVertexPointer*2+1]=1-currentTex.y;
Vector3f currentNorm=normals.get(Integer.parseInt(vertexData[2])-1);
normalsArray[currentVertexPointer*3]=currentNorm.x;
normalsArray[currentVertexPointer*3+1]=currentNorm.y;
normalsArray[currentVertexPointer*3+2]=currentNorm.z;

}
This is the code, can anyone help me?

Re: Texturing problems with Blender .obj models
« Reply #1 on: February 25, 2018, 22:04:47 »
I resolved my problem
To help someone with my same problem:
I used edge split in blender and splitted sharp edges after unwrapping the model

Re: Texturing problems with Blender .obj models
« Reply #2 on: July 22, 2018, 23:00:28 »
Hi I'm glad you solved your issue. I'm also having the same problem. Could you elaborate on what you did in blender?