Skip to content

Commit 1d5a3df

Browse files
committed
Fixes #28. Load texture is nos available
1 parent 1b6518d commit 1d5a3df

32 files changed

+1515
-20
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ The purpose of this application is to learn and share how to draw using OpenGL l
1212
* STereoLithography format (STL): https://en.wikipedia.org/wiki/STL_(file_format)
1313

1414

15-
News (23/04/2017)
15+
News (19/11/2017)
1616
=================
1717

18+
* Fixed #28: Load texture feature is now available
1819
* Fixed #18: Removed asReadOnlyBuffer() - not working on Android 7
1920
* Enhancement #17: Added support for TLS format
2021
* Fixed #16: Toogle point drawing
@@ -49,7 +50,7 @@ Whats next
4950
==========
5051

5152
* Stabilize app performance
52-
* Load multiple textures #3
53+
* Skeletal animations
5354
* Code refactoring
5455
* Carboard support
5556
* Chromecast support
@@ -126,6 +127,9 @@ ChangeLog
126127

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

130+
- 1.3.2 (19/11/2017)
131+
- (f) #28: Load texture available for any model having texture coordinates
132+
129133
- 1.3.1 (23/04/2017)
130134
- (f) #18: Removed asReadOnlyBuffer() because it is causing IndexOutOfBounds on Android 7
131135

app/build/outputs/apk/app-release.apk

11 KB
Binary file not shown.

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="org.andresoviedo.dddmodel2"
4-
android:versionCode="11"
5-
android:versionName="1.3.1" >
4+
android:versionCode="12"
5+
android:versionName="1.3.2" >
66

77
<uses-sdk
88
android:minSdkVersion="8"

app/src/main/assets/models/ToyPlane.mtl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Ni 1.00
1414
Ks 0.86 0.86 0.86
1515
newmtl phong1SG
1616
illum 4
17-
Kd 0.00 1.00 0.00
17+
Kd 1.00 1.00 1.00
1818
Ka 0.00 0.00 0.00
1919
Tf 1.00 1.00 1.00
2020
map_Kd DECALS.bmp

app/src/main/assets/models/truck.mtl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
newmtl cube1_auv
1+
newmtl blinn1SG
22
illum 4
33
Kd 1.00 1.00 1.00
44
Ka 0.00 0.00 0.00

app/src/main/java/org/andresoviedo/app/model3D/model/Object3DBuilder.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.andresoviedo.app.model3D.services.WavefrontLoader.Material;
1212
import org.andresoviedo.app.model3D.services.WavefrontLoader.Materials;
1313
import org.andresoviedo.app.model3D.services.WavefrontLoader.Tuple3;
14+
import org.andresoviedo.app.model3D.services.collada.loader.ColladaLoader;
1415
import org.andresoviedo.app.model3D.services.stl.STLLoader;
1516
import org.andresoviedo.app.model3D.services.wavefront.WavefrontLoader2;
1617
import org.andresoviedo.app.util.math.Math3DUtils;
@@ -595,7 +596,7 @@ public static Object3DData generateArrays(AssetManager assets, Object3DData obj)
595596
}
596597

597598

