Membuat Segitiga & Segiempat

Membuat Segitiga & Segiempat - Primitives Object

===

2.4 Membuat Segitiga & Segiempat

Segitiga dibuat dengan “GL_TRIANGLES”. Dan khusus untuk yang segiempat akan dibuat dengan 2 segitiga yang menjadi primitive object-nya. Ada beberapa cara yang dapat digunakan untuk membuat segiempat, diantaranya :

a. Menggunakan GL_TRIANGLE_STRIP

“// Draw the vertices as rectangle gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0,4); // 4 is number of data vertices_rect”

b. Menggunakan GL_TRIANGLES

“// Draw the vertices as rectangle gl.glDrawElements(GL10.GL_TRIANGLES, Indices.length, GL10.GL_UNSIGNED_SHORT, makeShortBuffer(Indices));”

Indices menyatakan urutan dari index vertex yang akan dikon- eksikan untuk membentuk segiempat. Masing-masing cara menggunakan

berbeda, untuk GL_TRIANGLE_STRIP menggunakan “makeFloatBuffer” untuk membuffer semua posisi vertice dan warna penyusun objek. Potongan scriptnya sebagai berikut

“// List all points to our vertex buffer gl.glVertexPointer(3, GL10.GL_FLOAT, 0, “// List all points to our vertex buffer gl.glVertexPointer(3, GL10.GL_FLOAT, 0,

FloatBuffer” dan sekaligus “makeShortBuffer”, pada cara yang kedua ini “makeShortBuffer” digunakan untuk draw objek segitiga. Spesifi- kasi kedua metode “public static FloatBuffer makeFloatBuffer(float[] arr)” untuk 4 bytes untuk membangun vertices dan color penyusun ob- jek, “public static ShortBuffer makeShortBuffer(short[] arr)” untuk 2 bytes untuk membangun urutan index dari vertex untuk draw objek sesuai dengan yang diinginkan.

“gl.glFrontFace(GL10.GL_CCW); // Front face in counter-clockwise / Counter-clockwise winding. // orientation gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face gl.glCullFace(GL10.GL_BACK); // Cull the back face (don't display) remove face with culling.”

Gambar 2.9 Membuat Segiempat dengan 2 Segitiga Karena segiempat tersebut dibentuk dengan menggunakan dua

segitiga, maka efek persilangkan pada sisi miring dari kedua segitiga akan nampak jelas. Untuk menghilangkan efek tersebut, dapat digunakan script di atas.

Pada Gambar 2.9, nilai-nilai “Indices” dapat dibentuk dengan cara mengambil index masing-masing vertex penyusun objek. Misal (x 1 ,y 1 ,z 1 ) disimpan pada vertices index 0, (x 2 ,y 2 ,z 2 ) pada index 1, (x 3 ,y 3 ,z 3 ) pada index 2, dan (x 4 ,y 4 ,z 4 ) pada index 3. Misal diketahui nilai vertex-vertex sebagai berikut

“private float[] vertices_rect = {

-1.0f, -1.0f, 0.0f, // 0. left-bottom-front 1.0f, -1.0f, 0.0f, // 1. right-bottom-front -1.0f, 1.0f, 0.0f, // 2. left-top-front 1.0f, 1.0f, 0.0f // 3. right-top-front

};” maka list dari index-index pada tersebut “Indices” adalah “// The order vertices_rect to connect them //(diagonal must crossing) private short[] Indices = { 0, 1, 2, 2, 1, 3 };”

Nilai dalam “Indices” tidak selalu sama, tetapi akan menghasilkan urutan yang berbeda jika diketahui urutan index dari nilai vertex-vertex juga berbeda, contoh diketahui

