gpt4 book ai didi

android - 如何使用 opengl-es 2.0 在 android 中填充多边形?

转载 作者:行者123 更新时间:2023-11-29 01:16:41 24 4
gpt4 key购买 nike

我想绘制如图所示的多边形并用颜色填充它。 Fill Polygon Simple polygon

我已经尝试过使用三角形的多边形,但它对我没有帮助。任何知道的人请帮助我。

OpenGLProjectRenderer.java

public class OpenGLProjectRenderer implements Renderer {

List<Float> points = new ArrayList<Float>();

private static final String TAG = "Renderer";
private static final int POSITION_COMPONENT_COUNT = 2;
private static final int BYTES_PER_FLOAT = 4;
private FloatBuffer vertexData = ByteBuffer
.allocateDirect(20000 * BYTES_PER_FLOAT)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
private Context context;
private int program;

private static final String A_POSITION = "a_Position";
private int aPositionLocation;

private static final String U_COLOR = "u_Color";
private int uColorLocation;

private HashMap<Integer, ArrayList<Float>> lines = new HashMap<Integer, ArrayList<Float>>();
int position = 0;

public OpenGLProjectRenderer(Context context) {

this.context = context;
}

@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUniform4f(uColorLocation, 1.0f, 0.0f, 0.0f, 1.0f);

for (int p = 0; p < lines.size(); p++) {

vertexData.put(toFloatarray(lines.get(p)));
int vertices = (int) lines.get(p).size() / 2;
int b = vertices % 4 == 0 ? vertices-1 : vertices - 2;
Log.d(TAG,""+lines.size());
glDrawArrays(GLES20.GL_LINE_LOOP, 0, lines.size());
vertexData.clear();
}

}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);

}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

String vertexShaderSource = TextResourceReader.readTextFileFromResource(
context, R.raw.simple_vertex_shader);
String fragmentShaderSource = TextResourceReader.readTextFileFromResource(
context, R.raw.simple_fragment_shader);

int vertexShader = ShaderHelper.compileVertexShader(vertexShaderSource);
int fragmentShader = ShaderHelper
.compileFragmentShader(fragmentShaderSource);
program = ShaderHelper.linkProgram(vertexShader, fragmentShader);
ShaderHelper.validateProgram(program);
glUseProgram(program);
uColorLocation = glGetUniformLocation(program, U_COLOR);
aPositionLocation = glGetAttribLocation(program, A_POSITION);
vertexData.position(0);
glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT,
GL_FLOAT, false, 0, vertexData);
glEnableVertexAttribArray(aPositionLocation);
}

ArrayList<Float> temp = new ArrayList<Float>();

public void handleTouchPress(float normalizedX, float normalizedY) {
Log.v(TAG + " handleTouchPress", points.size() + "");

temp.add(normalizedX);
temp.add(normalizedY);

lines.put(position, temp);
}

public void handleTouchDrag(float normalizedX, float normalizedY) {

Log.v(TAG + " handleTouchDrag", points.size() + "");
}

public float[] toFloatarray(List<Float> floatList) {

float[] floatArray = new float[floatList.size()];
int i = 0;

for (Float f : floatList) {
floatArray[i++] = (f != null ? f : Float.NaN);
}

return floatArray;
}

public void handleTouchUp(float normalizedX, float normalizedY) {
Log.v(TAG + " handleTouchUp", points.size() + "");

position++;

}}

使用上面的代码,我可以使用上面的代码使用 GL_LINE_LOOP 绘制多边形,但无法填充创建的多边形。

最佳答案

OpenGL ES 2.0 仅支持绘制三角形作为基本图元。使用三角形绘制多边形有 3 种方法,1)三角形2) 三角带3)三角扇

在您的情况下,如果您知道平面内的点,则可以尝试使用 triangle Fan 绘制多边形。

Here是一个画圆的例子。

关于android - 如何使用 opengl-es 2.0 在 android 中填充多边形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39074962/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com