Skip to content

Commit

Permalink
Fixed #1. Performance problems fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
andresoviedo committed Mar 26, 2017
1 parent d5d35db commit a0531e1
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 70 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ It is basically an android application with a 3D renderer (OpenGL ES 2.0) that c
The purpose of this application is to learn and share how to draw using OpenGL language


News (25/03/2017)
News (27/03/2017)
=================

* Fixed #9
* Fixed #1: Performance problems
* Fixed #9: IndexOutOfBoundsException
* /¡\ The app in the market is not up-to-date: I lost the keystore to sign the updates :(
* /¡\ Latest version: [app-debug.apk](app/build/outputs/apk/app-debug.apk)

Expand Down Expand Up @@ -129,10 +130,14 @@ ChangeLog

(f) fixed, (i) improved, (n) new feature

- 2.0.1 (25/03/2017)

- 1.2.3 (27/03/2017)
- (f) Fixed #1. Performance optimization

- 1.2.2 (25/03/2017)
- (f) Fixed #9. IOOBE loading face normals when faces had no texture or normals

- 2.0.0 (27/02/2017)
- 1.2.1 (27/02/2017)
- (f) Fixed loading external files issue #6
- (i) Project moved to gradle

Expand Down
Binary file modified app/build/outputs/apk/app-debug.apk
Binary file not shown.
10 changes: 6 additions & 4 deletions app/src/main/assets/models/cube.obj
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
mtllib cube.mtl

# (2)------(6)
# vertex indexes start at 1
#
# (3)------(7)
# /| /|
# / | / |
# (3)------(7) |
# (4)------(8) |
# | | | |
# | (0)----|-(4)
# | (1)----|-(5)
# | / | /
# |/ |/
# (1)------(5)
# (2)------(6)

v 0.0 0.0 0.0
v 0.0 0.0 1.0
Expand Down
30 changes: 16 additions & 14 deletions app/src/main/assets/models/cube2.obj
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
mtllib cube.mtl

# (2)------(6)

# vertex indexes start at 1
#
# (3)------(7)
# /| /|
# / | / |
# (3)------(7) |
# (4)------(8) |
# | | | |
# | (0)----|-(4)
# | (1)----|-(5)
# | / | /
# |/ |/
# (1)------(5)
# (2)------(6)

v 0.0 0.0 0.0
v 0.0 0.0 1.0
v 0.0 1.0 0.0
v 0.0 1.0 1.0
v 1.0 0.0 0.0
v 1.0 0.0 1.0
v 1.0 1.0 0.0
v 1.0 1.0 1.0
v 0.0 0.0 0.0
v 0.0 0.0 1.0
v 0.0 1.0 0.0
v 0.0 1.0 1.0
v 1.0 0.0 0.0
v 1.0 0.0 1.0
v 1.0 1.0 0.0
v 1.0 1.0 1.0

vn 0.0 0.0 1.0
vn 0.0 0.0 -1.0
Expand All @@ -33,7 +35,7 @@ vt 0.0 1.0

g cube
usemtl face1
f 1/4/2 7/2/2 5/3/2
f 1/4/2 7/2/2 5/3/2
f 1/4/2 3/1/2 7/2/2
usemtl face2
f 1/1/6 4/3/6 3/4/6
Expand Down
30 changes: 16 additions & 14 deletions app/src/main/assets/models/cube3.obj
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
mtllib CUBE.mtl

# (2)------(6)

# vertex indexes start at 1
#
# (3)------(7)
# /| /|
# / | / |
# (3)------(7) |
# (4)------(8) |
# | | | |
# | (0)----|-(4)
# | (1)----|-(5)
# | / | /
# |/ |/
# (1)------(5)
# (2)------(6)

v 0.0 0.0 0.0
v 0.0 0.0 1.0
v 0.0 1.0 0.0
v 0.0 1.0 1.0
v 1.0 0.0 0.0
v 1.0 0.0 1.0
v 1.0 1.0 0.0
v 1.0 1.0 1.0
v 0.0 0.0 0.0
v 0.0 0.0 1.0
v 0.0 1.0 0.0
v 0.0 1.0 1.0
v 1.0 0.0 0.0
v 1.0 0.0 1.0
v 1.0 1.0 0.0
v 1.0 1.0 1.0

vn 0.0 0.0 1.0
vn 0.0 0.0 -1.0
Expand All @@ -33,7 +35,7 @@ vt 0.0 1.0

g cube
usemtl face1
f 1/4/2 7/2/2 5/3/2
f 1/4/2 7/2/2 5/3/2
f 1/4/2 3/1/2 7/2/2
usemtl face2
f 1/1/6 4/3/6 3/4/6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public static Object3DData loadV5(AssetManager assets, String assetDir, String a
wfl.getFaceMats(), wfl.getMaterials());
data3D.setId(assetFilename);
data3D.setAssetsDir(assetDir);
data3D.setDrawMode(GLES20.GL_TRIANGLES);
// data3D.setDrawMode(GLES20.GL_TRIANGLES);
generateArrays(assets, data3D);
if (data3D.getVertexColorsArrayBuffer() != null) {
data3D.setVersion(5);
Expand Down Expand Up @@ -432,6 +432,9 @@ public Object3D getDrawer(Object3DData obj, boolean usingTextures, boolean using
}

public static Object3DData generateArrays(AssetManager assets, Object3DData obj) throws IOException {
int drawMode = GLES20.GL_TRIANGLES;
int drawSize = 0;

ArrayList<Tuple3> verts = obj.getVerts();
ArrayList<Tuple3> vertexNormals = obj.getNormals();
ArrayList<Tuple3> texCoords = obj.getTexCoords();
Expand All @@ -448,7 +451,9 @@ public static Object3DData generateArrays(AssetManager assets, Object3DData obj)

// TODO: generate face normals
FloatBuffer vertexArrayBuffer = null;
ShortBuffer drawOrderBuffer = null;
if (obj.isDrawUsingArrays()) {
Log.i("Object3DBuilder", "Generating vertex array buffer...");
vertexArrayBuffer = createNativeByteBuffer(3 * faces.getVerticesReferencesCount() * 4).asFloatBuffer();
for (int[] face : faces.facesVertIdxs) {
for (int i = 0; i < face.length; i++) {
Expand All @@ -457,19 +462,37 @@ public static Object3DData generateArrays(AssetManager assets, Object3DData obj)
vertexArrayBuffer.put(vertexBuffer.get(face[i] * 3 + 2));
}
}
}else{
Log.i("Object3DBuilder", "Generating draw order buffer...");
// this only works for faces made of a single triangle
drawOrderBuffer = createNativeByteBuffer(faces.getVerticesReferencesCount()*2).asShortBuffer();
for (int[] face : faces.facesVertIdxs) {
for (int i = 0; i < face.length; i++) {
drawOrderBuffer.put((short)face[i]);
}
}
}

boolean onlyTriangles = true;
List<int[]> drawModeList = new ArrayList<int[]>();
int currentVertexPos = 0;
for (int[] face : faces.facesVertIdxs) {
if (face.length == 3) {
drawModeList.add(new int[] { GLES20.GL_TRIANGLES, currentVertexPos, face.length });
} else {
onlyTriangles = false;
drawModeList.add(new int[] { GLES20.GL_TRIANGLE_FAN, currentVertexPos, face.length });
}
currentVertexPos += face.length;
}

if (onlyTriangles) {
// TODO: test this with all models
drawMode = GLES20.GL_TRIANGLES;
drawSize = 0;
drawModeList = null;
}

FloatBuffer vertexNormalsBuffer = createNativeByteBuffer(3 * vertexNormals.size() * 4).asFloatBuffer();
for (Tuple3 norm : vertexNormals) {
vertexNormalsBuffer.put(norm.getX());
Expand Down Expand Up @@ -541,15 +564,15 @@ public static Object3DData generateArrays(AssetManager assets, Object3DData obj)
if (texture != null) {
if (obj.getCurrentDir() != null) {
File file = new File(obj.getCurrentDir(), texture);
Log.v("materials", "Loading texture '" + file + "'...");
Log.i("materials", "Loading texture '" + file + "'...");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(file);
IOUtils.copy(fis, bos);
fis.close();
textureData = bos.toByteArray();
bos.close();
} else {
Log.v("materials", "Loading texture '" + obj.getAssetsDir() + texture + "'...");
Log.i("materials", "Loading texture '" + obj.getAssetsDir() + texture + "'...");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
InputStream fis = assets.open(obj.getAssetsDir() + texture);
IOUtils.copy(fis, bos);
Expand All @@ -563,6 +586,8 @@ public static Object3DData generateArrays(AssetManager assets, Object3DData obj)
}

obj.setVertexBuffer(vertexBuffer);
obj.setDrawOrder(drawOrderBuffer);
obj.setDrawSize(drawSize);
obj.setVertexNormalsBuffer(vertexNormalsBuffer);
obj.setTextureCoordsBuffer(textureCoordsBuffer);

Expand All @@ -571,6 +596,7 @@ public static Object3DData generateArrays(AssetManager assets, Object3DData obj)
obj.setTextureCoordsArrayBuffer(textureCoordsArraysBuffer);

obj.setDrawModeList(drawModeList);
obj.setDrawMode(drawMode);
obj.setVertexColorsArrayBuffer(colorArrayBuffer);
obj.setTextureData(textureData);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
public abstract class Object3DImpl implements Object3D {

private final String id;
// Transformations
private final float[] mMatrix = new float[16];
private final float[] rotationMatrix = new float[16];
private final float[] translationMatrix = new float[16];
// mvp matrix
private final float[] mvMatrix = new float[16];
private final float[] mvpMatrix = new float[16];
// OpenGL data
private final int mProgram;

Expand Down Expand Up @@ -105,10 +112,7 @@ public void draw(Object3DData obj, float[] pMatrix, float[] vMatrix, int drawMod
}

protected float[] getMMatrix(Object3DData obj) {
// Apply transformations
float[] mMatrix = new float[16];
float[] rotationMatrix = new float[16];
float[] translationMatrix = new float[16];


// calculate object transformation
Matrix.setIdentityM(rotationMatrix, 0);
Expand All @@ -124,13 +128,11 @@ protected float[] getMMatrix(Object3DData obj) {
}

protected float[] getMvMatrix(float[] mMatrix, float[] vMatrix) {
float[] mvMatrix = new float[16];
Matrix.multiplyMM(mvMatrix, 0, vMatrix, 0, mMatrix, 0);
return mvMatrix;
}

protected float[] getMvpMatrix(float[] mvMatrix, float[] pMatrix) {
float[] mvpMatrix = new float[16];
Matrix.multiplyMM(mvpMatrix, 0, pMatrix, 0, mvMatrix, 0);
return mvpMatrix;
}
Expand Down Expand Up @@ -284,25 +286,27 @@ protected void drawShape(Object3DData obj, int drawMode, int drawSize) {

if (drawModeList != null) {
if (drawOrderBuffer == null) {
for (int[] polygon : drawModeList) {
Log.d(obj.getId(),"Drawing single polygons using arrays...");
for (int j=0; j<drawModeList.size(); j++) {
int[] polygon = drawModeList.get(j);
int drawModePolygon = polygon[0];
int vertexPos = polygon[1];
int drawSizePolygon = polygon[2];
if (drawMode == GLES20.GL_LINE_LOOP && polygon[2] > 3) {
// is this wireframe?
Log.v("Object3DImpl",
"Drawing wireframe for '" + obj.getId() + "' (" + drawSizePolygon + ")...");
// Log.v("Object3DImpl","Drawing wireframe for '" + obj.getId() + "' (" + drawSizePolygon + ")...");
for (int i = 0; i < polygon[2] - 2; i++) {
Log.v("Object3DImpl",
"Drawing wireframe triangle '" + i + "' for '" + obj.getId() + "'...");
// Log.v("Object3DImpl","Drawing wireframe triangle '" + i + "' for '" + obj.getId() + "'...");
GLES20.glDrawArrays(drawMode, polygon[1] + i, 3);
}
} else {
GLES20.glDrawArrays(drawMode, polygon[1], polygon[2]);
}
}
} else {
for (int[] drawPart : drawModeList) {
Log.d(obj.getId(),"Drawing single polygons...");
for (int i=0; i<drawModeList.size(); i++) {
int[] drawPart = drawModeList.get(i);
int drawModePolygon = drawPart[0];
int vertexPos = drawPart[1];
int drawSizePolygon = drawPart[2];
Expand All @@ -313,19 +317,23 @@ protected void drawShape(Object3DData obj, int drawMode, int drawSize) {
} else {
if (drawOrderBuffer != null) {
if (drawSize <= 0) {
Log.d(obj.getId(),"Drawing all elements with mode '"+drawMode+"'...");
drawOrderBuffer.position(0);
GLES20.glDrawElements(drawMode, drawOrderBuffer.capacity(), GLES20.GL_UNSIGNED_SHORT,
drawOrderBuffer);
} else {
Log.d(obj.getId(),"Drawing single elements of size '"+drawSize+"'...");
for (int i = 0; i < drawOrderBuffer.capacity(); i += drawSize) {
drawOrderBuffer.position(i);
GLES20.glDrawElements(drawMode, drawSize, GLES20.GL_UNSIGNED_SHORT, drawOrderBuffer);
}
}
} else {
if (drawSize <= 0) {
Log.d(obj.getId(),"Drawing all triangles using arrays...");
GLES20.glDrawArrays(drawMode, 0, vertexBuffer.capacity() / COORDS_PER_VERTEX);
} else {
//Log.d(obj.getId(),"Drawing single triangles using arrays...");
for (int i = 0; i < vertexBuffer.capacity() / COORDS_PER_VERTEX; i += drawSize) {
GLES20.glDrawArrays(drawMode, i, drawSize);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ public void init() {

@Override
public void onLoadComplete(Object3DData data) {
data.setDrawMode(defaultDrawMode);
if ("ToyPlane.obj".equals(data.getId())) {
// TODO: make this parametrable
data.setDrawMode(GLES20.GL_TRIANGLE_FAN);
}
data.centerAndScale(5.0f);
addObject(data);

Expand Down Expand Up @@ -115,21 +118,19 @@ public void draw(Object3DData obj, float[] pMatrix, float[] vMatrix, int drawMod

}

protected void addObject(Object3DData obj) {
synchronized (objects) {
objects.add(obj);
}
protected synchronized void addObject(Object3DData obj) {
List<Object3DData> newList = new ArrayList<Object3DData>(objects);
newList.add(obj);
this.objects = newList;
requestRender();
}

private void requestRender() {
parent.getgLView().requestRender();
}

public List<Object3DData> getObjects() {
synchronized (objects) {
return new ArrayList<Object3DData>(objects);
}
public synchronized List<Object3DData> getObjects() {
return objects;
}

public void toggleWireframe() {
Expand Down
Loading

0 comments on commit a0531e1

Please sign in to comment.