598-
if (textureData != null) {
599+
//if (textureData != null) {
599600
ArrayList<Tuple3> texCoords = obj.getTexCoords();
600601
if (texCoords != null && texCoords.size() > 0) {
601602

@@ -633,10 +634,10 @@ public static Object3DData generateArrays(AssetManager assets, Object3DData obj)
633634
textureOk = true;
634635
}
635636

636-
// populate texture coords if ok
637+
// populate texture coords if ok (in case we have more than 1 texture and 1 is missing. see face.obj example)
637638
int[] text = faces.facesTexIdxs.get(i);
638639
for (int j = 0; j < text.length; j++) {
639-
if (textureOk) {
640+
if (textureData == null || textureOk) {
640641
anyTextureOk = true;
641642
textureCoordsArraysBuffer.put(counter++, textureCoordsBuffer.get(text[j] * 2));
642643
textureCoordsArraysBuffer.put(counter++, textureCoordsBuffer.get(text[j] * 2 + 1));
@@ -662,7 +663,7 @@ public static Object3DData generateArrays(AssetManager assets, Object3DData obj)
662663
Log.e("Object3DBuilder", "Failure to load texture coordinates", ex);
663664
}
664665
}
665-
}
666+
//}
666667
obj.setTextureData(textureData);
667668

668669
return obj;
@@ -869,6 +870,9 @@ public static void loadV6AsyncParallel(final Activity parent, final URL url, fin
869870
} else if (modelId.toLowerCase().endsWith(".stl")) {
870871
Log.i("Object3DBuilder", "Loading STL object from: "+url);
871872
STLLoader.loadSTLAsync(parent, url, callback);
873+
} else if (modelId.toLowerCase().endsWith(".dae")) {
874+
Log.i("Object3DBuilder", "Loading Collada object from: "+url);
875+
ColladaLoader.loadAsync(parent, url, callback);
872876
}
873877
}
874878

app/src/main/java/org/andresoviedo/app/model3D/model/Object3DImpl.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ protected boolean supportsTextures() {
518518
}
519519

