package classwork; import javax.media.opengl.*; import jocode.*; /** * GLART_2_orbit.java * * Three cubes simulate a very basic solar system, with a rotating sun * and orbiting earth and mars. */ public class GLART_2_orbit extends JOApp { float rotation = 0f; int marbleTextureHandle; /** * Main function just creates and runs the application. */ public static void main(String args[]) { GLART_2_orbit app = new GLART_2_orbit(); app.run(); } /** * Initialize the scene. Called by JOApp.run() */ public void setup() { // Create sphere texture marbleTextureHandle = makeTexture("images/marble.jpg"); // enable texture (or call JOApp.activateTexture(handle)) gl.glBindTexture(GL.GL_TEXTURE_2D, marbleTextureHandle); // enable texture rendering gl.glEnable(GL.GL_TEXTURE_2D); // enable depth test gl.glEnable(GL.GL_DEPTH_TEST); } /** * Render the scene. */ public void draw() { rotation += .5f; // Clear screen and depth buffer gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Select The Modelview Matrix (controls model orientation) gl.glMatrixMode(GL.GL_MODELVIEW); // Reset the Modelview matrix // this resets the coordinate system to center of screen gl.glLoadIdentity(); // Where is the 'eye' glu.gluLookAt( 0f, 1f, 5f, // eye position 0f, 0f, 0f, // target to look at 0f, 1f, 0f); // which way is up // draw crosshairs drawOrigin(); // draw large yellow sphere at center, turning slowly on its axis gl.glColor3f(1f,1f,0f); gl.glPushMatrix(); { gl.glRotatef(rotation/5f, 0, 1, 0); gl.glScalef(.6f, .6f, .6f); renderSphere(); } gl.glPopMatrix(); // draw orbiting green sphere // ?? what happens if you don't use PushMatrix/PopMatrix here? gl.glPushMatrix(); { // rotate coord system (so earth orbits sun) gl.glRotatef(rotation * .5f, 0, 1, 0); // shift coordinate system right gl.glTranslatef(2, 0, 0); // rotate coord system (to spin earth) gl.glRotatef(rotation * .3f, 0, 1, 0); // shrink it down gl.glScalef(.3f, .3f, .3f); // draw a green sphere gl.glColor3f(.3f,.9f,0); renderSphere(); } gl.glPopMatrix(); // draw orbiting red sphere gl.glPushMatrix(); { // rotate (so mars orbits sun) gl.glRotatef(rotation, 0, 1, 0); // shift coordinate system right gl.glTranslatef(3, 0, 0); // rotate coord system (to spin mars) gl.glRotatef(rotation * 3f, 0, 1, 0); // shrink gl.glScalef(.2f, .2f, .2f); // draw a cube gl.glColor3f(.9f,.3f,0f); renderSphere(); } gl.glPopMatrix(); } /** * Draw crosshairs at 0,0,0 */ public void drawOrigin() { gl.glColor3f(.5f, .5f, .5f); gl.glLineWidth(1); gl.glBegin(GL.GL_LINES); gl.glVertex3f( -1f, 0f, 0f); // left gl.glVertex3f( 1f, 0f, 0f); // right gl.glVertex3f( 0f, -1f, 0f); // bottom gl.glVertex3f( 0f, 1f, 0f); // top gl.glVertex3f( 0f, 0f, -1f); // far gl.glVertex3f( 0f, 0f, 1f); // near gl.glEnd(); gl.glBegin(GL.GL_LINE_STRIP); gl.glVertex3f( -1f, 0f, 1f); // left near gl.glVertex3f( 1f, 0f, 1f); // rite near gl.glVertex3f( 1f, 0f, -1f); // rite far gl.glVertex3f( -1f, 0f, -1f); // left far gl.glVertex3f( -1f, 0f, 1f); // left near (close) gl.glEnd(); } }