OpenGL and GLUT Overview• What is OpenGL & what can it do for me?... What Is OpenGL?– a cross-platform API – high-quality color images composed of geometric and image primitives • widely
Trang 1OpenGL Overview
Trang 2OpenGL and GLUT Overview
• What is OpenGL & what can it do for me?
Trang 3What Is OpenGL?
– a cross-platform API
– high-quality color images composed of geometric and image primitives
• widely used in CAD, virtual reality, scientific visualization, information visualization and video game development
Trang 4OpenGL Architecture
Trang 5OpenGL as a Renderer
– points, lines and polygons
– images and bitmaps
– separate pipeline for images and geometry
• linked through texture mapping
– colors, materials, light sources, etc.
Trang 6Related APIs
– glue between OpenGL and windowing systems
– part of OpenGL
– NURBS, tessellators, quadric shapes, etc.
– portable windowing API
– keyboard and mouse input
– Geometric primitives: cubes, spheres, and teapot
– not officially part of OpenGL
Trang 7OpenGL and Related APIs
X, Win32, Mac O/S
software and/or hardware
application program
OpenGL Motif widget or similar
Trang 8What is JOGL?
that allow OpenGL to be used in Java
Trang 9Setting Up JOGL 2.0x
• Download the latest "stable" release from JogAMP
http://jogamp.org/deployment/jogamp-current/archive Select "jogamp-all-platforms.7z"
– Copy the necessary jar-file, native libraries of your operating platform, and source-files into the appropriate sub-directories
– for Win32, copy
• "jar\gluegen-rt.jar", "jar\jogl.all.jar" into "jar";
• all files in "lib\windows-i586" into "lib";
• "gluegen-java-src.zip", "jogl-java-src.zip" into "src"
• Unzip the javadocs downloaded into "javadoc".
Trang 12Sample Template Program
pu blic class JO G L2Tem plate extends G LJPanel
im plem ents G LEventListener {
// Setup O penG L G raphics Renderer
private G LU glu ; // for the G L U tility
private G LU T glut ; // O penG L U tility Toolkit
/** Constructor to setup the G U I for this Com ponent */
pu blic JO G L2Tem plate() {
this.addG LEventListener( this );
}
Trang 13Sample Program: init event
/**
* Called back im m ediately after the O penG L context is initialized
* Can be used to perform one - tim e initialization Run only once.
*/
pu blic void init(G LAutoD raw able draw able) {
G L2 gl = draw able.getG L().getG L2(); // get the O penG L graphics context glu = new G LU (); // get G L U tilities
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // set background color
gl.glClearD epth(1.0f); // set clear depth value to farthest
gl.glEnable(G L_D EPTH _TEST); // enables depth testing
gl.glD epthFunc(G L_LEQ U AL); // the type of depth test to do
// best perspective correction
gl.glH int(G L_PERSPECTIVE_CO RRECTIO N _H IN T , G L_N ICEST );
// blends colors nicely, and sm oothes out lighting
gl.glShadeM odel(G L_SM O O TH );
// Your O penG L initialization code here
-}
Trang 14Sample Program: reshape event
/* Call - back handler for w indow re - size event Also called w hen the
draw able is fi rst set to visible */
pu blic void reshape(G LAutoD raw able draw able,
int x, int y, int w idth, int height) {
G L2 gl = draw able.getG L().getG L2();
if (height = = 0) height = 1; // prevent divide by zero
f l oat aspect = (f l oat) w idth / height;
// Set the view port (display area) to cover the entire w indow
gl.glView port(0, 0, w idth, height);
// Setup perspective projection,
// w ith aspect ratio m atches view port
gl.glM atrixM ode(G L_PRO JECTIO N ); // choose projection m atrix
gl.glLoadIdentity(); // reset projection m atrix
glu gluPerspective(45.0, aspect, 0.1, 100.0); // fovy, aspect, zN ear, zFar // Enable the m odel-view transform
gl.glM atrixM ode(G L_M O D ELVIEW );
gl.glLoadIdentity(); // reset
}
Trang 15Sample Program: display event
/* Called back by the anim ator to perform rendering */
pu blic void display(G LAutoD raw able draw able) {
// get the O penG L 2 graphics context
G L2 gl = draw able.getG L().getG L2();
// clear color and depth buff ers
gl.glClear(G L_CO LO R_BU FFER_BIT | G L_D EPTH _BU FFER_BIT );
gl.glLoadIdentity(); // reset the m odel-view m atrix
// Your O penG L rendering code here
// (Render a w hite triangle for testing)
gl.glTranslatef(0.0f, 0.0f, -6.0f); // translate into the screen
gl.glBegin(G L_TRIAN G LES ); // draw using triangles
Trang 16Sample Program: dispose event
**
* Called back before the O penG L context is destroyed
* Release resource such as buff ers.
*/
p ublic void dispose(G LAutoD raw able draw able) {
}
Trang 17pu blic static void m ain(String[] args) {
Sw ingU tilities.invokeLater( new Runnable() {
pu blic void run() {
// Create the O penG L rendering canvas
G LJPanel panel = new JO G L2Tem plate();
panel.setPreferredSize( new D im ension(640 , 480 ));
// Create a anim ator
FPSAnim ator anim ator = new FPSAnim ator(panel, 60 , true );
JFram e fram e = new JFram e();
fram e.getContentPane().add(panel);
fram e.addW indow Listener( new W indow Adapter() {
p u blic void w indow Closing(W indow Event e) {
// stop the anim ator stops before program exits.
new Thread() {
pu blic void run() {
if (anim ator.isStarted()) anim ator.stop();
fram e.setTitle( "JO G L2 Tem plate");
fram e.pack(); fram e.setVisible( true );
anim ator.start(); // start the anim ation loop
}
});
Trang 18Elementary Rendering
Trang 19Elementary Rendering
Trang 20OpenGL Geometric Primitives
• All geometric primitives are specified by vertices
GL_QUAD_STRIP GL_POLYGON
GL_TRIANGLE_STRIP GL_TRIANGLE_FAN
GL_POINTS
GL_LINES
GL_LINE_LOOP GL_LINE_STRIP
GL_TRIANGLES
GL_QUADS
Trang 22OpenGL Command Formats
Trang 23Enumerated Types
Trang 24Specifying Geometric Primitives
glBegin(primType);
glEnd();
– primType determines how vertices are combined
void drawRhombus(GL2 gl, float[] color) { gl.glColor3fv(color);
Trang 25OpenGL Color Models
color index mode
Display 1
24 25 26
ww
ww
RGBA mode
Trang 26Shapes Tutorial
Trang 27Controlling Rendering Appearance
Trang 28OpenGL’s State Machine
– rendering styles
– shading
– lighting
– texture mapping
Trang 29Manipulating OpenGL State
update OpenGL state
Trang 30Controlling current state
– cap is a feature like:
• GL_LIGHTING, GL_TEXTURE_2D, GL_CULL_FACE, GL_BLEND,
Trang 31OpenGL State Variables
• You can query the State Machine to determine the state of any variable
• Any feature you enable or disable with glEnable / glDisable as well as numeric settings set with glSet can be queried with the many variations of glGet
Trang 32Animation and Depth Buffering
Trang 33Depth Buffering and Animation
Trang 34Animation using Double Buffering
1
2 4 8 16
1
2 4 8 16
Front Buffer
Back Buffer
Display
Trang 35Animation and Double Buffering
– Default for JOGL
Trang 36Depth Buffering
and Hidden Surface Removal
1
2 4 8 16
1
2 4 8 16
Color Buffer
Depth Buffer
Display
Trang 37Hidden Surface Removal
Trang 38Depth Buffering Using OpenGL
Trang 39An Updated Program Template
public void display(G LAutoD raw able draw able) {
G L2 gl = draw able.getG L().getG L2();
gl.glClear(G L_CO LO R_BU FFER_BIT | G L_D EPTH _BU FFER_BIT );
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -5.0f);
}
Trang 40An Updated Program Template (cont)