Pemrograman OpenGL Dasar

Slides:



Advertisements
Similar presentations
SUJAN CHOWDHURY Lecturer, CSE, CUET
Advertisements

Programming with OpenGL - Getting started - Hanyang University Han Jae-Hyek.
Computer Graphics - Graphics Programming -
OpenGL Open a Win32 Console Application in Microsoft Visual C++.
OpenGL CMSC340 3D Character Design & Animation. What is OpenGL? A low-level graphics library specification designed for use with the C and C++ provides…
OPEN GL. Install GLUT Download package di sini Dari devcpp, buka Tools->PackageManager-
Lecture 3 Graphics Pipeline and Graphics Software
Chapter 2: Graphics Programming
Computer Graphics CSCE 441
#include int line_width = 1; void Display( void ) { glEnable( GL_LINE_STIPPLE ); glClearColor (0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT);
CS 4731 Lecture 2: Intro to 2D, 3D, OpenGL and GLUT (Part I) Emmanuel Agu.
 The success of GL lead to OpenGL (1992), a platform-independent API that was  Easy to use  Close enough to the hardware to get excellent performance.
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
30/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 1: Introduction.
Programming with OpenGL Part 0: 3D API March 1, 2007.
Programming with OpenGL Part 1: Background Mohan Sridharan Based on slides created by Edward Angel CS4395: Computer Graphics 1.
Computer Graphics Bing-Yu Chen National Taiwan University.
Introduction to OpenGL and GLUT GLUT. What is OpenGL? An application programming interface (API) A (low-level) Graphics rendering API Generate high-quality.
Using OpenGL in Visual C++ Opengl32.dll and glu32.dll should be in the system folder Opengl32.dll and glu32.dll should be in the system folder Opengl32.lib.
CSC 461: Lecture 41 CSC461: Lecture 4 Introduction to OpenGL Objectives: Development of the OpenGL API OpenGL Architecture -- OpenGL as a state machine.
Graphics Architectures & OpenGL API Introduction Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
CS 480/680 Computer Graphics Programming with Open GL Part 1: Background Dr. Frederick C Harris, Jr. Fall 2011.
Programming with OpenGL Part 1: Background
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and.
1. OpenGL/GLU/GLUT  OpenGL v4.0 (latest) is the “core” library that is platform independent  GLUT v3.7 is an auxiliary library that handles window creation,
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
Introduction to OpenGL Week 1 David Breen Department of Computer Science Drexel University Based on material from Ed Angel, University of New Mexico CS.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 1: Background Ed Angel Professor Emeritus.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Programming with OpenGL Review.
Introduction to OpenGL and GLUT. What’s OpenGL? An Application Programming Interface (API) A low-level graphics programming API – Contains over 250 functions.
C O M P U T E R G R A P H I C S Guoying Zhao 1 / 43 C O M P U T E R G R A P H I C S Guoying Zhao 1 / 43 Computer Graphics Programming with OpenGL I.
Color and Color Models Angel 2.5 Angel: Interactive Computer Graphics5E © Addison-Wesley
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
Pemrograman OpenGL Dasar Pertemuan 5 Hand out Komputer Grafik.
NoufNaief.net TA: Nouf Al-harbi.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor.
GLUT functions glutInit allows application to get command line arguments and initializes system gluInitDisplayMode requests properties for the window.
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
Introduction to Graphics Programming. Graphics API.
Graphics Graphics Korea University kucg.korea.ac.kr Graphics Programming 고려대학교 컴퓨터 그래픽스 연구실.
Introduction to Graphics Programming. Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
CS 480/680 Computer Graphics Programming with Open GL Part 2: Complete Programs Dr. Frederick C Harris, Jr. Fall 2011.
Hank Childs, University of Oregon Oct. 28th, 2016 CIS 441/541: Introduction to Computer Graphics Lecture 16: textures.
Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran
The Human Visual System vs The Pinhole camera
Programming with OpenGL Part 1: Background
Programming with OpenGL Part 1: Background
Programming with OpenGL Part 2: Complete Programs
Materi Anatomi OpenGL Fungsi GLUT Posisi Kamera Proyeksi
OpenGL API 2D Graphic Primitives
Programming with OpenGL Part 2: Complete Programs
גרפיקה ממוחשבת: מבוא ל-OpenGL
Programming with OpenGL Part 1: Background
Introduction to OpenGL
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 1: Background
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

Pemrograman OpenGL Dasar Materi 03 Grafik Komputer 1

The Programmer’s Interface Programmer sees the graphics system through a software interface: the Application Programmer Interface (API)

API Contents Functions that specify what we need to form an image Objects Viewer Light Source(s) Materials Other information Input from devices such as mouse and keyboard Capabilities of system

Object Specification Most APIs support a limited set of primitives including Points (0D object) Line segments (1D objects) Polygons (2D objects) Some curves and surfaces Quadrics Parametric polynomials All are defined through locations in space or vertices

OpenGL The success of GL lead to OpenGL (1992), a platform-independent API that was Easy to use Close enough to the hardware to get excellent performance Focus on rendering Omitted windowing and input to avoid window system dependencies

OpenGL Libraries OpenGL core library OpenGL Utility Library (GLU) OpenGL32 on Windows GL on most unix/linux systems (libGL.a) OpenGL Utility Library (GLU) Provides functionality in OpenGL core but avoids having to rewrite code Links with window system GLX for X window systems WGL for Windows AGL for Macintosh

