OpenGL ES for Android draw a point

 

 

In Android development of OpenGL ES applications are not debug shader code, so the point is to draw a good debugging method, in order to locate a number of problems will often result output on the screen, such as face recognition key project, we want to face to determine the key points is correct, the key point is plotted at the corresponding position on the face can be a good show face the key point is correct or not.

The following will complete a drawing point, the color point is determined by the application, the vertex shader code is as follows:

attribute vec4 vPosition;


void main() {
    gl_Position = vPosition;
    gl_PointSize = 5.0;
}

vPosition position is plotted points, passing by the application, gl_PointSize is a built-in variable size representative point, in pixels, pixel, pixels, important things to say three times, another point is the shape of a square, rectangular, square, not circular, because the pixels are square.

Some people may ask is it not in pixels can not be adapted to a large high-resolution display than on low-resolution devices, how if you want to draw a one-100th the size of the plotted points ah? If you want to draw one dot size 100 can be plotted in the form of a square drawn.

Draw point fragment shader code is as follows:

precision mediump float;
uniform vec4 u_color;
void main()
{
    gl_FragColor = u_color;
}

u_color variable is the point of color, passed by the application.

Create a program code is as follows:

fun createProgram() {
            var vertexCode =
                AssetsUtils.readAssetsTxt(
                    context = context,
                    filePath = "glsl/point_vs.glsl"
                )
            var fragmentCode =
                AssetsUtils.readAssetsTxt(
                    context = context,
                    filePath = "glsl/point_fs.glsl"
                )
            mProgramHandle = GLTools.createAndLinkProgram(vertexCode, fragmentCode)
        }

point_vs.glsl vertex shader and point_fs.glsl represent a fragment shader and files stored at assets / glsl directory, readAssetsTxt assets directory common method to read the file.

Get a handle parameters:

vPositionLoc = GLES20.glGetAttribLocation(mProgramHandle, "vPosition")
uColorLoc = GLES20.glGetUniformLocation(mProgramHandle, "u_color")

Vertex data initialization code is as follows:

val vertexBuffer = GLTools.array2Buffer(
            floatArrayOf(
                0F, 0F, 0F
            )
        )

Initialization data color, as follows:

  val colorBuffer = GLTools.array2Buffer(
            floatArrayOf(
                //r,g,b,a
                0F, 1F, 0F, 1F
            )
        )

draw:

GLES20.glUseProgram(mProgramHandle)


            vertexBuffer.position(0)
            GLES20.glEnableVertexAttribArray(vPositionLoc)
            GLES20.glVertexAttribPointer(vPositionLoc, 3, GLES20.GL_FLOAT, false, 0, vertexBuffer)


            GLES20.glUniform4fv(uColorLoc, 1, colorBuffer)


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

Setting color data and vertex data, GLES20.GL_POINTS represents the plotted points.

Read More:

Published 123 original articles · won praise 68 · Views 300,000 +

Guess you like

Origin blog.csdn.net/mengks1987/article/details/104090560