Skip to content

Commit 7fa093e

Browse files
committed
texture issue fixed #214
1 parent 571ef43 commit 7fa093e

File tree

17 files changed

+202
-78
lines changed

17 files changed

+202
-78
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +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 (25/07/2022)
16+
News (12/09/2022)
1717
=================
1818

19-
* New version released 3.3.0
19+
* New version released 3.3.1
2020
* New orthographic, isometric and free camera views
2121
* Interactive object orientation
22+
* Texture issues fixed
2223

2324
Demo
2425
====
@@ -194,6 +195,8 @@ ChangeLog
194195

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

198+
- 3.3.1 (12/09/2022)
199+
- (f) fixed texture issue + color issue + blending issue. fixed #214
197200
- 3.3.0 (23/06/2022)
198201
- (n) interactive object orientation
199202
- (n) isometric, orthographic and free camera view
745 Bytes
Binary file not shown.

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
44
package="org.andresoviedo.dddmodel2"
5-
android:versionCode="32"
6-
android:versionName="3.3.0">
5+
android:versionCode="33"
6+
android:versionName="3.3.1">
77

88
<uses-sdk
99
tools:overrideLibrary="android.support.compat, android.arch.lifecycle" />

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

Lines changed: 89 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,11 @@ public void draw(Object3DData obj, float[] pMatrix, float[] vMatrix, int drawMod
166166

167167
// pass in texture UV buffer
168168
int mTextureHandle = -1;
169-
if (textureId != -1 && supportsTextures()) {
170-
setTexture(textureId);
171-
mTextureHandle = setVBO("a_TexCoordinate", obj.getTextureBuffer(), TEXTURE_COORDS_PER_VERTEX);
169+
if (supportsTextures()) {
170+
if (textureId != -1) {
171+
setTexture(textureId);
172+
mTextureHandle = setVBO("a_TexCoordinate", obj.getTextureBuffer(), TEXTURE_COORDS_PER_VERTEX);
173+
}
172174
}
173175

174176
// pass in the SkyBox texture
@@ -275,6 +277,14 @@ private boolean supportsTextures() {
275277
return features.contains("a_TexCoordinate");
276278
}
277279

280+
private void setTextured(boolean textured) {
281+
int handle = GLES20.glGetUniformLocation(mProgram, "u_Textured");
282+
GLUtil.checkGlError("glGetUniformLocation");
283+
284+
GLES20.glUniform1i(handle, textured? 1 : 0);
285+
GLUtil.checkGlError("glUniform1i");
286+
}
287+
278288
private void setTexture(int textureId) {
279289
int mTextureUniformHandle = GLES20.glGetUniformLocation(mProgram, "u_Texture");
280290
GLUtil.checkGlError("glGetUniformLocation");
@@ -291,6 +301,8 @@ private void setTexture(int textureId) {
291301
GLES20.glUniform1i(mTextureUniformHandle, 0);
292302
GLUtil.checkGlError("glUniform1i");
293303

304+
// toggle texture feature on
305+
setTextured(true);
294306
}
295307

296308
private void setTextureCube(int textureId) {
@@ -407,65 +419,47 @@ private void drawTrianglesUsingIndex(Object3DData obj, int drawMode, int drawSiz
407419
}
408420
}*/
409421

422+
int size = obj.getElements().size();
410423
if (id != flags.get(obj.getElements())) {
411424
Log.i("GLES20Renderer", "Rendering elements... obj: " + obj.getId()
412-
+ ", total:" + obj.getElements().size());
425+
+ ", total:" + size);
413426
flags.put(obj.getElements(), this.id);
414427
}
415428

416-
for (int i = 0; i < obj.getElements().size(); i++) {
429+
// draw rest
430+
for (int i = 0; i < size; i++) {
417431

418432
// get next element
419433
Element element = obj.getElements().get(i);
420-
drawOrderBuffer = element.getIndexBuffer();
421434

422-
423-
// log event
424-
if (id != flags.get(element)) {
425-
Log.v("GLES20Renderer", "Rendering element " + i + ".... " + element);
435+
if (element.getMaterial() == null) {
436+
drawObjectElement(obj, drawMode, drawBufferType, size, i, element);
426437
}
438+
}
427439

428-
// FIXME: there may be element without texture. so a Different shader should be used
429-
430-
// set-up materials
431-
if (element.getMaterial() != null) {
432-
if (!supportsColors()) {
433-
setUniform4(element.getMaterial().getColor() != null ? element.getMaterial().getColor() :
434-
obj.getColor() != null ? obj.getColor() : DEFAULT_COLOR, "vColor");
435-
}
436-
if (element.getMaterial().getTextureId() != -1 && supportsTextures()) {
437-
setTexture(element.getMaterial().getTextureId());
438-
}
439-
}
440+
// draw opaque elements
441+
for (int i = 0; i < size; i++) {
440442

441-
if(drawUsingUnsignedInt == false){
442-
ShortBuffer indexShortBuffer = null;
443-
drawOrderBuffer.position(0);
444-
if (indexShortBuffer == null && drawOrderBuffer != null) {
445-
indexShortBuffer = IOUtils.createShortBuffer(((IntBuffer) drawOrderBuffer).capacity());
446-
for (int j = 0; j < indexShortBuffer.capacity(); j++) {
447-
indexShortBuffer.put((short) ((IntBuffer) drawOrderBuffer).get(j));
448-
}
449-
drawOrderBuffer = indexShortBuffer;
450-
}
443+
// get next element
444+
Element element = obj.getElements().get(i);
445+
if (element.getMaterial() != null && element.getMaterial().getAlpha() == 1.0f) {
446+
drawObjectElement(obj, drawMode, drawBufferType, size, i, element);
451447
}
448+
}
452449

453-
// draw element
454-
drawOrderBuffer.position(0);
455-
GLES20.glDrawElements(drawMode, drawOrderBuffer.capacity(), drawBufferType,
456-
drawOrderBuffer);
457-
boolean error = GLUtil.checkGlError("glDrawElements");
458-
if (drawUsingUnsignedInt && error) {
459-
drawUsingUnsignedInt = false;
460-
}
450+
// draw translucent elements
451+
for (int i = 0; i < size; i++) {
452+
453+
// get next element
454+
Element element = obj.getElements().get(i);
461455

462-
// log event
463-
if (id != flags.get(element)) {
464-
Log.v("GLES20Renderer", "Rendering element " + i + " finished");
465-
flags.put(element, this.id);
456+
if (element.getMaterial() != null && element.getMaterial().getAlpha() < 1.0f) {
457+
drawObjectElement(obj, drawMode, drawBufferType, size, i, element);
466458
}
467459
}
468460

461+
462+
469463
} else {
470464
//Log.d(obj.getId(),"Drawing single elements of size '"+drawSize+"'...");
471465
for (int i = 0; i < drawOrderBuffer.capacity(); i += drawSize) {
@@ -479,6 +473,57 @@ private void drawTrianglesUsingIndex(Object3DData obj, int drawMode, int drawSiz
479473
}
480474
}
481475

476+
private void drawObjectElement(Object3DData obj, int drawMode, int drawBufferType, int size, int i, Element element) {
477+
Buffer drawOrderBuffer = element.getIndexBuffer();
478+
479+
// log event
480+
if (id != flags.get(element)) {
481+
Log.v("GLES20Renderer", "Rendering element " + i + ".... " + element);
482+
}
483+
484+
// default is no textured
485+
if (supportsTextures()){
486+
setTextured(false);
487+
}
488+
489+
if (element.getMaterial() != null) {
490+
if (!supportsColors()) {
491+
setUniform4(element.getMaterial().getColor() != null ? element.getMaterial().getColor() :
492+
obj.getColor() != null ? obj.getColor() : DEFAULT_COLOR, "vColor");
493+
}
494+
if (element.getMaterial().getTextureId() != -1 && supportsTextures()) {
495+
setTexture(element.getMaterial().getTextureId());
496+
}
497+
}
498+
499+
if(drawUsingUnsignedInt == false){
500+
ShortBuffer indexShortBuffer = null;
501+
drawOrderBuffer.position(0);
502+
if (indexShortBuffer == null && drawOrderBuffer != null) {
503+
indexShortBuffer = IOUtils.createShortBuffer(((IntBuffer) drawOrderBuffer).capacity());
504+
for (int j = 0; j < indexShortBuffer.capacity(); j++) {
505+
indexShortBuffer.put((short) ((IntBuffer) drawOrderBuffer).get(j));
506+
}
507+
drawOrderBuffer = indexShortBuffer;
508+
}
509+
}
510+
511+
// draw element
512+
drawOrderBuffer.position(0);
513+
GLES20.glDrawElements(drawMode, drawOrderBuffer.capacity(), drawBufferType,
514+
drawOrderBuffer);
515+
boolean error = GLUtil.checkGlError("glDrawElements");
516+
if (drawUsingUnsignedInt && error) {
517+
drawUsingUnsignedInt = false;
518+
}
519+
520+
// log event
521+
if (id != flags.get(element)) {
522+
Log.v("GLES20Renderer", "Rendering element " + i + " finished");
523+
flags.put(element, this.id);
524+
}
525+
}
526+
482527
private void drawPolygonsUsingIndex(Buffer drawOrderBuffer, int drawBufferType, List<int[]> polygonsList) {
483528
// Log.d(obj.getId(),"Drawing single polygons using elements...");
484529
for (int i = 0; i < polygonsList.size(); i++) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public IntBuffer getIndexBuffer() {
8787
this.indexBuffer = IOUtils.createIntBuffer(indicesArray.size());
8888
this.indexBuffer.position(0);
8989
for (int i = 0; i < indicesArray.size(); i++) {
90-
this.indexBuffer.put(indicesArray.get(i));
90+
this.indexBuffer.put(i, indicesArray.get(i));
9191
}
9292
}
9393
return indexBuffer;

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,20 @@ public void setTextureId(int textureId) {
107107
}
108108

109109
public float[] getColor(){
110-
111-
// if there is texture, we don't take into account color
112-
// some models have color black so we need to x 1.0f
113-
if (this.getTextureData() != null){
114-
return COLOR_WHITE;
110+
if (this.color == null){
111+
this.color = new float[]{1f,1f,1f,1f};
115112
}
116113
if (this.diffuse != null){
117-
if (this.color == null){
118-
this.color = new float[4];
119-
}
120114
this.color[0] = this.diffuse[0];
121115
this.color[1] = this.diffuse[1];
122116
this.color[2] = this.diffuse[2];
123-
this.color[3] = this.alpha;
124117
}
118+
if (this.ambient != null){
119+
this.color[0] += this.ambient[0];
120+
this.color[1] += this.ambient[1];
121+
this.color[2] += this.ambient[2];
122+
}
123+
this.color[3] = this.alpha;
125124
return color;
126125
}
127126

engine/src/main/java/org/andresoviedo/android_3d_model_engine/services/collada/loader/MaterialLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private Material parseMaterial(String materialId) {
161161

162162
// parse color
163163
float[] color = null;
164-
float alpha = -1;
164+
float alpha = 1;
165165
if (colorNode != null) {
166166
String[] colorData = colorNode.getData().trim().replace(',', '.').split("\\s+");
167167
color = new float[]{Float.parseFloat(colorData[0]), Float.parseFloat(colorData[1]), Float.parseFloat(colorData[2]), Float

engine/src/main/java/org/andresoviedo/util/android/ContentUtils.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
public class ContentUtils {
4040

41+
public static final String MODELS_FOLDER = "models";
4142
/**
4243
* Documents opened by the user. This list helps finding the relative filenames found in the model
4344
*/
@@ -66,13 +67,29 @@ public static void clearDocumentsProvided() {
6667
}
6768

6869
public static void provideAssets(Activity activity) {
70+
Log.i("ContentUtils", "Registering assets... ");
6971
documentsProvided.clear();
72+
provideAssets(activity, MODELS_FOLDER);
73+
Log.i("ContentUtils", "Assets found: "+ documentsProvided.size()/2);
74+
}
75+
76+
private static void provideAssets(Activity activity, String directory) {
7077
try {
71-
for (String document : activity.getAssets().list("models")) {
72-
//documentsProvided.put(document, Uri.parse("android://"+activity().getPackageName()+"/assets/models/" + document));
73-
addUri("/models/"+document, Uri.parse("android://"+activity.getPackageName()+"/assets/models/" + document));
74-
// TODO: please remove this line. We would need to implement "relative" file lookup
75-
addUri(document, Uri.parse("android://"+activity.getPackageName()+"/assets/models/" + document));
78+
final String[] files = activity.getAssets().list(directory);
79+
if (files.length > 0) {
80+
for (String document : files) {
81+
final String[] files2 = activity.getAssets().list(directory + "/" + document);
82+
if (files2.length == 0) {
83+
//documentsProvided.put(document, Uri.parse("android://"+activity().getPackageName()+"/assets/models/" + document));
84+
final Uri assetUri = Uri.parse("android://" + activity.getPackageName() + "/assets/" + directory + "/" + document);
85+
addUri(directory + "/" + document, assetUri);
86+
// TODO: please remove this line. We would need to implement "relative" file lookup
87+
addUri("/" + directory + "/" + document, assetUri);
88+
} else {
89+
Log.i("ContentUtils", "Listing directory... " + directory);
90+
provideAssets(activity, directory + "/"+document);
91+
}
92+
}
7693
}
7794

7895
} catch (IOException ex) {
@@ -98,14 +115,20 @@ public static Uri getUri(String name) {
98115
*/
99116
public static InputStream getInputStream(String path) throws IOException {
100117
Uri uri = getUri(path);
118+
if (uri == null) {
119+
uri = getUri("models/"+path);
120+
}
121+
if (uri == null) {
122+
uri = getUri("models/"+path.replaceAll("\\\\","/"));
123+
}
101124
if (uri == null && currentDir != null) {
102125
uri = Uri.parse("file://" + new File(currentDir, path).getAbsolutePath());
103126
}
104127
if (uri != null) {
105128
return getInputStream(uri);
106129
}
107-
Log.w("ContentUtils", "Media not found: " + path);
108-
Log.w("ContentUtils", "Available media: " + documentsProvided);
130+
Log.e("ContentUtils", "Media not found: " + path);
131+
Log.d("ContentUtils", "Available media: " + documentsProvided);
109132
throw new FileNotFoundException("File not found: " + path);
110133
}
111134

@@ -114,6 +137,9 @@ public static InputStream getInputStream(URI uri) throws IOException {
114137
}
115138

116139
public static InputStream getInputStream(Uri uri) throws IOException {
140+
if (getCurrentActivity() == null){
141+
throw new IllegalStateException("There is no context configured. Did you call #setContext() before?");
142+
}
117143
Log.i("ContentUtils", "Opening stream ..." + uri);
118144
if (uri.getScheme().equals("android")) {
119145
if (uri.getPath().startsWith("/assets/")) {

engine/src/main/res/raw/shader_anim_light_texture_colors_frag

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ uniform vec4 vColorMask;
55
varying vec4 v_Color;
66

77
// texture
8+
uniform bool u_Textured;
89
uniform sampler2D u_Texture;
910
varying vec2 v_TexCoordinate;
1011

1112
void main(){
12-
gl_FragColor = v_Color * texture2D(u_Texture, v_TexCoordinate) * vColorMask;
13+
if (u_Textured){
14+
gl_FragColor = v_Color * texture2D(u_Texture, v_TexCoordinate) * vColorMask;
15+
} else {
16+
gl_FragColor = v_Color * vColorMask;
17+
}
18+
gl_FragColor[3] = v_Color[3];
1319
}
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
precision mediump float;
22

33
// colors
4-
uniform vec4 vColorMask;
54
varying vec4 v_Color;
5+
uniform vec4 vColorMask;
66

77
// texture
8+
uniform bool u_Textured;
89
uniform sampler2D u_Texture;
910
varying vec2 v_TexCoordinate;
1011

1112
void main(){
12-
gl_FragColor = v_Color * texture2D(u_Texture, v_TexCoordinate) * vColorMask;
13+
if (u_Textured){
14+
gl_FragColor = v_Color * texture2D(u_Texture, v_TexCoordinate) * vColorMask;
15+
} else {
16+
gl_FragColor = v_Color * vColorMask;
17+
}
18+
gl_FragColor[3] = v_Color[3];
1319
}

0 commit comments

Comments
 (0)