SlideShare a Scribd company logo
1 of 6
Grafika Komputer - Primitive &Transformations 
Membuat Objek Kotak Bergaris dengan menggunakan Eclips 
package com.example.Rectangle; 
import java.nio.ByteBuffer; 
import java.nio.ByteOrder; 
import java.nio.FloatBuffer; 
import javax.microedition.khronos.opengles.GL10; 
public class Rectangle { 
private float vertices[] = { 
-0.7f, -0.5f, 0.0f, // V1 - first vertex 
(x,y,z) 
-0.7f, 0.5f, 0.0f, // V1 - first vertex (x,y,z) 
0.7f, 0.5f, 0.0f, // V1 - first vertex (x,y,z) 
0.7f, -0.5f, 0.0f, // V2 - second vertex 
-0.7f, -0.5f, 0.0f // V1 - first vertex (x,y,z) 
}; 
private float vertices_color[] = { 
1.0f, 1.0f, 1.0f, 1.0f, // V1 - first vertex 
(x,y,z) 
1.0f, 1.0f, 1.0f, 1.0f, // V1 - first vertex 
(x,y,z) 
1.0f, 1.0f, 1.0f, 1.0f, // V1 - first vertex 
(x,y,z) 
1.0f, 1.0f, 1.0f, 1.0f, // V2 - second vertex 
1.0f, 1.0f, 1.0f, 1.0f // V1 - first vertex 
(x,y,z) 
}; 
public static FloatBuffer makeFloatBuffer(float[] arr){ 
ByteBuffer bb = ByteBuffer.allocateDirect(arr.length * 4); 
bb.order(ByteOrder.nativeOrder()); 
FloatBuffer fb = bb.asFloatBuffer(); 
fb.put(arr); 
fb.position(0); 
return fb; 
} 
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 leaving 
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, 0.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 [] { 
0.0f, 1.0f, 0.0f, // V1 - first vertex (x,y,z) 
0.0f, 0.0f, 0.0f, // V2 - second vertex 
})); 
// Draw the vertices as lines (menggambar garis dari titik-titik) 
gl.glDrawArrays(GL10.GL_LINES, 0, 2); 
//Disable the client state before leaving 
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
//gl.glDisableClientState(GL10.GL_COLOR_ARRAY); 
} 
} 
GIRenderer.java 
package com.example.Rectangle; 
import javax.microedition.khronos.opengles.GL10; 
import javax.microedition.khronos.egl.EGLConfig; 
import com.example.Rectangle.Rectangle; 
import android.opengl.GLU; 
import android.opengl.GLSurfaceView.Renderer; 
public class GlRenderer implements Renderer { 
private Rectangle object; // the triangle to be drawn 
/** Constructor to set the handed over context */ 
public GlRenderer() { 
this.object = new Rectangle(); 
} 
@Override 
public void onDrawFrame(GL10 gl) { 
// clear Screen and Depth Buffer
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | 
GL10.GL_DEPTH_BUFFER_BIT); 
gl.glLoadIdentity(); 
// menampilkan persegi dengan gradasi warna 
gl.glPushMatrix(); 
gl.glTranslatef(0.0f, 0.0f, -5.0f); 
object.draw_kotak(gl); 
gl.glPopMatrix(); 
//menampilkan arsiran garis 
gl.glPushMatrix(); 
gl.glTranslatef(0.0f, -0.5f, -5.0f); 
gl.glLineWidth(1.0f); 
gl.glEnable(GL10.GL_LINE_SMOOTH); 
object.draw_line(gl); 
gl.glPopMatrix(); 
//geser kanan 
gl.glPushMatrix(); 
gl.glTranslatef(0.1f, -0.5f, -5.0f); 
gl.glLineWidth(1.0f); 
gl.glEnable(GL10.GL_LINE_SMOOTH); 
object.draw_line(gl); 
gl.glPopMatrix(); 
gl.glPushMatrix(); 
gl.glTranslatef(0.2f, -0.5f, -5.0f); 
gl.glLineWidth(1.0f); 
gl.glEnable(GL10.GL_LINE_SMOOTH); 
object.draw_line(gl); 
gl.glPopMatrix(); 
gl.glPushMatrix(); 
gl.glTranslatef(0.3f, -0.5f, -5.0f); 
gl.glLineWidth(1.0f); 
gl.glEnable(GL10.GL_LINE_SMOOTH); 
object.draw_line(gl); 
gl.glPopMatrix(); 
gl.glPushMatrix(); 
gl.glTranslatef(0.4f, -0.5f, -5.0f); 
gl.glLineWidth(1.0f); 
gl.glEnable(GL10.GL_LINE_SMOOTH); 
object.draw_line(gl); 
gl.glPopMatrix(); 
gl.glPushMatrix(); 
gl.glTranslatef(0.5f, -0.5f, -5.0f); 
gl.glLineWidth(1.0f); 
gl.glEnable(GL10.GL_LINE_SMOOTH); 
object.draw_line(gl); 
gl.glPopMatrix(); 
gl.glPushMatrix(); 
gl.glTranslatef(0.6f, -0.5f, -5.0f); 
gl.glLineWidth(1.0f); 
gl.glEnable(GL10.GL_LINE_SMOOTH); 
object.draw_line(gl); 
gl.glPopMatrix(); 
//geser kiri 
gl.glPushMatrix(); 
gl.glTranslatef(-0.1f, -0.5f, -5.0f);
gl.glLineWidth(1.0f); 
gl.glEnable(GL10.GL_LINE_SMOOTH); 
object.draw_line(gl); 
gl.glPopMatrix(); 
gl.glPushMatrix(); 
gl.glTranslatef(-0.2f, -0.5f, -5.0f); 
gl.glLineWidth(1.0f); 
gl.glEnable(GL10.GL_LINE_SMOOTH); 
object.draw_line(gl); 
gl.glPopMatrix(); 
gl.glPushMatrix(); 
gl.glTranslatef(-0.3f, -0.5f, -5.0f); 
gl.glLineWidth(1.0f); 
gl.glEnable(GL10.GL_LINE_SMOOTH); 
object.draw_line(gl); 
gl.glPopMatrix(); 
gl.glPushMatrix(); 
gl.glTranslatef(-0.4f, -0.5f, -5.0f); 
gl.glLineWidth(1.0f); 
gl.glEnable(GL10.GL_LINE_SMOOTH); 
object.draw_line(gl); 
gl.glPopMatrix(); 
gl.glPushMatrix(); 
gl.glTranslatef(-0.5f, -0.5f, -5.0f); 
gl.glLineWidth(1.0f); 
gl.glEnable(GL10.GL_LINE_SMOOTH); 
object.draw_line(gl); 
gl.glPopMatrix(); 
gl.glPushMatrix(); 
gl.glTranslatef(-0.6f, -0.5f, -5.0f); 
gl.glLineWidth(1.0f); 
gl.glEnable(GL10.GL_LINE_SMOOTH); 
object.draw_line(gl); 
gl.glPopMatrix(); 
} 
@Override 
public void onSurfaceChanged(GL10 gl, int width, int height) { 
if(height == 0) { //Prevent 
A Divide By Zero By 
height = 1; //Making 
Height Equal One 
} 
gl.glViewport(0, 0, width, height); //Reset The Current 
Viewport 
gl.glMatrixMode(GL10.GL_PROJECTION); //Select The Projection 
Matrix 
gl.glLoadIdentity(); //Reset 
The Projection Matrix 
//Calculate The Aspect Ratio Of The Window 
GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 
0.1f, 100.0f); 
gl.glMatrixMode(GL10.GL_MODELVIEW); //Select The Modelview 
Matrix 
gl.glLoadIdentity(); //Reset 
The Modelview Matrix
} 
@Override 
public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
object = new Rectangle(); 
} 
} 
MainActivity.java 
package com.example.Rectangle; 
import com.example.Rectangle.GlRenderer; 
import android.opengl.GLSurfaceView; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Window; 
import android.view.WindowManager; 
public class MainActivity extends Activity { 
private GLSurfaceView glSurfaceView; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
// requesting to turn the title OFF 
requestWindowFeature(Window.FEATURE_NO_TITLE); 
// making it full screen 
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
WindowManager.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 GlRenderer()); 
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(); 
} 
} 
Hasil Screenshoot :

