Draw a Circle in Android Studio Java Vide0

After yous define shapes to be drawn with OpenGL, you probably want to depict them. Drawing shapes with the OpenGL ES 2.0 takes a bit more lawmaking than y'all might imagine, considering the API provides a great deal of control over the graphics rendering pipeline.

This lesson explains how to draw the shapes you defined in the previous lesson using the OpenGL ES 2.0 API.

Initialize shapes

Before you exercise any cartoon, yous must initialize and load the shapes you lot programme to draw. Unless the construction (the original coordinates) of the shapes you use in your program change during the course of execution, you should initialize them in the onSurfaceCreated() method of your renderer for memory and processing efficiency.

Kotlin

class MyGLRenderer : GLSurfaceView.Renderer {     ...     individual lateinit var mTriangle: Triangle     private lateinit var mSquare: Foursquare      override fun onSurfaceCreated(unused: GL10, config: EGLConfig) {         ...         // initialize a triangle         mTriangle = Triangle()         // initialize a foursquare         mSquare = Foursquare()     }     ... }            

Coffee

public class 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 Foursquare();     }     ... }            

Describe a shape

Drawing a defined shape using OpenGL ES 2.0 requires a meaning amount of code, considering you must provide a lot of details to the graphics rendering pipeline. Specifically, you must define the post-obit:

  • Vertex Shader - OpenGL ES graphics lawmaking 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 employ for cartoon i or more shapes.

Y'all need at least one vertex shader to describe a shape and i fragment shader to color that shape. These shaders must be compiled and so added to an OpenGL ES program, which is and then used to depict the shape. Hither is an instance of how to define bones shaders you can use to draw a shape in the Triangle class:

Kotlin

form Triangle {      individual val vertexShaderCode =             "attribute vec4 vPosition;" +             "void main() {" +             "  gl_Position = vPosition;" +             "}"      private val fragmentShaderCode =             "precision mediump float;" +             "uniform vec4 vColor;" +             "void principal() {" +             "  gl_FragColor = vColor;" +             "}"      ... }            

Java

public form Triangle {      private concluding String vertexShaderCode =         "attribute vec4 vPosition;" +         "void primary() {" +         "  gl_Position = vPosition;" +         "}";      private last String fragmentShaderCode =         "precision mediump float;" +         "uniform vec4 vColor;" +         "void principal() {" +         "  gl_FragColor = vColor;" +         "}";      ... }            

Shaders contain OpenGL Shading Linguistic communication (GLSL) code that must be compiled prior to using it in the OpenGL ES environment. To compile this lawmaking, create a utility method in your renderer course:

Kotlin

fun loadShader(type: Int, shaderCode: String): Int {      // create a vertex shader blazon (GLES20.GL_VERTEX_SHADER)     // or a fragment shader blazon (GLES20.GL_FRAGMENT_SHADER)     render GLES20.glCreateShader(type).also { shader ->          // add the source lawmaking to the shader and compile it         GLES20.glShaderSource(shader, shaderCode)         GLES20.glCompileShader(shader)     } }            

Java

public static int loadShader(int type, String shaderCode){      // create a vertex shader blazon (GLES20.GL_VERTEX_SHADER)     // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)     int shader = GLES20.glCreateShader(blazon);      // add the source code to the shader and compile it     GLES20.glShaderSource(shader, shaderCode);     GLES20.glCompileShader(shader);      render shader; }            

In social club to depict your shape, you must compile the shader lawmaking, add them to a OpenGL ES program object and then link the program. Do this in your fatigued object's constructor, so it is only done once.

Note: Compiling OpenGL ES shaders and linking programs is expensive in terms of CPU cycles and processing time, so y'all should avert doing this more than in one case. If you do non know the content of your shaders at runtime, you should build your lawmaking such that they only get created once so cached for later apply.

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 the vertex shader to program             GLES20.glAttachShader(it, vertexShader)              // add together the fragment shader to plan             GLES20.glAttachShader(it, fragmentShader)              // creates OpenGL ES program executables             GLES20.glLinkProgram(information technology)         }     } }            

Java

public class Triangle() {     ...      private terminal 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 together the vertex shader to plan         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 lot want to depict and how to describe it. Since drawing options can vary by shape, it's a good idea to accept your shape classes contain their own drawing logic.

Create a draw() method for drawing the shape. This lawmaking sets the position and color values to the shape's vertex shader and fragment shader, and then executes the drawing function.

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 together program to OpenGL ES environment     GLES20.glUseProgram(mProgram)      // go handle to vertex shader's vPosition fellow member     positionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition").also {          // Enable a handle to the triangle vertices         GLES20.glEnableVertexAttribArray(it)          // Prepare the triangle coordinate information         GLES20.glVertexAttribPointer(                 information technology,                 COORDS_PER_VERTEX,                 GLES20.GL_FLOAT,                 simulated,                 vertexStride,                 vertexBuffer         )          // get handle to fragment shader's vColor fellow member         mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor").also { colorHandle ->              // Set up colour for cartoon the triangle             GLES20.glUniform4fv(colorHandle, 1, color, 0)         }          // Draw the triangle         GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount)          // Disable vertex array         GLES20.glDisableVertexAttribArray(it)     } }            

Coffee

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 depict() {     // Add program to OpenGL ES surround     GLES20.glUseProgram(mProgram);      // go 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 information     GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,                                  GLES20.GL_FLOAT, false,                                  vertexStride, vertexBuffer);      // get handle to fragment shader's vColor fellow member     colorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");      // Prepare 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); }            

Once you have all this code in place, drawing this object just requires a call to the depict() method from within your renderer's onDrawFrame() method:

Kotlin

override fun onDrawFrame(unused: GL10) {     ...      triangle.draw() }            

Java

public void onDrawFrame(GL10 unused) {     ...      triangle.draw(); }            

When you run the application, information technology should look something like this:

Figure 1. Triangle fatigued without a projection or camera view.

There are a few problems with this lawmaking example. Starting time of all, information technology is not going to impress your friends. Secondly, the triangle is a bit 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's vertices take not been corrected for the proportions of the screen area where the GLSurfaceView is displayed. You can fix that trouble using a projection and camera view in the adjacent lesson.

Lastly, the triangle is stationary, which is a fleck tiresome. In the Add motion lesson, y'all make this shape rotate and make more than interesting utilise of the OpenGL ES graphics pipeline.

christensenwhavuld.blogspot.com

Source: https://developer.android.com/training/graphics/opengl/draw

0 Response to "Draw a Circle in Android Studio Java Vide0"

إرسال تعليق

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel