OBJ Loader for OpenGL + Glut - opengl

I'm working on a game for open GL, and I want to simply be able to load OBJ files, with their textures into my game. I want the loader to contain all the necessary code to map the textures to the object.
(Note I have scoured google, and not one example I've seen has been simple enough to bolt onto my currently existing code, I'm talking 2 headers, and 2 cpps maximum)
Could anybody provide me with one?

you could get started by this simple loader which loads objects in a wavefront .obj file into a OpenGL scene. This code restricts to just scanning the vertex information out of OBJ. You could enhance it further may be after getting the basic idea.
http://netization.blogspot.in/2014/10/loading-obj-files-in-opengl.html
https://github.com/nanosmooth/opengl_objloader

You can find mine at: https://github.com/NewbiZ/sandbox/tree/master/mar_tp1
It's a pretty simple one, but clean.
you will only need the Model class, which will load an OBJ file by filename: https://github.com/NewbiZ/sandbox/blob/master/mar_tp1/inc/mar_tp1/model.h
You may need to remove some things to make it fit on your project. Namely the dependency to ResourceManager which loads the textures and return their id, but that should not be much work.
Hope it helps.

Related

osgexport for Blender: Imported meshes impact scene ( possibly the lighting? )

Blender Version 2.79
OSG Version: 3.4.0-9
Operating System: Fedora
I have been using Blender's export utility to export obj files and then using osgconv to convert them to osg files. The files are then imported and rendered into a scene that looks like:
Image of the working scene before using the export tool
Today I installed osgexport by Cedric Pinson ( Github page: https://github.com/cedricpinson/osgexport ) to directly export from Blender to osgt files. I get the following results when I import those files and render them.
Image of the scene where everything goes dark and the lighting is wierd
Additional Details:
The code is set to follow the human character. The rest of the scene
is static.
When I use the old human model I get the working effect,
but the whole purpose of using the converter is to be able to export
its animations.
Any ideas? I saw the effect and I don't really even know where to start. The only difference is the outputted file from the converter. Everything else is the same. Also, if there is a newer/better way to export blender files into files that OSG can read, then I'm open to any and all suggestions.
Thank you in advance,
For what it's worth, the OSG forum/mailing list is usually pretty good about answering questions, and I believe I've seen Cedric's name there regularly, along with all the main contributors: http://forum.openscenegraph.org/
New users usually have to have their questions go through moderation, to cut down on spam, so just have some patience.
On this specific question, I have 3 suggestions:
Convert both files to osgt and run a diff. Ignore any numerical differences, look for things like node arrangement, material types, etc.
Try opening your exported model in osgviewer, just to see how the default settings display it. You can also easily play with things like backfaces, lighting settings, clipping planes, before dropping it into your application - press h to see all the run-time options.
Instantiate your light with all 3 types of lighting enabled. Particularly, I have found some models depend heavily on specular lighting, but some of the examples don't turn it on. Of course, if you use the full-white values shown here, you may oversaturate, but this is for an example:
osg::Light *light = new osg::Light;
light->setAmbient(osg::Vec4(1.0,1.0,1.0,1.0));
light->setDiffuse(osg::Vec4(1.0,1.0,1.0,1.0));
light->setSpecular(osg::Vec4(1.0,1.0,1.0,1.0)); // some examples don't have this one

How to convert .obj file to openGL?

I am finding difficulty in converting .obj file into openGL. I have found my model and here i have attach the main.cpp file for your reference. I am unsure of how to run the files.
Which file should I run when I called all these files in openGL.
Initially when I open OpenGL in Visual studio I called main.cpp file and run the file. But it shows many errors. The error says that cant find PDB file. What is that?
I'm just throwing in my answer to clear up the misconception leading to a question like that one:
How to convert .obj file (human head) to openGL?
The answer is: You don't.
OpenGL is a drawing API called from a program. It there for a program to make calls to draw points, lines and triangles. OpenGL is not a file format.
.OBJ is actually not very well defined name for a file format (there are literally hundreds of file formats that end .OBJ). But I take it you mean Wavefront OBJ. Anyway, files contain data. And while a program is data as well, a Wavefront OBJ file is not a program and can not be executed. Hence it makes no sense to even try to formulate a OBJ to OpenGL conversion.
What you need to do is writing a program, that reads the data from the Wavefront OBJ file, and uses that data to make the right calls to OpenGL. The other answers you already got link to such tutorials.
You will need to parse the .obj file then (depending on OpenGL version) load the parsed data into array/element_array buffers then use glDraw[Elements/Arrays] to work with in glsl or draw all the vertices using fixed functions.
But I think the real answer here lies in more study and not jumping in the deep water when you can hardly doggy paddle. You should do more basic exercises and get an understanding of the API.
Oh, and a .pdb file is for debugging in visual studio; I think it stands for program database.
Here is good tiny parser for obj
https://github.com/syoyo/tinyobjloader
You need:
1. parse data
2. create buffer object with data
3. draw buffer object
Yet another OBJ-loading tutorial http://www.opengl-tutorial.org/beginners-tutorials/tutorial-7-model-loading/

Load obj mtl Direct3D 11

Quick question, how do i load a .obj modle into directx 11 (d3d11.h) and also the .mtl file for materials. Thanks in advance.
You can write your own parser, obj is a pretty simple text format (format description)
Otherwise some loaders already exists, like Assimp .It only deals with decoding, you will still need to create vertex/index buffers from decoded data.
DirectX no longer comes with any libraries to load object files, such as *.obj, *.x and so on after version 9. You need to either download a library to open these files, or do it yourself since OBJ files are reasonably simple to parse.
See here for an example of this using OpenGL: http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Load_OBJ - the concepts should be reasonably easy to transition across.

how to export and import from blender to c++(.caf, .xrf , .cmf/xmf ....)?

i like game so much
at this time i realy try to built simple game , i start wiht build the object and the character (with animation ). but i have big problem to complete this project because i do not know how ti export and imprt this object to c++ , could anyone help me PLEASE??
You say you have a animated character in Blender format and wish to import it into your application written in C++, right?
To export data from Blender and use it in your application, consider using Open Asset Import Library. It reads a huge bunch of file formats. It even supports the native Blender file format, but currently with no animations. Note - I am biased here, since I'm one of the project's founders.
Unless it's for learning purposes, I'd strongly discourage you to write your own importers. It's painful and most likely to distract you from your original aim (write a game).
Finding proper exporters for Blender can be tricky, but I'd try Collada, X, MD5, 3DS, Obj, DXF .. usually, one of these formats works (keep in mind, however, that some support animations and more sophisticated materials while others don't).
C++ doesn't support natively 3D graphic rendering nor importing 3D models. My advice would be to first learn how to use Glut or SDL and OpenGL and try to import static models saved in a obj file format, which will be way simpler to load and display.

