如何提高使用 openGL 绘制的线条的质量。我不知道我这样做是否是最好的方式。我正在使用 ShareRenderer 和线路。但是正如您在图像中看到的那样,圆角渲染得不是很好。 我在触地和拖动时得到点,我认为这很容易画一条线,但我想这需要在 openGL 中以不同的方式完成。我想用手指描写字母来学习写字。我可以使用 openGL-es 1 或 2,没关系。此处用于图像的设备是三星 S3。
@Override
public void render(float delta) {
super.render(delta);
if(mPoints.size() < maxPoints) return;
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
for(int i=0; i < maxPoints-1; i++) {
Vector3 pnt = mPoints.get(i);
if(pnt.x == 0 && pnt.y == 0) continue;
Vector3 pnt2 = mPoints.get(i+1);
if(pnt2.x == 0 && pnt2.y == 0) continue;
shapeRenderer.line(pnt.x, pnt.y, pnt2.x, pnt2.y);
}
shapeRenderer.end();
}
@Override
public void resize(int width, int height) {
super.resize(width, height);
shapeRenderer.setProjectionMatrix(getCamera().combined);
Gdx.gl20.glLineWidth(200);
Gdx.gl.glEnable(GL20.GL_BLEND);
}
@Override
public boolean touchDown(int x, int y, int pointer, int button) {
currentFinger = pointer;
if (++numStrokes >= 2) { //number of strokes for a letter
numStrokes = 0;
clearAllPoints();
currentPointIndex = 0;
}
setPoint(0, 0);
return false;
}
@Override
public boolean touchDragged(int x, int y, int pointer) {
if(currentFinger!=pointer) return false;
Vector3 pnt = mPoints.get(currentPointIndex);
tempVector.set(x, y, 0);
getCamera().unproject(tempVector);
float dx = Math.abs(tempVector.x - pnt.x);
float dy = Math.abs(tempVector.y - pnt.y);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
setPoint(x, y);
}
return false;
}
更新了下面的渲染方法来绘制填充的矩形和圆形,填充的圆形效果更好。
public void render(float delta) {
super.render(delta);
if(mPoints.size() < maxPoints) return;
shapeRenderer.begin(ShapeRenderer.ShapeType.FilledRectangle);
for(int i=0; i < maxPoints-1; i++) {
Vector3 pnt = mPoints.get(i);
if(pnt.x == 0 && pnt.y == 0) continue;
Vector3 pnt2 = mPoints.get(i+1);
if(pnt2.x == 0 && pnt2.y == 0) continue;
//This creates a rectangle from the set of points see : http://stackoverflow.com/questions/7854043/drawing-rectangle-between-two-points-with-arbitrary-width
dist.set(pnt2.x - pnt.x, pnt2.y - pnt.y, 0);
pVector.set(dist.y, -dist.x, 0); //Find perpendicular (right angle) to points , basically swap x and y , make one negative
float length = (float) Math.sqrt(pVector.x * pVector.x + pVector.y * pVector.y); //Thats length of perpendicular
normVector.set(pVector.x - length, pVector.y-length, 0);
//shapeRenderer.filledCircle(pnt.x, pnt.y, lineWidth);
shapeRenderer.filledRect(pnt.x - pVector.x * rectWidth/2 , pnt.y - pVector.y * rectWidth /2, rectWidth, rectWidth);
}
shapeRenderer.end();
}
最佳答案
OpenGL ES 不支持 1.0 以外的 OpenGL 线宽,因此您应该避免在 Android 上使用它(参见 Libgdx gl10.glLineWidth())。
要获得更平滑、更圆润的“粗线条”,请将每个触摸点绘制为圆形,并用矩形连接相距超过几个像素的点。
参见:
关于android - libgdx opengl 行质量改进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20988597/