1 New topic for Java course: Introduction to 3D graphics programming with JOGL Dejan Mitrović Prof. Dr Mirjana Ivanović 2/29 10th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010 AGENDA 1. Motivation and goals 2. JOGL 3. Proposed subjects 4. Conclusions
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
New topic for Java course:Introduction to 3D graphics programming with JOGL
Dejan MitrovićProf. Dr Mirjana Ivanović
2/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
AGENDA
1. Motivation and goals
2. JOGL
3. Proposed subjects
4. Conclusions
2
3/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
1.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
4/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
1.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
3
5/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
AGENDA
1. Motivation and goals
2. JOGL
3. Proposed subjects
4. Conclusions
6/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
2.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 notbe designed ☺
4
7/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
2.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
8/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
2.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
5
9/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
AGENDA
1. Motivation and goals
2. JOGL
3. Proposed subjects
4. Conclusions
10/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
3. Proposed subjects
1. Introduction to 3D graphics programming(1‐hour lesson)
2. 3D rendering basics(1‐hour lesson)
3. World transformations(2‐hours lesson)
4. Texturing(2‐hours lesson)
5. Complex 3D models(2‐hours lesson)
6
11/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
3.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
12/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
3.1. Example: HelloWorld3D
public class HelloWorld3D extends JoglFrame{@Overrideprotected void initialize(GL gl, Color4f back,
16/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
3.2. Example: Hexagon (cont.)
// method for rendering the n-th vertexprivate void renderPoint(GL gl, int n, float z) {
// first set the n-th color, then send the coordinatesColor4f c = colors[n];gl.glColor3f(c.getRed(), c.getGreen(), c.getBlue());Vector3f v = vertices[n];gl.glVertex3f(v.getX(), v.getY(), z);
}
// using GL_TRIANGLE_FAN for hexagon renderingpublic 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 onegl.glBegin(GL.GL_TRIANGLE_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
9
17/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
3.2. Example: 3D Rendering
public class HelloWorld3D extends JoglFrame {private Hexagon hex = new Hexagon();private TextRenderer text;
// render a yellow teapot using GLUTgl.glColor3f(1.0f, 1.0f, 0.0f);glut.glutSolidTeapot(1.0f);// render a colored triangle behind the teapotgl.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();
18/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
3.2. Example: 3D Rendering (cont.)
// render a hexagon behind the trianglehex.renderTriangleFan(gl, -10.0f);// render 2D text at the top of the windowint 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 positiontext.draw("FPS: " + getFps(), 2, h - 16);text.draw("Camera: " + camera.getPosition().toString(), 2, h - 36);// end text renderingtext.endRendering();
} // render
public static void main(String[] args){
HelloWorld3D hw = new HelloWorld3D();hw.start("Hello World 3D", 640, 480, false);
}
} // class HelloWorld3D
10
19/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
3.2. Results
20/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
3.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()
11
21/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
3.3. Example: SolarSystem
public class Planet {private float distance; // from the sunprivate float revolution; // revolution angleprivate float scale; // size parameterprivate 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 colorgl.glColor3f(color.getRed(), color.getGreen(), color.getBlue());// every method that changes the world transformation // should always first save the current state and later restore itgl.glPushMatrix();// rotation around the sungl.glRotatef(revolution, 0.0f, 1.0f, 0.0f);gl.glTranslatef(distance, 0.0f, 0.0f);// scale the sphere and render itgl.glScalef(scale, scale, scale);glut.glutSolidSphere(1.0f, 25, 25);// restore the original world transformationgl.glPopMatrix();
}}
22/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
3.3. Example: SolarSystem (cont.)
public class SolarSystem extends JoglFrame {private Planet sun, venus, earth, mars; // miniature solar system
public static void main(String[] args) {// ommitted, the usual
}}
12
23/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
3.3. Results
24/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
3.4. Texturing
Textures and texture coordinates
Loading, enabling, disabling, and binding textures, assigning coordinates with glTexCoord2f()
Method glTexEnvf(), and environment modesGL_MODULATE and GL_REPLACE
Different ways of applying texture coordinates withGL_CLAMP and GL_REPEAT
2D texture rendering with TextureRenderer
13
25/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
3.4. Adding textures to SolarSystem
26/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
3.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
14
27/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
3.5. The end results
28/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010
4. 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
15
29/2910th Workshop "Software Engineering and Reverse Engineering", Ivanjica, Serbia, September 5 - 12, 2010