add and use utility to set Int color as uniform

This commit is contained in:
Hannes Janetzek 2013-03-16 01:35:41 +01:00
parent f262efee7f
commit 3e6ac7d5d4
6 changed files with 67 additions and 55 deletions

View File

@ -7,20 +7,53 @@
#define JNI(X) JNIEXPORT Java_org_oscim_utils_GlUtils_##X
static const char TAG[] = "org.oscim.utils.GLUtils";
#define COLOR_R(C) (((C >> 16) & 0xff) / 255.0f)
#define COLOR_G(C) (((C >> 8) & 0xff) / 255.0f)
#define COLOR_B(C) (((C >> 0) & 0xff) / 255.0f)
#define COLOR_A(C) (((C >> 24) & 0xff) / 255.0f)
static void
nativeClassInit(JNIEnv *_env)
void JNI(setColor)(JNIEnv *env, jclass* clazz, jint location, jint c, jfloat alpha)
{
if (alpha >= 1)
alpha = COLOR_A(c);
else if (alpha < 0)
alpha = 0;
else
alpha *= COLOR_A(c);
if (alpha == 1)
{
glUniform4f((GLint) location,
(GLfloat) COLOR_R(c),
(GLfloat) COLOR_G(c),
(GLfloat) COLOR_B(c),
(GLfloat) alpha);
}
else
{
glUniform4f((GLint) location,
(GLfloat) (COLOR_R(c) * alpha),
(GLfloat) (COLOR_G(c) * alpha),
(GLfloat) (COLOR_B(c) * alpha),
(GLfloat) alpha);
}
}
void JNI(init)(JNIEnv *env, jclass* clazz)
void JNI(setColorBlend)(JNIEnv *env, jclass* clazz, jint location, jint c1, jint c2, jfloat mix)
{
nativeClassInit(env);
__android_log_print(ANDROID_LOG_INFO, TAG, "all initialized");
float a1 = COLOR_A(c1) * (1 - mix);
float a2 = COLOR_A(c2) * mix;
glUniform4f((GLint) location,
(GLfloat) (COLOR_R(c1) * a1 + COLOR_R(c2) * a2),
(GLfloat) (COLOR_G(c1) * a1 + COLOR_G(c2) * a2),
(GLfloat) (COLOR_B(c1) * a1 + COLOR_B(c2) * a2),
(GLfloat) (a1 + a2));
}
#undef JNI
#define JNI(X) JNIEXPORT Java_org_oscim_utils_Matrix4_##X
@ -42,7 +75,6 @@ transposeM(float* mTrans, int mTransOffset, float* m, int mOffset);
static inline void
matrix4_proj(float* mat, float* vec);
jlong JNI(alloc)(JNIEnv *env, jclass* clazz)
{
return (long) calloc(16, sizeof(float));

View File

@ -167,12 +167,8 @@ public class LineTexRenderer {
LineTexLayer ll = (LineTexLayer) l;
Line line = ll.line;
if (line.stippleColor == null)
GLES20.glUniform4f(hTexColor, 1.0f, 1.0f, 1.0f, 1.0f);
else
GLES20.glUniform4fv(hTexColor, 1, line.stippleColor, 0);
GLES20.glUniform4fv(hBgColor, 1, line.color, 0);
GlUtils.setColor(hTexColor, line.stippleColor, 1);
GlUtils.setColor(hBgColor, line.color, 1);
float ps = FastMath.clamp((int) (s+0.5f), 1, 3);
GLES20.glUniform1f(hPatternScale, (GLRenderer.COORD_SCALE * line.stipple) / ps);

View File

@ -113,13 +113,13 @@ public final class PolygonRenderer {
GLState.blend(false);
if (a.blend == zoom)
GlUtils.setBlendColors(hPolygonColor,
GlUtils.setColorBlend(hPolygonColor,
a.color, a.blendColor, scale - 1.0f);
else
GlUtils.setColor(hPolygonColor, a.blendColor, 1);
} else {
if (a.color[3] != 1)
if (a.color < 0xff000000)
GLState.blend(true);
else
GLState.blend(false);

View File

@ -17,7 +17,6 @@ package org.oscim.theme.renderinstruction;
import org.oscim.core.Tag;
import org.oscim.theme.IRenderCallback;
import org.oscim.theme.RenderThemeHandler;
import org.oscim.utils.GlUtils;
import org.xml.sax.Attributes;
import android.graphics.Color;
@ -85,11 +84,11 @@ public final class Area extends RenderInstruction {
this.level = 0;
this.style = "";
this.fade = -1;
blendColor = null;
blendColor = 0;
blend = -1;
strokeWidth = 0;
color = GlUtils.colorToFloatP(fill);
color = fill;
}
/**
@ -126,12 +125,8 @@ public final class Area extends RenderInstruction {
// paintFill.setShader(shader);
// }
color = GlUtils.colorToFloatP(fill);
if (blend > 0)
blendColor = GlUtils.colorToFloatP(blendFill);
else
blendColor = null;
color = fill; //GlUtils.colorToFloatP(fill);
blendColor = blendFill; //GlUtils.colorToFloatP(blendFill);
this.blend = blend;
this.strokeWidth = strokeWidth;
@ -144,18 +139,11 @@ public final class Area extends RenderInstruction {
renderCallback.renderArea(this, this.level);
}
// @Override
// public void scaleStrokeWidth(float scaleFactor) {
// // if (paintOutline != null) {
// // paintOutline.setStrokeWidth(strokeWidth * scaleFactor);
// // }
// }
public String style;
private final int level;
public final float strokeWidth;
public final float color[];
public final int color;
public final int fade;
public final float blendColor[];
public final int blendColor;
public final int blend;
}

View File

@ -20,7 +20,6 @@ import java.util.regex.Pattern;
import org.oscim.core.Tag;
import org.oscim.theme.IRenderCallback;
import org.oscim.theme.RenderThemeHandler;
import org.oscim.utils.GlUtils;
import org.xml.sax.Attributes;
import android.graphics.Color;
@ -66,8 +65,9 @@ public final class Line extends RenderInstruction {
int stipple = 0;
float stippleWidth = 0;
float[] color = null;
float[] stippleColor = null;
int color = Color.RED;
int stippleColor = Color.BLACK;
if (line != null) {
color = line.color;
@ -90,8 +90,7 @@ public final class Line extends RenderInstruction {
else if ("src".equals(name)) {
//src = value;
} else if ("stroke".equals(name)) {
int stroke = Color.parseColor(value);
color = GlUtils.colorToFloatP(stroke);
color = Color.parseColor(value);
} else if ("width".equals(name)) {
width = Float.parseFloat(value);
} else if ("cap".equals(name)) {
@ -101,8 +100,7 @@ public final class Line extends RenderInstruction {
} else if ("stipple".equals(name)) {
stipple = Integer.parseInt(value);
} else if ("stipple-stroke".equals(name)) {
int stroke = Color.parseColor(value);
stippleColor = GlUtils.colorToFloatP(stroke);
stippleColor = Color.parseColor(value);
} else if ("stipple-width".equals(name)) {
stippleWidth = Float.parseFloat(value);
} else if ("fade".equals(name)) {
@ -117,13 +115,6 @@ public final class Line extends RenderInstruction {
}
}
// hint that sth is missing
if (color == null)
color = GlUtils.colorToFloatP(Color.RED);
if (stipple != 0 && stippleColor == null)
stippleColor = GlUtils.colorToFloatP(Color.GREEN);
// inherit properties from 'line'
if (line != null) {
// use stroke width relative to 'line'
@ -160,7 +151,7 @@ public final class Line extends RenderInstruction {
public final String style;
public final float width;
public final float[] color;
public final int color;
public final Cap cap;
public final boolean outline;
public final boolean fixed;
@ -169,14 +160,14 @@ public final class Line extends RenderInstruction {
public final float min;
public final int stipple;
public final float[] stippleColor;
public final int stippleColor;
public final float stippleWidth;
private Line(int level, String style, float[] color, float width,
private Line(int level, String style, int color, float width,
Cap cap, boolean fixed,
int stipple, float[] stippleColor, float stippleWidth,
int stipple, int stippleColor, float stippleWidth,
int fade, float blur, boolean isOutline, float min) {
this.level = level;
@ -222,10 +213,10 @@ public final class Line extends RenderInstruction {
this.fixed = true;
this.fade = -1;
this.stipple = 0;
this.stippleColor = null;
this.stippleColor = Color.BLACK;
this.stippleWidth = 0;
this.min = 0;
this.color = GlUtils.colorToFloatP(stroke);
this.color = stroke; //GlUtils.colorToFloatP(stroke);
}
public Line(int stroke, float width, int stipple) {
@ -238,10 +229,10 @@ public final class Line extends RenderInstruction {
this.fixed = true;
this.fade = -1;
this.stipple = stipple;
this.stippleColor = null;
this.stippleColor = Color.BLACK;
this.stippleWidth = 0.6f;
this.min = 0;
color = GlUtils.colorToFloatP(stroke);
color = stroke; //GlUtils.colorToFloatP(stroke);
}
@Override

View File

@ -26,6 +26,10 @@ import android.util.Log;
* Utility functions
*/
public class GlUtils {
public static native void setColor(int location, int color, float alpha);
public static native void setColorBlend(int location, int color1, int color2, float mix);
private static String TAG = "GlUtils";
public static void setTextureParameter(int min_filter, int mag_filter, int wrap_s, int wrap_t) {
@ -202,6 +206,7 @@ public class GlUtils {
return oom;
}
public static void setBlendColors(int handle, float[] c1, float[] c2, float mix) {
if (mix <= 0f)
GLES20.glUniform4fv(handle, 1, c1, 0);