Getting basic 3D models into an OpenGL app

Ok... I'm doing simple OpenGL ES programming and when I say simple, the most complicated things I do aren't much more than glorified beveled cubes and L-shapes. (Think very Tetris but in 3D.) However, getting all that vertex data into an app is either a) hand-coded (UGH!) or b) 3rd-party game engine (double-UGH!!!) or you use some 3rd-party filetype importer. (Partial Ugh!)
With one exception.
I've been using a program on the Mac called Cheetah3D which is a pretty good modeler (not great, but solid... good) and the one thing it has that I haven't seen elsewhere is the ability to export your models directly to c-ready header files. It exports all the arrays for you and even gives you the code to render them (albeit in a commented out block as a reference.)
While this is great for 90% of what we do, there are some things the modeler can't, like allowing me to specify the same vertex for two different faces but using a different normal for each one... or even to mix flat and curved normals (or rather how they render) in the same model.
Note those features are easily described in an .obj file since your normals and vertices are in different arrays that you can easily specify together with faces but they don't come out in the header file that way.
SO... obj files (or any other file type that will work for that matter...) best way to get it into your code?
Thoughts?
OBJ file format.
Anyone doing 3D work, should at sometime write a obj model loader.
Its a very simple file format.
v
t
n
f
Where:
v is the vertices.
t is the texture coordinates.
n is the normal.
f is the face.
An example obj file segment:
v 1.0 0.1 0.1
t 1.0 0.0
n 1.0 1.0
f 1/1/1 1/1/1 1/1/1
All it takes is storing four collections of the above, and when it comes to rendering the face of a vertex, use the index and perform simple look ups. Simple file I/O is very easy to do in any language, so all in all, using an obj file is quite easy.
Naturally, the downside to obj files is they can get quite big. Personally, I've never found this an issue, and often take the obj file format as a starting point and remove some of the duplication to create a custom file format specially for my application.
OBJ files were used quite a bit historically and it isn't that difficult to roll your own importer or at least an importer that works for simple models you care about. Just handle verts, colors & normals to start and you'll be fine for the simple models you are describing. Sure it will barf if you try to read in arbitrary OBJ files--but just don't do that. ;^)
You certainly don't want to recompile each time you modify your model, if I'm reading your cheetah3d comment right. That will get tedious very fast.
For sure, you don't want to embed anything inti your code, loading OBJ is a good option but different modelers can have their own implementations of OBJ, as there is no official standard for the format. It's simple but I've seen many situations where application X can't load an OBJ from app Y. So if you ever plan on authoring assets from different packages take a peek at their outputted OBJ files to make sure they follow the same conventions.
The best OBJ file documentation I've found is here http://www.martinreddy.net/gfx/3d/OBJ.spec
I've found it's a good starting point but if your app becomes more complex and you start delving into more complex material definitions on your objects then you might want to look into something more standardized like Collada - an XML based format ( https://collada.org ) that covers a lot more but would also be more complex to load into your app.
Also, another good option for generating OBJ files is Blender, a freely available and pretty solid modelling tool ( http://www.blender.org ).
I have written a reader/renderer for AC3D files that works fine on the iPhone (OpenGL ES)
Feel free to have a look at it here.
There is also an obj loader by Jeff Lamarche at google code.