520520
/**
521-
* Drawer using textures & multiple colors
521+
* Drawer using textures & colors
522522
*
523523
* @author andresoviedo
524524
*
@@ -599,7 +599,7 @@ class Object3DV5 extends Object3DImpl {
599599
" float distance = length(u_LightPos - modelViewVertex);\n "+
600600
" diffuse = diffuse * (1.0 / (1.0 + (0.05 * distance * distance)));\n"+
601601
// Add ambient lighting
602-
" diffuse = diffuse + 0.3;"+
602+
" diffuse = diffuse + 0.5;"+
603603
// Multiply the color by the illumination level. It will be interpolated across the triangle.
604604
" v_Color = a_Color * diffuse;\n"+
605605
" v_Color[3] = a_Color[3];"+ // correct alpha
@@ -680,7 +680,7 @@ class Object3DV6 extends Object3DImpl {
680680
" float distance = length(u_LightPos - modelViewVertex);\n "+
681681
" diffuse = diffuse * (1.0 / (1.0 + (0.05 * distance * distance)));\n"+
682682
// Add ambient lighting
683-
" diffuse = diffuse + 0.3;"+
683+
" diffuse = diffuse + 0.5;"+
684684
// Multiply the color by the illumination level. It will be interpolated across the triangle.
685685
" v_Color = a_Color * diffuse;\n"+
686686
" v_Color[3] = a_Color[3];"+ // correct alpha
@@ -734,7 +734,7 @@ protected boolean supportsMvMatrix() {
734734
}
735735

736736
/**
737-
* Drawer using color & lights
737+
* Drawer using colors & lights
738738
*
739739
* @author andresoviedo
740740
*
@@ -766,7 +766,7 @@ class Object3DV7 extends Object3DImpl {
766766
" float distance = length(u_LightPos - modelViewVertex);\n "+
767767
" diffuse = diffuse * (1.0 / (1.0 + (0.05 * distance * distance)));\n"+
768768
// Add ambient lighting
769-
" diffuse = diffuse + 0.3;"+
769+
" diffuse = diffuse + 0.5;"+
770770
// Multiply the color by the illumination level. It will be interpolated across the triangle.
771771
" v_Color = vColor * diffuse;\n"+
772772
" v_Color[3] = vColor[3];"+ // correct alpha
@@ -830,7 +830,6 @@ class Object3DV8 extends Object3DImpl {
830830
// light variables
831831
"uniform mat4 u_MVMatrix;\n"+
832832
"uniform vec3 u_LightPos;\n"+
833-
"attribute vec4 a_Color;\n"+
834833
"attribute vec3 a_Normal;\n"+
835834
// calculated color
836835
"varying vec4 v_Color;\n"+
@@ -850,7 +849,7 @@ class Object3DV8 extends Object3DImpl {
850849
" float distance = length(u_LightPos - modelViewVertex);\n "+
851850
" diffuse = diffuse * (1.0 / (1.0 + (0.05 * distance * distance)));\n"+
852851
// Add ambient lighting
853-
" diffuse = diffuse + 0.3;"+
852+
" diffuse = diffuse + 0.5;"+
854853
// Multiply the color by the illumination level. It will be interpolated across the triangle.
855854
" v_Color = vColor * diffuse;\n"+
856855
" v_Color[3] = vColor[3];"+ // correct alpha
@@ -873,7 +872,7 @@ class Object3DV8 extends Object3DImpl {
873872
// @formatter:on
874873

875874
public Object3DV8() {
876-
super("V8", vertexShaderCode, fragmentShaderCode, "a_Position", "a_TexCoordinate", "a_Normal");
875+
super("V8", vertexShaderCode, fragmentShaderCode, "vColor", "a_Position", "a_TexCoordinate", "a_Normal");
877876
}
878877

879878
@Override

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package org.andresoviedo.app.model3D.services;
22

3+
import java.io.ByteArrayOutputStream;
34
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.IOException;
7+
import java.io.InputStream;
48
import java.net.MalformedURLException;
59
import java.net.URL;
610
import java.util.ArrayList;
@@ -11,6 +15,7 @@
1115
import org.andresoviedo.app.model3D.model.Object3DData;
1216
import org.andresoviedo.app.model3D.view.ModelActivity;
1317
import org.andresoviedo.app.util.url.android.Handler;
18+
import org.apache.commons.io.IOUtils;
1419

1520
import android.os.SystemClock;
1621
import android.util.Log;
@@ -227,7 +232,7 @@ public void toggleLighting() {
227232
}
228233
else if (this.drawLighting && !this.rotatingLight){
229234
this.drawLighting = false;
230-
makeToastText("Lightsoff", Toast.LENGTH_SHORT);
235+
makeToastText("Lights off", Toast.LENGTH_SHORT);
231236
}
232237
else {
233238
this.drawLighting = true;
@@ -253,4 +258,23 @@ public void setSelectedObject(Object3DData selectedObject) {
253258
this.selectedObject = selectedObject;
254259
}
255260

261+
public void loadTexture(String path){
262+
if (objects.size() != 1) {
263+
makeToastText("Unavailable", Toast.LENGTH_SHORT);
264+
return;
265+
}
266+
267+
try {
268+
InputStream is = new FileInputStream(path);
269+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
270+
IOUtils.copy(is,bos);
271+
is.close();
272+
273+
Object3DData obj = objects.get(0);
274+
obj.setTextureData(bos.toByteArray());
275+
} catch (IOException ex) {
276+
makeToastText("Problem loading texture: "+ex.getMessage(), Toast.LENGTH_SHORT);
277+
}
278+
}
279+
256280
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.andresoviedo.app.model3D.services.collada.entities;
2+
3+
/**
4+
* Contains the extracted data for an animated model, which includes the mesh data, and skeleton (joints heirarchy) data.
5+
* @author Karl
6+
*
7+
*/
8+
public class AnimatedModelData {
9+
10+
private final SkeletonData joints;
11+
private final MeshData mesh;
12+
13+
public AnimatedModelData(MeshData mesh, SkeletonData joints){
14+
this.joints = joints;
15+
this.mesh = mesh;
16+
}
17+
18+
public SkeletonData getJointsData(){
19+
return joints;
20+
}
21+
22+
public MeshData getMeshData(){
23+
return mesh;
24+
}
25+
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.andresoviedo.app.model3D.services.collada.entities;
2+
3+
/**
4+
* Contains the extracted data for an animation, which includes the length of
5+
* the entire animation and the data for all the keyframes of the animation.
6+
*
7+
* @author Karl
8+
*
9+
*/
10+
public class AnimationData {
11+
12+
public final float lengthSeconds;
13+
public final KeyFrameData[] keyFrames;
14+
15+
public AnimationData(float lengthSeconds, KeyFrameData[] keyFrames) {
16+
this.lengthSeconds = lengthSeconds;
17+
this.keyFrames = keyFrames;
18+
}
19+
20+
}

0 commit comments

Comments
 (0)