Å tegne:>Planetarium
Planetarium
Andre typiske scener med lignende struktur er roboter med kropp - overarm - underarm - hånd - fingre. Når vi tegner bruker vi modelltransformasjoner på en slik måte at vi kan tegne en forgrening og komme oss tilbake i strukturen ved hjelp av matrisestakken.
Vi ser at denne tankegangen skiller seg litt fra måten vi tenkte på når vi stablet klosser 4 klosser. Der gikk vi hele tiden tilbake til utgangspunktet når vi skulle plassere en ny kloss.
Tegnekoden
public void drawPlanets(GL gl) { GLU glu = new GLU(); // spheres GLUquadric glpQ=glu.gluNewQuadric(); /** the sun in origo */ /** Material of the sun*/ someMaterials.SetMaterialGoldenSun(gl); /** radius 4 */ glu.gluSphere(glpQ,4.0f,20,20); gl.glPushMatrix(); /** a planet */ /** This planets material */ someMaterials.setMaterialGreenPlanet(gl); /** in XY-plane */ gl.glRotatef(m_pV,1.0f,0.0f,0.0f); /** distance 7 */ gl.glTranslatef(0.0f,7.0f,0.0f); /** radius 1 */ glu.gluSphere(glpQ,1.0f,20,20); /** with its only moon */ /** This moons material */ someMaterials.setMaterialSilverMoon(gl); /** in YZ-pane (of planet) */ gl.glRotatef(m_mV,1.0f,0.0f,0.0f); /** distance 1.5 */ gl.glTranslatef(0.0f,1.5f,0.0f); /** radius 0.5 */ glu.gluSphere(glpQ,0.5f,20,20); gl.glPopMatrix(); gl.glPushMatrix(); /** another planet */ /** This planets material */ someMaterials.setMaterialObsidianPlanet(gl); /** in XZ-plane */ gl.glRotatef(m_pV2,0.0f,1.0f,0.0f); /** distance 10 */ gl.glTranslatef(10.0f,0.0f,0.0f); /** radius 1 */ glu.gluSphere(glpQ,1.0f,20,20); gl.glPushMatrix(); /** with its first moon */ /** These moons material */ someMaterials.setMaterialWhiteMoon(gl); /** in XZ-pane (of planet) */ gl.glRotatef(m_mV2,0.0f,1.0f,0.0f); /** distance 3 */ gl.glTranslatef(3.0f,0.0f,0.0f); /** radius 0.5 */ glu.gluSphere(glpQ,0.5f,20,20); gl.glPopMatrix(); gl.glPushMatrix(); /** and second moon */ /** in XY-pane (of planet) */ gl.glRotatef(m_mV2,0.0f,0.0f,1.0f); /** distance 2 */ gl.glTranslatef(2.0f,0.0f,0.0f); /** radius 0.5 */ glu.gluSphere(glpQ,0.5f,20,20); gl.glPopMatrix(); gl.glPopMatrix(); glu.gluDeleteQuadric(glpQ); // increment model movement m_Vz += 0.5f; if (m_Vz > 360.0f) m_Vz = 0.5f; m_Vx += 0.1f; if (m_Vx > 360.0f) m_Vx = 0.1f; m_pV += 0.1f; if (m_pV > 360.0f) m_pV = 0.1f; m_mV += 0.9f; if (m_mV > 360.0f) m_mV = 0.9f; m_pV2 += 0.2f; if (m_pV2 > 360.0f) m_pV2 = 0.2f; m_mV2 += 0.9f; if (m_mV2 > 360.0f) m_mV2 = 0.0f; }
Merk at du kan stoppe bevegelsen med høyre museknapp.