OpenglEs 2.0 绘制线段颜色平滑渐变过渡

OpenglEs 2.0 绘制线段颜色平滑渐变过渡
在这里插入图片描述
介绍之前说明 由于opengels 2.0 无法直接绘制线段的粗细 ,本文使用的 GLES20.GL_POINTS 点的方式控制的线粗 在顶点着色器中 设置 gl_PointSize=3.0;

实现原理 单独设置每个顶点坐标的颜色

下面是实现的工具类 ,

package mypower.example.com.xsegment;

import android.graphics.Color;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

//测试类,opengels 实现连线的 颜色平滑过渡
public class SmoothColorTransition{
int frequency = 0;
//每个点的颜色集合
public List coordinateCollections = new ArrayList();
//每个点的坐标点集合
List colorCollection = new ArrayList();
//使用二阶贝塞尔曲线划线
//第一个点
float x1 = 0.2f;
float y1 = 0.2f;
//控制点
float x2 = 0.5f;
float y2 = 0.5f;
//第二个点
float x3 = 0.8f;
float y3 = 0.2f;

public void dianListMedel() {
    frequency=0;
    int startColor = argbTest(1.0f, 1.0f, 1.0f, 1.0f);
    int endColor = argbTest(1.0f, 0.0f, 0.0f, 0.0f);
    for (float i = 0; i < 1.0; i += 0.001f) {
        float code = (1 - i);
        //下面是贝塞尔曲线的二阶公式
        float x = (float) (Math.pow(code, 2) * x1 + 2 * i * code * x2 + Math.pow(i, 2) * x3);
        float y = (float) (Math.pow(code, 2) * y1 + 2 * i * code * y2 + Math.pow(i, 2) * y3);
        //添加顶点坐标
        coordinateCollections.add(x);
        coordinateCollections.add(y);
        frequency++;
        //frequency 1000(1/0.001f) 表示多少次完成颜色的渐变   frequency 当前的进度
        getCurrentColor((float) frequency / 1000, startColor, endColor);

    }
}
/**
 * 根据fraction值来计算当前的颜色。
 */
private void getCurrentColor(float fraction, int startColor, int endColor) {
    float redCurrent;
    float blueCurrent;
    float greenCurrent;
    float alphaCurrent;

    float redStart = Color.red(startColor);
    float blueStart = Color.blue(startColor);
    float greenStart = Color.green(startColor);
    float alphaStart = Color.alpha(startColor);

    float redEnd = Color.red(endColor);
    float blueEnd = Color.blue(endColor);
    float greenEnd = Color.green(endColor);
    float alphaEnd = Color.alpha(endColor);

    float redDifference = redEnd - redStart;
    float blueDifference = blueEnd - blueStart;
    float greenDifference = greenEnd - greenStart;
    float alphaDifference = alphaEnd - alphaStart;
    redCurrent = (float) (redStart + fraction * redDifference);
    blueCurrent = (float) (blueStart + fraction * blueDifference);
    greenCurrent = (float) (greenStart + fraction * greenDifference);
    alphaCurrent = (float) (alphaStart + fraction * alphaDifference);
    colorCollection.add(redCurrent/255);
    colorCollection.add(greenCurrent/255);
    colorCollection.add(blueCurrent/255);
    colorCollection.add(alphaCurrent/255);
}
//将rgb转int
public static int argbTest(float alpha, float red, float green, float blue) {
    return ((int) (alpha * 255.0f + 0.5f) << 24) |
            ((int) (red * 255.0f + 0.5f) << 16) |
            ((int) (green * 255.0f + 0.5f) << 8) |
            (int) (blue * 255.0f + 0.5f);
}

}

调用方式
public SmoothColorTransition smoothColorTransition=new SmoothColorTransition();
//初始化顶点和颜色的数据
smoothColorTransition.dianListMedel();
//转化为 FloatBuffer
mVertexBuffer = list2ByteBuffer(smoothColorTransition.coordinateCollections);
//转化为 FloatBuffer
colorBuffer = list2ByteBuffer(smoothColorTransition.colorCollection);

/**
 * 将list转换成字节缓冲区
 */
public static FloatBuffer list2ByteBuffer(List<Float> list) {
    ByteBuffer ibb = ByteBuffer.allocateDirect(list.size() * 4);
    ibb.order(ByteOrder.nativeOrder());
    FloatBuffer fbb = ibb.asFloatBuffer();
    for (Float f : list) {
        fbb.put(f);
    }
    ibb.position(0);
    fbb.position(0);
    return fbb;
}

有兴趣的同学可以关注
https://edu.csdn.net/course/detail/23746

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值