160 likes | 176 Views
Learn about OpenGL, GLU, GLX, WGL, AGL, JOGL, DirectX, Shaders, Animation, Vector Operations, and more. Explore code examples and understand the rendering pipeline.
E N D
OpenGL and Related Libraries DEMO 1 • WHAT IS OpenGL? • The OpenGL graphics system is an application programming interface (API) to graphics hardware DEMO 2 @ 2017 by Jim X. Chen: jchen@cs.gmu.edu .1.
The OpenGL Utility Library (GLU) contains several routines that use OpenGL commands. It is considered part of your OpenGL. • The OpenGL Extension to the X Window System (GLX) provides a means of creating an OpenGL context and associating it with a drawable window on a machine that uses the X. • WGL for Windows; AGL for Apple; PGL for OS/2 • OpenGL Auxiliary Library (AUX) or Utility Toolkit (GLUT) to make programming examples simpler. Works for X, Win, Apple, etc. OpenGL-Related Libraries • JOGL – Java OpenGL API @ 2017 by Jim X. Chen: jchen@cs.gmu.edu .2.
OpenGL and DirectX libraries • Theoretically, OpenGL and DirectX (Direct3D) are the same • OpenGL and Direct3D are mature graphics library • OpenGL is easier to learn and use, and is integrated in most modern graphics textbooks • DirectX is much more popular in PC game industry in the past @ 2017 by Jim X. Chen: jchen@cs.gmu.edu .3.
JOGL – Java for OpenGL • JOGL implements Java bindings for OpenGL. • Provides platform for 3D graphics applications in Java • JOGL provides full access to OpenGL functions and integrates with the AWT or Swing widget sets. • Part of a suite of open-source technologies initiated by the Game Technology Group at Sun Microsystems. • JOGL1_0_Frame * JOGL initial set up: * Open a frame with a background color @ 2017 by Jim X. Chen: jchen@cs.gmu.edu .4.
Shaders • Earlier GPU programming are through separate libraries: Cg, HLSL, etc. • OpenGL 4.x includes GLSL as a standard: shader programming that runs on GPU • Cg library faced out 2013 • Old CPU graphics pipeline faced out @ 2017 by Jim X. Chen: jchen@cs.gmu.edu
The rendering pipeline • Vertex shader: parallel vertex processing • Fragment shader: parallel fragment processing • JOGL1_0_Point • Draw a point with vertex and fragment shaders • JOGL1_1_PointVFfiles • vertex and fragment shader programs are stored in corresponding files @ 2017 by Jim X. Chen: jchen@cs.gmu.edu
Animation • Motion in a movie was achieved by projecting a sequence of pictures at 24 frames per second on the screen • 60 f/s is smoother than 30, and 120 is marginally better than 60. Refresh rates faster than 120 make no difference. • Double-buffering • The frontbufferis displayed while the backbufferis drawn. • When the drawing of a frame is complete, the two buffers are swapped. @ 2017 by Jim X. Chen: jchen@cs.gmu.edu .7.
JOGL’s Animator • JOGL1_2_Animate * Animate a point * Animator starts a thread that calls display() repetitively * Uniform sends a variable value from JOGL main program to the shader program • Technical details are covered in the program @ 2017 by Jim X. Chen: jchen@cs.gmu.edu
For Drawing an Object Just Once • JOGL1_2_Animate • comment out clearing the background: a shimmy line (alternating points in two buffers) • Treat double-buffer as a single-buffer • setting glDrawBuffer() to GL_FRONT with a glFlush() call. • To switch back to double-buffered, you need to set glDrawBuffer() to GL_BACK. • You can just choose to draw into both buffers: • glDrawBuffer(GL_FRONT_AND_BACK); @ 2017 by Jim X. Chen: jchen@cs.gmu.edu
Some old Examples Coded In OpenGL DEMO 1 DEMO 3 DEMO 5 DEMO 7 DEMO 2 DEMO 4 DEMO 6 DEMO 8 @ 2017 by Jim X. Chen: jchen@cs.gmu.edu .10.
Vector Operations • Vector: a and b • a=(a0, a1, a2); b=(b0, b1, b2); • It gives a length and a direction • Length: |a| = sqrt(a0*a0 + a1*a1 + a2*a2) • Direction: a/|a| = (a0 /|a| , a1 /|a| , a2 /|a| ) // normalize a vector to unit vector public void normalize(double a[]) { double d = Math.sqrt(a[0]*a[0] + a[1]*a[1]+ a[2]*a[2]); if (d == 0) {System.err.println("0 length vector: normalize()."); return;} a[0] /= d; a[1] /= d; a[2] /= d; } GLSL: normalize(vec3) or normalize(vec4) @ 2017 by Jim X. Chen: jchen@cs.gmu.edu
Vector Operations – Cont. • Addition/Subtraction • a+b = (a0+b0, a1+b1, a2+b2); • GLSL: vec3 ± vec3 • Dot Product (easy for finding cos) • ab = |a||b|cos = a0*b0 + a1*b1 + a2*b2 • GLSL: dot(vec3,vec3) or dot(vec4,vec4) public double dotprod(double[] a, double[] b) { return (a[0] * b[0] + a[1] * b[1] + a[2] * b[2]); } @ 2017 by Jim X. Chen: jchen@cs.gmu.edu
Vector Operations – Cont. • Cross Product (easy for finding the perpendicular vector, or normal) • a×b = a vector perpendicular to a&b • a×b = n|a||b|sin // cross product of two vectors • GLSL: cross(vec3,vec3) public void crossprod(double[] a, double[] b, double[] v) { v[0] = a[1] * b[2] - a[2] * b[1]; v[1] = a[2] * b[0] - a[0] * b[2]; v[2] = a[0] * b[1] - a[1] * b[0]; } @ 2017 by Jim X. Chen: jchen@cs.gmu.edu
Reflect v1 around n to v2 n v1 v2 • n is normalized; • dotprod(v1,n) is the projection length of v1 on n • 2*dotprod(v1,n) is twice of the above • 2*dotprod(v1,n)*n is the vector along n with the length • The above minus v1 will give you v2 v1 v2 v1 v2 v1 v2 @ 2017 by Jim X. Chen: jchen@cs.gmu.edu
Reflect v1 around n to v2 n v1 v2 // reflect v1 around n to v2 public void reflect(double v1[], double n[], double v2[]) { // v2 = 2*dot(v1, n)*n + v1 for (int i = 0; i < 3; i++) { v2[i] = 2 * dotprod(v1, n) * n[i] - v1[i]; } } GLSL: reflect(v1, n); v1 v2 v1 v2 v1 v2 @ 2017 by Jim X. Chen: jchen@cs.gmu.edu
HW1: due before next class • draw points that moves slowly along a circle • Draw multiple points that move and bounce inside the circle • Draw multiple points that bounces outside a circle and inside a rectangle @ 2017 by Jim X. Chen: jchen@cs.gmu.edu .16.