So I successfully loading my obj file but I obviously want to get materials on it. I added the vt(texture) to the OBJLoader and the Model.render but still rendering textureless. I have exported the file from Blender with UV unwrap so the materials and vt are appearing in the obj file. Do I need to do something with the mtl file? if so, how so?
Model.java
import static org.lwjgl.opengl.GL11.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.lwjgl.util.vector.*;
public class Model {
public List<Vector3f> vertices;
public List<Vector3f> normals;
public List<Face> faces;
public List<Vector2f> texcoords;
public static Model getModel(String s) throws IOException{
return new Model(s);
}
private Model(String path) throws IOException {
vertices = new ArrayList<Vector3f>();
normals = new ArrayList<Vector3f>();
faces = new ArrayList<Face>();
texcoords = new ArrayList<Vector2f>();
new OBJLoader(this, path);
}
public void render(){
glTranslatef(0, 0, 0);
glBegin(GL_TRIANGLES);
for (Face face : faces) {
Vector3f n1 = normals.get((int) face.normal.x -1);
Vector2f t1 = texcoords.get((int) face.texcoord.x -1);
Vector3f v1 = vertices.get((int) face.vertex.x -1);
Vector3f n2 = normals.get((int) face.normal.y -1);
Vector2f t2 = texcoords.get((int) face.texcoord.y -1);
Vector3f v2 = vertices.get((int) face.vertex.y -1);
Vector3f n3 = normals.get((int) face.normal.z -1);
Vector2f t3 = texcoords.get((int) face.texcoord.z -1);
Vector3f v3 = vertices.get((int) face.vertex.z -1);
glVertex3f(v1.x, v1.y, v1.z);
glTexCoord2f(t1.x, t1.y);
glNormal3f(n1.x, n1.y, n1.z);
glVertex3f(v2.x, v2.y, v2.z);
glTexCoord2f(t2.x, t2.y);
glNormal3f(n2.x, n2.y, n2.z);
glVertex3f(v3.x, v3.y, v3.z);
glTexCoord2f(t3.x, t3.y);
glNormal3f(n3.x, n3.y, n3.z);
}
glEnd();
}
}
OBJLoader.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.lwjgl.util.vector.*;
public class OBJLoader {
public OBJLoader(Model m, String path) throws FileNotFoundException, IOException {
BufferedReader read = new BufferedReader(new FileReader(new File(path)));
String line;
while((line = read.readLine()) != null) {
if (line.startsWith("v ")){
float x = Float.valueOf(line.split(" ")[1]);
float y = Float.valueOf(line.split(" ")[2]);
float z = Float.valueOf(line.split(" ")[3]);
m.vertices.add(new Vector3f(x,y,z));
}
if (line.startsWith("vn ")) {
float x = Float.valueOf(line.split(" ")[1]);
float y = Float.valueOf(line.split(" ")[2]);
float z = Float.valueOf(line.split(" ")[3]);
m.normals.add(new Vector3f(x,y,z));
}
if (line.startsWith("vt ")){
float x = Float.valueOf(line.split(" ")[1]);
float y = Float.valueOf(line.split(" ")[2]);
m.texcoords.add(new Vector2f(x,y));
}
if (line.startsWith("f ")) {
Vector3f vertexIndices = new Vector3f(
Float.valueOf(line.split(" ")[1].split("/")[0]),
Float.valueOf(line.split(" ")[2].split("/")[0]),
Float.valueOf(line.split(" ")[3].split("/")[0]));
Vector3f normalIndices = new Vector3f(
Float.valueOf(line.split(" ")[1].split("/")[2]),
Float.valueOf(line.split(" ")[2].split("/")[2]),
Float.valueOf(line.split(" ")[3].split("/")[2]));
Vector3f textureIndices = new Vector3f(
Float.valueOf(line.split(" ")[1].split("/")[1]),
Float.valueOf(line.split(" ")[2].split("/")[1]),
Float.valueOf(line.split(" ")[3].split("/")[1]));
m.faces.add(new Face(vertexIndices, normalIndices, textureIndices));
}
}
read.close();
}
}