Code Monkey home page Code Monkey logo

obj-loader's Introduction

OBJ LOADER

The quick to use single header OBJ loader

Quick Description

OBJ Loader is a simple, header only, .obj model file loader that will take in a path to a file, load it into the Loader class object, then allow you to get the data from each mesh loaded. This will load each mesh within the model with the corresponding data such as vertices, indices, and material. Plus a large array of vertices, indices and materials which you can do whatever you want with.

Prerequisites

OBJ Loader was made with compatibility in mind. So you only need the header file to get going as it uses only STD and self made data structures.

Installation

All you need to to is copy over the OBJ_Loader.h header file, include it in your solution, and you are good to go. It is located within the source folder.

Examples

Examples are found within the examples folder. In order to compile these you will need to link OBJ_Loader.h to the compiler in since it is not included within the example folders.

Quick Use Guide

  1. Include OBJ_Loader.h: '#include "OBJ_Loader.h"'
  2. Create a Loader Object: 'objl::Loader loader'
  3. Load a File Into the Loader Object: 'loader.LoadFile("path_to_object_and_name.obj")'
  4. Get What you want out of the Mesh Objects: 'cout << loader.LoadedMeshes[0].MeshName << endl'

Classes & Structures

These are all of the included classes and only the relevant members and methods. There are others to find if you want but these are all you will need to know to operate.

Vector2

  1. float X, Y : Position Variables

Vector3

  1. float X, Y, Z : Position Variables

Vertex

  1. Vector3 Position : Position vector
  2. Vector3 Normal : Normal vector
  3. Vector2 TextureCoordinate : Texture Coordinate vector

Material

  1. std::string name : Name of loaded material
  2. Vector3 Ka : Ambient color
  3. Vector3 Kd : Diffuse color
  4. Vector3 Ks : Specular color
  5. float Ns : Specular exponent
  6. float Ni : Optical density
  7. float d : Dissolve variable
  8. int illum : Illumination variable
  9. std::string map_Ka : Ambient texture map name
  10. std::string map_Kd : Diffuse texture map name
  11. std::string map_Ks : Specular texture map name
  12. std::string map_d : Alpha texture map name
  13. std::string map_bump : Bump map name

Mesh

  1. std::string MeshName : The Mesh Name given in the .obj
  2. std::vector Vertices : Vertex List
  3. std::vector Indices : Index List
  4. Material MeshMaterial : Material assigned to this mesh

Loader

  1. bool LoadFile(std::string Path) : Load a file from a path. Return true if found and loaded. Return false if not
  2. std::vector LoadedMeshes : Loaded Mesh Objects
  3. std::vector LoadedVertices : Loaded Vertex Objects
  4. std::vector LoadedIndices : Loaded Index Positions
  5. std::vector LoadedMaterials : Loaded Material Objects

Credits

Robert Smith

License

OBJ Loader uses the MIT license. The MIT license allows free use and modification of the software as long as they provide credit back to me and don't hold me liable for anything that may go wrong. If you want to read the license in full look into the file entitled: LICENSE

Version Information

Current Version

Version 2.0

Upcoming Features

---- THIS IS NO LONGER BEING SUPPORTED ----

  1. Bug Fixing
  2. Adding more variables

Previous Versions

Version 1.0 (September-13-2016)

  1. Base Implementation Done
  2. README Created
  3. LICENSE Created
  4. Mesh Index and Vertex Load Implemented

Version 2.0 (September-14-2016)

  1. Now Reads Material Data

obj-loader's People

Contributors

bly7 avatar schtiefel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

obj-loader's Issues

Incorrect number of vertices.

When I print out the number of vertices in a mesh I get 4096, but the mesh only has 1089 vertices according to blender and that there are 1089 lines in the file starting with "v ". Both loader.LoadedVertices and loader.LoadedMeshes[0].Vertices have 4096 vertices. The loaded mesh works fine with OpenGL using a vertex array. It is hard to think that this would be a bug so here is a part of my code:
objl::Loader loader; bool error = loader.LoadFile(path); if (error == 0) { printf("Failed to load %s\n", path); return; } printf("Vertices.. %d\n", loader.LoadedMeshes[0].Vertices.size());
(I am new to github and I do not know how to make it put line spaces in the code!)
Also I am using the latest version of c++.

No support for grouping

There is no support for grouping:

  • faces - useful if we want to represent a group of triangles as a larger polygon face in 3d

  • vertices - useful if we want to represent a group of vertices as a polyline in 3d.

Grouping is supported by the .obj format. Please see here: http://paulbourke.net/dataformats/obj/

Missing include "math.h"

powf requires the math.h header. Currently fails to compile on my system (mingw64 g++ 6.2.0).

Parsing faces with a lot of vertices problem

I know this is not longer supported.
But this project is kinda nice and easy to use, so ill public this issue.

After playing with 3D models in OpenGL i have noticed the following problem:
Almost the whole model rendered fine, but rims are completely messed up. I have never had this problem before.
I have checked source of this model and found out that exclusively wheels has faces with more then 4 vertices.
I assume this is the issue, because model is rendered fine in other application.

The issue

Screenshot from 2023-10-29 15-46-02

Outlines

Screenshot from 2023-10-29 15-47-03

How it supposed to look like

Screenshot from 2023-10-29 15-48-22

Multiple materials per mesh

Hey,

Is it possible to use more than 1 material per mesh ? I checked the code and it seems that it supports only 1 per mesh, but i could be wrong.

problem in code or optimization issue ?

starting with line 269:

			float y = (math::DotV3(math::CrossV3(u, w), n) / math::DotV3(n, n));
			float b = (math::DotV3(math::CrossV3(u, w), n) / math::DotV3(n, n));
			float a = 1 - y - b;

now I'm no Vector specialist but im pretty sure that y and b are the same, which could be achived a little cheaper. :)

Im not actually quite sure what happend here but I think you meant to put v somewhere in there. also note that the division can be cheapend when using pow(Magnitude(n),2) instead of Dot(n,n)

Strange issue with floating point

It makes me crazy )

When I'm trying to print vertices it rounds them to int.

printf("%.6f\n", curMesh.Vertices[j].Position.Y);

Shows me

Loaded mesh Cube_Cube.0012.076665
-2,000000
-2,000000
2,000000
-2,000000

When I set breakpoint and start debuging it display normal output

Loaded mesh Cube_Cube.0012.076665
-2.076665
-2.076665
2.076665
-2.076665

WTF?? )

feature Request ? Make Vectors POD

In order to allow the Vertecies to be uploaded to the GPU without side-effects directly it would be beneficial to make Vector3 and Vector2 POD types.

eg.: instead of:

struct Vector2
{
[...]
		// Bool Equals Operator Overload
		bool operator==(const Vector2& other) const
		{
			return (this->X == other.X && this->Y == other.Y);
		}
[...]
     float x,y,z;
};

like this:

struct Vector2
{
     float x,y,z;
};

inline bool operator==(const Vector2& lhs,const Vector2& rhs)
{
	return (lhs.X == rhs.X && lhs.Y == rhs.Y);
}

Although a little more complicated, it allows the compiler to more efficiently pack the data and thus one could directly generate vertexArrays by using offsetof()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.