2016/04/02 01:31

目录

优化 ledChar 函数

void ledChar(int,float,float,float,float);

// 构造数字
void ledChar(int n, float xa,float xb, float ya, float yb){
float x = vTexCoord.x;
float y = vTexCoord.y;
float x1 = xa;
float x2 = xa+xb;
float y1 = ya;
float y2 = ya+yb;
float ox = (x2+x1)/2.0;
float oy = (y2+y1)/2.0;
float dx = (x2-x1)/10.0;
float dy = (y2-y1)/10.0;
float b = (x2-x1)/20.0;
int num = n;

// 设定调试区显示范围
if(x >= x1 && x <= x2 && y >= y1 && y <= y2) {
// 设置调试区背景色
gl_FragColor = vec4(0.2,0.2,0.8,.5);
// 分别绘制出 LED 形式的数字 1~0
if((num==1 && (x > x2-dx)) ||
(num==2 && ((y > y2-dy) || (x > x2-dx && y > oy-dy/2.0) || (y > oy-dy/2.0 && y < oy+dy/2.0) || (x < x1+dx && y < oy+dy/2.0) || (y < y1+dy))) ||
(num==3 && ((y > y2-dy) || (x > x2-dx) || (y > oy-dy/2.0 && y < oy+dy/2.0) ||  (y < y1+dy))) ||
(num==4 && ((x < x1+dx && y > oy-dy/2.0) ||(x > x2-dx) || (y > oy-dy/2.0 && y < oy+dy/2.0))) ||
(num==5 && ((y > y2-dy) || (x < x1+dx && y > oy-dy/2.0)|| (y > oy-dy/2.0 && y < oy+dy/2.0) || (x>x2-dx && y <oy-dy/2.0) || (y<y1+dy))) ||
(num==6 && ((y > y2-dy) || (x < x1+dx)|| (y > oy-dy/2.0 && y < oy+dy/2.0) || (x>x2-dx && y <oy-dy/2.0) || (y<y1+dy))) ||
(num==7 && ((y > y2-dy) || (x > x2-dx))) ||
(num==8 && ((y > y2-dy) || (x < x1+dx)|| (y > oy-dy/2.0 && y < oy+dy/2.0) || (x>x2-dx) || (y<y1+dy))) ||
(num==9 && ((y > y2-dy) || (x < x1+dx && y > oy-dy/2.0)||(y > oy-dy/2.0 && y < oy+dy/2.0)|| (x>x2-dx) || (y<y1+dy))) ||
(num==0 && ((y > y2-dy) || (x < x1+dx) || (x>x2-dx) || (y<y1+dy)))
)
{
gl_FragColor = vec4(0,1,0,.5);
}
}
}


传统LED数字绘制原理图 VS. 新想到的利用"矩形掩码"绘制原理图:

float x = vTexCoord.x;
float y = vTexCoord.y;

void ledRectChar(int,float,float,float,float);
bool inRect(float,float,float,float);

bool inRect(float x1,float x2, float y1, float y2){
if(x>x1 && x<x2 && y>y1 && y<y2) { return true; } else { return false; }
}

