Android Program to Draw Dynamic Circles
After y'all define shapes to be fatigued with OpenGL, you probably want to depict them. Cartoon shapes with the OpenGL ES 2.0 takes a chip more code than you might imagine, because the API provides a great deal of control over the graphics rendering pipeline.
This lesson explains how to depict the shapes you defined in the previous lesson using the OpenGL ES 2.0 API.
Initialize shapes
Before you do whatsoever drawing, you lot must initialize and load the shapes you programme to describe. Unless the structure (the original coordinates) of the shapes you lot apply in your program change during the course of execution, you should initialize them in the onSurfaceCreated()
method of your renderer for retentiveness and processing efficiency.
Kotlin
form 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 foursquare mSquare = Square() } ... }
Java
public form MyGLRenderer implements GLSurfaceView.Renderer { ... individual Triangle mTriangle; private Square mSquare; public void onSurfaceCreated(GL10 unused, EGLConfig config) { ... // initialize a triangle mTriangle = new Triangle(); // initialize a foursquare mSquare = new Square(); } ... }
Depict a shape
Drawing a divers shape using OpenGL ES 2.0 requires a significant amount of code, because you must provide a lot of details to the graphics rendering pipeline. Specifically, yous must define the following:
- Vertex Shader - OpenGL ES graphics code for rendering the vertices of a shape.
- Fragment Shader - OpenGL ES code for rendering the face of a shape with colors or textures.
- Program - An OpenGL ES object that contains the shaders you desire to use for drawing i or more shapes.
You need at least one vertex shader to describe a shape and ane fragment shader to colour that shape. These shaders must be compiled so added to an OpenGL ES program, which is so used to draw the shape. Here is an instance of how to define bones shaders yous tin can use to draw a shape in the Triangle
class:
Kotlin
class Triangle { individual val vertexShaderCode = "attribute vec4 vPosition;" + "void main() {" + " gl_Position = vPosition;" + "}" private val fragmentShaderCode = "precision mediump bladder;" + "compatible vec4 vColor;" + "void main() {" + " gl_FragColor = vColor;" + "}" ... }
Java
public class Triangle { private final Cord vertexShaderCode = "aspect vec4 vPosition;" + "void chief() {" + " gl_Position = vPosition;" + "}"; private final String fragmentShaderCode = "precision mediump bladder;" + "uniform vec4 vColor;" + "void main() {" + " gl_FragColor = vColor;" + "}"; ... }
Shaders contain OpenGL Shading Language (GLSL) code that must be compiled prior to using it in the OpenGL ES environs. To compile this code, create a utility method in your renderer class:
Kotlin
fun loadShader(type: Int, shaderCode: String): Int { // create a vertex shader type (GLES20.GL_VERTEX_SHADER) // or a fragment shader blazon (GLES20.GL_FRAGMENT_SHADER) return GLES20.glCreateShader(type).also { shader -> // add the source code to the shader and compile information technology GLES20.glShaderSource(shader, shaderCode) GLES20.glCompileShader(shader) } }
Coffee
public static int loadShader(int type, Cord shaderCode){ // create a vertex shader blazon (GLES20.GL_VERTEX_SHADER) // or a fragment shader blazon (GLES20.GL_FRAGMENT_SHADER) int shader = GLES20.glCreateShader(blazon); // add the source lawmaking to the shader and compile it GLES20.glShaderSource(shader, shaderCode); GLES20.glCompileShader(shader); return shader; }
In order to draw your shape, you must compile the shader code, add them to a OpenGL ES program object and and so link the program. Exercise this in your drawn object'south constructor, and so it is only done one time.
Note: Compiling OpenGL ES shaders and linking programs is expensive in terms of CPU cycles and processing fourth dimension, and then you should avoid doing this more than than once. If you practise not know the content of your shaders at runtime, you should build your code such that they only get created in one case and then cached for later employ.
Kotlin
class Triangle { ... private var mProgram: Int init { ... val vertexShader: Int = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode) val fragmentShader: Int = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode) // create empty OpenGL ES Program mProgram = GLES20.glCreateProgram().also { // add together the vertex shader to programme GLES20.glAttachShader(it, vertexShader) // add the fragment shader to program GLES20.glAttachShader(it, fragmentShader) // creates OpenGL ES programme executables GLES20.glLinkProgram(it) } } }
Java
public course Triangle() { ... private final int mProgram; public Triangle() { ... 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(); // 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); } }
At this point, you are ready to add the actual calls that depict your shape. Cartoon shapes with OpenGL ES requires that you specify several parameters to tell the rendering pipeline what you want to draw and how to draw it. Since drawing options tin can vary by shape, it's a good idea to have your shape classes contain their own drawing logic.
Create a draw()
method for cartoon the shape. This code sets the position and colour values to the shape's vertex shader and fragment shader, and then executes the drawing function.
Kotlin
individual var positionHandle: Int = 0 individual var mColorHandle: Int = 0 private val vertexCount: Int = triangleCoords.size / COORDS_PER_VERTEX private val vertexStride: Int = COORDS_PER_VERTEX * iv // iv bytes per vertex fun describe() { // Add program to OpenGL ES environs GLES20.glUseProgram(mProgram) // become handle to vertex shader'due south vPosition fellow member positionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition").also { // Enable a handle to the triangle vertices GLES20.glEnableVertexAttribArray(it) // Gear up the triangle coordinate data GLES20.glVertexAttribPointer( it, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer ) // become handle to fragment shader'southward vColor fellow member mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor").as well { colorHandle -> // Set color for drawing the triangle GLES20.glUniform4fv(colorHandle, i, color, 0) } // Draw the triangle GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount) // Disable vertex assortment GLES20.glDisableVertexAttribArray(it) } }
Java
private int positionHandle; private int colorHandle; private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX; private concluding 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 fellow 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, simulated, vertexStride, vertexBuffer); // get handle to fragment shader'southward vColor member colorHandle = GLES20.glGetUniformLocation(mProgram, "vColor"); // Fix color for drawing the triangle GLES20.glUniform4fv(colorHandle, 1, color, 0); // Draw the triangle GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount); // Disable vertex assortment GLES20.glDisableVertexAttribArray(positionHandle); }
Once you have all this code in place, drawing this object simply requires a call to the draw()
method from within your renderer's onDrawFrame()
method:
Kotlin
override fun onDrawFrame(unused: GL10) { ... triangle.describe() }
Coffee
public void onDrawFrame(GL10 unused) { ... triangle.draw(); }
When you run the application, it should wait something like this:
Figure 1. Triangle drawn without a projection or camera view.
At that place are a few problems with this lawmaking example. First of all, it is not going to impress your friends. Secondly, the triangle is a fleck squashed and changes shape when you change the screen orientation of the device. The reason the shape is skewed is due to the fact that the object'due south vertices have not been corrected for the proportions of the screen area where the GLSurfaceView
is displayed. You tin gear up that trouble using a projection and camera view in the next lesson.
Lastly, the triangle is stationary, which is a bit boring. In the Add motion lesson, you lot make this shape rotate and make more interesting use of the OpenGL ES graphics pipeline.
Source: https://developer.android.com/training/graphics/opengl/draw
0 Response to "Android Program to Draw Dynamic Circles"
Post a Comment