Commits

Anonymous committed b4e4679

Added BasicRoom java code.

Comments (0)

Files changed (3)

opengl/java/p3/BasicRoom.java

+/*
+ * BasicRoom.java
+ *
+ * Simple jogl demo:
+ * Displays a simple room as viewed by a single perspective camera 
+ * that is oriented by moving the mouse.
+ *
+ * The "virtual camera" in this demo is very basic. 
+ * It maps mouseX and mouseY directly to an absolute yaw and pitch.
+ *
+ * Ben Porter, August 2010
+ */
+
+import javax.media.opengl.*;
+import javax.media.opengl.glu.*;
+import javax.media.opengl.awt.*;
+import com.jogamp.opengl.util.*;
+import com.jogamp.opengl.util.gl2.GLUT;
+
+import java.awt.*;
+import java.awt.event.*;
+
+public class BasicRoom extends Frame implements GLEventListener, MouseMotionListener, WindowListener
+{
+	static GLU glu;
+	static GLUT glut;
+	static GLCanvas canvas;
+
+	static int width = 800, height = 800;
+	static int[] INSETS = new int[2]; // need this so we can get proper mouse coordinates
+	
+	int mouseX, mouseY;
+
+	Room room;
+
+	public static void main(String[] args)
+	{
+		BasicRoom frame = new BasicRoom();
+		frame.setTitle("A basic room rendered with OpenGL");
+		frame.setSize(width,height);
+		frame.setVisible(true);
+
+		INSETS[0] = frame.getInsets().left;
+		INSETS[1] = frame.getInsets().top;
+	}	
+
+	public BasicRoom()
+	{
+		/* create default capabilities object. Init options can be set here */
+		GLCapabilities capabilities = new GLCapabilities(GLProfile.get(GLProfile.GL2));
+		
+	
+		/* create canvas with specified capabilities */
+		canvas = new GLCanvas(capabilities);
+		canvas.addGLEventListener(this);
+		canvas.addMouseMotionListener(this);
+		addWindowListener(this);
+		add(canvas,BorderLayout.CENTER);
+	
+		glu = new GLU();		// for gluPerspective
+		glut = new GLUT();	// for glutCube
+
+		mouseX = width/2;
+		mouseY = height/2;
+		room = new Room();
+	}
+
+
+	public void init(GLAutoDrawable drawable)
+	{
+		GL2 gl = drawable.getGL().getGL2();
+		gl.glClearColor(1.0f,1.0f,1.0f,1.0f);	// set bg to white
+		gl.glEnable(GL2.GL_DEPTH_TEST);			// enable depth buffering
+		gl.glEnable(GL2.GL_LIGHTING);			// enable lighting
+		gl.glEnable(GL2.GL_LIGHT0);				// use default light
+		gl.glEnable(GL2.GL_COLOR_MATERIAL); // use glColor to specify material colour 
+		gl.glColorMaterial(GL2.GL_FRONT_AND_BACK, GL2.GL_DIFFUSE);
+
+	}
+
+	public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h)
+	{
+		GL2 gl = drawable.getGL().getGL2();
+		
+		width = w;
+		height = h;
+
+		/* CAMERA: Set up projection matrix */
+		// set correct aspect ratio for perspective projection
+		gl.glMatrixMode(GL2.GL_PROJECTION);
+		gl.glLoadIdentity();
+		glu.gluPerspective(60,1.0*width/height,1,100);
+	
+		// Change mode back to modelview
+		gl.glMatrixMode(GL2.GL_MODELVIEW);
+		gl.glLoadIdentity();
+	}
+
+	public void display(GLAutoDrawable drawable)
+	{
+		GL2 gl = drawable.getGL().getGL2();
+		
+		// display callback
+		gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
+		gl.glLoadIdentity();
+
+		/* CAMERA: The viewing transformation. */
+		glu.gluLookAt(
+				0,
+				Room.dy/2,
+				0,
+				0,Room.dy/2,10,
+				0,1,0);
+		gl.glTranslatef(0,Room.dy/2,0);
+		gl.glRotatef(-90.f+180.f*mouseY/height,1,0,0);
+		gl.glRotatef(-180.f+360.f*mouseX/width,0,1,0);
+		gl.glTranslatef(0,-Room.dy/2,0);
+
+		/* Place the light in absolute world coordinates. */		
+		float lightPosition[] = {0,Room.dy/2,0,1};
+		gl.glLightfv(GL2.GL_LIGHT0,GL2.GL_POSITION,lightPosition,0);
+
+		/* Draw the room */
+
+		room.draw(drawable);	
+
+		gl.glFlush();
+	}
+
+	public void dispose(GLAutoDrawable d){}
+
+	public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
+	{}
+
+	public void mouseMoved(MouseEvent e)
+	{
+		int x = e.getX();
+		int y = e.getY();
+		mouseX = x - INSETS[0];
+		mouseY = height - y - INSETS[1]; // vertical screencoords flipped
+		canvas.repaint();
+	}
+
+	public void mouseDragged(MouseEvent e)
+	{}
+	
+	public void windowClosing(WindowEvent e)
+	{
+		setVisible(false);
+		System.exit(0);
+	} 
+	
+	public void windowActivated(WindowEvent e){}
+	public void windowClosed(WindowEvent e){}
+	public void windowDeactivated(WindowEvent e){}
+	public void windowDeiconified(WindowEvent e){}
+	public void windowIconified(WindowEvent e){}
+	public void windowOpened(WindowEvent e){}
+}

