Skip to content

Commit dd982da

Browse files
committed
#61 shader refactoring
1 parent 16fd0bc commit dd982da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+373
-1461
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ You may need one of this glasses to view models in 3D virtual reality.
164164
Documentation
165165
=============
166166

167-
https://github.com/the3deers/android-3D-model-viewer/wiki
167+
Working on it...
168168

169169

170170
Acknowledgement
@@ -204,12 +204,12 @@ 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
209-
- (i) texture loading refactoring
210-
- (i) shader refactoring
207+
- 3.4.1 (23/09/2022)
208+
- (f) bug fixing: lighting, textures, etc #176
209+
- (i) texture loading refactoring #61
210+
- (i) shader refactoring & deduplication #61
211211
- 3.4.0 (17/09/2022)
212-
- (n) GLTF basic support
212+
- (n) GLTF basic support #176
213213
- 3.3.1 (12/09/2022)
214214
- (f) fixed texture issue + color issue + blending issue. fixed #214
215215
- (f) fixed texture issue #204

engine/src/main/java/org/andresoviedo/android_3d_model_engine/camera/IsometricCamera.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ private void rotateWithMatrix(float[] cross) {
155155
final float[] cross2 = Math3DUtils.crossProduct(posN, cross);
156156
final double dot = Math3DUtils.dotProduct(axis, cross2);
157157

158-
Log.v("IsometricCamera", "rotateWithMatrix. dot: " + dot
158+
/*Log.v("IsometricCamera", "rotateWithMatrix. dot: " + dot
159159
+ ", axis: " + Arrays.toString(axis)
160160
+ ", cross: " + Arrays.toString(cross)
161-
+ ", angle: " + Math3DUtils.calculateAngleBetween(axis, cross));
161+
+ ", angle: " + Math3DUtils.calculateAngleBetween(axis, cross));*/
162162
final double angle = Math.signum(dot) * Math.PI / 2;
163163

164164
final float[] rotMatrix = new float[16];
@@ -177,7 +177,7 @@ private void rotateWithMatrix(float[] cross) {
177177

178178
saveAndAnimate(newPos[0], newPos[1], newPos[2], newUp[0], newUp[1], newUp[2]);
179179

180-
Log.v("IsometricCamera", "Rotating... action: " + delegate.getAnimation());
180+
//Log.v("IsometricCamera", "Rotating... action: " + delegate.getAnimation());
181181
}
182182

183183
private void saveAndAnimate(float xp, float yp, float zp, float xu, float yu, float zu) {

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

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class GLES20Renderer implements Renderer {
4444
private static final int TEXTURE_COORDS_PER_VERTEX = 2;
4545
private static final int COLOR_COORDS_PER_VERTEX = 4;
4646

47-
private final static float[] DEFAULT_COLOR = {1.0f, 1.0f, 1.0f, 1.0f};
47+
private final static float[] DEFAULT_COLOR = {0.0f, 1.0f, 1.0f, 1.0f};
4848
private final static float[] NO_COLOR_MASK = {1.0f, 1.0f, 1.0f, 1.0f};
4949

5050
// specification
@@ -75,6 +75,10 @@ class GLES20Renderer implements Renderer {
7575
*/
7676
private static Map<Object, Object> flags = new HashMap<>();
7777

78+
private boolean texturesEnabled = true;
79+
private boolean lightingEnabled = true;
80+
private boolean animationEnabled = true;
81+
7882
static GLES20Renderer getInstance(String id, String vertexShaderCode, String fragmentShaderCode) {
7983
Set<String> shaderFeatures = new HashSet<>();
8084
testShaderFeature(shaderFeatures, vertexShaderCode, "u_MMatrix");
@@ -83,6 +87,7 @@ static GLES20Renderer getInstance(String id, String vertexShaderCode, String fra
8387
testShaderFeature(shaderFeatures, vertexShaderCode, "a_Color");
8488
testShaderFeature(shaderFeatures, vertexShaderCode, "a_TexCoordinate");
8589
testShaderFeature(shaderFeatures, vertexShaderCode, "u_LightPos");
90+
testShaderFeature(shaderFeatures, fragmentShaderCode, "u_LightPos");
8691
testShaderFeature(shaderFeatures, vertexShaderCode, "in_jointIndices");
8792
testShaderFeature(shaderFeatures, vertexShaderCode, "in_weights");
8893
testShaderFeature(shaderFeatures, fragmentShaderCode, "u_TextureCube");
@@ -112,6 +117,18 @@ private GLES20Renderer(String id, String vertexShaderCode, String fragmentShader
112117
Log.d("GLES20Renderer", "Compiled 3D Drawer (" + id + ") with id " + mProgram);
113118
}
114119

120+
public void setTexturesEnabled(boolean texturesEnabled) {
121+
this.texturesEnabled = texturesEnabled;
122+
}
123+
124+
public void setLightingEnabled(boolean lightingEnabled) {
125+
this.lightingEnabled = lightingEnabled;
126+
}
127+
128+
public void setAnimationEnabled(boolean animationEnabled) {
129+
this.animationEnabled = animationEnabled;
130+
}
131+
115132
@Override
116133
public void draw(Object3DData obj, float[] pMatrix, float[] vMatrix, int textureId, float[] lightPosInWorldSpace, float[] colorMask, float[] cameraPos, int drawMode, int drawSize) {
117134

@@ -128,6 +145,8 @@ public void draw(Object3DData obj, float[] pMatrix, float[] vMatrix, int texture
128145
return;
129146
}
130147

148+
//setFeatureFlag("u_Debug",false);
149+
131150
// mvp matrix for position + lighting + animation
132151
if(supportsMMatrix()) {
133152
setUniformMatrix4(obj.getModelMatrix(), "u_MMatrix");
@@ -146,8 +165,12 @@ public void draw(Object3DData obj, float[] pMatrix, float[] vMatrix, int texture
146165

147166
// pass in color or colors array
148167
int mColorHandle = -1;
149-
if (supportsColors()) {
150-
mColorHandle = setVBO("a_Color", obj.getColorsBuffer(), COLOR_COORDS_PER_VERTEX);
168+
if (supportsColors()){
169+
setFeatureFlag("u_Coloured",false);
170+
if (obj.getColorsBuffer() != null) {
171+
mColorHandle = setVBO("a_Color", obj.getColorsBuffer(), COLOR_COORDS_PER_VERTEX);
172+
setFeatureFlag("u_Coloured", true);
173+
}
151174
} else {
152175
setUniform4(obj.getColor() != null? obj.getColor() : DEFAULT_COLOR,"vColor");
153176
}
@@ -158,8 +181,9 @@ public void draw(Object3DData obj, float[] pMatrix, float[] vMatrix, int texture
158181
// pass in texture UV buffer
159182
int mTextureHandle = -1;
160183
if (supportsTextures()) {
161-
162-
setFeatureFlag("u_Textured", false);
184+
setFeatureFlag("u_Textured", texturesEnabled);
185+
}
186+
if (supportsTextures() && obj.getTextureBuffer() != null) {
163187

164188
// load color map
165189
if (obj.getMaterial().getTextureId() == -1 &&
@@ -238,19 +262,25 @@ public void draw(Object3DData obj, float[] pMatrix, float[] vMatrix, int texture
238262
}
239263

240264
// pass in light position for lighting
241-
if (lightPosInWorldSpace != null && supportsLighting()) {
265+
if (supportsLighting()) {
266+
boolean toggle = lightingEnabled && obj.getNormalsBuffer() != null;
267+
setFeatureFlag("u_Lighted", toggle);
242268
setUniform3(lightPosInWorldSpace,"u_LightPos");
243269
setUniform3(cameraPos,"u_cameraPos");
244270
}
245271

246272
// pass in joint transformation for animated model
247273
int in_weightsHandle = -1;
248274
int in_jointIndicesHandle = -1;
249-
if (supportsJoints() && obj instanceof AnimatedModel) {
250-
in_weightsHandle = setVBO("in_weights", ((AnimatedModel) obj).getVertexWeights(), COORDS_PER_VERTEX);
251-
in_jointIndicesHandle = setVBO("in_jointIndices", ((AnimatedModel) obj).getJointIds(), COORDS_PER_VERTEX);
252-
setUniformMatrix4(((AnimatedModel) obj).getBindShapeMatrix(), "u_BindShapeMatrix");
253-
setJointTransforms((AnimatedModel) obj);
275+
if (supportsJoints()){
276+
boolean toggle = this.animationEnabled && obj instanceof AnimatedModel;
277+
if(toggle) {
278+
in_weightsHandle = setVBO("in_weights", ((AnimatedModel) obj).getVertexWeights(), COORDS_PER_VERTEX);
279+
in_jointIndicesHandle = setVBO("in_jointIndices", ((AnimatedModel) obj).getJointIds(), COORDS_PER_VERTEX);
280+
setUniformMatrix4(((AnimatedModel) obj).getBindShapeMatrix(), "u_BindShapeMatrix");
281+
setJointTransforms((AnimatedModel) obj);
282+
}
283+
setFeatureFlag("u_Animated", toggle);
254284
}
255285

256286
// draw mesh
@@ -537,16 +567,28 @@ private void drawObjectElement(Object3DData obj, int drawMode, int drawBufferTyp
537567
Log.v("GLES20Renderer", "Rendering element " + i + ".... " + element);
538568
}
539569

570+
if (element.getMaterial() != null && element.getMaterial().getColor() != null){
571+
setUniform4(element.getMaterial().getColor(), "vColor");
572+
} else if (obj.getColor() != null){
573+
setUniform4(obj.getColor(), "vColor");
574+
} else {
575+
setUniform4(DEFAULT_COLOR, "vColor");
576+
}
577+
578+
// default is no textured
579+
if (supportsColors()){
580+
setFeatureFlag("u_Coloured", obj.getColorsBuffer() != null);
581+
}
582+
540583
// default is no textured
541584
if (supportsTextures()){
542-
setFeatureFlag("u_Textured", false);
585+
setFeatureFlag("u_Textured", obj.getTextureBuffer() != null
586+
&& element.getMaterial() != null && element.getMaterial().getTextureId() != -1
587+
&& texturesEnabled);
543588
}
544589

545590
if (element.getMaterial() != null) {
546-
if (!supportsColors()) {
547-
setUniform4(element.getMaterial().getColor() != null ? element.getMaterial().getColor() :
548-
obj.getColor() != null ? obj.getColor() : DEFAULT_COLOR, "vColor");
549-
}
591+
550592
// load color map
551593
if (element.getMaterial().getTextureId() == -1 &&
552594
element.getMaterial().getColorTexture() != null){
@@ -600,17 +642,17 @@ private void drawObjectElement(Object3DData obj, int drawMode, int drawBufferTyp
600642
}
601643
if (element.getMaterial().getTextureId() != -1 && supportsTextures()) {
602644
setTexture(element.getMaterial().getTextureId(), "u_Texture", 0);
603-
setFeatureFlag("u_Textured",true);
645+
setFeatureFlag("u_Textured",texturesEnabled);
604646
}
605647

606648
if (element.getMaterial().getNormalTextureId() != -1 && supportsTextures()) {
607649
setTexture(element.getMaterial().getNormalTextureId(), "u_NormalTexture", 1);
608-
setFeatureFlag("u_NormalTextured",true);
650+
setFeatureFlag("u_NormalTextured",texturesEnabled);
609651
}
610652

611653
if (element.getMaterial().getEmissiveTextureId() != -1 && supportsTextures()){
612654
setTexture(element.getMaterial().getEmissiveTextureId(), "u_EmissiveTexture", 2);
613-
setFeatureFlag("u_EmissiveTextured", true);
655+
setFeatureFlag("u_EmissiveTextured", texturesEnabled);
614656
}
615657
}
616658

0 commit comments

Comments
 (0)