Courses/CS 491ab/Winter 2008/Quan Lau
From CSWiki
Contents |
[edit] Week 1 - January 4, 2008
introduction
[edit] Week 2 - January 11, 2008
[edit] JOGL (Java OpenGL)
Jogl is a Java programming language binding for the OpenGL 3D graphics API. For those of you who don't know, OpenGL is written in the C programming language. I already have some experience using OpenGL but very little with Java's 2D/3D APIs and since I'm programming with Java mostly nowadays, I thought it would be convenient to expand my knowledge of OpenGL while using the more familiar language. And plus it would be interesting to learn how Jogl integrates with Java's other library functions like ones for multithreading.
This week, I had demonstrated the difference/similarities between the code for the OpenGL and Jogl APIs.
This simple OpenGL application creates a window (frame), a white canvas, and renders a green square on the canvas. It's implemented using the C language.
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
const int screenWidth = 500;
const int screenHeight = 500;
void myInit(void){
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1,1,-1,1); // world-window (l,r,b,t)
glViewport(0,0,screenWidth,screenHeight); // viewport (x,y,w,h)
}
/////////////// Callback Function
void display(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3d(0.0, 1.0, 0.0 );
glBegin(GL_POLYGON);
glVertex2d(-0.25, -0.25);
glVertex2d(-0.25, 0.25);
glVertex2d(0.25, 0.25);
glVertex2d(0.25, -0.25);
glVertex2d(-0.25, -0.25);
glEnd();
glFlush();
}
void main(int argc, char **argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(screenWidth,screenHeight);
glutInitWindowPosition(0,0);
glutCreateWindow("A simple OpenGL demo");
glutDisplayFunc(display); // register event
myInit();
glutMainLoop();
}
Now, here's the same program implemented using Java and the Jogl API.
import javax.swing.JFrame;
import javax.media.opengl.GLCanvas;
public class sampleJogl{
public static void main(String[] args) {
JFrame f = new JFrame("A simple Jogl demo");
GLCanvas canvas = new GLCanvas();
canvas.addGLEventListener(new Square());
f.add(canvas);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500,500);
f.setVisible(true);
}
}//end class
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
public class Square implements GLEventListener{
private static final GLU glu = new GLU();
public void init(GLAutoDrawable gld){
GL gl = gld.getGL();
gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
gl.glViewport(0, 0, 500, 500); // viewport (x,y,w,h)
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(-1.0, 1.0, -1.0, 1.0); //world-window (l,r,b,t)
}
public void display(GLAutoDrawable drawable){
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glColor3d(0.0, 1.0, 0.0 );
gl.glBegin(GL.GL_POLYGON);
gl.glVertex2d(-0.25, -0.25);
gl.glVertex2d(-0.25, 0.25);
gl.glVertex2d(0.25, 0.25);
gl.glVertex2d(0.25, -0.25);
gl.glVertex2d(-0.25, -0.25);
gl.glEnd();
}
public void reshape(GLAutoDrawable drawable,
int x, int y, int width, int height) {}
public void displayChanged(GLAutoDrawable drawable,
boolean modeChanged, boolean deviceChanged) {}
}//end class
[edit] Week 3 - January 18, 2008
For this week, I wanted to test how easy it would be to be able to port an OpenGL program that I wrote, a while back, to Java using Jogl. As I've found out, it isn't just a matter of copying-and-pasting and making minor modifications to the code (so that the code conforms to Java syntax). As it turns out, OpenGL programs handle events differently from Java programs in general. OpenGL typically uses one single thread of execution to not only render an image but to handle mouse, keyboard, and other types of events. This is different from Java programs that handles events. Java's event handling is inherently multithreaded which means a separate thread is used to draw the image, another thread used to handle mouse events, another used to handle keyboard, and so forth. So the code that I wrote for the OpenGL program would not run properly in the Java environment. This means, I had to significantly modify some parts of my code. The result of my 'fix' (probably introduced other problems) is a major decrease in the performance of the program.
For the remainder of this week, I will try to figure out how to solve this problem before moving on to the next research ...
[edit] Week 4 - January 25, 2008
This week, I showed off my amazing demo of a tank-like object moving around and shooting off stuff. The program demonstrated the integration of OpenGL rendering via Jogl on a GLCanvas component, Java's Swing GUI components (JRadioButton and JLabel), and even Java's multithread functions (used to simulate rapid firing). Writing the program was mostly easy. Minor issues I had was putting the components together using a layout manager. Initially I'd tried using Java's FlowLayout class but my GLCanvas wouldn't display correctly. However, switching over to BorderLayout class fixed the problem. Awesome.
[edit] Week 5 - February 1, 2008
[edit] JInput
Spent some time learning about this input management API for Java over the week. Some of the previous programs that I'd presented demonstrated the ability for user interaction via keyboard and mouse. These programs implemented the KeyListener & MouseListener interfaces to handle the user input events. However, the code to my program only allowed one input and response at a time (or frame). In other words, if I try to hold down more than 2 keys simultaneously, my program will only be able to receive the first key it detects and respond to the callback associated with that key only. Obviously, most video games are not interesting if the game can only respond to one input at a time. With the help of JInput, I can write my program to handle simultaneous inputs and respond accordingly.
here's a basic tutorial on how to use JInput:
The following code sets up JInput. A system-print output is automatically generated to let the user know what input devices (on the user's computer) JInput was able to detect.
ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment(); Controller[] contr = ce.getControllers();
the next piece of code assigns a handler to your keyboard & mouse:
for(int i=0; i < contr.length ;++i){ Controller controller = contr[i]; if (controller.getType() == Controller.Type.KEYBOARD) Keyboard keyboard = (Keyboard) controller; else if (controller.getType() == Controller.Type.MOUSE) Mouse mouse = (Mouse) controller; }
now you can test what the user has inputted by using the handlers 'mouse' & 'keyboard' like in the following code:
keyboard.poll() if(keyboard.isKeyDown(Component.Identifier.Key.RETURN)) startgame = true;
Apparently, this only works for GUI based programs (I could be wrong about this), so make sure you have at least a JFrame setup.
and I wrote a Pong game.
[edit] Week 6 - February 8, 2008
[edit] Camera Orbiting
Presented a couple of demo programs that renders 3D objects. Creating the objects was just a matter of making some appropriate function calls provided by OpenGL utility toolkits like GLUT or GLU. (If I wanted to create the objects from the bottom up, i.e. using just primitives like points & lines, it would require figuring out a lot of things like location of vertices, calculating normals to each surface quad, & so forth) So using the provided functions to create basic geometric objects is very convenient. The main feature of the demos is the capability to manipulate the camera in a way that allows the viewer to see the objects or 'scene' at any angle. So the viewer can "orbit" the camera around the object to obtain any desired vantage point. I wasn't able to figure out how the math works to make the camera orbit (I came close though using Spherical coordinates). So I had to search online for code to accomplish this task and this is the code I found:
public void polarView(double distance, double twist, double elevation, double azimuth, double zoom){
glTranslated(0.0, 0.0, -distance);
gl.glRotated(-twist, 0.0, 0.0, 1.0);
gl.glRotated(-elevation, 1.0, 0.0, 0.0);
gl.glRotated(azimuth, 0.0, 0.0, 1.0);
gl.glScaled(zoom, zoom, zoom);
}
Camera Orbiting is accomplished through a combination of linear transformations (translating, rotation, and scaling). So in fact, the camera isn't moving at all. The camera is stationary while the scene or coordinate system is the thing that's actually changing. But the resulting effect is the same.
[edit] Week 7 - February 15, 2008
[edit] 3D Collision Detection
I converted my 2-dimensional Pong game, that I wrote & demonstrated to the class a few weeks ago, to 3-D. So technically, this would be my first 3D game that I've ever written. The point of this was to give myself more practice in writing programs that renders 3D objects and to be more proficient at manipulating the camera. Since this is 3D, the pong-ball should also have the freedom to move in all directions in space. Accomplishing this was just a matter of adding the z-(depth) component to the ball & every other object in the game. By the way, every object is encapsulated in a bounding cube now, so I had to basically overhaul the BoundingBox class that I'd used in the 2D version. The biggest challenge I had during the conversion process was figuring out how to write the code to detect collision between objects and how they would repond after collision. This required me to do some online research & the code I found fitted nicely with what I wanted.
Here's the code to perform collision detection of 2 bounding-cubes:
public boolean isCollide(BoundingBox3D otherBox) {
return otherBox != this &&
otherBox.max.x > min.x &&
otherBox.min.x < max.x &&
otherBox.max.y > min.y &&
otherBox.min.y < max.y &&
otherBox.max.z > min.z &&
otherBox.min.z < max.z;
}
[edit] Week 8 - February 22, 2008
This week I've created a simple 3D world (just using Java, Jogl to draw the scene, & anything else I've used in the past few weeks. This is really just an exercise for me to see if I can create one without the aid of a sophiscated high-level framework. The most important aspect of this world is the use of the camera in a way to simulate a first-person point of view. So you can navigate & see what the player is seeing as you go around exploring the world.
[edit] Week 9 - February 29, 2008
So I finally decided my official project will be on the development of a 3D Virtual Reality of our school's campus. The goal of this project is to allow anyone to take a virtual tour of the campus. Last week, I created a program of a simple 3D world populated with objects like houses, stairs, etc. Although the house was very simple to create (because it consist of only a collection of appropriately placed scaled-cubes), it was quite a tedious task to design it. If I'm going to construct all the buildings for this campus, I am going to need a 3D modeling software that will help me do the job quickly and accurately (as much as possible).
[edit] Blender
This week I've looked into a 3D modeling software called Blender. Blender is a 3D animation program released as free software. It can be used for modeling, UV unwrapping, texturing, rigging, skinning, animating, rendering, particle and other simulations, non-linear editing, compositing, and creating interactive 3D applications.
Exporting the model from Blender. Once I was able to create a "model", I had to figure out a way to export the model from Blender and into my Java programs. Blender has a plugin called 'Blend2Java' that will export the model into an XML file that contains all the information needed to reconstruct the model in Java (like location of vertices, normals to the surfaces, texture, color information, etc.). I then simply used Java's standard package library class called 'XMLDecoder' to load the model into my Java program. Below is the code to do so:
[edit] XMLDecoder & Java3D
code to load the exported model (contained in an XML file) to Java:
import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
public Shape3D loadBlend(String filename){
Shape3D shape = null;
XMLDecoder de;
try {
de = new XMLDecoder(new BufferedInputStream(new
FileInputStream(filename)));
shape = (Shape3D) de.readObject();
de.close();
} catch(Exception e) {e.printStackTrace();}
return shape;
}
The loaded model will be handled by Java3D's Shape3D object type. The next step was figuring out how to extract the data in the Shape3D objects to have it rendered onto the screen. This required me to learn about Java3D programming (and the concept of Scene Graphs in particular).
[edit] Week 10 - March 7, 2008
[edit] Senior Project
[edit] Brief project description
A 3D Virtual Reality of the CSULA's main campus (and possibly the student housing grounds). This will be a Java application. All the buildings and objects will be created using a 3D modeling software called Blender and then exported into Java. Users may take a virtual tour of the campus and get (somewhat) familiar with building locations relative to each other. The experience will be very much like playing a 1st-person perspective video game where one explores a virtual world.
[edit] Anticipated users
Ideally, the school's official website will feature my program and that the users will be anyone who would like to explore the school's campus without physically doing so. This would be particularly useful for not only new students but for current students who need to visualize how to get to a certain location (like a classroom).
But realistically, due to the fact that I have never done something like this before, my program would most likely end up incomplete or complete but very unprofessional looking. And I'm sure the school has a high standard in choosing applications to be featured on its website.
If that's the case, the users could simply be people who would like to learn about the things that I have investigated in my attempt at this project. And so my project would really be a tutorial on creating a virtual world using Java and the many external libraries that are available for free.
[edit] Main conceptual (i.e., user-level) objects
Users will assume the role of the "tourist" in which they will view the virtual world from the point of view of the camera (1st-person POV).
[edit] Primary conceptual (i.e., user-level) operations
Using the keyboard, users can navigate the world by moving around in all directions on a horizontal plane, orient their view point (i.e. head) at any angle, and even be able to teleport to specific locations instantly (by querying an existing location at a GUI text field).
[edit] Why I am interested in this project
I'm interested in computer graphics and ways to manipulate the graphics, like creating animation. My initial idea for a project was to create some type of video game. But I could'nt think of an idea for a game so I chose this idea instead. This project will provide me with practice and experience in creating 3D virtual worlds which is something I'll need to know how to do if I want to get into video game programming in the future.
[edit] Status
I've learned:
- the basics of Blender (3D modeling software). I can create objects that resemble buildings (it's just a matter of joining a set of scaled-cubes together). I can export the models to a format file called '.obj' and then import it into my Java application.
- a lot of the features contained in the game-engine that I'll be using called 'Xith3D' (a package full of APIs for scene graph management, physics, input manag., model loading, creating sound, etc). So far, I'm able to set up a basic skeleton program that creates an empty virtual world (with only a floor to walk on). The program contains code to allow the player to navigate the world in a similar fashion to a First Person Shooter (FPS) video game. And also the program is able to load the models from Blender.
Other softwares I'll be using:
- Google Earth - to capture accurate (aerial) photos of the campus.
- GIMP (GNU Image Manipulation Program) - an Adobe Photoshop-like program that's entirely free. I plan on taking a lot of digital photos of buildings on campus as well. And this program will help me convert the photos into 'textures' for the models that I create on Blender.

