diff --git a/app/src/main/java/com/google/ar/core/examples/java/common/java3d/Appearance.java b/app/src/main/java/com/google/ar/core/examples/java/common/java3d/Appearance.java index 272795f..002266e 100644 --- a/app/src/main/java/com/google/ar/core/examples/java/common/java3d/Appearance.java +++ b/app/src/main/java/com/google/ar/core/examples/java/common/java3d/Appearance.java @@ -6,6 +6,8 @@ private TextureAttributes textureAttribute = null; private TexCoordGeneration texCoordGeneration = null; private TextureUnitState[] stateArray; + private TransparencyAttributes transparencyAttributes = null; + private ColoringAttributes coloringAttributes = null; public void setMaterial(Material m) { this.material = m; @@ -57,6 +59,22 @@ this.texCoordGeneration = texCoordGeneration; } + public TransparencyAttributes getTransparencyAttributes() { + return transparencyAttributes; + } + + public void setTransparencyAttributes(TransparencyAttributes transparencyAttributes) { + this.transparencyAttributes = transparencyAttributes; + } + + public ColoringAttributes getColoringAttributes() { + return coloringAttributes; + } + + public void setColoringAttributes(ColoringAttributes coloringAttributes) { + this.coloringAttributes = coloringAttributes; + } + @Override public NodeComponent cloneNodeComponent() { Appearance ap = new Appearance(); diff --git a/app/src/main/java/com/google/ar/core/examples/java/common/java3d/ColoringAttributes.java b/app/src/main/java/com/google/ar/core/examples/java/common/java3d/ColoringAttributes.java new file mode 100644 index 0000000..91c277e --- /dev/null +++ b/app/src/main/java/com/google/ar/core/examples/java/common/java3d/ColoringAttributes.java @@ -0,0 +1,51 @@ +package com.google.ar.core.examples.java.common.java3d; + +public class ColoringAttributes extends NodeComponent { + public static final int FASTEST = 0; + + public static final int NICEST = 1; + + public static final int SHADE_FLAT = 2; + + public static final int SHADE_GOURAUD = 3; + + private Color3f color; + + private int shadeModel; + + public ColoringAttributes() { + color = new Color3f(1.0f, 1.0f, 1.0f); + shadeModel = SHADE_GOURAUD; + } + + public ColoringAttributes(Color3f color, int shadeModel) { + this.color = color; + this.shadeModel = shadeModel; + } + + public ColoringAttributes(float r, float g, float b, int shadeModel) { + this.color = new Color3f(r, g, b); + this.shadeModel = shadeModel; + } + + public Color3f getColor() { + return color; + } + + public void setColor(Color3f color) { + this.color = color; + } + + public int getShadeModel() { + return shadeModel; + } + + public void setShadeModel(int shadeModel) { + this.shadeModel = shadeModel; + } + + @Override + public NodeComponent cloneNodeComponent() { + return new ColoringAttributes(color.clone(), shadeModel); + } +} diff --git a/app/src/main/java/com/google/ar/core/examples/java/common/java3d/GraphicsContext3D.java b/app/src/main/java/com/google/ar/core/examples/java/common/java3d/GraphicsContext3D.java index 48ddffd..68f87c4 100644 --- a/app/src/main/java/com/google/ar/core/examples/java/common/java3d/GraphicsContext3D.java +++ b/app/src/main/java/com/google/ar/core/examples/java/common/java3d/GraphicsContext3D.java @@ -1,5 +1,6 @@ package com.google.ar.core.examples.java.common.java3d; +import android.opengl.GLES11Ext; import android.opengl.GLES20; import android.opengl.GLU; import android.opengl.GLUtils; @@ -44,8 +45,6 @@ gl.glDepthFunc(GL10.GL_LEQUAL); gl.glDepthMask(true); - // ライトを有効にする - gl.glEnable(GL10.GL_LIGHTING); // どの光源を使用するか指定 gl.glEnable(GL10.GL_LIGHT0); @@ -184,14 +183,52 @@ public void setAppearance(Appearance appearance) { this.appearance = appearance; + TransparencyAttributes transparencyAttributes = appearance.getTransparencyAttributes(); Material material = appearance.getMaterial(); - if (material != null) { + if (material != null && material.getLightingEnable()) { + // ライトを有効にする + gl.glEnable(GL10.GL_LIGHTING); // 表面属性の設定 - gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_DIFFUSE, material.diffuse, 0); - gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_AMBIENT, material.ambient, 0); - gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_SPECULAR, material.specular, 0); - gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_EMISSION, material.emissive, 0); - gl.glMaterialf(GL10.GL_FRONT, GL10.GL_SHININESS, material.shininess); + if (transparencyAttributes == null) { + gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_DIFFUSE, material.diffuse, 0); + gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_AMBIENT, material.ambient, 0); + gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_SPECULAR, material.specular, 0); + gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_EMISSION, material.emissive, 0); + gl.glMaterialf(GL10.GL_FRONT, GL10.GL_SHININESS, material.shininess); + } else { + float diffuse[] = material.diffuse.clone(); + float ambient[] = material.ambient.clone(); + float specular[] = material.specular.clone(); + float emissive[] = material.emissive.clone(); + diffuse[3] = transparencyAttributes.getTransparency(); + ambient[3] = transparencyAttributes.getTransparency(); + specular[3] = transparencyAttributes.getTransparency(); + emissive[3] = transparencyAttributes.getTransparency(); + gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_DIFFUSE, diffuse, 0); + gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_AMBIENT, ambient, 0); + gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_SPECULAR, specular, 0); + gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_EMISSION, emissive, 0); + gl.glMaterialf(GL10.GL_FRONT, GL10.GL_SHININESS, material.shininess); + } + } else { + // ライトを無効にする + gl.glDisable(GL10.GL_LIGHTING); + Color3f color; + if (appearance.getColoringAttributes() != null) { + color = appearance.getColoringAttributes().getColor(); + } else { + color = new Color3f(1.0f, 1.0f, 1.0f); + } + if (transparencyAttributes == null) { + gl.glColor4f(color.r, color.g, color.b, 1.0f); + } else { + gl.glColor4f(color.r, color.g, color.b, transparencyAttributes.getTransparency()); + } + } + if (transparencyAttributes != null) { + // 透過属性が指定されている場合 + gl.glDepthMask(false); + setTransparencyAttributes(transparencyAttributes); } if (appearance.getTextureUnitCount() == 0) { // テクスチャユニットを使っていない場合(通常のテクスチャの場合) @@ -232,10 +269,39 @@ } } + public void resetAppearance(Appearance appearance) { + if (appearance.getTextureUnitCount() == 0) { + // テクスチャユニットを使っていない場合(通常のテクスチャの場合) + if (appearance.getTexture() != null) { + gl.glDisable(GL10.GL_TEXTURE_2D); + } + } else { + // テクスチャユニットを使っている場合 + for (int n = 0; n < appearance.getTextureUnitCount(); n++) { + gl.glActiveTexture(GL10.GL_TEXTURE0 + n); + gl.glDisable(GL10.GL_TEXTURE_2D); + } + gl.glActiveTexture(GL10.GL_TEXTURE0); + } + if (appearance.getTransparencyAttributes() != null) { + // 透明属性を使っている場合 + gl.glDisable(GL10.GL_BLEND); + gl.glDepthMask(true); + } + Material material = appearance.getMaterial(); + if (material != null) { + if (material.getLightingEnable()) { + // ライトを有効にする + gl.glDisable(GL10.GL_LIGHTING); + } + } + } + public void draw(Shape3D node) { if (node == null) return; setAppearance(node.getAppearance()); draw(node.getGeometry()); + resetAppearance(node.getAppearance()); } public void draw(Geometry g) { @@ -354,21 +420,40 @@ } gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); } - if (appearance.getTextureUnitCount() == 0) { - // テクスチャユニットを使っていない場合(通常のテクスチャの場合) - if (appearance.getTexture() != null) { - gl.glDisable(GL10.GL_TEXTURE_2D); - } - } else { - // テクスチャユニットを使っている場合 - for (int n = 0; n < appearance.getTextureUnitCount(); n++) { - gl.glActiveTexture(GL10.GL_TEXTURE0 + n); - gl.glDisable(GL10.GL_TEXTURE_2D); - } - gl.glActiveTexture(GL10.GL_TEXTURE0); + } + + private void setTransparencyAttributes(TransparencyAttributes ta) { + int transparencyMode = ta.getTransparencyMode(); + switch (transparencyMode) { + case TransparencyAttributes.FASTEST: + case TransparencyAttributes.NICEST: + case TransparencyAttributes.BLENDED: + gl.glEnable(GL10.GL_BLEND); + gl.glBlendFunc(getBlendFunction(ta.getSrcBlendFunction()), getBlendFunction(ta.getDstBlendFunction())); + break; } } + private int getBlendFunction(int blendFunction) { + switch (blendFunction) { + case TransparencyAttributes.BLEND_SRC_ALPHA: + return GL10.GL_SRC_ALPHA; + case TransparencyAttributes.BLEND_SRC_COLOR: + return GL10.GL_SRC_COLOR; + case TransparencyAttributes.BLEND_ONE_MINUS_SRC_ALPHA: + return GL10.GL_ONE_MINUS_SRC_ALPHA; + case TransparencyAttributes.BLEND_ONE_MINUS_SRC_COLOR: + return GL10.GL_ONE_MINUS_SRC_COLOR; + case TransparencyAttributes.BLEND_ONE_MINUS_DST_COLOR: + return GL10.GL_ONE_MINUS_DST_COLOR; + case TransparencyAttributes.BLEND_ONE: + return GL10.GL_ONE; + case TransparencyAttributes.BLEND_ZERO: + return GL10.GL_ZERO; + } + return GL10.GL_ZERO; + } + private void setTextureAttributes(TextureAttributes ta) { int textureMode = ta.getTextureMode(); switch (textureMode) { diff --git a/app/src/main/java/com/google/ar/core/examples/java/common/java3d/Material.java b/app/src/main/java/com/google/ar/core/examples/java/common/java3d/Material.java index f8c426b..847aae5 100644 --- a/app/src/main/java/com/google/ar/core/examples/java/common/java3d/Material.java +++ b/app/src/main/java/com/google/ar/core/examples/java/common/java3d/Material.java @@ -1,25 +1,15 @@ package com.google.ar.core.examples.java.common.java3d; public class Material extends NodeComponent { - float diffuseR = 1.0f; - float diffuseG = 1.0f; - float diffuseB = 1.0f; - float ambientR = 0.2f; - float ambientG = 0.2f; - float ambientB = 0.2f; - float specularR = 1.0f; - float specularG = 1.0f; - float specularB = 1.0f; - float emissiveR = 0.0f; - float emissiveG = 0.0f; - float emissiveB = 0.0f; - float shininess = 64.0f; + float shininess = 64.0f; float[] diffuse = null; float[] ambient = null; float[] specular = null; float[] emissive = null; + boolean lightingEnable = true; public Material() { + lightingEnable = true; setDiffuseColor(1.0f, 1.0f, 1.0f); setAmbientColor(0.2f, 0.2f, 0.2f); setSpecularColor(1.0f, 1.0f, 1.0f); @@ -28,30 +18,18 @@ } public void setDiffuseColor(float r, float g, float b) { - this.diffuseR = r; - this.diffuseG = g; - this.diffuseB = b; diffuse = new float[]{r, g ,b, 1.0f}; } public void setAmbientColor(float r, float g, float b) { - this.ambientR = r; - this.ambientG = g; - this.ambientB = b; ambient = new float[]{r, g ,b, 1.0f}; } public void setSpecularColor(float r, float g, float b) { - this.specularR = r; - this.specularG = g; - this.specularB = b; specular = new float[]{r, g ,b, 1.0f}; } public void setEmissiveColor(float r, float g, float b) { - this.emissiveR = r; - this.emissiveG = g; - this.emissiveB = b; emissive = new float[]{r, g ,b, 1.0f}; } @@ -59,14 +37,23 @@ this.shininess = shininess; } + public boolean getLightingEnable() { + return lightingEnable; + } + + public void setLightingEnable(boolean lightingEnable) { + this.lightingEnable = lightingEnable; + } + @Override public NodeComponent cloneNodeComponent() { Material m = new Material(); - m.setDiffuseColor(diffuseR, diffuseG, diffuseB); - m.setAmbientColor(ambientR, ambientG, ambientB); - m.setSpecularColor(specularR, specularG, specularB); - m.setEmissiveColor(emissiveR, emissiveG, emissiveB); + m.setDiffuseColor(diffuse[0], diffuse[1], diffuse[2]); + m.setAmbientColor(ambient[0], ambient[1], ambient[2]); + m.setSpecularColor(specular[0], specular[1], specular[2]); + m.setEmissiveColor(emissive[0], emissive[1], emissive[2]); m.setShininess(shininess); + m.setLightingEnable(lightingEnable); return m; } diff --git a/app/src/main/java/com/google/ar/core/examples/java/common/java3d/TransparencyAttributes.java b/app/src/main/java/com/google/ar/core/examples/java/common/java3d/TransparencyAttributes.java new file mode 100644 index 0000000..4258a5d --- /dev/null +++ b/app/src/main/java/com/google/ar/core/examples/java/common/java3d/TransparencyAttributes.java @@ -0,0 +1,93 @@ +package com.google.ar.core.examples.java.common.java3d; + +public class TransparencyAttributes extends NodeComponent { + public static final int FASTEST = 0; + + public static final int NICEST = 1; + + public static final int BLENDED = 2; + + public static final int SCREEN_DOOR = 3; + + public static final int NONE = 4; + + public static final int BLEND_ZERO = 0; + + public static final int BLEND_ONE = 1; + + public static final int BLEND_SRC_ALPHA = 2; + + public static final int BLEND_ONE_MINUS_SRC_ALPHA = 3; + + public static final int BLEND_DST_COLOR = 4; + + public static final int BLEND_ONE_MINUS_DST_COLOR = 5; + + public static final int BLEND_SRC_COLOR = 6; + + public static final int BLEND_ONE_MINUS_SRC_COLOR = 7; + + static final int BLEND_CONSTANT_COLOR = 8; + + static final int MAX_BLEND_FUNC_TABLE_SIZE = 9; + + private int transparencyMode = NONE; + + private float transparency = 0.0f; + + private int srcBlendFunction = BLEND_SRC_ALPHA; + + private int dstBlendFunction = BLEND_ONE_MINUS_SRC_ALPHA; + + public TransparencyAttributes() { + this(NONE, 0.0f); + } + + public TransparencyAttributes(int tMode, float tVal) { + this(tMode, tVal, BLEND_SRC_ALPHA, BLEND_ONE_MINUS_SRC_ALPHA); + } + + public TransparencyAttributes(int tMode, float tVal, int srcBlendFunction, int dstBlendFunction) { + transparencyMode = tMode; + transparency = tVal; + this.srcBlendFunction = srcBlendFunction; + this.dstBlendFunction = dstBlendFunction; + } + + public int getTransparencyMode() { + return transparencyMode; + } + + public void setTransparencyMode(int transparencyMode) { + this.transparencyMode = transparencyMode; + } + + public float getTransparency() { + return transparency; + } + + public void setTransparency(float transparency) { + this.transparency = transparency; + } + + public int getSrcBlendFunction() { + return srcBlendFunction; + } + + public void setSrcBlendFunction(int srcBlendFunction) { + this.srcBlendFunction = srcBlendFunction; + } + + public int getDstBlendFunction() { + return dstBlendFunction; + } + + public void setDstBlendFunction(int dstBlendFunction) { + this.dstBlendFunction = dstBlendFunction; + } + + @Override + public NodeComponent cloneNodeComponent() { + return new TransparencyAttributes(transparencyMode, transparency, srcBlendFunction, dstBlendFunction); + } +}