Skip to Content

OBJLoader

This is a WIP project - Expect breaking changes to occur.

Overview

OBJLoader is responsible for loading .obj files and creating a Mesh instance from the data.

Textures are handled by the TextureLoader class, which is used by the OBJLoader to load textures specified in the .obj file.

We currently do not support .mtl files. This means that we do not support materials, and all textures will be loaded via atlases. This may change in future, please open an issue if you want to see this feature implemented.

OBJ features supported:

  • v - Vertex positions
  • vt - Texture coordinates
  • vn - Vertex normals
  • f - Faces (triangles only)

Each face must be in one of these formats:

  • v - Position only
  • v/vt - Position and UV
  • v/vt/vn - Position, UV and normal
  • v//vn - Position and normal, no UV

Example usage:

Mesh mesh = OBJLoader.load("/models/sword.obj"); mesh.create();

load(String resourcePath)

public static Mesh load(String resourcePath) {}

Loads an OBJ file from the classpath and returns a Mesh ready to be uploaded to the GPU via Mesh#create().

  • The loader de-duplicates vertices: if two face corners share the same position/UV/normal
    combination they will reuse the same index, keeping the index buffer as small as possible.

  • Vertex color defaults to white (1, 1, 1) for all vertices loaded
    from OBJ, since the format does not carry per-vertex color.

  • You can tint the object via the shader or swap the color buffer after loading if needed.

parseFace(...)

private static void parseFace(String[] tokens, List<Vector3f> positions, List<Vector2f> uvs, List<Vector3f> normals, List<Vertex> vertexList, List<Integer> indexList, Map<String, Integer> cache) {}

Parses a single f line, resolving each corner into a Vertex and appending the resulting triangle indices to indexList.

Only triangulated faces (exactly 3 corners) are supported. Export with “Triangulate Faces” enabled in Blender or equivalent.

parseVec3()

private static Vector3f parseVec3(String[] tokens) {}

Parses a v or vn line into a Vector3f. Expects exactly 3 components, returns each token as a float.

parseVec2()

private static Vector2f parseVec2(String[] tokens) {}

Parses a vt line into a Vector2f. Split tokens, where [1], [2] are [u], [v]. Returns the parsed UV coordinates.

Last updated on