GLSurfaceView
代码见OpenGL ES->顶点着色器和片段着色器代码,只修改顶点数组,片段着色器的颜色,和绘制方式进行不同图元绘制
绘制点
GL_POINTS
方式
// 顶点数据
val vertices = floatArrayOf(
0.8f, -0.8f, 0.0f,
-0.8f, -0.8f, 0.0f,
0.0f, 0.8f, 0.0f,
)
// 顶点着色器代码
val vertexShaderCode = """#version 300 es
layout (location = 0) in vec4 aPosition;
void main() {
gl_Position = aPosition;
}""".trimIndent()
// 片段着色器代码
val fragmentShaderCode = """#version 300 es
precision mediump float;
uniform vec4 vColor;
out vec4 fragColor;
void main() {
fragColor = vColor;
}""".trimIndent()
// 获得顶点数据
val positionHandle = GLES30.glGetAttribLocation(program, "aPosition")
GLES30.glEnableVertexAttribArray(positionHandle)
GLES30.glVertexAttribPointer(positionHandle, 3, GLES30.GL_FLOAT, false, 0, 0)
// 设置片段着色器的颜色
val colorHandle = GLES30.glGetUniformLocation(program, "vColor")
GLES30.glUniform4f(colorHandle, 1.0f,0.5f,0.5f,1.0f)
// 绘制
GLES30.glDrawArrays(GL_POINTS, 0, 3)
// 禁用顶点数据
GLES30.glDisableVertexAttribArray(positionHandle)
绘制线段
GL_LINES
方式
// 顶点数据
val vertices = floatArrayOf(
0.5f, 0.5f, 0.0f,
-0.5f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
)
// 顶点着色器代码
val vertexShaderCode = """#version 300 es
layout (location = 0) in vec4 aPosition;
void main() {
gl_Position = aPosition;
}""".trimIndent()
// 片段着色器代码
val fragmentShaderCode = """#version 300 es
precision mediump float;
uniform vec4 vColor;
out vec4 fragColor;
void main() {
fragColor = vColor;
}""".trimIndent()
// 获得顶点数据
val positionHandle = GLES30.glGetAttribLocation(program, "aPosition")
GLES30.glEnableVertexAttribArray(positionHandle)
GLES30.glVertexAttribPointer(positionHandle, 3, GLES30.GL_FLOAT, false, 0, 0)
// 设置片段着色器的颜色
val colorHandle = GLES30.glGetUniformLocation(program, "vColor")
GLES30.glUniform4f(colorHandle, 1.0f,0.5f,0.5f,1.0f)
GLES30.glLineWidth(20f) // 设置线条宽度,否则看不到线条
// 绘制
GLES30.glDrawArrays(GL_LINES, 0, 4)
// 禁用顶点数据
GLES30.glDisableVertexAttribArray(positionHandle)
GL_LINE_STRIP
方式
// 顶点数据
val vertices = floatArrayOf(
0.5f, 0.5f, 0.0f,
-0.5f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
)
// 顶点着色器代码
val vertexShaderCode = """#version 300 es
layout (location = 0) in vec4 aPosition;
void main() {
gl_Position = aPosition;
}""".trimIndent()
// 片段着色器代码
val fragmentShaderCode = """#version 300 es
precision mediump float;
uniform vec4 vColor;
out vec4 fragColor;
void main() {
fragColor = vColor;
}""".trimIndent()
// 获得顶点数据
val positionHandle = GLES30.glGetAttribLocation(program, "aPosition")
GLES30.glEnableVertexAttribArray(positionHandle)
GLES30.glVertexAttribPointer(positionHandle, 3, GLES30.GL_FLOAT, false, 0, 0)
// 设置片段着色器的颜色
val colorHandle = GLES30.glGetUniformLocation(program, "vColor")
GLES30.glUniform4f(colorHandle, 1.0f,0.5f,0.5f,1.0f)
GLES30.glLineWidth(20f) // 设置线条宽度,否则看不到线条
// 绘制
GLES30.glDrawArrays(GL_LINE_STRIP, 0, 4)
// 禁用顶点数据
GLES30.glDisableVertexAttribArray(positionHandle)
GL_LINE_LOOP
方式
// 顶点数据
val vertices = floatArrayOf(
0.5f, 0.5f, 0.0f,
-0.5f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
)
// 顶点着色器代码
val vertexShaderCode = """#version 300 es
layout (location = 0) in vec4 aPosition;
void main() {
gl_Position = aPosition;
}""".trimIndent()
// 片段着色器代码
val fragmentShaderCode = """#version 300 es
precision mediump float;
uniform vec4 vColor;
out vec4 fragColor;
void main() {
fragColor = vColor;
}""".trimIndent()
// 获得顶点数据
val positionHandle = GLES30.glGetAttribLocation(program, "aPosition")
GLES30.glEnableVertexAttribArray(positionHandle)
GLES30.glVertexAttribPointer(positionHandle, 3, GLES30.GL_FLOAT, false, 0, 0)
// 设置片段着色器的颜色
val colorHandle = GLES30.glGetUniformLocation(program, "vColor")
GLES30.glUniform4f(colorHandle, 1.0f,0.5f,0.5f,1.0f)
GLES30.glLineWidth(20f) // 设置线条宽度,否则看不到线条
// 绘制
GLES30.glDrawArrays(GL_LINE_LOOP, 0, 4)
// 禁用顶点数据
GLES30.glDisableVertexAttribArray(positionHandle)
绘制三角形
GL_TRIANGLES
方式
// 顶点数据
val vertices = floatArrayOf(
0.5f, 0f, 0.0f,
0f, 0.5f, 0.0f,
-0.5f, 0f, 0.0f,
-0.6f, 0f, 0.0f,
0f, -0.6f, 0.0f,
0.6f, 0f, 0.0f,
)
// 顶点着色器代码
val vertexShaderCode = """#version 300 es
layout (location = 0) in vec4 aPosition;
void main() {
gl_Position = aPosition;
}""".trimIndent()
// 片段着色器代码
val fragmentShaderCode = """#version 300 es
precision mediump float;
uniform vec4 vColor;
out vec4 fragColor;
void main() {
fragColor = vColor;
}""".trimIndent()
// 获得顶点数据
val positionHandle = GLES30.glGetAttribLocation(program, "aPosition")
GLES30.glEnableVertexAttribArray(positionHandle)
GLES30.glVertexAttribPointer(positionHandle, 3, GLES30.GL_FLOAT, false, 0, 0)
// 设置片段着色器的颜色
val colorHandle = GLES30.glGetUniformLocation(program, "vColor")
GLES30.glUniform4f(colorHandle, 1.0f,0.5f,0.5f,1.0f)
GLES30.glLineWidth(20f) // 设置线条宽度,否则看不到线条
// 绘制
GLES30.glDrawArrays(GL_TRIANGLES, 0, 6)
// 禁用顶点数据
GLES30.glDisableVertexAttribArray(positionHandle)