GLUT OpenGL Utility Toolkit (GLUT) Provides functionality common to all window systems Open a window Get input from mouse and keyboard Menus Event-driven Code is portable but GLUT lacks the functionality of a good toolkit for a specific platform No slide bars

Software Organization application program OpenGL Motif widget or similar GLUT GLX, AGL or WGL GLU GL X, Win32, Mac O/S software and/or hardware

OpenGL function format function name dimensions glVertex3f(x,y,z) x,y,z are floats belongs to GL library glVertex3fv(p) p is a pointer to an array

Example glBegin(GL_POLYGON) glVertex3f(0.0, 0.0, 0.0); type of object location of vertex glBegin(GL_POLYGON) glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 1.0, 0.0); glVertex3f(0.0, 0.0, 1.0); glEnd( ); end of object definition

OpenGL Primitives GL_POINTS GL_POLYGON GL_LINE_STRIP GL_LINES GL_LINE_LOOP GL_TRIANGLES GL_QUAD_STRIP GL_TRIANGLE_STRIP GL_TRIANGLE_FAN

A Simple Program Generate a square on a solid background

simple.c #include <GL/glut.h> void mydisplay(){ glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5); glEnd(); glFlush(); } int main(int argc, char** argv){ glutCreateWindow("simple"); glutDisplayFunc(mydisplay); glutMainLoop();

OpenGL #defines Most constants are defined in the include files gl.h, glu.h and glut.h Note #include <GL/glut.h> should automatically include the others Examples glBegin(GL_POLYGON) glClear(GL_COLOR_BUFFER_BIT) include files also define OpenGL data types: GLfloat, GLdouble,….

Event Loop Note that the program defines a display callback function named mydisplay Every glut program must have a display callback The display callback is executed whenever OpenGL decides the display must be refreshed, for example when the window is opened The main function ends with the program entering an event loop

Defaults simple.c is too simple Makes heavy use of state variable default values for Viewing Colors Window parameters Next version will make the defaults more explicit

Program Structure Most OpenGL programs have a similar structure that consists of the following functions main(): defines the callback functions opens one or more windows with the required properties enters event loop (last executable statement) init(): sets the state variables Viewing Attributes callbacks Display function Input and window functions

simple.c revisited In this version, we shall see the same output but we have defined all the relevant state values through function calls using the default values In particular, we set Colors Viewing conditions Window properties

main.c includes gl.h define window properties display callback #include <GL/glut.h> int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("simple"); glutDisplayFunc(mydisplay); init(); glutMainLoop(); } define window properties display callback set OpenGL state enter event loop

GLUT functions glutInit allows application to get command line arguments and initializes system gluInitDisplayMode requests properties for the window (the rendering context) RGB color Single buffering Properties logically ORed together glutWindowSize in pixels glutWindowPosition from top-left corner of display glutCreateWindow create window with title “simple” glutDisplayFunc display callback glutMainLoop enter infinite event loop

init.c black clear color opaque window fill/draw with white void init() { glClearColor (0.0, 0.0, 0.0, 1.0); glColor3f(1.0, 1.0, 1.0); glMatrixMode (GL_PROJECTION); glLoadIdentity (); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); } opaque window fill/draw with white viewing volume

RGB color Each color component is stored separately in the frame buffer Usually 8 bits per component in buffer Note in glColor3f the color values range from 0.0 (none) to 1.0 (all), whereas in glColor3ub the values range from 0 to 255

Indexed Color Colors are indices into tables of RGB values Requires less memory indices usually 8 bits not as important now Memory inexpensive Need more colors for shading

Color and State The color as set by glColor becomes part of the state and will be used until changed Colors and other attributes are not part of the object but are assigned when the object is rendered We can create conceptual vertex colors by code such as glColor glVertex

Smooth Color glShadeModel Default is smooth shading OpenGL interpolates vertex colors across visible polygons Alternative is flat shading Color of first vertex determines fill color glShadeModel (GL_SMOOTH) or GL_FLAT

Rangkuman API berfungsi sebagai perantara antara aplikasi dengan hardware Membuat tampilan 2 dimensi sederhana menggunakan OpenGL Penjelasan Fungsi-fungsi dasar pembentuk program OpenGL

Contoh Soal Buat tampilan sebagai berikut:

Jawaban #include "stdafx.h" #include <GL/glut.h> void display() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_TRIANGLES); glClearColor(1.0,1.0,1.0,1.0); glColor3f(1.0,0.0,0.0); glVertex3f(-2.0,-2.0,0.0); glColor3f(0.0,1.0,0.0); glVertex3f(0.0,2.0,0.0); glColor3f(0.0,0.0,1.0); glVertex3f(2.0,-2.0,0.0); glEnd(); glFlush(); }

void myinit() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-2.0,2.0,-2.0,2.0); glMatrixMode(GL_MODELVIEW); glClearColor(1.0,1.0,1.0,1.0); glColor3f(0.0,0.0,0.0); } int main(int argc, char* argv[]) if (argv[1] != NULL) { n=atoi(argv[1]); else n=5; glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(100,100); glutCreateWindow("Segitiga Warna"); glutDisplayFunc(display); myinit(); glutMainLoop(); return 0;

Contoh Soal Buatlah tampilan program Sierpinski Gasket

Referensi Edward Angel, “Interactive Computer Graphics Fourth Edition”, Pearson, 2006, ch 2, p 46 – 84 F. S. Hill, Jr., “Computer Graphics Using OpenGL Second Edition”, Prentice Hall, 2001, ch 2, p 39 - 63