More Related Content

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Recently uploaded (20)

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

Grafika komputer - Membuat Objek Kotak Bergaris dengan Java Eclipse

  • 1. Grafika Komputer - Primitive &Transformations Membuat Objek Kotak Bergaris dengan menggunakan Eclips package com.example.Rectangle; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import javax.microedition.khronos.opengles.GL10; public class Rectangle { private float vertices[] = { -0.7f, -0.5f, 0.0f, // V1 - first vertex (x,y,z) -0.7f, 0.5f, 0.0f, // V1 - first vertex (x,y,z) 0.7f, 0.5f, 0.0f, // V1 - first vertex (x,y,z) 0.7f, -0.5f, 0.0f, // V2 - second vertex -0.7f, -0.5f, 0.0f // V1 - first vertex (x,y,z) }; private float vertices_color[] = { 1.0f, 1.0f, 1.0f, 1.0f, // V1 - first vertex (x,y,z) 1.0f, 1.0f, 1.0f, 1.0f, // V1 - first vertex (x,y,z) 1.0f, 1.0f, 1.0f, 1.0f, // V1 - first vertex (x,y,z) 1.0f, 1.0f, 1.0f, 1.0f, // V2 - second vertex 1.0f, 1.0f, 1.0f, 1.0f // V1 - first vertex (x,y,z) }; public static FloatBuffer makeFloatBuffer(float[] arr){ ByteBuffer bb = ByteBuffer.allocateDirect(arr.length * 4); bb.order(ByteOrder.nativeOrder()); FloatBuffer fb = bb.asFloatBuffer(); fb.put(arr); fb.position(0); return fb; } 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);
  • 2. gl.glColorPointer(4, GL10.GL_FLOAT, 0, makeFloatBuffer(vertices_color)); gl.glDrawArrays(GL10.GL_TRIANGLES, 2, 3); //Disable the client state before leaving 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, 0.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 [] { 0.0f, 1.0f, 0.0f, // V1 - first vertex (x,y,z) 0.0f, 0.0f, 0.0f, // V2 - second vertex })); // Draw the vertices as lines (menggambar garis dari titik-titik) gl.glDrawArrays(GL10.GL_LINES, 0, 2); //Disable the client state before leaving gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); //gl.glDisableClientState(GL10.GL_COLOR_ARRAY); } } GIRenderer.java package com.example.Rectangle; import javax.microedition.khronos.opengles.GL10; import javax.microedition.khronos.egl.EGLConfig; import com.example.Rectangle.Rectangle; import android.opengl.GLU; import android.opengl.GLSurfaceView.Renderer; public class GlRenderer implements Renderer { private Rectangle object; // the triangle to be drawn /** Constructor to set the handed over context */ public GlRenderer() { this.object = new Rectangle(); } @Override public void onDrawFrame(GL10 gl) { // clear Screen and Depth Buffer
  • 3. gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); // menampilkan persegi dengan gradasi warna gl.glPushMatrix(); gl.glTranslatef(0.0f, 0.0f, -5.0f); object.draw_kotak(gl); gl.glPopMatrix(); //menampilkan arsiran garis gl.glPushMatrix(); gl.glTranslatef(0.0f, -0.5f, -5.0f); gl.glLineWidth(1.0f); gl.glEnable(GL10.GL_LINE_SMOOTH); object.draw_line(gl); gl.glPopMatrix(); //geser kanan gl.glPushMatrix(); gl.glTranslatef(0.1f, -0.5f, -5.0f); gl.glLineWidth(1.0f); gl.glEnable(GL10.GL_LINE_SMOOTH); object.draw_line(gl); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslatef(0.2f, -0.5f, -5.0f); gl.glLineWidth(1.0f); gl.glEnable(GL10.GL_LINE_SMOOTH); object.draw_line(gl); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslatef(0.3f, -0.5f, -5.0f); gl.glLineWidth(1.0f); gl.glEnable(GL10.GL_LINE_SMOOTH); object.draw_line(gl); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslatef(0.4f, -0.5f, -5.0f); gl.glLineWidth(1.0f); gl.glEnable(GL10.GL_LINE_SMOOTH); object.draw_line(gl); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslatef(0.5f, -0.5f, -5.0f); gl.glLineWidth(1.0f); gl.glEnable(GL10.GL_LINE_SMOOTH); object.draw_line(gl); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslatef(0.6f, -0.5f, -5.0f); gl.glLineWidth(1.0f); gl.glEnable(GL10.GL_LINE_SMOOTH); object.draw_line(gl); gl.glPopMatrix(); //geser kiri gl.glPushMatrix(); gl.glTranslatef(-0.1f, -0.5f, -5.0f);
  • 4. gl.glLineWidth(1.0f); gl.glEnable(GL10.GL_LINE_SMOOTH); object.draw_line(gl); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslatef(-0.2f, -0.5f, -5.0f); gl.glLineWidth(1.0f); gl.glEnable(GL10.GL_LINE_SMOOTH); object.draw_line(gl); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslatef(-0.3f, -0.5f, -5.0f); gl.glLineWidth(1.0f); gl.glEnable(GL10.GL_LINE_SMOOTH); object.draw_line(gl); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslatef(-0.4f, -0.5f, -5.0f); gl.glLineWidth(1.0f); gl.glEnable(GL10.GL_LINE_SMOOTH); object.draw_line(gl); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslatef(-0.5f, -0.5f, -5.0f); gl.glLineWidth(1.0f); gl.glEnable(GL10.GL_LINE_SMOOTH); object.draw_line(gl); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslatef(-0.6f, -0.5f, -5.0f); gl.glLineWidth(1.0f); gl.glEnable(GL10.GL_LINE_SMOOTH); object.draw_line(gl); gl.glPopMatrix(); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { if(height == 0) { //Prevent A Divide By Zero By height = 1; //Making Height Equal One } gl.glViewport(0, 0, width, height); //Reset The Current Viewport gl.glMatrixMode(GL10.GL_PROJECTION); //Select The Projection Matrix gl.glLoadIdentity(); //Reset The Projection Matrix //Calculate The Aspect Ratio Of The Window GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f); gl.glMatrixMode(GL10.GL_MODELVIEW); //Select The Modelview Matrix gl.glLoadIdentity(); //Reset The Modelview Matrix
  • 5. } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { object = new Rectangle(); } } MainActivity.java package com.example.Rectangle; import com.example.Rectangle.GlRenderer; import android.opengl.GLSurfaceView; import android.os.Bundle; import android.app.Activity; import android.view.Window; import android.view.WindowManager; public class MainActivity extends Activity { private GLSurfaceView glSurfaceView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // requesting to turn the title OFF requestWindowFeature(Window.FEATURE_NO_TITLE); // making it full screen getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.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 GlRenderer()); setContentView(glSurfaceView); } /** * Remember to resume the glSurface */ @Override protected void onResume() { super.onResume(); glSurfaceView.onResume(); } /**
  • 6. * Also pause the glSurface */ @Override protected void onPause() { super.onPause(); glSurfaceView.onPause(); } } Hasil Screenshoot :