opengl/java/p3/Makefile

+# This Makefile will compile BasicRoom.java
+# To run it you will also need to specify the class path
+# which is printed to the console upon compilation.
+# 
+# You will also need to have the jogl dlls in your PATH, e.g.,
+# 	set PATH=C:\lib\jogl\lib;%PATH%
+# 
+# Set JOGLDIR to point to the location of the jogl libraries 
+#   (the .jar and .dll files)
+#
+# Ben Porter, August 2010
+
+# Class path separator, should be a ":" if using linux
+CPS = ;
+
+JAVAC = javac
+JAVA = java
+
+JOGLDIR = C:\lib\jogl\lib
+JOGLLIBS = jogl.all.jar gluegen-rt.jar nativewindow.all.jar
+JOGLLIBS_EXP = $(addprefix $(addsuffix \,$(JOGLDIR)),$(JOGLLIBS))
+
+# Build the Class Path
+space :=
+space +=
+JOGLLIBS_LIST = $(subst $(space),$(CPS),$(strip $(JOGLLIBS_EXP)))
+CLASSPATH = .$(CPS)$(JOGLLIBS_LIST)
+
+all: BasicRoom
+
+BasicRoom: BasicRoom.java Room.java
+	$(JAVAC) -cp $(CLASSPATH) $^

opengl/java/p3/Room.java

