Rozengain - New Media Development Blog

Syndicate content
Updated: 1 hour 28 min ago

Blender Cycles Texture Nodes How-to

Fri, 01/20/2012 - 15:46

Blender’s current internal renderer is quite old and is being updated with a modern renderer called Cycles.
It is included in the current version but you have to manually select it. It’s still in its early stages and still has a limited feature set.
The results are already very promising though. They look much more realistic and you can see a render preview inside the 3D view!
The performance is also much, much better. It uses the GPU for rendering (OpenCL/CUDA) which speeds things up significantly.
Experimental builds can be grabbed from http://www.graphicall.org. For the best performance select a build that has “Importance Sampling“. This reduced the render time and the amount of noise.

More or less a note-to-self, this is how you configure a texture node in Cycles (I couldn’t really find this anywhere):

which results in:

Rajawali Tutorial 4: Optimisation

Mon, 01/16/2012 - 09:57

The onSurfaceCreated() method in the Renderer is called every time the rendering starts or whenever the context is lost. This typically happens when the orientation changes or when the device wakes up after going to sleep.

When the context is lost, all the textures are deleted from memory. This is not something Rajawali inflicts upon you. This is the way OpenGL ES on Android works. So you always have to re-create your textures in the onSurfaceCreated() method.

Object instantiation however is something that needs to be done only once. So in onSurfaceCreated you could use the following code (make sure you set mClearChildren to false):

@Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { mClearChildren = false; if(sceneInitialized == false) { mLight= new DirectionalLight(); mLight.setPosition(3, 5, -3); mSphere = = new Sphere(1, 24, 24); mSphere.addLight(mLight); addChild(mSphere); } SimpleMaterial simple = new SimpleMaterial(); mSphere.setMaterial(simple); Bitmap texture = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.mytexture); mSphere.addTexture(mTextureManager.addTexture(texture)); }

This speeds things up significantly when the context has to be re-created, especially when you use large models.

There’s another optimisation you can use when large models are involved. It’s called serialization. This prevents you from having to load a .obj file and parse it every time the application starts up. It basically writes all the relevant data (vertices, indices, normal, texture coordinates and colors) to a binary file on disk.

So the .obj file has to be loaded only once during development. Once the file has loaded you can save it to the sdcard.
In order to do this you first have to set the permission in AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Then the .obj model can be loaded in and turned into a serialized file:

FileOutputStream fos; try { // GET SDCARD PATH File sdcardStorage = Environment.getExternalStorageDirectory(); String sdcardPath = sdcardStorage.getParent() + java.io.File.separator + sdcardStorage.getName(); // CREATE A NEW FILE TO WRITE THE DATA TO File f = new File( sdcardPath+File.separator + "my_serialized_model.ser"); fos = new FileOutputStream(f); ObjectOutputStream os = new ObjectOutputStream(fos); // GET THE .OBJ FILE AND PARSE IT ObjParser parser = new ObjParser(mContext.getResources(), mTextureManager, R.raw.my_model_obj); parser.parse(); // WRITE THE OBJECT TO DISK USING BaseObject3D's toSerializedObject3D() METHOD os.writeObject(parser.getParsedObject().getChildByName("MyModelObjectName").toSerializedObject3D()); os.close(); } catch (Exception e) { e.printStackTrace(); }

When this is done the code and the .obj file can be discarded. The serialized file is written to the sd card and should be copied into to your resources folder (res/raw). The serialized model is now ready to be loaded into your application:

try { ObjectInputStream ois; ois = new ObjectInputStream(mContext.getResources().openRawResource(R.raw.my_serialized_model)); mMyModel = new BaseObject3D((SerializedObject3D)ois.readObject()); addChild(mMyModel); ois.close(); } catch (Exception e) { e.printStackTrace(); }

WIN!

Rajawali Tutorial 3: Materials

Mon, 12/05/2011 - 14:18

This small tutorial is going to focus on the syntax for creating materials only.
If you want to go over the basics then please read this first:

Alright. Let’s create some materials

Simple Material

A basic material is just a color or a texture without any lighting enabled. This renders a green cube:

SimpleMaterial simple = new SimpleMaterial(); mCube.setMaterial(simple); mCube.setColor(0xff009900);

This renders a textured cube:

SimpleMaterial simple = new SimpleMaterial(); mCube.setMaterial(simple); Bitmap texture = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.mytexture); mCube.addTexture(mTextureManager.addTexture(texture)); Gouraud Material

This material can be used in combination with a light. For more info check out this Wikipedia article.

mLight = new DirectionalLight(); mLight.setPosition(0, 5, -3); GouraudMaterial gouraud = new GouraudMaterial(); gouraud.setSpecularColor(0xffffff00); // yellow mCube.setMaterial(gouraud); mCube.setLight(mLight); Phong Material

Use this material to get nice specular highlights. See this article on Wikipedia.

PhongMaterial phong = new PhongMaterial(); phong.setSpecularColor(0xffffffff); // white phong.setAmbientcolor(0xffffff00); // yellow phong.setShininess(0.5f); mCube.setMaterial(phong); mCube.setLight(mLight); Environment map material

This is a reflective material. It requires 6 textures. Check this Wikipedia article for more information.

Bitmap[] textures = new Bitmap[6]; String ns = "com.mynamespace.myapp:drawable/"; String dns = "com.mynamespace.myapp"; String prefix = ""; textures[0] = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier(ns + prefix + "_posx", null, dns)); textures[1] = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier(ns + prefix + "_negx", null, dns)); textures[2] = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier(ns + prefix + "_posy", null, dns)); textures[3] = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier(ns + prefix + "_negy", null, dns)); textures[4] = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier(ns + prefix + "_posz", null, dns)); textures[5] = BitmapFactory.decodeResource(mContext.getResources(), mContext.getResources().getIdentifier(ns + prefix + "_negz", null, dns)); TextureInfo tInfo = mTextureManager.addCubemapTextures(textures); CubeMapMaterial mat = new CubeMapMaterial(); mat.addTexture(tInfo); mCube.setMaterial(mat); DirectionalLight light = new DirectionalLight(); light.setPosition(3, 5, -3); mCube.setLight(light);

More materials will be added soon. If you have a specific request, just ping me an email