Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Appearance OpenGL setup, add perspective coorection support #8

Merged
merged 1 commit into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions app/src/main/java/javax/microedition/m3g/Appearance.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package javax.microedition.m3g;

import javax.microedition.khronos.opengles.GL10;

public class Appearance extends Object3D {

int numTextureUnits = 8;
Expand Down Expand Up @@ -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() {
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/javax/microedition/m3g/Background.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
22 changes: 9 additions & 13 deletions app/src/main/java/javax/microedition/m3g/CompositingMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}
26 changes: 13 additions & 13 deletions app/src/main/java/javax/microedition/m3g/Fog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
50 changes: 8 additions & 42 deletions app/src/main/java/javax/microedition/m3g/Graphics3D.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
15 changes: 11 additions & 4 deletions app/src/main/java/javax/microedition/m3g/Material.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 11 additions & 14 deletions app/src/main/java/javax/microedition/m3g/PolygonMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down