OpenGL API

JOGL and Swing are event driven; the components (e.g. display) are initialised, and callback functions are set up for various events (hence the event handlers). The event loop handles all updates and responses; the main body quits after set up.

Variables

  • GLProfile glp = GLProfile.getDefault();
  • GLCapabilities glc = new GLCapabilities(glp);
    • GLCapabilities store specific things the rendering context should do
    • e.g. for full screen multi sampling for filtering
      • capabilities.setNumSamples(4);
      • capabilities.setSampleBuffers(true);
  • GLJPanel panel = new GLJPanel(glCapabilities);
    • JPanel = lightweight container display. GLJPanel = JPanel + Canvas
  • JFrame jframe = new JFrame("Title");
    • Jframes are Swing windows; we use them for graphics.
    • jframe.getContentPane().add(panel, BorderLayout.CENTER);
    • jframe.setSize(1024, 768);
    • jframe.setVisible(true);
  • GLAutoDrawable drawable;
    • Automatically passed in to certain functions. Allows a gl2 to be created.
  • GL2 gl = drawable.getGL().getGL2();
    • GL2 objects provide access to OpenGL methods and constants.
    • They’re stateful, so whilst they can be accessed as statics, they store information about the colour and size of things that are being drawn.

Methods

All methods are performed on a gl2 object (e.g. gl2.glBegin())

  • Applies an implementation specific ‘hint’
  • GL_LINE_SMOOTH_HINT: Indicates the sampling quality of antialiased lines.
  • Hinting GL_NICEST can result in more pixel fragments being generated during rasterization, if a larger filter function is applied.

[[/cell]]

  • TextureData data = TextureIO.newTextureData(glp, file, false, "png")
  • the first 0 is level of detail (the default is 0), the second 0 is border
  • Actually describe the features of the texture

[[/cell]]

glAttachShader(program, vert or frag shader) Attaches a frag or vert shader to the program
glBegin(/*GL_CONSTANT*/) Starts off a drawing command.
glBindTexture(GL.GL_TEXTURE_2D, textures[i]) Sets the number at textures[i] to be the currently modified texture
glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA)
  • Blends alpha values - there are different blend functions, but this is what malcolm mentions
  • Also required for prefiltering
glCompileShader(vertShader) Compiles a shader source
glCreateProgram() Creates a blank vertex+fragment shader
glCreateShader(GL2.GL_VERTEX_SHADER OR GL_FRAGMENT_SHADER) use as int vert = glCreateShader(); // reserves a shader ID
glEnable(/*GL_CONSTANT*/) Enables a feature
glEnd() Closes a drawing command
glFlush() glFlush ensures that previous OpenGL commands must complete in finite time. It contrasts with glFinish() which will block until the commands are completed.
glFrustum(left, right, bottom, top, near, far)
  • Creates a projection (for perspective camera)
  • the first 4 arguments refer to the near plane
  • near and far are positive integers
  • just like orthographic has gluOrtho, this has gluPerspective
glGenTextures(n, textures, 0) Generates n unique ‘texture names’ (numbers to refer to textures with). Stores them in the textures array passed in.
glGetShaderiv(vertShader, GL2.GL_COMPILE_STATUS, compiled, 0) Gets whether it compiled successfully
glHint(HINT, VALUE)
glLightfv(GL2.GL_LIGHT0, TYPE, data, 0) Takes a light source, and then either:
  • GL_POSITION or GL_AMBIENT/GL_SPECULAR/GL_DIFFUSE
  • A vector (array) of the data (4 floats, last one is typically 1)
  • 0 - the offset for the array
glLightModelfv(type, data, 0)
  • type is e.g. GL_LIGHT_MODEL_AMBIENT
  • data is once again an array
glLinkProgram(shader program);
glLoadIdentity() Initialises a matrix to the default.
glMaterialfv(face, type, data, 0)
  • face = e.g. GL_FRONT
  • GL_AMBIENT/GL_SPECULAR/GL_DIFFUSE/GL_SHININESS (Phong type)
  • All subsequent vertices have this value until it is reset with another call to glMaterialfv
glMatrixMode() Sets the matrix mode to say which kind of matrix we are modifying
glNormal3d(nx, ny, nz) Called before a vertex is drawn to set the normal correctly
glPolygonOffset(factor, units)
  • Adds an offset to the pseudo depth of everything drawn after this calll
  • offset = depth slope of polygon* factor + smallest possible depth amount * units
glPopMatrix()
  • Resets the matrix state to be the last saved one
  • Saves the current matrix state to the stack
glRotated(angle, x, y, z) x y z are axis of rotation
glScaled(sx, sy, sz)
glShaderSource(vertShader, num elements in following, shader src variable (array of strings), lengths of each string in prev array, 0) Associates shader details with a shader id
glTexCoord2d(s,t) Written before a glVertex to specify where in the bound texture it will be
glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, lighting_value) Lighting will either be GL_REPLACE (no lighting) or GL_MODULATE (lighting)
glTexImage2D(GL.GL_TEXTURE_2D, 0, data.getInternalFormat(), data.getWidth(), data.getHeight(), 0, data.getPixelFormat(), data.getPixelType(), data.getBuffer())
glTexParameteri(GL.GL_TEXTURE_2D, filter Type, filter value) TexParameter need to be called twice, once with filter type = GL_TEXTURE_MIN_FILTER and once with GL_TEXTURE_MAG_FILTER. A table of their results can be seen under texturing.
glTranslated(dx, dy, dz);
glUseProgram(shaderprogram) Sets the shader to be used
glValidateProgram(shaderprogram) Checks the program is valid
glVertex[2-3][df]v?() Draws a point. Can be 2 or 3 depending on the dimension, double or float, and optionally a vector instead of individual args. If given a vector it takes a starting position in it (typically 0).

Functions gotten from implementing GLEventListener

public void reshape(GLAutoDrawable d, int x, int y, int w, int h);
Called whenever the viewport changes size; allows aspect ratio to be adjusted by setting the matrix mode and redrawing

Other methods

  • glu.gluBuild2DMipmaps(GL.GL_TEXTURE_2D, data.getInternalFormat(), data.getWidth(), data.getHeight(), data.getPixelFormat(), data.getPixelType(), data.getBuffer());

Because nobody designed things to take in a texture, they all take in separate things of the texture
glu.gluLookAt(eyeX, eyeY, eyeZ, lookAtX, lookAtY, lookAtZ, upX, upY, upZ);
Shortcut for setting up the view transform to look at the world in camera coords
Camera is at eye, it’s looking at lookAt, and typically up vector is 0,1,0
upVector ensures that they vector is rotated at the right angle around the vector from eye to lookAt

  • glu.gluOrtho2d(left, right, bottom, top);

Creates an orthographic projection within specified boundaries (in world coords).
Near and far are set to be -1 and 1 respectively

  • glu.gluOrtho(left, right, bottom, top, near, far);

Creates a 3D orthographic projection, near and far (the z plane) are as specified.

  • glu.gluPerspective(fieldOfView, aspectRatio, near, far);

fieldOfView is angles (in the y direction), aspect ratio is x:y, near and far are positive

Typical Things

A main class might typically extend a JFrame (given Swing), and then implement various key/motion/event listeners.