Skip to content

Commit 7c60983

Browse files
committed
2.7.0 - lighting + bug fixing
1 parent 318383d commit 7c60983

29 files changed

+427
-166
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ The application does not use any third party library.
1313
* Collada format (DAE): https://en.wikipedia.org/wiki/COLLADA
1414

1515

16-
News (06/11/2019)
16+
News (13/11/2019)
1717
=================
1818

19-
* Fix: Shader lightning fixed !
20-
* Fix: Better support for multiple geometries
19+
* New: version 2.7.0
20+
* Fix: Shader lighting improved !
21+
* Fix: Better support for Collada files
22+
* Fix: Performance issues fixed
2123
* Fix: Bugs fixed
2224

2325

@@ -60,10 +62,8 @@ The app comes with some included 3D models that were taken for free from Interne
6062
Whats next
6163
==========
6264

63-
* Blender support
6465
* 3D without glasses
6566
* Augmented reality
66-
* Improve overall performance
6767

6868

6969
Features
@@ -179,9 +179,12 @@ ChangeLog
179179

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

182-
- 2.6.1 (06/10/2019)
183-
- (f) fixed light rendering issues on shaders #125
182+
- 2.7.0 (13/11/2019)
183+
- (n) new blending force mode to 50%
184+
- (f) fixed light rendering issues on shaders #125 (diffuse + specular)
184185
- (f) fixed bugs when DAE had multiple geometries #125
186+
- (f) fixed textures not being linked issue
187+
- (f) fixed performance issues: now rendering below 5% cpu & no ram allocation
185188
- 2.6.0 (20/10/2019)
186189
- (n) #81 Support for collada files with multiple geometries
187190
- (f) #94 fixed setVisible(boolean)
2.16 KB
Binary file not shown.

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xmlns:tools="http://schemas.android.com/tools"
44
package="org.andresoviedo.dddmodel2"
55
android:versionCode="23"
6-
android:versionName="2.6.1" >
6+
android:versionName="2.7.0" >
77

88
<uses-sdk
99
android:minSdkVersion="14"