“private float[] vertices_rect = { -1.0f, 1.0f, 0.0f, // 0, Top Left -1.0f, -1.0f, 0.0f, // 1, Bottom Left 1.0f, -1.0f, 0.0f, // 2, Bottom Right 1.0f, 1.0f, 0.0f, // 3, Top Right

Gambar 2.10 Segitiga dan segiempat Gambar 2.10 Segitiga dan segiempat

“// The order vertices_rect to connect them //(diagonal must crossing) private short[] Indices = { 0, 1, 2, 0, 2, 3 };”

Source Code 2.5 Code Membuat Segitiga & Segiempat

ESRender.java

package com.example.imacho.createtrianglenrect;

import android.opengl.GLSurfaceView.Renderer; import android.opengl.GLU; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10;

/** * Created by Imacho on 4/5/2015. */

public class ESRender implements Renderer {

private Triangle_n_Rectangle trian- gle_n_rectangle;

// the object to be drawn

/** Constructor to set the handed over context */ public ESRender() {

this.triangle_n_rectangle = new Trian- gle_n_Rectangle(); }

@Override public void onDrawFrame(GL10 gl) {

gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // set background with white color //gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // set background with black color

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // clear Screen and Depth Buffer

// display drawing triangle one color /*gl.glPushMatrix();

// start freeze state/event to each object gl.glTranslatef(0.0f, -2.0f, -5.0f); trian-

gle_n_rectangle.draw_triangle_one_color(gl); gl.glPopMatrix(); // end freeze state/event to each object */

// display drawing triangle three color // display drawing triangle three color

gle_n_rectangle.draw_triangle_three_color(gl); gl.glPopMatrix(); // end freeze state/event to each object

// display rectangle dengan cara ke-1 gl.glPushMatrix(); // start freeze

state/event to each object gl.glTranslatef(2.5f, 0.0f, 0.0f); gl.glTranslatef(0.0f, 0.0f, -5.0f); trian-

gle_n_rectangle.draw_rectangle_cara1(gl); gl.glPopMatrix();

// display rectangle dengan cara ke-2 gl.glPushMatrix(); // start freeze

state/event to each object gl.glTranslatef(-2.5f, 0.0f, 0.0f); gl.glTranslatef(0.0f, 0.0f, -5.0f); trian-

gle_n_rectangle.draw_rectangle_cara2(gl); gl.glPopMatrix();

@Override public void onSurfaceChanged(GL10 gl, int width,

int height) { if (height == 0) height = 1; // To prevent divide by zero float aspect = (float) width / height; // Set the viewport (display area) to cover

the entire window gl.glViewport(0, 0, width, height); // Setup perspective projection, with aspect

ratio matches viewport gl.glMatrixMode(GL10.GL_PROJECTION); // Se- lect projection matrix gl.glLoadIdentity(); // Reset projection ma- trix // Use perspective projection GLU.gluPerspective(gl, 45, aspect, 0.1f,

100.f); gl.glMatrixMode(GL10.GL_MODELVIEW); // Select model-view matrix gl.glLoadIdentity(); // Reset }

@Override public void onSurfaceCreated(GL10 gl, EGLConfig

config) { triangle_n_rectangle = new Trian- gle_n_Rectangle(); }

MainActivity.java

package com.example.imacho.createtrianglenrect;

import android.app.Activity; import android.opengl.GLSurfaceView; import android.os.Bundle; import android.view.Menu; import android.view.Window; import android.view.WindowManager;

/** * Created by Imacho on 4/5/2015. */

public class MainActivity extends Activity {

/** The OpenGL view */ private GLSurfaceView glSurfaceView;

@Override protected void onCreate(Bundle savedIn-

stanceState) { super.onCreate(savedInstanceState);

// requesting to turn the title OFF requestWindowFea- ture(Window.FEATURE_NO_TITLE);

// making it full screen getWin- dow().setFlags(WindowManager.LayoutParams.FLAG_ FULLSCREEN,

WindowManag- er.LayoutParams.FLAG_FULLSCREEN);

// Initiate the Open GL view and create an instance with this activity glSurfaceView = new GLSur- faceView(this);

// set our renderer to be the main ren- derer with the current activity context glSurfaceView.setRenderer(new ESRen- // set our renderer to be the main ren- derer with the current activity context glSurfaceView.setRenderer(new ESRen-

* Remember to resume the glSurface */

@Override protected void onResume() {

super.onResume(); glSurfaceView.onResume();

/** * Also pause the glSurface */

@Override protected void onPause() {

super.onPause(); glSurfaceView.onPause();

@Override public boolean onCreateOptionsMenu(Menu

menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflat- er().inflate(R.menu.menu_main, menu); return true; } }

Triangle_n_Rectangle.java

package com.example.imacho.createtrianglenrect;

import android.util.Log; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

/** * Created by Imacho on 4/5/2015. */

public class Triangle_n_Rectangle { public class Triangle_n_Rectangle {

null; float x,y; float step_loop=0.001f; float x1,y1; float x2,y2; float array_factor=2.0f; private int loop_line,loop_line_color;

private float[] vertices_rect = { -1.0f, -1.0f, 0.0f, // 0. left-bottom- front 1.0f, -1.0f, 0.0f, // 1. right-bottom- front -1.0f, 1.0f, 0.0f, // 2. left-top-front 1.0f, 1.0f, 0.0f // 3. right-top-front

private float[] color_rect = { // Vertices for color 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f

// The order vertices_rect to connect them (diag- onal must crossing)

private short[] Indices = { 0, 1, 2, 2, 1, 3 };

public Triangle_n_Rectangle() { }

// Point to our vertex buffer, return buffer holding the vertices // a float is 4 bytes, therefore we multiply the number if // vertices with 4. public static FloatBuffer makeFloatBuffer(float[]

arr) { ByteBuffer bb = ByteBuff- er.allocateDirect(arr.length * 4); bb.order(ByteOrder.nativeOrder()); FloatBuffer fb = bb.asFloatBuffer(); fb.put(arr); fb.position(0); return fb;

// short is 2 bytes, therefore we multiply the number if // vertices with 2. public static ShortBuffer makeShortBuffer(short[]

arr) { ByteBuffer bb = ByteBuff- er.allocateDirect(arr.length * 2); bb.order(ByteOrder.nativeOrder()); ShortBuffer sb = bb.asShortBuffer(); arr) { ByteBuffer bb = ByteBuff- er.allocateDirect(arr.length * 2); bb.order(ByteOrder.nativeOrder()); ShortBuffer sb = bb.asShortBuffer();

/** The draw method for the primitive object with the GL context */

public void draw_triangle_one_color(GL10 gl){

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

// set the global colour for the triangle gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);

// Point to our vertex buffer gl.glVertexPointer(3, GL10.GL_FLOAT, 0, make-

FloatBuffer(new float [] { -1.0f, -1.0f, 0.0f, // V1 - first vertex (x,y,z) 1.0f, -1.0f, 0.0f,

// V2 - second vertex 0.0f, 1.0f, 0.0f // V3 - third ver- tex (x,y,z) }));

// Draw the vertices as triangle gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0,

// set the global colour for wire the trian- gle

gl.glColor4f(0.0f, 0.0f, 0.0f, 1.0f);

// Draw the wire as edge triangle gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, 3);

//Disable the client state before leaving

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); }

public void draw_triangle_three_color(GL10 gl){

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// Point to our vertex buffer gl.glVertexPointer(3, GL10.GL_FLOAT, 0, make-

FloatBuffer(new float [] { -1.0f, -1.0f, 0.0f, // V1 - first vertex (x,y,z) 1.0f, -1.0f, 0.0f,

// V2 - second vertex 0.0f, 1.0f, 0.0f // V3 - third ver- tex (x,y,z) }));

// Set/ mapping the colour for object gl.glColorPointer(4, GL10.GL_FLOAT, 0, make-

FloatBuffer(new float[]{ 1.0f, 0.0f, 0.0f, 1.0f, // first FloatBuffer(new float[]{ 1.0f, 0.0f, 0.0f, 1.0f, // first

// Draw the vertices as triangle gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0,

// set the global colour for wire the trian- gle

gl.glColor4f(0.0f, 0.0f, 0.0f, 1.0f);

// Draw the wire as edge triangle gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, 3);

//Disable the client state before leaving

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); gl.glDisableClientState(GL10.GL_COLOR_ARRAY); }

public void draw_rectangle_cara1(GL10 gl) { gl.glFrontFace(GL10.GL_CCW); // Front face in counter-clockwise / Counter-clockwise winding. // orientation gl.glEnable(GL10.GL_CULL_FACE); // Enable

cull face gl.glCullFace(GL10.GL_BACK); // Cull the back face (don't display) / What faces to remove with the face culling.

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// set the global colour for the triangle //gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);

// List all points to our vertex buffer gl.glVertexPointer(3, GL10.GL_FLOAT, 0, make-

FloatBuffer(vertices_rect));

// Set/ mapping the colour for object gl.glColorPointer(4, GL10.GL_FLOAT, 0, make-

FloatBuffer(color_rect));

// Draw the vertices as rectangle gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0,4);

// 4 is number of data vertices_rect

//Disable the client state before leaving

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

// Disable face culling. gl.glDisable(GL10.GL_CULL_FACE); // Disable face culling. gl.glDisable(GL10.GL_CULL_FACE);

gl.glFrontFace(GL10.GL_CCW); // Front face in counter-clockwise / Counter-clockwise winding. // orientation gl.glEnable(GL10.GL_CULL_FACE); // Enable

cull face gl.glCullFace(GL10.GL_BACK); // Cull the back face (don't display) / What faces to remove with the face culling.

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// set the global colour for the triangle //gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);

// Lish all points to our vertex buffer gl.glVertexPointer(3, GL10.GL_FLOAT, 0, make-

FloatBuffer(vertices_rect));

// Set/ mapping the colour for object gl.glColorPointer(4, GL10.GL_FLOAT, 0, make-

FloatBuffer(color_rect));

// Draw the vertices as rectangle gl.glDrawElements(GL10.GL_TRIANGLES, Indi-

ces.length, GL10.GL_UNSIGNED_SHORT, makeShort- Buffer(Indices));

//Disable the client state before leaving

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

// Disable face culling. gl.glDisable(GL10.GL_CULL_FACE);

Source Code 2.6 Code Lain Membuat Segitiga Sederhana ESRender.java

package com.example.primitives_project_es;

import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; //import android.opengl.GLU; import android.opengl.GLSurfaceView.Renderer; import android.opengl.GLU; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; //import android.opengl.GLU; import android.opengl.GLSurfaceView.Renderer; import android.opengl.GLU;

/** Constructor to set the handed over context */ public ESRender() {

this.primitivesobject = new PrimitivesObject(); this.primitivespolarobject = new PrimitivesPolar- Object();

@Override public void onDrawFrame(GL10 gl) {

// clear Screen and Depth Buffer // set background dgn warna putih gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

// Reset the Modelview Matrix gl.glLoadIdentity();

// segitiga dengan gradasi warna gl.glPushMatrix(); gl.glRotatef(-sudut, 0.0f, 0.0f, 1.0f); gl.glTranslatef(0.0f, 0.0f, -6.5f); gl.glRotatef(180, 0.0f, 0.0f, 1.0f); primitivesobject.draw_segitiga(gl);

gl.glPopMatrix();

@Override public void onSurfaceChanged(GL10 gl, int width, int height) {

if (height == 0) height = 1; // To prevent divide by zero float aspect = (float) width / height; // Set the viewport (display area) to cover the entire window gl.glViewport(0, 0, width, height); // Setup perspective projection, with aspect ra- tio matches viewport gl.glMatrixMode(GL10.GL_PROJECTION); // Select projection matrix gl.glLoadIdentity(); // Reset projection matrix // Use perspective projection GLU.gluPerspective(gl, 45, aspect, 0.1f, 100.f); gl.glMatrixMode(GL10.GL_MODELVIEW); // Select model-view matrix gl.glLoadIdentity(); // Reset

@Override @Override

primitivesobject = new PrimitivesObject(); primitivespolarobject = new PrimitivesPolar- Object();

MainActivity.java

package com.example.primitives_project_es;

import android.opengl.GLSurfaceView; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.Window; import android.view.WindowManager;

public class MainActivity extends Activity {

/** The OpenGL view */ private GLSurfaceView glSurfaceView;

@Override protected void onCreate(Bundle savedIn-

stanceState) { super.onCreate(savedInstanceState);

//setContentView(R.layout.activity_main);

// requesting to turn the title OFF requestWindowFea- ture(Window.FEATURE_NO_TITLE); // making it full screen getWin- dow().setFlags(WindowManager.LayoutParams.FLAG_ FULLSCREEN,

WindowManag- er.LayoutParams.FLAG_FULLSCREEN);

// Initiate the Open GL view and // create an instance with this activi-

ty glSurfaceView = new GLSur- faceView(this);

// set our renderer to be the main ren- // set our renderer to be the main ren-

der()); setContentView(glSurfaceView); }

* Remember to resume the glSurface */

@Override protected void onResume() {

super.onResume(); glSurfaceView.onResume();

/** * Also pause the glSurface */

@Override protected void onPause() {

super.onPause(); glSurfaceView.onPause();

@Override public boolean onCreateOptionsMenu(Menu

menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflat- er().inflate(R.menu.main, menu); return true; } }

PrimitivesObject.java

package com.example.primitives_project_es;

import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLES20;

public class PrimitivesObject {

private float vertices[] = { -0.5f, -0.5f, 0.0f,

// V1 - first vertex

// (x,y,z) -0.5f, 0.5f, 0.0f, // V2 0.5f, 0.5f, 0.0f, // V3 0.5f, -0.5f, 0.0f, // V4 -0.5f, -0.5f, 0.0f // V5

private float[] vertices_quad = { // Vertices for the square -1.0f, -1.0f, 0.0f, // 0. left-bottom 1.0f, -1.0f, 0.0f, // 1. right- bottom -1.0f, 1.0f, 0.0f, // 2. left-top 1.0f, 1.0f, 0.0f // 3. right-top

private float vertices_color[] = { 1.0f, 0.0f, 0.0f, 1.0f, // CV1 - first

// color // (red,green,blue)

0.0f, 1.0f, 0.0f, 1.0f, // CV2 0.0f, 0.0f, 1.0f, 1.0f, // CV3 0.0f, 1.0f, 0.0f, 1.0f, // CV4 1.0f, 0.0f, 0.0f, 1.0f // CV5

private float vertices_circle[] = { 0.0f, 0.0f, 0.0f }; private float vertices_circle_color[] = { 0.0f, 0.0f, 0.0f, 0.5f };

private float vertices_line[] = { 0.0f, 0.0f, 0.0f };

private float vertices_line_color[] = { 0.0f, 0.0f, 0.0f, 1.0f }; private int batas_sudut = 360; float jari_jari; float a, b; float x, y; float step = 3.0f, step_line = 0.2f; float x1, y1; float x2, y2; private int loop, loop_color, loop_line,

loop_line_color;

public PrimitivesObject() {

// ============ start to generate verti- ces to circle

// ========================== // Inisialisasi jari_jari = 1.0f;

// Titik Pusat a = 0.0f; b = 0.0f; // Titik Pusat a = 0.0f; b = 0.0f;

loop = 3; loop_color = 4; vertices_circle = new float[(int) (3 *

batas_sudut / step) * 3]; vertices_circle_color = new float[(int) (3 * batas_sudut / step) * 4]; for (float teta = 0; teta <= 2 * ba- tas_sudut; teta += step) { vertices_circle[loop] = (float) ((x - a) * Math.cos((teta / 180) * (22 / 7)) - ((y - b) * Math.sin((teta / 180) * (22 / 7))) + a); vertices_circle[loop + 1] = (float) ((x - a) * Math.sin((teta / 180) * (22 / 7)) - ((y - b) * Math.cos((teta / 180) * (22 / 7))) + b); vertices_circle[loop + 2] = 0; loop += 3;

// mengenerate warna untuk setiap vertex vertices_circle_color[loop_color] = (float) ((x - a) * Math.cos((teta / 180) * (22 / 7)) - ((y - b) * Math.sin((teta / 180) * (22 / 7))) + a); vertices_circle_color[loop_color + 1] = (float) ((x - a) * Math.sin((teta / 180) * (22 / 7)) - ((y - b) * Math.cos((teta / 180) * (22 / 7))) + b); vertices_circle_color[loop_color + 2] = 0.5f; vertices_circle_color[loop_color + 3] = 0.5f;

loop_color += 4;

} // ============= end for generate verti-

ces to circle // ====================

// ============ start to generate verti- ces to line

// ========================== x1 = -1.0f; y1 = -1.0f; x2 = 1.0f; y2 = 1.0f;

loop_line = 3; loop_line = 3;

- x1) / step_line) * 3]; vertices_line_color = new float[(int) (2 * (x2 - x1) / step_line) * 4];

float m = (y2 - y1) / (x2 - x1); for (x = x1; x <= x2; x += step_line) {

vertices_line[loop_line] = (float) (x); vertices_line[loop_line + 1] = (float) (m * (x - x1) + y1); vertices_line[loop_line + 2] = 0; loop_line += 3;

// mengenerate warna untuk setiap vertex verti- ces_line_color[loop_line_color] = (float) (0.5 * x); verti- ces_line_color[loop_line_color + 1] = (float) (0.5 * m

* (x - x1) + y1); verti- ces_line_color[loop_line_color + 2] = 1.0f; verti- ces_line_color[loop_line_color + 3] = 1.0f; loop_line_color += 4; } // ============= end for generate verti-

ces to line ==================== }

// Point to our vertex buffer, return buffer holding the vertices public static FloatBuffer makeFloatBuffer(float[] arr) { ByteBuffer bb = ByteBuff- er.allocateDirect(arr.length * 4); bb.order(ByteOrder.nativeOrder()); FloatBuffer fb = bb.asFloatBuffer(); fb.put(arr); fb.position(0); return fb;

/** The draw method for the primitive object with the GL context */ public void draw_points(GL10 gl) {

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

// gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// set the colour for the points (pem- berian warna untuk titik) gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);

// Point to our vertex buffer (mendata nilai lokasi/posisi titik) gl.glVertexPointer(3, GL10.GL_FLOAT, 0, makeFloatBuffer(new float[] { 1.0f, 1.0f, 0.0f, // V1 - first vertex (x,y,z) 1.0f, 0.8f, 0.0f, // V2 1.0f, 0.6f, 0.0f, // V3 1.0f, 0.4f, 0.0f, // V4 1.0f, 0.2f, 0.0f, // V5 1.0f, 0.0f, 0.0f, // V6 1.0f, -0.2f, 0.0f, // V7 1.0f, -0.4f, 0.0f, // V8 1.0f, -0.6f, 0.0f, // V9 1.0f, -0.8f, 0.0f, // V10 1.0f, -1.0f, 0.0f, // V11

0.8f, -1.0f, 0.0f, // V12 0.6f, -1.0f, 0.0f, // V13 0.4f, -1.0f, 0.0f, // V14 0.2f, -1.0f, 0.0f, // V15 0.0f, -1.0f, 0.0f, // V16 -0.2f, -1.0f, 0.0f, // V17 -0.4f, -1.0f, 0.0f, // V18 -0.6f, -1.0f, 0.0f, // V19 -0.7f, -1.0f, 0.0f, // V20 -0.8f, -1.0f, 0.0f, // V21 -1.0f, -1.0f, 0.0f, // V22

// Draw the vertices as points (menggam- bar titik-titik) gl.glDrawArrays(GL10.GL_POINTS, 0, 22);

// Disable the client state before leav- ing

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

// gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

public void draw_line(GL10 gl) {

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

// gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// set the colour for the line (pemberian warna untuk garis) gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);

// Point to our vertex buffer (mendata nilai lokasi/posisi titik yang // menyusun garis) gl.glVertexPointer(3, GL10.GL_FLOAT, 0,

makeFloatBuffer(new float[] {

1.0f, 1.0f, 0.0f, // V1 - first vertex (x,y,z) -1.0f, -1.0f, 0.0f, // V2 - second vertex }));

// Draw the vertices as lines (menggambar garis dari titik-titik) gl.glDrawArrays(GL10.GL_LINES, 0, 2); /*

* gl.glDrawElements(GL10.GL_LINES, 2, GL10.GL_UNSIGNED_SHORT, * makeFloatBuffer(new float [] { 1.0f, 1.0f, 0.0f, // V1 - first vertex * (x,y,z) -1.0f, -1.0f, 0.0f, // V2 - second vertex })); */

// Disable the client state before leav- ing

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

// gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

public void draw_line_color(GL10 gl) {

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// set the colour for the line (pemberian warna untuk garis) gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);

// Point to our vertex buffer (mendata nilai lokasi/posisi titik yang // menyusun garis) gl.glVertexPointer(3, GL10.GL_FLOAT, 0,

makeFloatBuffer(vertices_line));

// memetakan warna untuk setiap vertex gl.glColorPointer(4, GL10.GL_FLOAT, 0,

makeFloatBuff- er(vertices_line_color));

// Draw the vertices as lines (menggambar garis dari titik-titik) gl.glDrawArrays(GL10.GL_LINE_STRIP, 0, (int) (2 * (x2 - x1) / step_line)); /* * gl.glDrawElements(GL10.GL_LINES, 2, GL10.GL_UNSIGNED_SHORT, * makeFloatBuffer(new float [] { 1.0f, 1.0f, 0.0f, // V1 - first vertex

* (x,y,z) -1.0f, -1.0f, 0.0f, // V2 - second vertex })); */

// Disable the client state before leav- ing

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

public void draw_circle(GL10 gl) {

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

// gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// set the colour for the object circle gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);

// create VBO from buffer with glBuffer- Data() gl.glVertexPointer(3, GL10.GL_FLOAT, 0, makeFloatBuff- er(vertices_circle));

// draw circle as filled shape // gl.glDrawArrays(GL10.GL_TRIANGLE_FAN,

1, (int) ((int) // 2*batas_sudut/step));

// draw circle contours // gl.glDrawArrays(GL10.GL_LINES, 1,

(int) ((int) 2*batas_sudut/step)); // // membuat garis putus-putus pada tepi lingkaran gl.glDrawArrays(GL10.GL_LINES, 1, (int) ((int) 2 * batas_sudut / step)); // gl.glDrawArrays(GL10.GL_LINE_STRIP, 1, (int) ((int) // 2*batas_sudut/step)); // gl.glDrawArrays(GL10.GL_POINTS, 1,

(int) ((int) 2*batas_sudut/step));

// Disable the client state before leav- ing

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

// gl.glDisableClientState(GL10.GL_COLOR_ARRAY); }

public void draw_circle_color(GL10 gl) {

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// set the colour edge for the object circle gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);

// create VBO from buffer with glBuffer- Data() gl.glVertexPointer(3, GL10.GL_FLOAT, 0, makeFloatBuff- er(vertices_circle));

// memetakan warna untuk setiap vertex gl.glColorPointer(4, GL10.GL_FLOAT, 0,

makeFloatBuff- er(vertices_circle_color));

// draw circle as filled shape // gl.glDrawArrays(GL10.GL_TRIANGLE_FAN,

1, (int) ((int) // 2*batas_sudut/step)); gl.glDrawArrays(GL10.GL_LINE_STRIP, 1,

(int) ((int) 2 * ba- tas_sudut / step));

// Disable the client state before leav- ing

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

gl.glDisableClientState(GL10.GL_COLOR_ARRAY); }

public void draw_kotak(GL10 gl) {

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// Point to our vertex buffer gl.glVertexPointer(3, GL10.GL_FLOAT, 0,

makeFloatBuffer(vertices));

// Draw the vertices as square gl.glColorPointer(4, GL10.GL_FLOAT, 0,

makeFloatBuffer(vertices_color)); gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);

gl.glColorPointer(4, GL10.GL_FLOAT, 0, makeFloatBuffer(vertices_color)); gl.glDrawArrays(GL10.GL_TRIANGLES, 2, 3);

// Disable the client state before leav- ing

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

gl.glDisableClientState(GL10.GL_COLOR_ARRAY); gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// set the colour for the triangle gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);

gl.glVertexPointer(3, GL10.GL_FLOAT, 0, makeFloatBuffer(new float[] { -0.5f, -0.5f, 0.0f, // V1 - first vertex (x,y,z) 0.5f, -0.5f, 0.0f, // V2 - second vertex 0.0f, 0.5f, 0.0f // V3 - third vertex

// Draw the vertices as triangle gl.glColorPointer(4, GL10.GL_FLOAT, 0,

makeFloatBuffer(vertices_color)); gl.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);

// Disable the client state before leav- ing

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

// Render the shape quad public void draw_quad(GL10 gl) {

// Enable vertex-array and define its buffer

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

gl.glVertexPointer(3, GL10.GL_FLOAT, 0, makeFloatBuffer(vertices_quad)); // Draw the primitives from the vertex- array directly

gl.glPolygonOffset(0.0f, 1.0f); gl.glScalef(0.5f, 0.5f, 0.5f); gl.glColor4f(0.5f, 0.5f, 1.0f, 1.0f); //

Set the current color (NEW) gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices_quad.length / 3); //gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices_quad.length / 3); //gl.glDrawArrays(GL10.GL_LINES, 0, ver- tices_quad.length / 3); Set the current color (NEW) gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices_quad.length / 3); //gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices_quad.length / 3); //gl.glDrawArrays(GL10.GL_LINES, 0, ver- tices_quad.length / 3);

Gambar 2.11 Membuat Segitiga Sederhana

Source Code 2.7 Code Membuat Segiempat ESRender.java

package com.example.primitives_project_es;

import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; //import android.opengl.GLU; import android.opengl.GLSurfaceView.Renderer; import android.opengl.GLU;

public class ESRender implements Renderer {

// the primitive object to be drawn private PrimitivesPolarObject primitivespolar- object; float sudut=0.0f; float step_sudut=0.0f;

private PrimitivesObject primitivesobject;

/** Constructor to set the handed over context */ public ESRender() {

this.primitivesobject = new Primi- tivesObject(); this.primitivespolarobject = new Primi- tivesPolarObject();

@Override public void onDrawFrame(GL10 gl) {

// clear Screen and Depth Buffer gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // set background dgn warna putih //gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // set background dgn warna hitam

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

// Reset the Modelview Matrix gl.glLoadIdentity();

// menampilkan persegi dengan gradasi warna gl.glPushMatrix(); gl.glTranslatef(0.0f, 0.0f, - 5.0f); primitivesobject.draw_kotak(gl);

gl.glPopMatrix(); }

@Override public void onSurfaceChanged(GL10 gl, int width,

int height) {

if (height == 0) height = 1; // To prevent divide by zero float aspect = (float) width / height; // Set the viewport (display area) to

cover the entire window gl.glViewport(0, 0, width, height); // Setup perspective projection, with as-

pect ratio matches viewport gl.glMatrixMode(GL10.GL_PROJECTION); // Select projection matrix gl.glLoadIdentity(); // Reset projection matrix

// Use perspective projection GLU.gluPerspective(gl, 45, aspect, 0.1f,

100.f); gl.glMatrixMode(GL10.GL_MODELVIEW); // Select model-view matrix

gl.glLoadIdentity(); // Reset

@Override public void onSurfaceCreated(GL10 gl, EGLConfig

config) {

//primitivesobject = new Primi-

tivesObject();

primitivesobject = new Primi-

tivesObject(); primitivespolarobject = new PrimitivesPo- larObject();

MainActivity.java

package com.example.primitives_project_es;

import android.opengl.GLSurfaceView; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.Window; import android.view.WindowManager;

public class MainActivity extends Activity {

/** The OpenGL view */ private GLSurfaceView glSurfaceView;

@Override protected void onCreate(Bundle savedInstanceS-

tate) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main);

// requesting to turn the title OFF requestWindowFea- ture(Window.FEATURE_NO_TITLE); // making it full screen getWin- dow().setFlags(WindowManager.LayoutParams.FLAG_FULLSC REEN,

WindowManag- er.LayoutParams.FLAG_FULLSCREEN);

// Initiate the Open GL view and // create an instance with this activity glSurfaceView = new GLSurfaceView(this);

// set our renderer to be the main renderer with

// the current activity context glSurfaceView.setRenderer(new ESRender()); setContentView(glSurfaceView);

/** * Remember to resume the glSurface */

@Override protected void onResume() {

super.onResume(); glSurfaceView.onResume();

* Also pause the glSurface */

@Override protected void onPause() {

super.onPause(); glSurfaceView.onPause();

} @Override public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }

PrimitivesObject.java

package com.example.primitives_project_es;

import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLES20;

public class PrimitivesObject {

private float vertices[] = { -0.5f, -0.5f, 0.0f, // V1 - first vertex

// (x,y,z) -0.5f, 0.5f, 0.0f, // V2 0.5f, 0.5f, 0.0f, // V3 0.5f, -0.5f, 0.0f, // V4 -0.5f, -0.5f, 0.0f // V5

private float[] vertices_quad = { // Vertices for the square -1.0f, -1.0f, 0.0f, // 0. left-bottom 1.0f, -1.0f, 0.0f, // 1. right- bottom -1.0f, 1.0f, 0.0f, // 2. left-top 1.0f, 1.0f, 0.0f // 3. right-top

private float vertices_color[] = { 1.0f, 0.0f, 0.0f, 1.0f, // CV1 - first

// color

// (red,green,blue) 0.0f, 1.0f, 0.0f, 1.0f, // CV2 0.0f, 0.0f, 1.0f, 1.0f, // CV3 0.0f, 1.0f, 0.0f, 1.0f, // CV4 1.0f, 0.0f, 0.0f, 1.0f // CV5

private float vertices_circle[] = { 0.0f, 0.0f, 0.0f }; private float vertices_circle_color[] = { 0.0f, 0.0f, 0.0f, 0.5f };

private float vertices_line[] = { 0.0f, 0.0f, 0.0f };

private float vertices_line_color[] = { 0.0f, 0.0f, 0.0f, 1.0f }; private int batas_sudut = 360; float jari_jari; float a, b; float x, y; float step = 3.0f, step_line = 0.2f; float x1, y1; float x2, y2; private int loop, loop_color, loop_line,

loop_line_color;

public PrimitivesObject() {

// ============ start to generate verti- ces to circle

// ========================== // Inisialisasi jari_jari = 1.0f;

// Titik Pusat a = 0.0f; b = 0.0f;

x = a + jari_jari; y = b;

loop = 3; loop_color = 4; vertices_circle = new float[(int) (3 *

batas_sudut / step) * 3]; vertices_circle_color = new float[(int) (3 * batas_sudut / step) * 4]; for (float teta = 0; teta <= 2 * ba- tas_sudut; teta += step) { vertices_circle[loop] = (float) ((x - a) * Math.cos((teta / 180) * (22 / 7)) - ((y - b) * Math.sin((teta / 180) * (22 / 7))) + a); vertices_circle[loop + 1] = (float) ((x - a) * Math.sin((teta / 180) * (22 / 7)) - ((y - b) *

Math.cos((teta / 180) * (22 / 7))) + b); vertices_circle[loop + 2] = 0; loop += 3;

// mengenerate warna untuk setiap vertex vertices_circle_color[loop_color] = (float) ((x - a) * Math.cos((teta / 180) * (22 / 7)) - ((y - b) * Math.sin((teta / 180) * (22 / 7))) + a); vertices_circle_color[loop_color + 1] = (float) ((x - a) * Math.sin((teta / 180) * (22 / 7)) - ((y - b) * Math.cos((teta / 180) * (22 / 7))) + b); vertices_circle_color[loop_color + 2] = 0.5f; vertices_circle_color[loop_color + 3] = 0.5f;

loop_color += 4;

} // ============= end for generate verti-

ces to circle // ====================

// ============ start to generate verti- ces to line

// ========================== x1 = -1.0f; y1 = -1.0f; x2 = 1.0f; y2 = 1.0f;

loop_line = 3; loop_line_color = 4; vertices_line = new float[(int) (2 * (x2

- x1) / step_line) * 3]; vertices_line_color = new float[(int) (2 * (x2 - x1) / step_line) * 4];

float m = (y2 - y1) / (x2 - x1); for (x = x1; x <= x2; x += step_line) {

vertices_line[loop_line] = (float) (x); vertices_line[loop_line + 1] = (float) (m * (x - x1) + y1); vertices_line[loop_line + 2] = 0; loop_line += 3;

// mengenerate warna untuk setiap vertex verti- ces_line_color[loop_line_color] = (float) (0.5 * x); verti- ces_line_color[loop_line_color + 1] = (float) (0.5 * m

* (x - x1) + y1); verti- ces_line_color[loop_line_color + 2] = 1.0f; verti- ces_line_color[loop_line_color + 3] = 1.0f; loop_line_color += 4; } // ============= end for generate verti-

ces to line ==================== }

// Point to our vertex buffer, return buffer holding the vertices public static FloatBuffer makeFloatBuffer(float[] arr) { ByteBuffer bb = ByteBuff- er.allocateDirect(arr.length * 4); bb.order(ByteOrder.nativeOrder()); FloatBuffer fb = bb.asFloatBuffer(); fb.put(arr); fb.position(0); return fb;

/** The draw method for the primitive object with the GL context */ public void draw_points(GL10 gl) {

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

// gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// set the colour for the points (pem- berian warna untuk titik) gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);

// Point to our vertex buffer (mendata nilai lokasi/posisi titik) gl.glVertexPointer(3, GL10.GL_FLOAT, 0, makeFloatBuffer(new float[] { 1.0f, 1.0f, 0.0f, // V1 - first vertex (x,y,z) 1.0f, 0.8f, 0.0f, // V2 1.0f, 0.6f, 0.0f, // V3 1.0f, 0.4f, 0.0f, // V4 1.0f, 0.2f, 0.0f, // V5 1.0f, 0.0f, 0.0f, // V6 1.0f, -0.2f, 0.0f, // V7 1.0f, -0.4f, 0.0f, // V8 1.0f, -0.6f, 0.0f, // V9 1.0f, -0.8f, 0.0f, // V10 1.0f, -1.0f, 0.0f, // V11

0.8f, -1.0f, 0.0f, // V12 0.6f, -1.0f, 0.0f, // V13 0.4f, -1.0f, 0.0f, // V14 0.2f, -1.0f, 0.0f, // V15 0.0f, -1.0f, 0.0f, // V16

-0.2f, -1.0f, 0.0f, // V17 -0.4f, -1.0f, 0.0f, // V18 -0.6f, -1.0f, 0.0f, // V19 -0.7f, -1.0f, 0.0f, // V20 -0.8f, -1.0f, 0.0f, // V21 -1.0f, -1.0f, 0.0f, // V22

// Draw the vertices as points (menggam- bar titik-titik) gl.glDrawArrays(GL10.GL_POINTS, 0, 22);

// Disable the client state before leav- ing

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

// gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

public void draw_line(GL10 gl) {

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

// gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// set the colour for the line (pemberian warna untuk garis) gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);

// Point to our vertex buffer (mendata nilai lokasi/posisi titik yang // menyusun garis) gl.glVertexPointer(3, GL10.GL_FLOAT, 0,

makeFloatBuffer(new float[] { 1.0f, 1.0f, 0.0f, // V1 - first vertex (x,y,z) -1.0f, -1.0f, 0.0f, // V2 - second vertex }));

// Draw the vertices as lines (menggambar garis dari titik-titik) gl.glDrawArrays(GL10.GL_LINES, 0, 2); /*

* gl.glDrawElements(GL10.GL_LINES, 2, GL10.GL_UNSIGNED_SHORT, * makeFloatBuffer(new float [] { 1.0f, 1.0f, 0.0f, // V1 - first vertex * (x,y,z) -1.0f, -1.0f, 0.0f, // V2 - second vertex })); */

// Disable the client state before leav- ing

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

// gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

public void draw_line_color(GL10 gl) {

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// set the colour for the line (pemberian warna untuk garis) gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);

// Point to our vertex buffer (mendata nilai lokasi/posisi titik yang // menyusun garis) gl.glVertexPointer(3, GL10.GL_FLOAT, 0,

makeFloatBuffer(vertices_line));

// memetakan warna untuk setiap vertex gl.glColorPointer(4, GL10.GL_FLOAT, 0,

makeFloatBuff- er(vertices_line_color));

// Draw the vertices as lines (menggambar garis dari titik-titik) gl.glDrawArrays(GL10.GL_LINE_STRIP, 0, (int) (2 * (x2 - x1) / step_line)); /* * gl.glDrawElements(GL10.GL_LINES, 2, GL10.GL_UNSIGNED_SHORT, * makeFloatBuffer(new float [] { 1.0f, 1.0f, 0.0f, // V1 - first vertex * (x,y,z) -1.0f, -1.0f, 0.0f, // V2 - second vertex })); */

// Disable the client state before leav- ing

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

public void draw_circle(GL10 gl) {

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

// gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// set the colour for the object circle gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);

// create VBO from buffer with glBuffer- Data() gl.glVertexPointer(3, GL10.GL_FLOAT, 0, makeFloatBuff- er(vertices_circle));

// draw circle as filled shape // gl.glDrawArrays(GL10.GL_TRIANGLE_FAN,

1, (int) ((int) // 2*batas_sudut/step));

// draw circle contours // gl.glDrawArrays(GL10.GL_LINES, 1,

(int) ((int) 2*batas_sudut/step)); // // membuat garis putus-putus pada tepi lingkaran gl.glDrawArrays(GL10.GL_LINES, 1, (int) ((int) 2 * batas_sudut / step)); // gl.glDrawArrays(GL10.GL_LINE_STRIP, 1, (int) ((int) // 2*batas_sudut/step)); // gl.glDrawArrays(GL10.GL_POINTS, 1,

(int) ((int) 2*batas_sudut/step));

// Disable the client state before leav- ing

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

// gl.glDisableClientState(GL10.GL_COLOR_ARRAY); }

public void draw_circle_color(GL10 gl) {

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// set the colour edge for the object circle gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);

// create VBO from buffer with glBuffer- Data() gl.glVertexPointer(3, GL10.GL_FLOAT, 0, makeFloatBuff- er(vertices_circle));

// memetakan warna untuk setiap vertex gl.glColorPointer(4, GL10.GL_FLOAT, 0,

makeFloatBuff- er(vertices_circle_color));

// draw circle as filled shape // gl.glDrawArrays(GL10.GL_TRIANGLE_FAN,

1, (int) ((int) // 2*batas_sudut/step)); gl.glDrawArrays(GL10.GL_LINE_STRIP, 1,

(int) ((int) 2 * ba- (int) ((int) 2 * ba-

// Disable the client state before leav- ing

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

gl.glDisableClientState(GL10.GL_COLOR_ARRAY); }

public void draw_kotak(GL10 gl) {

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// Point to our vertex buffer gl.glVertexPointer(3, GL10.GL_FLOAT, 0,

makeFloatBuffer(vertices));

// Draw the vertices as square gl.glColorPointer(4, GL10.GL_FLOAT, 0,

makeFloatBuffer(vertices_color)); gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);

gl.glColorPointer(4, GL10.GL_FLOAT, 0, makeFloatBuffer(vertices_color)); gl.glDrawArrays(GL10.GL_TRIANGLES, 2, 3);

// Disable the client state before leav- ing

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

public void draw_segitiga(GL10 gl) {

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// set the colour for the triangle gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);

gl.glVertexPointer(3, GL10.GL_FLOAT, 0, makeFloatBuffer(new float[] { -0.5f, -0.5f, 0.0f, // V1 - first vertex (x,y,z) 0.5f, -0.5f, 0.0f, // V2 - second vertex 0.0f, 0.5f, 0.0f // V3 - third vertex

// Draw the vertices as triangle gl.glColorPointer(4, GL10.GL_FLOAT, 0,

makeFloatBuffer(vertices_color)); gl.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);

// Disable the client state before leav- ing

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

// Render the shape quad public void draw_quad(GL10 gl) {

// Enable vertex-array and define its buffer

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, makeFloatBuffer(vertices_quad)); // Draw the primitives from the vertex- array directly gl.glPolygonOffset(0.0f, 1.0f); gl.glScalef(0.5f, 0.5f, 0.5f); gl.glColor4f(0.5f, 0.5f, 1.0f, 1.0f); //

Set the current color (NEW) gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices_quad.length / 3); //gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices_quad.length / 3); //gl.glDrawArrays(GL10.GL_LINES, 0, ver- tices_quad.length / 3);

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); }

Gambar 2.12 Membuat Segiempat