void ledRectChar(int n, float xa,float xb, float ya, float yb){
float x1 = xa;
float x2 = xa+xb;
float y1 = ya;
float y2 = ya+yb;
float ox = (x2+x1)/2.0;
float oy = (y2+y1)/2.0;
float dx = (x2-x1)/10.0;
float dy = (y2-y1)/10.0;
float b = (x2-x1)/20.0;
int num = n;

// 设定调试区显示范围
if(x >= x1 && x <= x2 && y >= y1 && y <= y2) {
// 设置调试区背景色为绿色
gl_FragColor = vec4(0.2,1.0,0.2,1.0);
// 分别绘制出 LED 形式的数字 1~0 , 用黑色绘制1个或2个矩形,由矩形以外的绿色区域组成字型
if((num==1 && (inRect(x1,ox-dx,y1,y2) || inRect(ox+dx,x2,y1,y2))) ||
(num==2 && (inRect(x1,x2-dx,oy+dy/2.0,y2-dy) || inRect(x1+dx,x2,y1+dy,oy-dy/2.0))) ||
(num==3 && (inRect(x1,x2-dx,oy+dy/2.0,y2-dy) || inRect(x1,x2-dx,y1+dy,oy-dy/2.0))) ||
(num==4 && (inRect(x1+dx,x2-dx,oy+dy/2.0,y2) || inRect(x1,x2-dx,y1,oy-dy/2.0))) ||
(num==5 && (inRect(x1+dx,x2,oy+dy/2.0,y2-dy) || inRect(x1,x2-dx,y1+dy,oy-dy/2.0))) ||
(num==6 && (inRect(x1+dx,x2,oy+dy/2.0,y2-dy) || inRect(x1+dx,x2-dx,y1+dy,oy-dy))) ||
(num==7 && inRect(x1,x2-dx,y1,y2-dy)) ||
(num==8 && (inRect(x1+dx,x2-dx,oy+dy/2.0,y2-dy) || inRect(x1+dx,x2-dx,y1+dy,oy-dy/2.0))) ||
(num==9 && (inRect(x1+dx,x2-dx,oy+dy/2.0,y2-dy) || inRect(x1,x2-dx,y1+dy,oy-dy/2.0))) ||
(num==0 && inRect(x1+dx,x2-dx,y1+dy,y2-dy))
)
{
gl_FragColor = vec4(0,0,0,.5);
}
}
}


// ! 说明, 这是错的, 编译不通过
#define inRect(x1,x2,y1,y2) x>(x1)&&x<(x2)&&y>(y1)&&y<(y2)?true:false


改进为可用的原型

• 多位正整数
• 多位浮点数
• 负数
• 给出表示范围和准确度

表示多位正整数

highp int nbr=8293;   // number to display

float m=0.96;
while (nbr>0)
{   m=m-0.015;
int nn=nbr-((nbr/10)*10);
ledChar(nn, m, 0.01, 0.96, 0.01);
nbr=nbr/10;
}


新增的两种字型:小数点和负号

(num==10 && (inRect(x1,x2,oy-dy,y2) || inRect(x1,ox-dx*2.0,y1,oy-dy) || inRect(ox+dx*2.0,x2,y1,oy-dy) )) ||
(num==11 && (inRect(x1,x2,oy+dy,y2) || inRect(x1,x2,y1,oy-dy)))


void ledRectChar(int n, float xa,float xb, float ya, float yb){
float x1 = xa;
float x2 = xa+xb;
float y1 = ya;
float y2 = ya+yb;
float ox = (x2+x1)/2.0;
float oy = (y2+y1)/2.0;
float dx = (x2-x1)/10.0;
float dy = (y2-y1)/10.0;
float b = (x2-x1)/20.0;
int num = n;

// 设定调试区显示范围
if(x >= x1 && x <= x2 && y >= y1 && y <= y2) {
// 设置调试区背景色
gl_FragColor = vec4(0.2,1.0,0.2,1.0);
// 分别绘制出 LED 形式的数字 1~0 , 用黑色绘制1个或2个矩形,由矩形以外的绿色区域组成字型
if((num==1 && (inRect(x1,ox-dx,y1,y2) || inRect(ox+dx,x2,y1,y2))) ||
(num==2 && (inRect(x1,x2-dx,oy+dy/2.0,y2-dy) || inRect(x1+dx,x2,y1+dy,oy-dy/2.0))) ||
(num==3 && (inRect(x1,x2-dx,oy+dy/2.0,y2-dy) || inRect(x1,x2-dx,y1+dy,oy-dy/2.0))) ||
(num==4 && (inRect(x1+dx,x2-dx,oy+dy/2.0,y2) || inRect(x1,x2-dx,y1,oy-dy/2.0))) ||
(num==5 && (inRect(x1+dx,x2,oy+dy/2.0,y2-dy) || inRect(x1,x2-dx,y1+dy,oy-dy/2.0))) ||
(num==6 && (inRect(x1+dx,x2,oy+dy/2.0,y2-dy) || inRect(x1+dx,x2-dx,y1+dy,oy-dy))) ||
(num==7 && inRect(x1,x2-dx,y1,y2-dy)) ||
(num==8 && (inRect(x1+dx,x2-dx,oy+dy/2.0,y2-dy) || inRect(x1+dx,x2-dx,y1+dy,oy-dy/2.0))) ||
(num==9 && (inRect(x1+dx,x2-dx,oy+dy/2.0,y2-dy) || inRect(x1,x2-dx,y1+dy,oy-dy/2.0))) ||
(num==0 && inRect(x1+dx,x2-dx,y1+dy,y2-dy)) ||
// 传入10则绘制小数点, 传入11则绘制负号, 传入12则清空
(num==10 && (inRect(x1,x2,oy-dy,y2) || inRect(x1,ox-dx*2.0,y1,oy-dy) || inRect(ox+dx*2.0,x2,y1,oy-dy) )) ||
(num==11 && (inRect(x1,x2,oy+dy,y2) || inRect(x1,x2,y1,oy-dy))) ||
(num==12)
)
{
gl_FragColor = vec4(0,0,0,.5);
}
}
}


