Tải bản đầy đủ (.pptx) (40 trang)

OpenGL overview (đồ họa máy TÍNH SLIDE)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (584.61 KB, 40 trang )

OpenGL Overview


OpenGL and GLUT Overview



What is OpenGL & what can it do for me?



OpenGL in windowing systems



Why GLUT



A GLUT program template


What Is OpenGL?



Graphics rendering API







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


OpenGL Architecture


OpenGL as a Renderer



Geometric primitives





points, lines and polygons

Image Primitives




images and bitmaps

separate pipeline for images and geometry





linked through texture mapping

Rendering depends on state



colors, materials, light sources, etc.


Related APIs





AGL, GLX, WGL



glue between OpenGL and windowing systems

GLU (OpenGL Utility Library)





part of OpenGL
NURBS, tessellators, quadric shapes, etc.

GLUT (OpenGL Utility Toolkit)






portable windowing API
keyboard and mouse input
Geometric primitives: cubes, spheres, and teapot
not officially part of OpenGL


OpenGL and Related APIs

application program

OpenGL Motif

GLUT

widget or similar

GLX, AGL
or WGL


X, Win32, Mac O/S

software and/or hardware

GLU
GL


What is JOGL?



Java OpenGL is a wrapper library by the Game Technology Group at Sun Microsystems
that allow OpenGL to be used in Java.





The base OpenGL C API is accessed in JOGL via Java Native Interface (JNI) calls.
The straightforward mapping of the OpenGL C API to Java methods.
Other Java OpenGL wrappers: GL4Java, JGL, LWJGL (Light-Weight Java Game Library)


Setting Up JOGL 2.0x



Download the latest "stable" release from JogAMP

Select "jogamp-all-platforms.7z".



Setup Library




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".


Preliminaries



Headers Files


// must import OpenGL....
im p ort javax.media.opengl.GL2;
// import GL constants
im p ort static javax.media.opengl.GL2.*;
im p ort javax.media.opengl.glu.GLU;
im p ort com.jogamp.opengl.util.gl2.GLUT
im p ort javax.media.opengl.GLAutoDrawable;
im p ort javax.media.opengl.GLCapabilities;
im p ort javax.media.opengl.GLEventListener;
im p ort javax.media.opengl.GLProfile;
im p ort javax.media.opengl.awt.GLJPanel;

...


JOGL Basics



Create an OpenGL drawable is a surface for graphics rendering: GLCanvas and
GLJPanel.



Register GLEventListener to handle OpenGL events: init, display, reshape,
dispose



Register input listeners: keyboard, mouse, etc.



Sample Template Program
p u b lic class JO G L2Tem plate exten ds G LJPanel
im plem ents G LEventListener {
// Setup O penG L G raphics Renderer
p rivate G LU glu; // for the G L U tility
p rivate G LU T glut; // O penG L U tility Toolkit

/** Constructor to setup the G U Ifor this Com ponent */
p u b lic JO G L2Tem plate() {
th is.addG LEventListener(this);
}


Sample 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.
*/
p u b lic 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 = n ew 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 ----}


Sample 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.*/
p u b lic void reshape(G LAutoD raw able draw able,
in t x, in t y, in t w idth, in t height) {
G L2 gl= draw able.getG L().getG L2();
if (height = = 0) height = 1; // prevent divide by zero
fl
o at aspect = (f l
o at) 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,
// Enable the m odel-view transform
gl.glM atrixM ode(G L_M O D ELVIEW );
gl.glLoadIdentity(); // reset
}

zN ear,zFar



Sample Program: display event
/* Called back by the anim ator to perform rendering.*/
p u b lic 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
gl.glVertex3f(0.0f, 1.0f,0.0f);
gl.glVertex3f(-1.0f,-1.0f,0.0f);
gl.glVertex3f(1.0f, -1.0f, 0.0f);
gl.glEnd();
}


Sample Program: dispose event
**
* C alled back before the O penG L context is destroyed.
* Release resource such as buff
ers.
*/
p u b lic void dispose(G LAutoD raw able draw able) {
}



p u b lic static void m ain(String[] args) {
Sw ingU tilities.invokeLater(new Runnable() {
p u b lic void run() {
// Create the O penG L rendering canvas
G LJPanelpanel= 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 b lic void w indow Closing(W indow Event e) {
// stop the anim ator stops before program exits.
n ew Thread() {
p u b lic void run() {
if (anim ator.isStarted()) anim ator.stop();
System .exit(0);
}
}.start();
}
});
fram e.setTitle("JO G L2 Tem plate");
fram e.pack(); fram e.setVisible(true);
anim ator.start(); // start the anim ation loop
}
});



Elementary Rendering


Elementary Rendering



Geometric Primitives



Managing OpenGL State



OpenGL Buffers


OpenGL Geometric Primitives



All geometric primitives are specified by vertices

GL_LINES
GL_POLYGON
GL_POINTS

GL_LINE_STRIP


GL_LINE_LOOP

GL_TRIANGLES

GL_QUADS
GL_QUAD_STRIP
GL_TRIANGLE_STRIP

GL_TRIANGLE_FAN


Simple Example

void drawRhombus(GL2 gl, float[] color) {
gl.glColor3fv(color);
gl.glBegin(GL2.G L_Q U AD S);
gl.glVertex2f(0.0f, 0.0f);
gl.glVertex2f(1.0f, 0.0f);
gl.glVertex2f(1.5f, 1.118f);
gl.glVertex2f(0.5f, 1.118f);
gl.glEnd();
}


OpenGL Command Formats
glVertex3fv(v)

Data Type

Number of

components
2 - (x,y)
3 - (x,y,z)
4 - (x,y,z,w)

b

- byte

ub - unsigned byte
s

- short

us - unsigned short
i

- int

ui - unsigned int
f

- float

d

- double

Vector
omit "v" for

scalar form
glVertex2f(x, y)


Enumerated Types



OpenGL defines numerous types for compatibility:


Specifying Geometric Primitives



Primitives are specified using
glBegin(primType);
glEnd();



primType determines how vertices are combined
void drawRhombus(GL2 gl, float[] color) {
gl.glColor3fv(color);
gl.glBegin(GL_QUADS);
gl.glVertex2f(0.0f, 0.0f);
gl.glVertex2f(1.0f, 0.0f);
gl.glVertex2f(1.5f, 1.118f);
gl.glVertex2f(0.5f, 1.118f);
gl.glEnd();

}


OpenGL Color Models



RGBA or Color Index
color index mode
Red

Green

Blue

0
1

Display

1

2

2

4
8

3

ww
www

16

ww

24

123

219

25
26
www

RGBA mode

74


×