我正在尝试用trin TRIANGLE_STRIP替换GL_LINE_STRIP行 我想做的是我计算了每个顶点的法线向量,并在顶点着色器中使用它们以在法线方向上偏移顶点 基本上我想沿着这条线画一个矩形。 我在Android上使用Opengles 2.0
这是我的着色器。
uniform mat4 m;
uniform mat4 v;
uniform mat4 p;
attribute vec3 pos;
attribute vec2 normale;
void main()
{
float linewidth = 4.0;
mat4 mv = v * m;
vec4 delta = vec4(linewidth * normale.x, linewidth * normale.y, 0.0,0.0);
vec4 pos2 = mv * vec4(pos.xy, 0.0, 1.0) ;
gl_Position = p * (pos2 + delta);
}
//在这里,我为每个点添加两个顶点,并为每条线计算法线。
for (int i = 0, valuesLength = values.length; i < valuesLength; i++) {//todo do not allocate
float value = (float) values[i] / maxValue; // make it [0:1]
Vertex v1 = new Vertex();//todo rewrite without allocatinos
Vertex v2 = new Vertex();//todo rewrite without allocatinos
float x = i;
float y = value;
if (vertices.size() > 0) {
// copy normale vecotrs from prev vertices
Vertex t1 = new Vertex();
Vertex t2 = new Vertex();
t1.x = x;
t1.y = y;
t1.z = 1;
t1.normale_x = vertices.get(vertices.size() - 2).normale_x;
t1.normale_y = vertices.get(vertices.size() - 2).normale_y;
t2.x = x;
t2.y = y;
t2.z = -1;
t2.normale_x = vertices.get(vertices.size() - 1).normale_x;
t2.normale_y = vertices.get(vertices.size() - 1).normale_y;
vertices.add(t1);
vertices.add(t2);
}
v1.x = x ;
v1.y = y ;
v1.z = 1;
v2.x = x;
v2.y = y;
v2.z = -1;
vertices.add(v1);
vertices.add(v2);
if (i == valuesLength - 1) {
int j = i - 1;
//todo last vertex
} else {
double nextValue = (float) values[i + 1] / column.maxValue;
double dy = nextValue - value;
double l = (float) Math.sqrt(1f + dy * dy);
dy /= l;
double dx = 1f / l;
v1.normale_x = (float) -dy * 10;
v1.normale_y = (float) dx;
v2.normale_x = (float) dy * 10;
v2.normale_y = (float) -dx;
}
}
这是我的循环
Matrix.orthoM(PROJ, 0, 0, w, 0, h, -1.0f, 1.0f);
Matrix.setIdentityM(MODEL, 0);
Matrix.scaleM(MODEL, 0, mywpx/(float)maxXValue, myhpx , 1.0f);
Matrix.setIdentityM(VIEW, 0);
Matrix.translateM(VIEW, 0, dxpx, dypx ,0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, vertices.size());
我可能缺少MVP的东西,或者忘记了多方面的东西。
如果有人能指出我在哪里缺少什么或也许有一些好的资源,我将非常感激