From ca5d7a745df563d13851d63fc654b8fb7b0451e1 Mon Sep 17 00:00:00 2001 From: cerg2010cerg2010 Date: Tue, 27 Jun 2017 14:16:26 +0000 Subject: [PATCH] Refactor Appearance OpenGL setup, add perspective coorection support --- .../javax/microedition/m3g/Appearance.java | 53 ++++++++++++++++++- .../javax/microedition/m3g/Background.java | 2 +- .../microedition/m3g/CompositingMode.java | 22 ++++---- .../main/java/javax/microedition/m3g/Fog.java | 26 ++++----- .../javax/microedition/m3g/Graphics3D.java | 50 +++-------------- .../java/javax/microedition/m3g/Material.java | 15 ++++-- .../javax/microedition/m3g/PolygonMode.java | 25 ++++----- 7 files changed, 104 insertions(+), 89 deletions(-) diff --git a/app/src/main/java/javax/microedition/m3g/Appearance.java b/app/src/main/java/javax/microedition/m3g/Appearance.java index 3bd7bc126..dc1eaf417 100644 --- a/app/src/main/java/javax/microedition/m3g/Appearance.java +++ b/app/src/main/java/javax/microedition/m3g/Appearance.java @@ -1,5 +1,7 @@ package javax.microedition.m3g; +import javax.microedition.khronos.opengles.GL10; + public class Appearance extends Object3D { int numTextureUnits = 8; @@ -58,8 +60,8 @@ public Material getMaterial() { return material; } - public void setCompositingMode(CompositingMode compositingMode) { - this.compositingMode = compositingMode; + public void setCompositingMode(CompositingMode comp) { + this.compositingMode = comp; } public CompositingMode getCompositingMode() { @@ -77,4 +79,51 @@ public Texture2D getTexture(int index) { throw new IndexOutOfBoundsException("index must be in [0," + numTextureUnits + "]"); return textures[index]; } + + void setupGL(GL10 gl) { + if (compositingMode != null) + compositingMode.setupGL(gl); + else { + gl.glDepthFunc(GL10.GL_LEQUAL); + gl.glDepthMask(true); + + gl.glColorMask(true, true, true, true); + + gl.glAlphaFunc(GL10.GL_GEQUAL, 0.0f); + gl.glDisable(GL10.GL_ALPHA_TEST); + + //gl.glDisable(GL10.GL_BLEND); + gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); + gl.glEnable(GL10.GL_BLEND); + + gl.glDisable(GL10.GL_POLYGON_OFFSET_FILL); + } + + if (polygonMode != null) + polygonMode.setupGL(gl); + else { + gl.glCullFace(GL10.GL_BACK); + gl.glEnable(GL10.GL_CULL_FACE); + + gl.glShadeModel(GL10.GL_SMOOTH); + + gl.glFrontFace(GL10.GL_CCW); + + gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); + + gl.glLightModelf(GL10.GL_LIGHT_MODEL_TWO_SIDE, GL10.GL_FALSE); + } + + if (material != null) + material.setupGL(gl); + else { + gl.glDisable(GL10.GL_COLOR_MATERIAL); + gl.glDisable(GL10.GL_LIGHTING); + } + + if (fog != null) + fog.setupGL(gl); + else + gl.glDisable(GL10.GL_FOG); + } } diff --git a/app/src/main/java/javax/microedition/m3g/Background.java b/app/src/main/java/javax/microedition/m3g/Background.java index 2183a6c30..86e1310a7 100644 --- a/app/src/main/java/javax/microedition/m3g/Background.java +++ b/app/src/main/java/javax/microedition/m3g/Background.java @@ -33,7 +33,7 @@ public Background() { vertexBuffer = ByteBuffer.allocateDirect(4 * 3 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); vertexBuffer.put(vertexArray); vertexBuffer.flip(); - // 4 elements, 2 coordinates per element, float type + // 4 elements, 2 coordinates per element, float type textureBuffer = ByteBuffer.allocateDirect(4 * 2 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); textureArray = new float[4 * 2]; } diff --git a/app/src/main/java/javax/microedition/m3g/CompositingMode.java b/app/src/main/java/javax/microedition/m3g/CompositingMode.java index 29c87c1a3..499f3bf82 100644 --- a/app/src/main/java/javax/microedition/m3g/CompositingMode.java +++ b/app/src/main/java/javax/microedition/m3g/CompositingMode.java @@ -71,6 +71,7 @@ public boolean isAlphaWriteEnabled() { public void setBlending(int blending) { switch (blending) { case ALPHA: + System.out.println("Alpha CompositingMode"); case ALPHA_ADD: case MODULATE: case MODULATE_X2: @@ -115,25 +116,20 @@ public void setDepthOffset(float depthOffsetFactor, float depthOffsetUnits) { this.depthOffsetUnits = depthOffsetUnits; } - void setupGL(GL10 gl, boolean depthBufferEnabled) { + void setupGL(GL10 gl) { gl.glDepthFunc(depthTestEnabled ? GL10.GL_LEQUAL : GL10.GL_ALWAYS); -/* - // Setup depth testing - if (depthBufferEnabled && depthTestEnabled) - gl.glEnable(GL10.GL_DEPTH_TEST); - else - gl.glDisable(GL10.GL_DEPTH_TEST);*/ // Setup depth and color writes gl.glDepthMask(depthWriteEnabled); gl.glColorMask(colorWriteEnabled, colorWriteEnabled, colorWriteEnabled, alphaWriteEnabled); // Setup alpha testing - if (alphaThreshold > 0) { + if (alphaThreshold == 0.0f) + gl.glDisable(GL10.GL_ALPHA_TEST); + else { gl.glAlphaFunc(GL10.GL_GEQUAL, alphaThreshold); gl.glEnable(GL10.GL_ALPHA_TEST); - } else - gl.glDisable(GL10.GL_ALPHA_TEST); + } // Setup blending if (blending != REPLACE) { @@ -158,10 +154,10 @@ void setupGL(GL10 gl, boolean depthBufferEnabled) { gl.glDisable(GL10.GL_BLEND); // Setup depth offset - if (depthOffsetFactor != 0 || depthOffsetUnits != 0) { - gl.glPolygonOffset(depthOffsetFactor, depthOffsetUnits); + gl.glPolygonOffset(depthOffsetFactor, depthOffsetUnits); + if (depthOffsetFactor != 0 || depthOffsetUnits != 0) gl.glEnable(GL10.GL_POLYGON_OFFSET_FILL); - } else + else gl.glDisable(GL10.GL_POLYGON_OFFSET_FILL); } } diff --git a/app/src/main/java/javax/microedition/m3g/Fog.java b/app/src/main/java/javax/microedition/m3g/Fog.java index c4ebcdc84..5e0325859 100644 --- a/app/src/main/java/javax/microedition/m3g/Fog.java +++ b/app/src/main/java/javax/microedition/m3g/Fog.java @@ -69,20 +69,20 @@ public float getFarDistance() { } void setupGL(GL10 gl) { - gl.glFogf(GL10.GL_FOG_MODE, getGLFogMode(this.mode)); - gl.glFogfv(GL10.GL_FOG_COLOR, Color.intToFloatArray(this.color), 0); - gl.glFogf(GL10.GL_FOG_DENSITY, this.density); - gl.glFogf(GL10.GL_FOG_START, this.nearDistance); - gl.glFogf(GL10.GL_FOG_END, this.farDistance); - gl.glEnable(GL10.GL_FOG); - } - - private int getGLFogMode(int mode) { - switch (mode) { + switch (this.mode) { + case LINEAR: + gl.glEnable(GL10.GL_FOG); + gl.glFogf(GL10.GL_FOG_MODE, GL10.GL_LINEAR); + gl.glFogf(GL10.GL_FOG_START, this.nearDistance); + gl.glFogf(GL10.GL_FOG_END, this.farDistance); + gl.glFogfv(GL10.GL_FOG_COLOR, Color.intToFloatArray(this.color), 0); + break; case EXPONENTIAL: - return GL10.GL_EXP; - default: - return GL10.GL_LINEAR; + gl.glEnable(GL10.GL_FOG); + gl.glFogf(GL10.GL_FOG_MODE, GL10.GL_EXP); + gl.glFogf(GL10.GL_FOG_DENSITY, this.density); + gl.glFogfv(GL10.GL_FOG_COLOR, Color.intToFloatArray(this.color), 0); + break; } } diff --git a/app/src/main/java/javax/microedition/m3g/Graphics3D.java b/app/src/main/java/javax/microedition/m3g/Graphics3D.java index f3700ec58..a6e20f1cc 100644 --- a/app/src/main/java/javax/microedition/m3g/Graphics3D.java +++ b/app/src/main/java/javax/microedition/m3g/Graphics3D.java @@ -68,8 +68,6 @@ public final class Graphics3D { private int maxLights = 1; private boolean lightHasChanged = false; - private CompositingMode defaultCompositioningMode = new CompositingMode(); - private PolygonMode defaultPolygonMode = new PolygonMode(); private Background defaultBackground = new Background(); private boolean cameraHasChanged = false; @@ -157,7 +155,7 @@ private void populateProperties() { implementationProperties.put(PROPERTY_SUPPORT_TRUECOLOR, new Boolean(true)); implementationProperties.put(PROPERTY_SUPPORT_DITHERING, new Boolean(false)); implementationProperties.put(PROPERTY_SUPPORT_MIPMAPPING, new Boolean(false)); - implementationProperties.put(PROPERTY_SUPPORT_PERSPECTIVE_CORRECTION, new Boolean(false)); + implementationProperties.put(PROPERTY_SUPPORT_PERSPECTIVE_CORRECTION, new Boolean(true)); implementationProperties.put(PROPERTY_MAX_LIGHTS, new Integer(maxLights)); implementationProperties.put(PROPERTY_MAX_VIEWPORT_WIDTH, new Integer(maxViewportWidth)); implementationProperties.put(PROPERTY_MAX_VIEWPORT_HEIGHT, new Integer(maxViewportHeight)); @@ -300,7 +298,7 @@ else if (renderTarget instanceof Image2D) { public void clear(Background background) { /*if (!targetBound) { - throw new IllegalStateException("Graphics3D does not have a rendering target"); + throw new IllegalStateException("Graphics3D does not have a rendering target"); }*/ if (background != null) @@ -625,7 +623,7 @@ public void render(VertexBuffer vertices, IndexBuffer triangles, Appearance appe } // Appearance - setAppearance(appearance); + appearance.setupGL(gl); // Scene gl.glMatrixMode(GL10.GL_MODELVIEW); @@ -724,7 +722,7 @@ private void drawMesh(VertexBuffer vb, IndexBuffer ib, Appearance app, Transform tr.put(transform).position(0); gl.glMultMatrixf(tr); } - setAppearance(app); + app.setupGL(gl); VertexArray colors = vb.getColors(); if (colors != null) { @@ -855,15 +853,17 @@ private void drawMesh(VertexBuffer vb, IndexBuffer ib, Appearance app, Transform } private void renderNode(Node node, Transform transform) { - if (node instanceof Mesh) { Mesh mesh = (Mesh) node; int subMeshes = mesh.getSubmeshCount(); VertexBuffer vertices = mesh.getVertexBuffer(); for (int i = 0; i < subMeshes; ++i) - if (mesh.getAppearance(i) != null) + if (mesh.getAppearance(i) != null) { /*drawMesh*/ + /*if (mesh.getAppearance(i).getCompositingMode() != null && mesh.getAppearance(i).getCompositingMode().getBlending() == CompositingMode.ALPHA) + System.out.println("CompositingMode!!!");*/ render(vertices, mesh.getIndexBuffer(i), mesh.getAppearance(i), transform); + } } else if (node instanceof Sprite3D) { Sprite3D sprite = (Sprite3D) node; sprite.render(gl, transform); @@ -873,40 +873,6 @@ private void renderNode(Node node, Transform transform) { } - void setAppearance(Appearance appearance) { - if (appearance == null) - throw new NullPointerException("Appearance must not be null"); - - // Polygon mode - PolygonMode polyMode = appearance.getPolygonMode(); - if (polyMode == null) { - polyMode = defaultPolygonMode; - } - polyMode.setupGL(gl); - - // Material - if (appearance.getMaterial() != null) { - appearance.getMaterial().setupGL(gl, polyMode.getLightTarget()); - } else { - gl.glDisable(GL10.GL_LIGHTING); - } - - // Fog - if (appearance.getFog() != null) { - appearance.getFog().setupGL(gl); - } else { - gl.glDisable(GL10.GL_FOG); - } - - // Compositing mode - if (appearance.getCompositingMode() != null) { - appearance.getCompositingMode().setupGL(gl, depthBufferEnabled); - } else { - defaultCompositioningMode.setupGL(gl, depthBufferEnabled); - } - - } - int getTextureUnitCount() { return maxTextureUnits; } diff --git a/app/src/main/java/javax/microedition/m3g/Material.java b/app/src/main/java/javax/microedition/m3g/Material.java index 2e77b4214..2042761d8 100644 --- a/app/src/main/java/javax/microedition/m3g/Material.java +++ b/app/src/main/java/javax/microedition/m3g/Material.java @@ -68,13 +68,20 @@ public boolean isVertexColorTrackingEnabled() { return isVertexColorTrackingEnabled; } - void setupGL(GL10 gl, int lightTarget) { - gl.glEnable(GL10.GL_LIGHTING); + void setupGL(GL10 gl) { + if (isVertexColorTrackingEnabled) + gl.glEnable(GL10.GL_COLOR_MATERIAL); + else { + gl.glDisable(GL10.GL_COLOR_MATERIAL); + + gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, Color.intToFloatArray(ambientColor), 0); + gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, Color.intToFloatArray(diffuseColor), 0); + } gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_EMISSION, Color.intToFloatArray(emissiveColor), 0); - gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, Color.intToFloatArray(ambientColor), 0); - gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, Color.intToFloatArray(diffuseColor), 0); gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, Color.intToFloatArray(specularColor), 0); gl.glMaterialf(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, shininess); + + gl.glEnable(GL10.GL_LIGHTING); } boolean isCompatible(AnimationTrack track) { diff --git a/app/src/main/java/javax/microedition/m3g/PolygonMode.java b/app/src/main/java/javax/microedition/m3g/PolygonMode.java index 267497fa5..f0a25ef73 100644 --- a/app/src/main/java/javax/microedition/m3g/PolygonMode.java +++ b/app/src/main/java/javax/microedition/m3g/PolygonMode.java @@ -79,28 +79,25 @@ public boolean isPerspectiveCorrectionEnabled() { } void setupGL(GL10 gl) { - // Setup shading - if (shading == SHADE_SMOOTH) - gl.glShadeModel(GL10.GL_SMOOTH); - else - gl.glShadeModel(GL10.GL_FLAT); - // Setup culling if (culling == CULL_NONE) gl.glDisable(GL10.GL_CULL_FACE); else { + gl.glCullFace((culling == CULL_BACK) ? GL10.GL_BACK : GL10.GL_FRONT); gl.glEnable(GL10.GL_CULL_FACE); - if (culling == CULL_BACK) - gl.glCullFace(GL10.GL_BACK); - else - gl.glCullFace(GL10.GL_FRONT); } + // Setup two sided lighting + gl.glLightModelf(GL10.GL_LIGHT_MODEL_TWO_SIDE, twoSidedLightingEnabled ? GL10.GL_TRUE : GL10.GL_FALSE); + + // Setup shading + gl.glShadeModel((shading == SHADE_FLAT) ? GL10.GL_FLAT : GL10.GL_SMOOTH); + // Setup winding - if (winding == WINDING_CCW) - gl.glFrontFace(GL10.GL_CCW); - else - gl.glFrontFace(GL10.GL_CW); + gl.glFrontFace((winding == WINDING_CW) ? GL10.GL_CW : GL10.GL_CCW); + + // Setup perspective correction + gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, perspectiveCorrectionEnabled ? GL10.GL_NICEST : GL10.GL_FASTEST); } int getLightTarget() {