gpt4 book ai didi

android - 使用 GL_POINTS 生成多个点

转载 作者:行者123 更新时间:2023-11-30 00:54:44 32 4
gpt4 key购买 nike

我正在尝试在 Android 中使用 OpenGL ES 生成随机等高线图。我创建了一个 Point 类用于在屏幕上绘制点,但我不知道绘制随机点的方法应该是什么。

例如,为了显示 2 个点,我创建了 2 个对象

mPoint = new Point();

mPoint2 = new Point();

在我的渲染器中并在 onDrawFrame 方法中绘制它们

mPoint.draw(scratch,0.0f,0.0f,0.0f);
mPoint2.draw(scratch,0.0f,0.5f,0.0f);

我现在要生成 500 个点,创建 500 个可能重叠的对象(相同的 x、y、z 坐标)听起来不合适。到目前为止,我的画点方法正确吗?我应该如何继续创建包含大量点的 View ?

我的积分等级

public class Point {
private int mProgram, mPositionHandle, mColorHandle, mMVPMatrixHandle;


private final String vertexShaderCode =
// This matrix member variable provides a hook to manipulate
// the coordinates of the objects that use this vertex shader
"uniform mat4 uMVPMatrix;" +
"uniform vec4 vPosition;" +
"void main() {" +
" gl_Position = uMVPMatrix * vPosition;" +
" gl_PointSize = 20.0;" +
"}";

private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor;" +
"}";


// Set color with red, green, blue and alpha (opacity) values
float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };

public Point(){
int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
vertexShaderCode);
int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
fragmentShaderCode);

// create empty OpenGL ES Program
mProgram = GLES20.glCreateProgram();

// create empty OpenGL ES Program
mProgram = GLES20.glCreateProgram();

// add the vertex shader to program
GLES20.glAttachShader(mProgram, vertexShader);

// add the fragment shader to program
GLES20.glAttachShader(mProgram, fragmentShader);

// creates OpenGL ES program executables
GLES20.glLinkProgram(mProgram);
}

public void draw(float[] mvpMatrix,float px,float py,float pz) {
float positionCoords[] = {px,py,pz,1.0f};

// Add program to OpenGL ES environment
GLES20.glUseProgram(mProgram);

// get handle to vertex shader's vPosition member
mPositionHandle = GLES20.glGetUniformLocation(mProgram, "vPosition");

mColorHandle = GLES20.glGetUniformLocation(mProgram,"vColor");

// Set color for drawing the triangle
GLES20.glUniform4fv(mColorHandle, 1, color, 0);

GLES20.glUniform4fv(mPositionHandle, 1, positionCoords, 0);

// get handle to shape's transformation matrix
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");

//TRANSLATION
float[] transMatrix = new float[16];

Matrix.setIdentityM(transMatrix,0);
Matrix.translateM(transMatrix,0,0.5f,0,0);
Matrix.multiplyMM(transMatrix,0,mvpMatrix,0,transMatrix,0);


// Apply the projection and view transformation
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, transMatrix, 0);

GLES20.glDrawArrays(GLES20.GL_POINTS, 0, 1);
}

}

编辑:我希望能够每 10 秒生成一次。

最佳答案

这不是一个好方法。每个点一次绘制调用将产生巨大的驱动程序开销和整体糟糕的 GPU 性能。您应该将您的位置和颜色批量放入一个唯一的顶点缓冲区中,并且只绘制一次。

此外,GetUniformLocation() 意味着在链接着色器程序后每个制服调用一次。它可能真的很慢,或者驱动程序可以缓存它们,但你自己做也没什么坏处。

关于android - 使用 GL_POINTS 生成多个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40365579/

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