Skip to content

Commit 6556eae

Browse files
committed
#176 bug fixing
1 parent 68de152 commit 6556eae

15 files changed

+82
-48
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ ChangeLog
204204

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

207+
- 3.4.1 (20/09/2022)
208+
- (f) bug fixing: lighting, textures, etc
207209
- 3.4.0 (17/09/2022)
208210
- (n) GLTF basic support
209211
- 3.3.1 (12/09/2022)

engine/src/main/java/org/andresoviedo/android_3d_model_engine/drawer/GLES20Renderer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ public void draw(Object3DData obj, float[] pMatrix, float[] vMatrix, int drawMod
168168
// pass in texture UV buffer
169169
int mTextureHandle = -1;
170170
if (supportsTextures()) {
171-
if (textureId != -1) {
171+
setFeatureFlag("u_Textured", false);
172+
if (textureId != -1 && obj.getTextureBuffer() != null) {
172173
setTexture(textureId, "u_Texture", 0);
173174
mTextureHandle = setVBO("a_TexCoordinate", obj.getTextureBuffer(), TEXTURE_COORDS_PER_VERTEX);
174175
setFeatureFlag("u_Textured", true);
@@ -500,9 +501,8 @@ private void drawObjectElement(Object3DData obj, int drawMode, int drawBufferTyp
500501
setFeatureFlag("u_NormalTextured",true);
501502
}
502503

503-
if (obj.getMaterial().getEmissiveTextureId() != -1){
504-
// set normal texture
505-
setTexture(obj.getMaterial().getEmissiveTextureId(), "u_EmissiveTexture", 2);
504+
if (element.getMaterial().getEmissiveTextureId() != -1 && supportsTextures()){
505+
setTexture(element.getMaterial().getEmissiveTextureId(), "u_EmissiveTexture", 2);
506506
setFeatureFlag("u_EmissiveTextured", true);
507507
}
508508
}
@@ -530,7 +530,7 @@ private void drawObjectElement(Object3DData obj, int drawMode, int drawBufferTyp
530530

531531
// log event
532532
if (id != flags.get(element)) {
533-
Log.v("GLES20Renderer", "Rendering element " + i + " finished");
533+
Log.i("GLES20Renderer", "Rendering element " + i + " finished");
534534
flags.put(element, this.id);
535535
}
536536
}

engine/src/main/java/org/andresoviedo/android_3d_model_engine/model/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class Constants {
1919
public static final float DEFAULT_CAMERA_POSITION = UNIT * 2;
2020

2121
public static final float SKYBOX_SIZE = UNIT * 100;
22+
public static final float[] LIGHT_BULB_LOCATION = {0, 0, 0, 1};
2223

2324

2425
static final float ROOM_CENTER_SIZE = 0.01f;

engine/src/main/java/org/andresoviedo/android_3d_model_engine/model/Object3DData.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,6 @@ public Object3DData setColor(float[] color) {
430430
// set color only if valid data
431431
if (color != null) {
432432

433-
// assert
434-
if (color.length != 4) {
435-
throw new IllegalArgumentException("color should be RGBA");
436-
}
437-
438433
// color variable when using single color
439434
this.getMaterial().setDiffuse(color);
440435
this.getMaterial().setAlpha(color[3]);

engine/src/main/java/org/andresoviedo/android_3d_model_engine/objects/Normals.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,12 @@ private static Object3DData buildNormalLinesByIndices(Object3DData obj) {
125125
normalsObj.setVertexBuffer(IOUtils.createFloatBuffer(normalsVertexArray, 3));
126126
normalsObj.setNormalsBuffer(IOUtils.createFloatBuffer(normalsNormalsArray, 3));
127127
normalsObj.setDrawMode(GLES20.GL_LINES).setColor(new float[]{1f, 1f, 1f, 1f});
128-
normalsObj.setScale(obj.getScale());
128+
/*normalsObj.setScale(obj.getScale());
129129
normalsObj.setRotation(obj.getRotation());
130130
normalsObj.setLocation(obj.getLocation());
131-
normalsObj.setBindTransform(obj.getBindTransform());
131+
normalsObj.setBindTransform(obj.getBindTransform());*/
132+
normalsObj.setModelMatrix(obj.getModelMatrix());
133+
normalsObj.setReadOnly(true);
132134
normalsObj.setElements(normalsElements);
133135
normalsObj.setDrawUsingArrays(false);
134136

@@ -219,10 +221,12 @@ private static AnimatedModel buildNormalLinesForElements(AnimatedModel obj) {
219221
normalsObj.setVertexBuffer(IOUtils.createFloatBuffer(newVertexArray, 3));
220222
normalsObj.setNormalsBuffer(IOUtils.createFloatBuffer(newNormalsArray, 3));
221223
normalsObj.setDrawMode(GLES20.GL_LINES);
222-
normalsObj.setScale(obj.getScale());
224+
/*normalsObj.setScale(obj.getScale());
223225
normalsObj.setRotation(obj.getRotation());
224226
normalsObj.setLocation(obj.getLocation());
225-
normalsObj.setBindTransform(obj.getBindTransform());
227+
normalsObj.setBindTransform(obj.getBindTransform());*/
228+
normalsObj.setModelMatrix(obj.getModelMatrix());
229+
normalsObj.setReadOnly(true);
226230
normalsObj.setElements(newElements);
227231
normalsObj.setDrawUsingArrays(false);
228232
normalsObj.doAnimation(obj.getAnimation());
@@ -278,10 +282,12 @@ private static Object3DData calculateNormalsLines(Object3DData obj) {
278282

279283
Object3DData normalsObj = new Object3DData(normalsLines);
280284
normalsObj.setDrawMode(GLES20.GL_LINES).setColor(new float[]{1f, 0f, 0f, 1f});
281-
normalsObj.setScale(obj.getScale());
285+
/* normalsObj.setScale(obj.getScale());
282286
normalsObj.setRotation(obj.getRotation());
283287
normalsObj.setLocation(obj.getLocation());
284-
normalsObj.setBindTransform(obj.getBindTransform());
288+
normalsObj.setBindTransform(obj.getBindTransform());*/
289+
normalsObj.setModelMatrix(obj.getModelMatrix());
290+
normalsObj.setReadOnly(true);
285291

286292
Log.i("Normals", "New face normal lines object created");
287293
return normalsObj;
@@ -309,10 +315,12 @@ private static Object3DData calculateNormalsLinesByIndices(Object3DData obj) {
309315

310316
Object3DData normalsObj = new Object3DData(normalsLines);
311317
normalsObj.setDrawMode(GLES20.GL_LINES).setColor(new float[]{1f, 0f, 0f, 1f});
312-
normalsObj.setScale(obj.getScale());
318+
/*normalsObj.setScale(obj.getScale());
313319
normalsObj.setRotation(obj.getRotation());
314320
normalsObj.setLocation(obj.getLocation());
315-
normalsObj.setBindTransform(obj.getBindTransform());
321+
normalsObj.setBindTransform(obj.getBindTransform());*/
322+
normalsObj.setModelMatrix(obj.getModelMatrix());
323+
normalsObj.setReadOnly(true);
316324

317325
Log.i("Normals", "New face normal lines object created");
318326

@@ -351,10 +359,12 @@ private static Object3DData buildNormalLines(Object3DData obj) {
351359

352360
Object3DData normalsObj = new Object3DData(normalsLines);
353361
normalsObj.setDrawMode(GLES20.GL_LINES).setColor(new float[]{1f, 1f, 1f, 1f});
354-
normalsObj.setScale(obj.getScale());
362+
/*normalsObj.setScale(obj.getScale());
355363
normalsObj.setRotation(obj.getRotation());
356364
normalsObj.setLocation(obj.getLocation());
357-
normalsObj.setBindTransform(obj.getBindTransform());
365+
normalsObj.setBindTransform(obj.getBindTransform());*/
366+
normalsObj.setModelMatrix(obj.getModelMatrix());
367+
normalsObj.setReadOnly(true);
358368

359369
Log.v("Normals", "New face normal lines object created. vertices: " + normalsLines.capacity() / 3);
360370
return normalsObj;

engine/src/main/java/org/andresoviedo/android_3d_model_engine/services/SceneLoader.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public class SceneLoader implements LoadListener, EventListener {
169169
/**
170170
* Light bulb 3d data
171171
*/
172-
private final Object3DData lightBulb = Point.build(new float[]{0, 0, 0}).setId("light");
172+
private final Object3DData lightBulb = Point.build(Constants.LIGHT_BULB_LOCATION).setId("light");
173173
/**
174174
* Animator
175175
*/
@@ -201,8 +201,9 @@ public SceneLoader(Activity main, URI uri, int type) {
201201
this.uri = uri;
202202
this.type = type;
203203

204-
float light_distance = Constants.SKYBOX_SIZE;
205-
lightBulb.setLocation(new float[]{light_distance,light_distance/2,0});
204+
float light_distance = Constants.UNIT;
205+
lightBulb.setLocation(new float[]{light_distance/2,light_distance,0});
206+
lightBulb.setColor(Constants.COLOR_WHITE);
206207
}
207208

208209
public void addListener(EventListener listener){
@@ -286,9 +287,9 @@ public final void onDrawFrame() {
286287
private void animateLight() {
287288
if (!rotatingLight) return;
288289

289-
// animate light - Do a complete rotation every 5 seconds.
290-
long time = SystemClock.uptimeMillis() % 5000L;
291-
float angleInDegrees = (360.0f / 5000.0f) * ((int) time);
290+
// animate light - Do a complete rotation every 60 seconds.
291+
long time = SystemClock.uptimeMillis() % 60000L;
292+
float angleInDegrees = (360.0f / 60000.0f) * ((int) time);
292293
lightBulb.setRotation(new float[]{0, angleInDegrees, 0});
293294
}
294295

@@ -926,9 +927,9 @@ private void rescale(List<Object3DData> datas, float newScale, float[] newPositi
926927
Log.v("SceneLoader", "Mew model scale: " + Arrays.toString(data.getScale()));
927928

928929
// relocate
929-
float localTranlactionX = original.getLocation()[0] * scaleFactor;
930-
float localTranlactionY = original.getLocation()[1] * scaleFactor;
931-
float localTranlactionZ = original.getLocation()[2] * scaleFactor;
930+
float localTranlactionX = original.getLocation()[0] * scaleFactor + globalDifference[0];
931+
float localTranlactionY = original.getLocation()[1] * scaleFactor + globalDifference[1];
932+
float localTranlactionZ = original.getLocation()[2] * scaleFactor + globalDifference[2];
932933
data.setLocation(new float[]{localTranlactionX, localTranlactionY, localTranlactionZ});
933934
Log.v("SceneLoader", "Mew model location: " + Arrays.toString(data.getLocation()));
934935

engine/src/main/java/org/andresoviedo/android_3d_model_engine/view/ModelRenderer.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,20 @@ public class ModelRenderer implements GLSurfaceView.Renderer {
104104

105105
// light
106106
private final float[] tempVector4 = new float[4];
107-
private final float[] lightPosInWorldSpace = new float[3];
108-
private final float[] cameraPosInWorldSpace = new float[3];
109-
private final float[] lightPosition = new float[]{0, 0, 0, 1};
107+
private final float[] lightPosInWorldSpace = new float[4];
108+
private final float[] cameraPosInWorldSpace = new float[4];
109+
// private final float[] lightPosition = new float[]{0, 0, 0, 1};
110110

111111
// Decoration
112112
private final List<Object3DData> extras = new ArrayList<>();
113113
private final Object3DData axis = Axis.build().setId("axis").setSolid(false)
114114
.setScale(Constants.UNIT,Constants.UNIT,Constants.UNIT);
115115
private final Object3DData gridx = Grid.build(-Constants.GRID_WIDTH, 0f, -Constants.GRID_WIDTH, Constants.GRID_WIDTH, 0f, Constants.GRID_WIDTH, Constants.GRID_SIZE)
116116
.setColor(Constants.COLOR_RED_TRANSLUCENT).setId("grid-x").setSolid(false)
117-
.setScale(Constants.UNIT,Constants.UNIT,Constants.UNIT);;
117+
.setScale(Constants.UNIT,Constants.UNIT,Constants.UNIT);
118118
private final Object3DData gridy = Grid.build(-Constants.GRID_WIDTH, -Constants.GRID_WIDTH, 0f, Constants.GRID_WIDTH, Constants.GRID_WIDTH, 0f, Constants.GRID_SIZE)
119119
.setColor(Constants.COLOR_GREEN_TRANSLUCENT).setId("grid-y").setSolid(false)
120-
.setScale(Constants.UNIT,Constants.UNIT,Constants.UNIT);;
120+
.setScale(Constants.UNIT,Constants.UNIT,Constants.UNIT);
121121
private final Object3DData gridz = Grid.build(0, -Constants.GRID_WIDTH, -Constants.GRID_WIDTH, 0, Constants.GRID_WIDTH, Constants.GRID_WIDTH, Constants.GRID_SIZE)
122122
.setColor(Constants.COLOR_BLUE_TRANSLUCENT).setId("grid-z").setSolid(false)
123123
.setScale(Constants.UNIT,Constants.UNIT,Constants.UNIT);
@@ -518,9 +518,8 @@ private void onDrawFrame(float[] viewMatrix, float[] projectionMatrix, float[] v
518518

519519
// Calculate position of the light in world space to support lighting
520520
if (scene.isRotatingLight()) {
521-
// FIXME: memory leak
522521
Matrix.multiplyMV(tempVector4, 0, scene.getLightBulb().getModelMatrix(), 0,
523-
Math3DUtils.to4d(scene.getLightBulb().getLocation()), 0);
522+
Constants.LIGHT_BULB_LOCATION, 0);
524523
lightPosInWorldSpace[0] = tempVector4[0];
525524
lightPosInWorldSpace[1] = tempVector4[1];
526525
lightPosInWorldSpace[2] = tempVector4[2];
@@ -804,8 +803,8 @@ else if (scene.isDrawSkeleton() && objData instanceof AnimatedModel && ((Animate
804803
else {
805804
if (!infoLogged.containsKey(objData.getId() + "render")) {
806805
Log.i("ModelRenderer", "Rendering object... " + objData.getId());
807-
Log.d("ModelRenderer", objData.toString());
808-
Log.d("ModelRenderer", drawerObject.toString());
806+
Log.d("ModelRenderer", objData.getId()+": "+objData);
807+
Log.d("ModelRenderer",objData.getId()+": "+ drawerObject);
809808
infoLogged.put(objData.getId() + "render", true);
810809
}
811810
drawerObject.draw(objData, projectionMatrix, viewMatrix,

engine/src/main/res/raw/shader_anim_light_texture_colors_vert

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ attribute vec4 a_Color;
1414
varying vec4 v_Color;
1515

1616
// textures
17+
uniform bool u_Textured;
1718
attribute vec2 a_TexCoordinate;
1819
varying vec2 v_TexCoordinate;
1920

@@ -85,5 +86,7 @@ void main(){
8586
v_Color[3] = a_Color[3]; // correct alpha
8687

8788
// pass texture to fragment shader
88-
v_TexCoordinate = a_TexCoordinate;
89+
if (u_Textured) {
90+
v_TexCoordinate = a_TexCoordinate;
91+
}
8992
}

engine/src/main/res/raw/shader_anim_light_texture_vert

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ uniform vec4 vColor;
1414
varying vec4 v_Color;
1515

1616
// textures
17+
uniform bool u_Textured;
1718
attribute vec2 a_TexCoordinate;
1819
varying vec2 v_TexCoordinate;
1920

@@ -85,5 +86,7 @@ void main(){
8586
v_Color[3] = vColor[3]; // correct alpha
8687

8788
// pass texture to fragment shader
88-
v_TexCoordinate = a_TexCoordinate;
89+
if (u_Textured) {
90+
v_TexCoordinate = a_TexCoordinate;
91+
}
8992
}

engine/src/main/res/raw/shader_anim_texture_colors_vert

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ attribute vec4 a_Color;
1414
varying vec4 v_Color;
1515

1616
// textures
17+
uniform bool u_Textured;
1718
attribute vec2 a_TexCoordinate;
1819
varying vec2 v_TexCoordinate;
1920

@@ -52,5 +53,7 @@ void main(){
5253
v_Color = a_Color;
5354

5455
// pass texture to fragment shader
55-
v_TexCoordinate = a_TexCoordinate;
56+
if (u_Textured) {
57+
v_TexCoordinate = a_TexCoordinate;
58+
}
5659
}

0 commit comments

Comments
 (0)