PDA

View Full Version : GLES 1.0 - incorrect rendering



yaoglu
09-10-2016, 06:28 AM
I am loading .obj file and render it in android emulator. Although 3rd-party .obj viewers show model correctly (open3mod), when I launch it on android emulator it looks strange, it renders rectangle box with rounded corners weird. I already tried to render object with glDrawElements with indices from .obj and tried to add normals information - the result almost does not change.



import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import java.util.Hashtable;
import java.util.ArrayList;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.nio.FloatBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MyGLSurfaceView extends GLSurfaceView implements GLSurfaceView.Renderer
{
private Context context;
private Hashtable<String, ArrayList<Float>> obj;

public MyGLSurfaceView(Context context)
{
super(context);
setRenderer(this);
this.context = context;
}

public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
obj = new Hashtable<String, ArrayList<Float>>();

try
{
BufferedReader reader = new BufferedReader(
new InputStreamReader(context.getAssets().open("calculator.obj")));

ArrayList<Float> vertex = new ArrayList<Float>();

String name = null;
String line = null;
while ((line = reader.readLine()) != null)
{
if (line.startsWith("v "))
{
String[] parts = line.substring(2).trim().split("\\s+");

vertex.add(Float.valueOf(Float.valueOf(parts[0]).floatValue() / 2f)); // scale to smaller twice
vertex.add(Float.valueOf(Float.valueOf(parts[1]).floatValue() / 2f)); // scale to smaller twice
vertex.add(Float.valueOf(Float.valueOf(parts[2]).floatValue() / 2f)); // scale to smaller twice
}

if (line.startsWith("f "))
{
String[] parts = line.substring(2).trim().split("\\s+");

obj.get(name).add(vertex.get(Integer.valueOf(parts[0]).intValue() - 1));
obj.get(name).add(vertex.get(Integer.valueOf(parts[1]).intValue() - 1));
obj.get(name).add(vertex.get(Integer.valueOf(parts[2]).intValue() - 1));
}

if (line.startsWith("g "))
{
name = line.substring(2).trim();

obj.put(name, new ArrayList<Float>());
}
}

reader.close();
}
catch (Exception e)
{
System.exit(0);
}
}

public void onSurfaceChanged(GL10 gl, int width, int height)
{
gl.glViewport(0, 0, width, height);
}

public void onDrawFrame(GL10 gl)
{
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glClearDepthf(1.0f);

int i;

ByteBuffer fByteBuffer = ByteBuffer.allocateDirect(obj.get("calculator").size() * 4);
fByteBuffer.order(ByteOrder.nativeOrder());
FloatBuffer faces = fByteBuffer.asFloatBuffer();
for (i = 0; i < obj.get("calculator").size(); i++)
faces.put(obj.get("calculator").get(i).floatValue());
faces.position(0);

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, faces);

gl.glDrawArrays(GL10.GL_TRIANGLES, 0, faces.capacity() / 3);
}
}

2291