# 繪製形狀

## 初始化形狀

### Kotlin

```class MyGLRenderer : GLSurfaceView.Renderer {
...
private lateinit var mTriangle: Triangle
private lateinit var mSquare: Square

override fun onSurfaceCreated(unused: GL10, config: EGLConfig) {
...
// initialize a triangle
mTriangle = Triangle()
// initialize a square
mSquare = Square()
}
...
}
```

### Java

```public class MyGLRenderer implements GLSurfaceView.Renderer {

...
private Triangle mTriangle;
private Square   mSquare;

public void onSurfaceCreated(GL10 unused, EGLConfig config) {
...
// initialize a triangle
mTriangle = new Triangle();
// initialize a square
mSquare = new Square();
}
...
}
```

## 繪製形狀

• Vertex 著色器 - OpenGL ES 圖形程式碼來算繪形狀的頂點。
• 片段著色器 - OpenGL ES 程式碼，用於算繪形狀有顏色或紋理的形狀。
• 程式 - OpenGL ES 物件，其中包含您想用於繪製一或多個形狀的著色器。

### Kotlin

```class Triangle {

"attribute vec4 vPosition;" +
"void main() {" +
"  gl_Position = vPosition;" +
"}"

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

...
}
```

### Java

```public class Triangle {

"attribute vec4 vPosition;" +
"void main() {" +
"  gl_Position = vPosition;" +
"}";

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

...
}
```

### Kotlin

```fun loadShader(type: Int, shaderCode: String): Int {

}
}
```

### Java

```public static int loadShader(int type, String shaderCode){

}
```

### Kotlin

```class Triangle {
...

private var mProgram: Int

init {
...

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

// creates OpenGL ES program executables
}
}
}
```

### Java

```public class Triangle() {
...

private final int mProgram;

public Triangle() {
...

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

// creates OpenGL ES program executables
}
}
```

### Kotlin

```private var positionHandle: Int = 0
private var mColorHandle: Int = 0

private val vertexCount: Int = triangleCoords.size / COORDS_PER_VERTEX
private val vertexStride: Int = COORDS_PER_VERTEX * 4 // 4 bytes per vertex

fun draw() {
// Add program to OpenGL ES environment
GLES20.glUseProgram(mProgram)

// get handle to vertex shader's vPosition member
positionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition").also {

// Enable a handle to the triangle vertices
GLES20.glEnableVertexAttribArray(it)

// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(
it,
COORDS_PER_VERTEX,
GLES20.GL_FLOAT,
false,
vertexStride,
vertexBuffer
)

// get handle to fragment shader's vColor member
mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor").also { colorHandle ->

// Set color for drawing the triangle
GLES20.glUniform4fv(colorHandle, 1, color, 0)
}

// Draw the triangle
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount)

// Disable vertex array
GLES20.glDisableVertexAttribArray(it)
}
}
```

### Java

```private int positionHandle;
private int colorHandle;

private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex

public void draw() {
// Add program to OpenGL ES environment
GLES20.glUseProgram(mProgram);

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

// Enable a handle to the triangle vertices
GLES20.glEnableVertexAttribArray(positionHandle);

// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer);

// get handle to fragment shader's vColor member
colorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

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

// Draw the triangle
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);

// Disable vertex array
GLES20.glDisableVertexAttribArray(positionHandle);
}
```

### Kotlin

```override fun onDrawFrame(unused: GL10) {
...

mTriangle.draw()
}
```

### Java

```public void onDrawFrame(GL10 unused) {
...

mTriangle.draw();
}
```

[]
[]