V

Avatar



Modified converter_to_threejs.py

Threejs includes a nice fbx – and other formats – converter script to Threejs’s JSON format, but there was some issues and improvements i felt the need for, so i’ve created a modified version of it.

This modified version includes the following changes:

  • Map AmbientColor texture slot to Three.js LightMap
  • Material map filenames only, full path removed
  • New console arg “-b” used to override MeshLambertMaterial by MeshBasicMaterial
  • Creates a local “/maps” folder next to .js file and save all textures to it

First thing i’ve noticed was there in no way to get lightmaps to work when using Maya, so i had to have a slot that did just that. Also when adding textures to materials the script would always fail when attempting to copy textures, because the source would try to copy into itself. Not sure why it was made like that, but it didn’t work for me. Copying the textures into a local folder was a need as i want things relative to my project root.

You can find the modified script here: GIST





Rendering partial Geometry in Three.js

Update: I was told by Mr.Doob that it is possible to render partial geometry using offsets/drawcalls, therefore no need to change the library. There is a catch.. It only works for indexed geometry, which in my specific case would not do the trick, still it is good to know there is such an option.

—–

One thing i’ve missed when using Threejs was the possibility to render a set of a Geometry. In my case a large BufferGeometry pool.

Setting up one was quite easy:

 // Non-indexed geometry data
var numVertices = 1024 * 10;
cubePoolGeometry.addAttribute( "position", new THREE.Float32Attribute( numVertices, 3 ) );
cubePoolGeometry.addAttribute( "color", new THREE.Float32Attribute( numVertices, 3 ) );
cubePoolGeometry.addAttribute( "uv", new THREE.Float32Attribute( numVertices, 2 ) );
cubePoolGeometry.addAttribute( "normal", new THREE.Float32Attribute( numVertices, 3 ) );

When it renders the geometry it will internally use the size of the buffer, being the total length allocated at init time. Internal code shows exactly that:

 _gl.drawArrays( _gl.TRIANGLES, 0, position.array.length / 3 );
_this.info.render.vertices += position.array.length / 3;
_this.info.render.faces += position.array.length / 9;

This will always render the full batch, which i did not want. The change was easy and could become a good thing for future releases of the library

 _gl.drawArrays( _gl.TRIANGLES, 0, position.numItems / 3 );
_this.info.render.vertices += position.numItems / 3;
_this.info.render.faces += position.numItems / 9;

This allows rendering a subset of the geometry by changing the attribute variable numItems, like so:

 // Render only first 20%
var numVertices = 1024 * 2;
cubePoolGeometry.attributes.position.numItems = numVertices * 3;
//cubePoolGeometry.attributes.color.numItems = numVertices * 3;
//cubePoolGeometry.attributes.uv.numItems = numVertices * 2;
//cubePoolGeometry.attributes.normal.numItems = numVertices * 3;

The full buffer is untouched, you can still get the total size by querying the array length (or save it for future reference).





Clip Planes

I wanted to write about GLSL clip planes, but you will find that Prideout wrote about it and it pretty much says what it has to say. Here it is, Clip Planes: http://github.prideout.net/clip-planes/





C/C++ enable/disable code blocks with comments

Sometimes you’re writing code and need a quick way to enable/disable a section of code. That happens to me and most probably it happens to you too. I know of 2 ways to do this. You’ll need to change a character and compile the file, but that’s it. This does not allow you to change things in runtime, it’s all in compile time.

We can use C/C++ multiline comments /* */, to enable/disable blocks of code by changing a character. Check the following example:

/**/
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glDepthMask( GL_FALSE );
glEnable( GL_STENCIL_TEST );
/*/
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE );
glDepthMask( GL_TRUE );
glDisable( GL_DEPTH_TEST );
glDisable( GL_STENCIL_TEST );
/**/

To enable the second block -and disable the first- change /**/ into /*/ from the first line.
An alternative to the comment method is to use the #if #endif preprocessor directives. Using the same code example, if we wanted to enable the first block we’ll change it as follows:

#if 1
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glDepthMask( GL_FALSE );
glEnable( GL_STENCIL_TEST );
#else
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE );
glDepthMask( GL_TRUE );
glDisable( GL_DEPTH_TEST );
glDisable( GL_STENCIL_TEST );
#endif

To switch blocks, change #if 1 to #if 0. That’s pretty much it.

The example code above is not the best example at all, but take it as just that. An example. During your code  sessions you’ll find better use for this i’m sure about that. Hope it helps.





AntTweakBar with Core GL OS X

I’ve been trying to get AntTweakBar (version 116) to compile and run as my default GUI but ended up finding some obstacles that took me a couple days to find a solution. Should have been a smooth ride, but ended up in a couple of crazy days and an easy solution found just minutes ago.

Let’s begin with some information. It was pretty easy to compile following the instructions found on the website. Under Windows it took a few minutes to get it up and running, even as a Static Library. Problems began when i’ve tried to compile on OS X 10.9:

  1. It reports 2 errors with the declaration of glDrawElements and glShaderSource. It differs from the declaration from GL header.
    Easy fix: Just copy the declaration from GL header to ATB.
  2. Compiling as a static library was OK. After reading it’s change log i realised they have static library support but i had taken the longest path and was trying to get it done manually, so it took me more time that i had planned.

After some time, i finally got the wanted the static library compiled and linking successfully,  i got the following error:

“AntTweakBar: OpenGL Core Profile functions cannot be loaded.
ERROR(AntTweakBar) >> Cannot load OpenGL library dynamically”

Tracked that problem down to the line:

gl_dyld = dlopen("OpenGL", RTLD_LAZY );

gl_dyld was always NULL, which tells me the library wasn’t being loaded/found (really?!). The fix i found is to change the first parameter to use the full path to OpenGL as follows:

gl_dyld = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY );

 

It is a very simple fix and worked just fine for me, but, it took me a few days of frustration and travel. After all i was supposed to get this up and running last Thursday. It’s nearly one week later.