Question

I use Blender with .obj and .mtl export files for android OpenGL ES 2.0

Below image describe what i want to draw with vertices,and faces..

blender screenshot

ObjParse obje = new ObjParse(context,R.raw.sample_obj,R.raw.sample_mtl);
float[] koordinates = obje.getVertexKoordinates();
byte[] indicies = obje.getIndicies();
float[] colors = obje.getColors();

This codes bring me some array data for drawing. And i made FLoatBuffer and ByteBuffer this arrays..

GLES20.glVertexAttribPointer(position, 3, GLES20.GL_FLOAT, false, stride,floatBufKoor);
GLES20.glEnableVertexAttribArray(position);
GLES20.glVertexAttribPointer(color, 4, GLES20.GL_FLOAT, false, stride, floatBufColors);
GLES20.glEnableVertexAttribArray(color);

GLES20.glDrawElements(GLES20.GL_TRIANGLES,cizilecek,GLES20.GL_UNSIGNED_BYTE,byteBufIndicies);

Finally i get gradient colors,but i want solid color per faces.. How can i do this?

EDIT:

When i compile app i get this screenshot:

android screenshot

Was it helpful?

Solution

There is a limited way to color the faces of your glDrawElements mesh by triangle (as shown in Blender) instead of by vertex (as shown in the lower image):

  1. Pass the position attribute as a vec4 instead of vec3, use a stride of 4 for float[] koordinates.
  2. Set the fourth (w) value of each vertex to a "dictionary" value corresponding to each color. E.g. 0.0f = yellow, 0.5f = red, 1.0f = white. The middle value will be the "default."
  3. In the vertex shader, pass the w value to the frag shader as a varying, then reset the w to 1.0 before performing matrix transformations like perspective etc. Use < or > 0.5f due to interpolation.
  4. In the fragment shader, compare the varying to fixed values and set fragment color to predefined values before performing shading adjustments.

You can use a little more creativity (binary masks?) to index more colors but each strategy is limited to using a single w for the color (without being able to use a mat3 as an attribute in glDrawElements in ES2.0). You can also pass float[] colors (only one entry per color) as a uniform vec3 array and read color values from there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top