Title: New topic for Java course: Introduction to 3D graphics programming with JOGL
1New topic for Java courseIntroduction to 3D
graphics programming with JOGL
- Dejan MitrovicProf. Dr Mirjana Ivanovic
2AGENDA
- Motivation and goals
- JOGL
- Proposed subjects
- Conclusions
31.1. Motivation
- Game development represents a large portion of
the software development industry, BUT, so far
very few programming courses given at our
Department were focused at this area - The proposed set of subjects constitutes a small
part of the recently updated Computer graphics
course, which has received high grades from
students
41.2. Goals
- Provide students with an opportunity to practice
Java programming and extend their OO programming
skills in an exciting environment - Introduce the proposed set of subjects near the
end of the introductory, or at the beginning of
the advanced OO course, depending on the
students progress over the semester - Give them a solid background for later, more
advanced topics of the Computer graphics course
5AGENDA
- Motivation and goals
- JOGL
- Proposed subjects
- Conclusions
62.1. Why JOGL?
- Based on OpenGL - a professional, powerful, free,
cross-platform graphics programming library - Straightforward integration with other GUI and
graphics Java APIs and libraries (e.g. Swing,
Java2D) - Can serve as an example of how an OO system
should not be designed ?
72.2. JOGLext A JOGL extension library
- A library of helper classes built on top of JOGL
- Handles common, boring, but necessary tasks,
includes functions for vector and matrix-based
calculations, etc. - Purpose enable students to develop simple 3D
applications without any (mathematical or game
programming) background - Although, experience in playing FPS games can
prove valuable for 3D camera handling
82.3. Core JOGLext classes
- JoglFrame a base class for all students
applications, handles display mode changes,
precise timing measurements, various maintenance
tasks, etc. - Camera a flexible, programmable, FPS-like 3D
camera for navigation through generated scenes - Mesh a framework for loading and rendering
external complex 3D models
9AGENDA
- Motivation and goals
- JOGL
- Proposed subjects
- Conclusions
103. Proposed subjects
- Introduction to 3D graphics programming(1-hour
lesson) - 3D rendering basics(1-hour lesson)
- World transformations(2-hours lesson)
- Texturing(2-hours lesson)
- Complex 3D models(2-hours lesson)
113.1. Introduction to 3D graphics programming
- What is a graphics programming library?
- Introduction to OpenGL and JOGL
- Elements of a 3D application frustum definition,
overview of projection and viewing
transformations, viewports, etc. - Main classes of JOGLext, creating a very first 3D
application by implementing methods of JoglFrame
123.1. Example HelloWorld3D
public class HelloWorld3D extends JoglFrame
_at_Override protected void initialize(GL gl,
Color4f back, Vector3f cameraPos,
Vector3f cameraDir) // TODO initialize
camera, set clear color _at_Override
protected void render(GL gl, double elapsedTime)
// TODO perform rendering public
static void main(String args)
HelloWorld3D hw new HelloWorld3D() //
Caption Hello World 3D // Frame width
640px // Frame height 480px //
Fullscreen mode false hw.start("Hello World
3D", 640, 480, false)
133.1. Results
143.2. 3D rendering basics
- Coordinate system and 3D object specification
using vertices - Methods glVertex3f() and glColor3f()
- Different modes of rendering with glBegin() and
glEnd() triangle lists, strips, and fans, quads,
and polygons - Rendering built-in objects with GLUT
- Basic 2D text rendering with TextRenderer
153.2. Example Hexagon
public class Hexagon // vertices definition
private static final Vector3f vertices new
Vector3f new Vector3f( 0.0f, 0.0f,
0.0f), new Vector3f(-1.0f, 0.0f, 0.0f),
new Vector3f(-0.5f, 1.0f, 0.0f), new
Vector3f( 0.5f, 1.0f, 0.0f), new Vector3f(
1.0f, 0.0f, 0.0f), new Vector3f( 0.5f,
-1.0f, 0.0f), new Vector3f(-0.5f, -1.0f,
0.0f) // each vertex will be of a
different color private static final Color4f
colors new Color4f new Color4f(1.0f,
0.0f, 0.0f), new Color4f(0.0f, 1.0f, 0.0f),
new Color4f(1.0f, 1.0f, 0.0f), new
Color4f(1.0f, 1.0f, 1.0f), new Color4f(0.0f,
0.0f, 1.0f), new Color4f(1.0f, 0.0f, 1.0f),
new Color4f(0.0f, 1.0f, 1.0f)
163.2. Example Hexagon (cont.)
// method for rendering the n-th vertex
private void renderPoint(GL gl, int n, float z)
// first set the n-th color, then send the
coordinates Color4f c colorsn
gl.glColor3f(c.getRed(), c.getGreen(),
c.getBlue()) Vector3f v verticesn
gl.glVertex3f(v.getX(), v.getY(), z) //
using GL_TRIANGLE_FAN for hexagon rendering
public void renderTriangleFan(GL gl, float z)
// the first 3 vertices make up 1 triangle
// every other vertex is connected to the first
and the the previous one gl.glBegin(GL.GL_TRIA
NGLE_FAN) renderPoint(gl, 0, z)
renderPoint(gl, 1, z) renderPoint(gl, 2,
z) renderPoint(gl, 3, z)
renderPoint(gl, 4, z) renderPoint(gl, 5,
z) renderPoint(gl, 6, z)
renderPoint(gl, 1, z) gl.glEnd() //
class Hexagon
173.2. Example 3D Rendering
public class HelloWorld3D extends JoglFrame
private Hexagon hex new Hexagon() private
TextRenderer text _at_Override protected void
initialize(GL gl, Color4f back,
Vector3f cameraPos, Vector3f cameraDir) //
initialize TextRenderer Font font new
Font("Tahoma", Font.BOLD, 12) text new
TextRenderer(font) _at_Override protected
void render(GL gl, double elapsedTime) //
render a yellow teapot using GLUT
gl.glColor3f(1.0f, 1.0f, 0.0f)
glut.glutSolidTeapot(1.0f) // render a
colored triangle behind the teapot
gl.glBegin(GL.GL_TRIANGLES) gl.glColor3f(
1.0f, 0.0f, 0.0f) gl.glVertex3f(-1.0f,
-0.5f, -5.0f) gl.glColor3f( 0.0f, 1.0f,
0.0f) gl.glVertex3f( 1.0f, -0.5f, -5.0f)
gl.glColor3f( 1.0f, 1.0f, 1.0f)
gl.glVertex3f( 0.0f, 0.5f, -5.0f)
gl.glEnd()
183.2. Example 3D Rendering (cont.)
// render a hexagon behind the triangle
hex.renderTriangleFan(gl, -10.0f) // render
2D text at the top of the window int w
getWidth() int h getHeight()
text.beginRendering(w, h) text.setColor(1.0f,
1.0f, 0.0f, 1.0f) // first line - frame
rate, below it - camera position
text.draw("FPS " getFps(), 2, h - 16)
text.draw("Camera " camera.getPosition().toStri
ng(), 2, h - 36) // end text rendering
text.endRendering() // render public
static void main(String args)
HelloWorld3D hw new HelloWorld3D()
hw.start("Hello World 3D", 640, 480, false)
// class HelloWorld3D
193.2. Results
203.3. World transformations
- Motivation behind and the effects of world
transformations - With just a brief overview of low level matrix
math - Methods glTranslatef(), glRotatef(), and
glScalef() - Combining multiple transformations
- Storing and restoring transformations with
glPushMatrix() and glPopMatrix()
213.3. Example SolarSystem
public class Planet private float distance
// from the sun private float revolution //
revolution angle private float scale //
size parameter private Color4f color //
color of the planet public Planet(float
distance, float revolution, float scale, Color4f
color) // ommitted, save parameters
public void render(GL gl, GLUT glut) //
set the planet color gl.glColor3f(color.getRed
(), color.getGreen(), color.getBlue()) //
every method that changes the world
transformation // should always first save
the current state and later restore it
gl.glPushMatrix() // rotation around the
sun gl.glRotatef(revolution, 0.0f, 1.0f,
0.0f) gl.glTranslatef(distance, 0.0f,
0.0f) // scale the sphere and render it
gl.glScalef(scale, scale, scale)
glut.glutSolidSphere(1.0f, 25, 25) //
restore the original world transformation
gl.glPopMatrix()
223.3. Example SolarSystem (cont.)
public class SolarSystem extends JoglFrame
private Planet sun, venus, earth, mars //
miniature solar system protected void
initialize(GL gl, Color4f backColor,
Vector3f cameraPos, Vector3f cameraDir) //
initialize camera cameraPos.assign(1.75f,
17.0f, 38.47f) cameraDir.assign(0.0f,
-0.49f, -0.87f) // initialize planets
sun new Planet(0.0f, 0.0f, 5.0f, new
Color4f(1.0f, 1.0f, 0.0f)) venus new
Planet(10.0f, -160.0f, 0.7f, new Color4f(0.59f,
0.3f, 0.0f)) earth new Planet(15.0f,
-80.0f, 1.0f, new Color4f(0.0f, 0.0f, 1.0f))
mars new Planet(20.0f, -60.0f, 0.5f, new
Color4f(1.0f, 0.0f, 0.0f)) protected void
render(GL gl, double elapsedTime)
sun.render(gl, glut) venus.render(gl,
glut) earth.render(gl, glut)
mars.render(gl, glut) public static void
main(String args) // ommitted, the usual
233.3. Results
243.4. Texturing
- Textures and texture coordinates
- Loading, enabling, disabling, and binding
textures, assigning coordinates with
glTexCoord2f() - Method glTexEnvf(), and environment modes
GL_MODULATE and GL_REPLACE - Different ways of applying texture coordinates
with GL_CLAMP and GL_REPEAT - 2D texture rendering with TextureRenderer
253.4. Adding textures to SolarSystem
263.5. Complex 3D models
- The concept of meshes, their organization into
sub-meshes of vertices with shared properties - Using the Mesh class for loading and rendering 3D
models - OBJ file format for storing meshes
273.5. The end results
284. Conclusions
- Graphics programming with JOGL represents an
exciting environment for practicing Java
programming and extending students OO
programming skills (such as aggregation,
inheritance and polymorphism, working with
arrays, etc.) - JOGLext enables students to dive into the 3D
application development without much previous
knowledge of the subject - The proposed set of subjects is simple enough,
yet it provides them with enough material for
creating real 3D applications - The subjects also serve as a good foundation for
senior, more advance Computer graphics course
29- Thank you! ?
- Questions? Suggestions?