/** * @param year * @param month * @param day * @param hour * @param minute */ public SunInfo( int year, int month, int day, int hour, int minute ) { this.year = year; this.month = month; if ( month <= 0 || month > 12 ) { this.month = 1; } this.day = day; if ( day <= 0 || day > 32 ) { this.day = 1; } this.hour = hour; if ( hour < 0 || hour >= 24 ) { this.hour = 0; } this.minute = minute; if ( minute < 0 || minute >= 60 ) { this.minute = 0; } daysSinceVernalEquinox = getDaySinceVernalEquinox(); }
/** * Get the euclidean position of the sun. * * @param latitude * @return the euclidean position of the sun. */ public float[] getEucledianPosition( double latitude ) { double vPos = getVerticalSunposition( latitude ); double hPos = getHorizontalSunPosition(); return new float[] { (float) Math.sin( hPos ), (float) Math.sin( vPos ), (float) -Math.abs( Math.cos( hPos ) ) }; }
SunInfo pos = new SunInfo( cal ); light_position = pos.getEucledianPosition( 51.7 ); Vectors3f.scale( -1, light_position ); float[] ambientAndDiffuse = pos.calculateSunlight( 51.7 ); float intens = pos.calcSunlightIntensity( ambientAndDiffuse, 0.5f );
private void init( GL gl ) { float[] cc = JOGLUtils.convertColorFloats( request.getBackgroundColor() ); gl.glClearColor( cc[0], cc[1], cc[2], 0.0f ); SunInfo pos = request.getSceneParameters().getSunPosition(); float[] light_position = pos.getEucledianPosition( sceneLatitude ); Vectors3f.scale( -1, light_position ); float[] ambientAndDiffuse = pos.calculateSunlight( sceneLatitude ); // float intens = pos.calcSunlightIntensity( ambientAndDiffuse, 0.5f ); gl.glLightfv( GL.GL_LIGHT0, GL.GL_POSITION, new float[] { light_position[0], light_position[1], light_position[2], 0 }, 0 ); gl.glLightfv( GL.GL_LIGHT0, GL.GL_AMBIENT, new float[] { ambientAndDiffuse[0], ambientAndDiffuse[1], ambientAndDiffuse[2], 1 }, 0 ); gl.glMaterialfv( GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT, new float[] { ambientAndDiffuse[0], ambientAndDiffuse[1], ambientAndDiffuse[2], 1 }, 0 ); gl.glLightfv( GL.GL_LIGHT0, GL.GL_DIFFUSE, new float[] { ambientAndDiffuse[0], ambientAndDiffuse[1], ambientAndDiffuse[2], 1 }, 0 ); }
SunInfo pos = new SunInfo( cal );
/** * This method calculates the color of the sunlight for the current time and the given latitude. This method is * taken from deegree2, the values are undocumented. * * @param latitude * * @return a the color of the sunlight for the given latitude. */ public float[] calculateSunlight( double latitude ) { double vDir = getVerticalSunposition( latitude ); // rb: 7.25 is? float c = 7.25f * ( (float) Math.sin( vDir ) ); float[] color = new float[3]; color[0] = min( 1, ( ( BASE_LIGHT_INTENSITY + ( c / 16f ) + 0.05f ) * 0.6f ) ); color[1] = min( 1, ( ( BASE_LIGHT_INTENSITY + ( c / 18.5f ) + 0.05f ) * 0.6f ) ); color[2] = min( 1, ( ( BASE_LIGHT_INTENSITY + ( c / 17f ) + 0.05f ) * 0.55f ) ); return color; }
/** * calculates the solar altitude for given latitude, year, month, date, hour and minute * * @param latitude * latitude of the the viewers position * @return the solar altitude in radians for the given latitude */ public double getVerticalSunposition( double latitude ) { // Hour Angle (H), // Solar Declination (D), // Latitude (L) // solar altitude (A). // sin(A) = sin(D)*sin(L) + cos(D)*cos(L)*cos(H) double rad23_5 = 0.41015237421866745057706; // double days = getDaySinceVernalEquinox( year, month, date ); double sinD = Math.sin( rad23_5 ) * Math.sin( Math.toRadians( daysSinceVernalEquinox * 360.0 / 365.0 ) ); double cosD = Math.cos( Math.asin( sinD ) ); // the sun hour angle is zero when the object is on the meridian double h = getHorizontalSunPosition() - Math.toRadians( 180 ); double radL = Math.toRadians( latitude ); double sinA = sinD * Math.sin( radL ) + cosD * Math.cos( radL ) * Math.cos( h ); return Math.asin( sinA ); }
/** * Constructs a sunposition at the current (local) time */ public SunInfo() { GregorianCalendar calendar = new GregorianCalendar(); this.year = calendar.get( Calendar.YEAR ); this.month = calendar.get( Calendar.MONTH ) + 1; this.day = calendar.get( Calendar.DAY_OF_MONTH ); this.hour = calendar.get( Calendar.HOUR_OF_DAY ); this.minute = calendar.get( Calendar.MINUTE ); daysSinceVernalEquinox = getDaySinceVernalEquinox(); }