app/src/main/java/org/andresoviedo/app/model3D/demo/SceneLoader.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public class SceneLoader implements LoaderTask.Callback {
5656
* Enable or disable blending (transparency)
5757
*/
5858
private boolean isBlendingEnabled = true;
59+
/**
60+
* Force transparency
61+
*/
62+
private boolean isBlendingForced = false;
5963
/**
6064
* Whether to draw objects as wireframes
6165
*/
@@ -71,7 +75,7 @@ public class SceneLoader implements LoaderTask.Callback {
7175
/**
7276
* Whether to draw face normals. Normally used to debug models
7377
*/
74-
// TODO: toggle feature normals + blending
78+
// TODO: toggle feature this
7579
private boolean drawNormals = false;
7680
/**
7781
* Whether to draw using textures
@@ -401,14 +405,29 @@ public boolean isAnaglyph() {
401405
}
402406

403407
public void toggleBlending() {
404-
this.isBlendingEnabled = !isBlendingEnabled;
405-
makeToastText("Blending "+isBlendingEnabled, Toast.LENGTH_SHORT);
408+
if (this.isBlendingEnabled && !this.isBlendingForced){
409+
makeToastText("Blending forced", Toast.LENGTH_SHORT);
410+
this.isBlendingEnabled = true;
411+
this.isBlendingForced = true;
412+
} else if (this.isBlendingForced){
413+
makeToastText("Blending disabled", Toast.LENGTH_SHORT);
414+
this.isBlendingEnabled = false;
415+
this.isBlendingForced = false;
416+
} else {
417+
makeToastText("Blending enabled", Toast.LENGTH_SHORT);
418+
this.isBlendingEnabled = true;
419+
this.isBlendingForced = false;
420+
}
406421
}
407422

408423
public boolean isBlendingEnabled() {
409424
return isBlendingEnabled;
410425
}
411426

427+
public boolean isBlendingForced() {
428+
return isBlendingForced;
429+
}
430+
412431
@Override
413432
public void onStart(){
414433
ContentUtils.setThreadActivity(parent);

app/src/main/java/org/andresoviedo/app/model3D/view/ModelRenderer.java

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
public class ModelRenderer implements GLSurfaceView.Renderer {
2828

2929
private final static String TAG = ModelRenderer.class.getName();
30+
/**
31+
* Add 0.5f to the alpha component to the global shader so we can see through the skin
32+
*/
33+
private static final float[] BLENDING_FORCED_MASK_COLOR = {1.0f, 1.0f, 1.0f, 0.5f};
3034
// frustrum - nearest pixel
3135
private static final float near = 1f;
3236
// frustrum - fartest pixel
@@ -67,7 +71,8 @@ public class ModelRenderer implements GLSurfaceView.Renderer {
6771
private final float[] modelViewMatrix = new float[16];
6872
private final float[] projectionMatrix = new float[16];
6973
private final float[] viewProjectionMatrix = new float[16];
70-
private final float[] lightPosInEyeSpace = new float[4];
74+
private final float[] lightPosInWorldSpace = new float[4];
75+
private final float[] cameraPosInWorldSpace = new float[3];
7176

7277
// 3D stereoscopic matrix (left & right camera)
7378
private final float[] viewMatrixLeft = new float[16];
@@ -169,10 +174,14 @@ public void onDrawFrame(GL10 unused) {
169174
return;
170175
}
171176

177+
float[] colorMask = null;
172178
if (scene.isBlendingEnabled()) {
173179
// Enable blending for combining colors when there is transparency
174180
GLES20.glEnable(GLES20.GL_BLEND);
175181
GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA);
182+
if (scene.isBlendingForced()){
183+
colorMask = BLENDING_FORCED_MASK_COLOR;
184+
}
176185
} else {
177186
GLES20.glDisable(GLES20.GL_BLEND);
178187
}
@@ -182,6 +191,9 @@ public void onDrawFrame(GL10 unused) {
182191

183192
// recalculate mvp matrix according to where we are looking at now
184193
Camera camera = scene.getCamera();
194+
cameraPosInWorldSpace[0]=camera.xPos;
195+
cameraPosInWorldSpace[1]=camera.yPos;
196+
cameraPosInWorldSpace[2]=camera.zPos;
185197
if (camera.hasChanged()) {
186198
// INFO: Set the camera position (View matrix)
187199
// The camera has 3 vectors (the position, the vector where we are looking at, and the up position (sky)
@@ -228,19 +240,19 @@ public void onDrawFrame(GL10 unused) {
228240

229241

230242
if (!scene.isStereoscopic()) {
231-
this.onDrawFrame(viewMatrix, projectionMatrix, viewProjectionMatrix, lightPosInEyeSpace, null);
243+
this.onDrawFrame(viewMatrix, projectionMatrix, viewProjectionMatrix, lightPosInWorldSpace, colorMask, cameraPosInWorldSpace);
232244
return;
233245
}
234246

235247

236248
if (scene.isAnaglyph()) {
237249
// INFO: switch because blending algorithm doesn't mix colors
238250
if (anaglyphSwitch) {
239-
this.onDrawFrame(viewMatrixLeft, projectionMatrixLeft, viewProjectionMatrixLeft, lightPosInEyeSpace,
240-
COLOR_RED);
251+
this.onDrawFrame(viewMatrixLeft, projectionMatrixLeft, viewProjectionMatrixLeft, lightPosInWorldSpace,
252+
COLOR_RED, cameraPosInWorldSpace);
241253
} else {
242-
this.onDrawFrame(viewMatrixRight, projectionMatrixRight, viewProjectionMatrixRight, lightPosInEyeSpace,
243-
COLOR_BLUE);
254+
this.onDrawFrame(viewMatrixRight, projectionMatrixRight, viewProjectionMatrixRight, lightPosInWorldSpace,
255+
COLOR_BLUE, cameraPosInWorldSpace);
244256
}
245257
anaglyphSwitch = !anaglyphSwitch;
246258
return;
@@ -251,14 +263,14 @@ public void onDrawFrame(GL10 unused) {
251263
// draw left eye image
252264
GLES20.glViewport(0, 0, width / 2, height);
253265
GLES20.glScissor(0, 0, width / 2, height);
254-
this.onDrawFrame(viewMatrixLeft, projectionMatrixLeft, viewProjectionMatrixLeft, lightPosInEyeSpace,
255-
null);
266+
this.onDrawFrame(viewMatrixLeft, projectionMatrixLeft, viewProjectionMatrixLeft, lightPosInWorldSpace,
267+
null, cameraPosInWorldSpace);
256268

257269
// draw right eye image
258270
GLES20.glViewport(width / 2, 0, width / 2, height);
259271
GLES20.glScissor(width / 2, 0, width / 2, height);
260-
this.onDrawFrame(viewMatrixRight, projectionMatrixRight, viewProjectionMatrixRight, lightPosInEyeSpace,
261-
null);
272+
this.onDrawFrame(viewMatrixRight, projectionMatrixRight, viewProjectionMatrixRight, lightPosInWorldSpace,
273+
null, cameraPosInWorldSpace);
262274
}
263275
}catch (Exception ex){
264276
Log.e("ModelRenderer", "Fatal exception: "+ex.getMessage(), ex);
@@ -267,7 +279,7 @@ public void onDrawFrame(GL10 unused) {
267279
}
268280

269281
private void onDrawFrame(float[] viewMatrix, float[] projectionMatrix, float[] viewProjectionMatrix,
270-
float[] lightPosInEyeSpace, float[] colorMask) {
282+
float[] lightPosInWorldSpace, float[] colorMask, float[] cameraPosInWorldSpace) {
271283

272284

273285
SceneLoader scene = main.getModelActivity().getScene();
@@ -279,31 +291,32 @@ private void onDrawFrame(float[] viewMatrix, float[] projectionMatrix, float[] v
279291

280292
// Calculate position of the light in world space to support lighting
281293
if (scene.isRotatingLight()) {
282-
Matrix.multiplyMV(lightPosInEyeSpace, 0, scene.getLightBulb().getModelMatrix(), 0, scene.getLightPosition(), 0);
294+
Matrix.multiplyMV(lightPosInWorldSpace, 0, scene.getLightBulb().getModelMatrix(), 0, scene.getLightPosition(), 0);
283295
// Draw a point that represents the light bulb
284-
lightBulbDrawer.draw(scene.getLightBulb(), projectionMatrix, viewMatrix, -1, lightPosInEyeSpace,
285-
colorMask);
296+
lightBulbDrawer.draw(scene.getLightBulb(), projectionMatrix, viewMatrix, -1, lightPosInWorldSpace,
297+
colorMask, cameraPosInWorldSpace);
286298
} else {
287-
// FIXME: memory leak
288-
lightPosInEyeSpace = new float[]{scene.getCamera().xPos, scene.getCamera().yPos,
289-
scene.getCamera().zPos, 0f};
299+
lightPosInWorldSpace[0] = scene.getCamera().xPos;
300+
lightPosInWorldSpace[1] = scene.getCamera().yPos;
301+
lightPosInWorldSpace[2] = scene.getCamera().zPos;
302+
lightPosInWorldSpace[3] = 0;
290303
}
291304

292305
// FIXME: memory leak
293306
if (scene.isDrawNormals()) {
294-
lightBulbDrawer.draw(Object3DBuilder.buildLine(new float[]{lightPosInEyeSpace[0],
295-
lightPosInEyeSpace[1], lightPosInEyeSpace[2], 0, 0, 0}), projectionMatrix,
307+
lightBulbDrawer.draw(Object3DBuilder.buildLine(new float[]{lightPosInWorldSpace[0],
308+
lightPosInWorldSpace[1], lightPosInWorldSpace[2], 0, 0, 0}), projectionMatrix,
296309
viewMatrix, -1,
297-
lightPosInEyeSpace,
298-
colorMask);
310+
lightPosInWorldSpace,
311+
colorMask, cameraPosInWorldSpace);
299312
}
300313
}
301314

302315
// draw axis
303316
if (scene.isDrawAxis()){
304317
Object3D basicDrawer = drawer.getPointDrawer();
305318
basicDrawer.draw(axis, projectionMatrix, viewMatrix, axis.getDrawMode(), axis
306-
.getDrawSize(),-1, lightPosInEyeSpace, colorMask);
319+
.getDrawSize(),-1, lightPosInWorldSpace, colorMask, cameraPosInWorldSpace);
307320
}
308321

309322

@@ -337,12 +350,12 @@ private void onDrawFrame(float[] viewMatrix, float[] projectionMatrix, float[] v
337350
// load model texture
338351
Integer textureId = textures.get(objData.getTextureData());
339352
if (textureId == null && objData.getTextureData() != null) {
340-
//Log.i("ModelRenderer","Loading texture '"+objData.getTextureFile()+"'...");
353+
Log.i("ModelRenderer","Loading texture '"+objData.getTextureFile()+"'...");
341354
ByteArrayInputStream textureIs = new ByteArrayInputStream(objData.getTextureData());
342355
textureId = GLUtil.loadTexture(textureIs);
343356
textureIs.close();
344357
textures.put(objData.getTextureData(), textureId);
345-
//Log.i("GLUtil", "Loaded texture ok");
358+
Log.i("GLUtil", "Loaded texture ok. id: "+textureId);
346359
}
347360
if (textureId == null){
348361
textureId = -1;
@@ -351,7 +364,7 @@ private void onDrawFrame(float[] viewMatrix, float[] projectionMatrix, float[] v
351364
// draw points
352365
if (objData.getDrawMode() == GLES20.GL_POINTS){
353366
Object3D basicDrawer = drawer.getPointDrawer();
354-
basicDrawer.draw(objData, projectionMatrix, viewMatrix, GLES20.GL_POINTS,lightPosInEyeSpace);
367+
basicDrawer.draw(objData, projectionMatrix, viewMatrix, GLES20.GL_POINTS, lightPosInWorldSpace, cameraPosInWorldSpace);
355368
}
356369

357370
// draw wireframe
@@ -368,8 +381,8 @@ else if (scene.isDrawWireframe() && objData.getDrawMode() != GLES20.GL_POINTS
368381
wireframes.put(objData, wireframe);
369382
}
370383
drawerObject.draw(wireframe, projectionMatrix, viewMatrix, wireframe.getDrawMode(),
371-
wireframe.getDrawSize(), textureId, lightPosInEyeSpace,
372-
colorMask);
384+
wireframe.getDrawSize(), textureId, lightPosInWorldSpace,
385+
colorMask, cameraPosInWorldSpace);
373386
animator.update(wireframe, scene.isShowBindPose());
374387
}catch(Error e){
375388
Log.e("ModelRenderer",e.getMessage(),e);
@@ -380,7 +393,7 @@ else if (scene.isDrawWireframe() && objData.getDrawMode() != GLES20.GL_POINTS
380393
else if (scene.isDrawPoints() || objData.getFaces() == null || !objData.getFaces().loaded()){
381394
drawerObject.draw(objData, projectionMatrix, viewMatrix
382395
, GLES20.GL_POINTS, objData.getDrawSize(),
383-
textureId, lightPosInEyeSpace, colorMask);
396+
textureId, lightPosInWorldSpace, colorMask, cameraPosInWorldSpace);
384397
}
385398

386399
// draw skeleton
@@ -394,13 +407,13 @@ else if (scene.isDrawSkeleton() && objData instanceof AnimatedModel && ((Animate
394407
animator.update(skeleton, scene.isShowBindPose());
395408
drawerObject = drawer.getDrawer(skeleton, false, scene.isDrawLighting(), scene
396409
.isDoAnimation(), scene.isDrawColors());
397-
drawerObject.draw(skeleton, projectionMatrix, viewMatrix,-1, lightPosInEyeSpace, colorMask);
410+
drawerObject.draw(skeleton, projectionMatrix, viewMatrix,-1, lightPosInWorldSpace, colorMask, cameraPosInWorldSpace);
398411
}
399412

400413
// draw solids
401414
else {
402415
drawerObject.draw(objData, projectionMatrix, viewMatrix,
403-
textureId, lightPosInEyeSpace, colorMask);
416+
textureId, lightPosInWorldSpace, colorMask, cameraPosInWorldSpace);
404417
}
405418

406419
// Draw bounding box
@@ -412,7 +425,7 @@ else if (scene.isDrawSkeleton() && objData instanceof AnimatedModel && ((Animate
412425
}
413426
Object3D boundingBoxDrawer = drawer.getBoundingBoxDrawer();
414427
boundingBoxDrawer.draw(boundingBoxData, projectionMatrix, viewMatrix, -1,
415-
lightPosInEyeSpace, colorMask);
428+
lightPosInWorldSpace, colorMask, cameraPosInWorldSpace);
416429
}
417430

418431
// Draw normals
@@ -429,7 +442,8 @@ else if (scene.isDrawSkeleton() && objData instanceof AnimatedModel && ((Animate
429442
Object3D normalsDrawer = drawer.getDrawer(normalData,false,false,scene.isDoAnimation(),
430443
false);
431444
animator.update(normalData, scene.isShowBindPose());
432-
normalsDrawer.draw(normalData, projectionMatrix, viewMatrix, -1, null);
445+
normalsDrawer.draw(normalData, projectionMatrix, viewMatrix, -1, null,
446+
lightPosInWorldSpace, cameraPosInWorldSpace);
433447
}
434448
}
435449

0 commit comments

Comments
 (0)