浮点数和负数

void showFloat(float f){
int myNum[20];
int k = 0;
int iPart = int(floor(abs(f)));
int fPart = int(fract(abs(f))*100000.0);
float m=0.86;

// 初始化数组,全部置为代表黑色的12
for(int i=0; i<20; i++){
myNum[i] = 12;
}

// 插入小数部分
while (fPart>0)
{
// 从个位开始, 依次取出个位,十位,百位,千位...的数字值
myNum[k++]=fPart-((fPart/10)*10);
fPart=fPart/10;
}

// 如果是0
if(f==0.0){myNum[k++] = 0;}

// 插入小数点
myNum[k++] = 10;

// 插入整数部分
while (iPart>0)
{
myNum[k++]=iPart-((iPart/10)*10);
iPart=iPart/10;
}

// 如果是负数,则插入代表负号的11
if(f<0.0) { myNum[k++]=11;}

// 循环输出数字数组
for(int i=0; i<20; i++)
{
m = m-0.03;
ledRectChar(myNum[i], m, 0.02, 0.6, 0.15);
}

}


• showFloat(float(-1234));

显示范围和准确度

• showFloat(2097152.411);

• showFloat(2097152.11);

可用原型的完整代码

myShader = {
vsBase = [[
uniform mat4 modelViewProjection;
uniform vec2 uResolution;

attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;

varying lowp vec4 vColor;
varying highp vec2 vTexCoord;

void main() {
vColor=color;
vTexCoord = texCoord;

gl_Position = modelViewProjection * position;
}
]],
fsBase = [[
precision highp float;
uniform lowp sampler2D texture;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;

float x = vTexCoord.x;
float y = vTexCoord.y;

void ledChar(int,float,float,float,float);
void ledRectChar(int,float,float,float,float);
void showInt(int);
void showFloat(float);
bool inRect(float,float,float,float);

void main() {
lowp vec4 col = texture2D( texture, vTexCoord ) * vColor;

// 默认全部设置为黑色
gl_FragColor = vec4(.1,.1,.1,1);

showFloat(-.1111111);
//showFloat(float(-9765));

}

void showFloat(float f){
int myNum[20];
int k = 0;
int iPart = int(floor(abs(f)));
int fPart = int(fract(abs(f))*100000.0);
float m=0.86;

// 初始化数组,全部置为代表黑色的12
for(int i=0; i<20; i++){
myNum[i] = 12;
}

// 插入小数部分
while (fPart>0)
{
// 从个位开始, 依次取出个位,十位,百位,千位...的数字值
myNum[k++]=fPart-((fPart/10)*10);
fPart=fPart/10;
}

// 如果是0
if(f==0.0){myNum[k++] = 0;}

// 插入小数点
myNum[k++] = 10;

// 插入整数部分
while (iPart>0)
{
myNum[k++]=iPart-((iPart/10)*10);
iPart=iPart/10;
}

// 如果是负数,则插入代表负号的11
if(f<0.0) { myNum[k++]=11;}

// 循环输出数字数组
for(int i=0; i<20; i++)
{
m = m-0.03;
ledRectChar(myNum[i], m, 0.02, 0.6, 0.15);
}
}

bool inRect(float x1,float x2, float y1, float y2){
if(x>x1 && x<x2 && y>y1 && y<y2) { return true; } else { return false; }
}

void ledRectChar(int n, float xa,float xb, float ya, float yb){
float x1 = xa;
float x2 = xa+xb;
float y1 = ya;
float y2 = ya+yb;
float ox = (x2+x1)/2.0;
float oy = (y2+y1)/2.0;
float dx = (x2-x1)/10.0;
float dy = (y2-y1)/10.0;
float b = (x2-x1)/20.0;
int num = n;

// 设定调试区显示范围
if(x >= x1 && x <= x2 && y >= y1 && y <= y2) {
// 设置调试区背景色
gl_FragColor = vec4(0.2,1.0,0.2,1.0);
// 分别绘制出 LED 形式的数字 1~0 , 用黑色绘制1个或2个矩形,由矩形以外的绿色区域组成字型
if((num==1 && (inRect(x1,ox-dx,y1,y2) || inRect(ox+dx,x2,y1,y2))) ||
(num==2 && (inRect(x1,x2-dx,oy+dy/2.0,y2-dy) || inRect(x1+dx,x2,y1+dy,oy-dy/2.0))) ||
(num==3 && (inRect(x1,x2-dx,oy+dy/2.0,y2-dy) || inRect(x1,x2-dx,y1+dy,oy-dy/2.0))) ||
(num==4 && (inRect(x1+dx,x2-dx,oy+dy/2.0,y2) || inRect(x1,x2-dx,y1,oy-dy/2.0))) ||
(num==5 && (inRect(x1+dx,x2,oy+dy/2.0,y2-dy) || inRect(x1,x2-dx,y1+dy,oy-dy/2.0))) ||
(num==6 && (inRect(x1+dx,x2,oy+dy/2.0,y2-dy) || inRect(x1+dx,x2-dx,y1+dy,oy-dy))) ||
(num==7 && inRect(x1,x2-dx,y1,y2-dy)) ||
(num==8 && (inRect(x1+dx,x2-dx,oy+dy/2.0,y2-dy) || inRect(x1+dx,x2-dx,y1+dy,oy-dy/2.0))) ||
(num==9 && (inRect(x1+dx,x2-dx,oy+dy/2.0,y2-dy) || inRect(x1,x2-dx,y1+dy,oy-dy/2.0))) ||
(num==0 && inRect(x1+dx,x2-dx,y1+dy,y2-dy)) ||
// 传入10则绘制小数点, 传入11则绘制负号, 传入12则清空
(num==10 && (inRect(x1,x2,oy-dy,y2) || inRect(x1,ox-dx*2.0,y1,oy-dy) || inRect(ox+dx*2.0,x2,y1,oy-dy) )) ||
(num==11 && (inRect(x1,x2,oy+dy,y2) || inRect(x1,x2,y1,oy-dy))) ||
(num==12)
)
{
gl_FragColor = vec4(0,0,0,.5);
}
}
}

]]
}


配套Codea代码

-- Shader debug
displayMode(OVERLAY)
function setup()
m = mesh()

-- m.texture = "Documents:univer"
m:setColors(color(220,200,200,255))

parameter.watch("m.vertices[1]")
end

function draw()
background(0)
m:draw()
end

function touched(touch)

end


3 评论
5 收藏
2