+/**
+ * Room: A simple room object that draws itself using OpenGL calls.
+ *
+ * Ben Porter, August 2010
+ */
+
+import javax.media.opengl.*;
+import javax.media.opengl.glu.*;
+import javax.media.opengl.awt.*;
+import com.jogamp.opengl.util.*;
+import com.jogamp.opengl.util.gl2.GLUT;
+
+/**
+ * Room encapsulates the geometry of a room and can draw itself 
+ * (using world coordinates).
+ *
+ * A room is 100x10x100 (x,y,z) with y being the vertical axis.
+ * The floor is centered at the world origin.
+ * There are some random objects placed in the room.
+ *
+ * Lighting, depth testing, and color material should be enabled
+ * in order to render the room correctly.
+ */
+public class Room {
+	static public final float dx = 100, dy = 10, dz = 100;
+
+	public void draw(GLAutoDrawable drawable){
+		GL2 gl = drawable.getGL().getGL2();
+		
+		float dCol[] = {0,0,0,1,1,1}; // {1,.5f,.5f,1,.2f,.2f};
+		
+		// Draw the floor
+		float coords[] = {-dx/2,-dz/2,dx/2,dz/2};
+		float dC[] = {1,1};
+		gl.glNormal3f(0,1,0);
+		drawCheckeredQuad(gl,coords,-2,0,dC,dCol);
+		
+		// Draw the ceiling
+		gl.glNormal3f(0,-1,0);
+		drawCheckeredQuad(gl,coords,1,dy,dC,dCol);
+
+		// Draw the four walls
+		// -x,x
+		for(int i=0;i<2;i++){
+			float c[] = {0,-dz/2,dy,dz/2};
+			if (i==0) gl.glNormal3f(1,0,0);
+			else gl.glNormal3f(-1,0,0);
+			drawCheckeredQuad(gl,c,0-3*i,-dx/2 + dx*i,dC,dCol);
+		}
+		// -z,z
+		for(int i=0;i<2;i++){
+			float c[] = {-dx/2,0,dx/2,dy};
+			if (i==0) gl.glNormal3f(0,0,1);
+			else gl.glNormal3f(0,0,-1);
+			drawCheckeredQuad(gl,c,2-3*i,-dz/2 + dz*i,dC,dCol);
+		}
+
+
+		// place some objects in the scene
+		// 1. a red teapot at (20,3,20)
+		GLUT glut = new GLUT();
+
+		gl.glColor3f(1,0,0);
+		gl.glPushMatrix();
+		gl.glTranslatef(20,3f,20);
+		glut.glutSolidTeapot(4);
+		gl.glPopMatrix();
+
+		// 2. A blue sphere at (-20,dy/2,-10)
+		gl.glColor3f(0,0,1);
+		gl.glPushMatrix();
+		gl.glTranslatef(-20,dy/2,-10);
+		glut.glutSolidSphere(3,32,32);
+		gl.glPopMatrix();
+
+		// 3. A yellow icosahedron at (-5,dy/2,15)
+		gl.glColor3f(1,1,0);
+		gl.glPushMatrix();
+		gl.glTranslatef(-5,dy/2,15);
+		gl.glScalef(5,5,5);
+		glut.glutSolidIcosahedron();
+		gl.glPopMatrix();
+	}
+
+	public void drawCheckeredQuad(GL2 gl, float[] coords, int plane, float d, float dC[], float col[]){
+		boolean white = false;
+
+		gl.glBegin(GL2.GL_QUADS);
+		for(float a=coords[0];a<coords[2];a+=dC[0]){
+			white = !white;
+			boolean black = !white;
+
+			for(float b=coords[1];b<coords[3];b+=dC[1]){
+				black = !black;
+				if (black)
+					gl.glColor3f(col[0],col[1],col[2]);
+				else
+					gl.glColor3f(col[3],col[4],col[5]);
+
+				// draw a quad from coord (a,b) to (a+dC[0],b+dC[1])
+				// (a,b) map to (x,y,z) based on plane
+				if (plane==-2 || plane==1){
+					gl.glVertex3f(a,d,b);
+					gl.glVertex3f(a+dC[0],d,b);
+					gl.glVertex3f(a+dC[0],d,b+dC[1]);
+					gl.glVertex3f(a,d,b+dC[1]);					
+				}
+				else if (plane==0 || plane==-3){
+					gl.glVertex3f(d,a,b);
+					gl.glVertex3f(d,a,b+dC[1]);
+					gl.glVertex3f(d,a+dC[0],b+dC[1]);
+					gl.glVertex3f(d,a+dC[0],b);
+				}
+				else if (plane==2 || plane==-1){
+					gl.glVertex3f(a,b,d);
+					gl.glVertex3f(a,b+dC[1],d);
+					gl.glVertex3f(a+dC[0],b+dC[1],d);
+					gl.glVertex3f(a+dC[0],b,d);
+				}
+			}
+		}		
+		gl.glEnd();		
+	}
+}