Commit 177a5a6d authored by Matteo De Carlo's avatar Matteo De Carlo

Added moon!

- updated version to 1.2.0 (version 3)
- added moon
- added preference to choose celestial body
parent c56bd49c
......@@ -15,8 +15,8 @@ android {
applicationId "org.covolunablu.marswallpaper"
minSdkVersion 15
targetSdkVersion 27
versionCode 2
versionName "1.1.0"
versionCode 3
versionName "1.2.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
......
precision mediump float;
uniform sampler2D u_ColorTexture;
uniform sampler2D u_NormalTexture;
varying vec3 v_pos;
varying vec3 v_normal;
varying vec3 v_lightDirection;
varying vec2 v_out_textureCoordinate;
#define M_PI 3.1415926535897932384626433832795
void main() {
// Texture coordinate
vec3 v_color = texture2D(u_ColorTexture, v_out_textureCoordinate).rgb;
vec2 sampled_normal = texture2D( u_NormalTexture, v_out_textureCoordinate ).rg*2.0 - 1.0;
sampled_normal *= vec2(-1.0, 1.0);
// light
float orizon_closeness = 0.04/tan(v_normal.z+0.1 *M_PI/2.0);
vec3 v_normal = normalize(
vec3(sampled_normal, 0.0) + v_normal
);
float diff = max(0.00, dot(v_normal, v_lightDirection));
vec3 color = diff * v_color;// + vec3(0.1, 0.1, 0.1);
color.r = max(0.0, color.r - orizon_closeness);
color.g = max(0.0, color.g - orizon_closeness);
color.b = max(0.0, color.b - orizon_closeness);
gl_FragColor = vec4(color, 1.0);
}
\ No newline at end of file
uniform mat4 u_MVMatrix;
uniform mat4 u_MVPMatrix;
uniform mat4 u_NormalMatrix;
attribute vec4 v_Position;
attribute vec2 v_TextureCoordinate;
varying vec3 v_pos;
varying vec3 v_normal;
varying vec3 v_lightDirection;
varying vec2 v_out_textureCoordinate;
// radius of mars is 1.0 in this program
// distance(mars,sun) / radius(mars) = 73757.1913261543
// 73757.1913261543 = sqrt(2) * 104308.4202959946
#define DISTANCE_TIMES_SQRT_2 104308.4202959946
const vec3 lightPosition = vec3(-DISTANCE_TIMES_SQRT_2, 0.0, DISTANCE_TIMES_SQRT_2);
void main() {
vec4 v_position4 = u_MVMatrix * v_Position;
vec3 v_position3 = v_position4.xyz / v_position4.w;
vec3 normal = v_Position.xyz;
v_pos = v_Position.xyz / v_Position.w;
v_normal = (u_NormalMatrix * vec4(normal, 1.0)).xyz;
v_lightDirection = normalize(lightPosition - v_position3);
v_out_textureCoordinate = v_TextureCoordinate;
gl_Position = u_MVPMatrix * v_Position;
}
\ No newline at end of file
......@@ -39,7 +39,7 @@ public class MyGLRenderer implements GLSurfaceView.Renderer
private final MatrixStack mModelViewMatrixStack = new MatrixStack();
// Objects
private Planet mMars;
private Planet mPlanet;
private SkyMap mSkyMap;
// Rendering variables
......@@ -67,8 +67,12 @@ public class MyGLRenderer implements GLSurfaceView.Renderer
GLES20.glDepthFunc(GLES20.GL_LEQUAL);
GLES20.glDepthMask(true);
// initialize mars
mMars = Planet.GenerateMars(this.mContext);
// initialize planet
String planetTypeString = PreferenceManager
.getDefaultSharedPreferences(mContext)
.getString("planet", "MARS");
Planet.PlanetEnum planetType = Planet.PlanetEnumFromString(planetTypeString);
mPlanet = Planet.GeneratePlanet(this.mContext, planetType);
//mSkyMap = SkyMap.GenerateSkyMap(this.mContext); delayed load
}
......@@ -86,6 +90,9 @@ public class MyGLRenderer implements GLSurfaceView.Renderer
@Override
public void onDrawFrame(GL10 gl)
{
String planetType = PreferenceManager
.getDefaultSharedPreferences(mContext)
.getString("planet", "MARS");
String skymap_res = PreferenceManager
.getDefaultSharedPreferences(mContext)
.getString("starmap_res", "2");
......@@ -99,6 +106,8 @@ public class MyGLRenderer implements GLSurfaceView.Renderer
GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
}
// Set planet type (automatic check if change are necessary inside)
mPlanet.setPlanetType(mContext, planetType);
// planet rotation
long time = getTimePassed();
......@@ -129,13 +138,13 @@ public class MyGLRenderer implements GLSurfaceView.Renderer
// Draw shape
//mTriangle.draw(mModelViewMatrix, mMVPMatrix, mNormalMatrix);
//mSquare.draw(mModelViewMatrix, mMVPMatrix, mNormalMatrix);
mMars.draw(mModelViewMatrix, mMVPMatrix, mNormalMatrix);
mPlanet.draw(mModelViewMatrix, mMVPMatrix, mNormalMatrix);
mModelViewMatrixStack.glPopMatrix();
// SKYBOX
if (render_sky) {
if (mSkyMap == null) {
mSkyMap = SkyMap.GenerateSkyMap(this.mContext, skymap_res);
mSkyMap = SkyMap.GenerateSkyMap(mContext, skymap_res);
} else {
mSkyMap.setSkymapRes(mContext, skymap_res);
}
......
......@@ -111,12 +111,12 @@ class DonationsActivity : FragmentActivity() {
*/
private const val FLATTR_PROJECT_URL = "https://git.covolunablu.org/portaloffreedom/MarsWallpaper"
// FLATTR_URL without http:// !
private const val FLATTR_URL = "flattr.com/thing/712895/dschuermannandroid-donations-lib-on-GitHub"
private const val FLATTR_URL = "flattr.com/@portaloffreedom"
/**
* Bitcoin
*/
private const val BITCOIN_ADDRESS = "12Y6zbBYoRxf8kBrjau3WedjtzvcACvPMk"
private const val BITCOIN_ADDRESS = ""
}
}
\ No newline at end of file
......@@ -15,7 +15,7 @@ import java.nio.FloatBuffer;
*/
public class Planet extends RenderableObject {
private static final String TAG = "Mars";
private static final String TAG = "Planet";
protected final FloatBuffer textCoordBuffer;
private int mMVMatrixHandle;
......@@ -26,8 +26,92 @@ public class Planet extends RenderableObject {
private int mColorTextureDataHandle;
private int mNormalTextureDataHandle;
public enum PlanetEnum {
MOON,
MARS,
MERCURY,
JUPITER,
VENUS,
SATURN,
EARTH,
}
public static PlanetEnum PlanetEnumFromString(String name)
{
switch (name) {
case "MOON": return PlanetEnum.MOON;
case "MARS": return PlanetEnum.MARS;
case "MERCURY": return PlanetEnum.MERCURY;
case "JUPITER": return PlanetEnum.JUPITER;
case "VENUS": return PlanetEnum.VENUS;
case "SATURN": return PlanetEnum.SATURN;
case "EARTH": return PlanetEnum.EARTH;
default:
throw new RuntimeException("Unrecognized String for PlanetEnum");
}
}
private PlanetEnum planetType;
public void setPlanetType(Context context, String planetType)
{
setPlanetType(context, PlanetEnumFromString(planetType));
}
public void setPlanetType(Context context, PlanetEnum planetType)
{
if (this.planetType == planetType) {
return;
}
switch (planetType) {
case MOON:
this.vertexShaderAsset = MoonAssets.VERTEX_SHADER_ASSET;
this.fragmentShaderAsset = MoonAssets.FRAGMENT_SHADER_ASSET;
this.colorTextureAsset = MoonAssets.COLOR_TEXTURE_ASSET;
this.normalTextureAsset = MoonAssets.NORMAL_TEXTURE_ASSET;
break;
case MARS:
this.vertexShaderAsset = MarsAssets.VERTEX_SHADER_ASSET;
this.fragmentShaderAsset = MarsAssets.FRAGMENT_SHADER_ASSET;
this.colorTextureAsset = MarsAssets.COLOR_TEXTURE_ASSET;
this.normalTextureAsset = MarsAssets.NORMAL_TEXTURE_ASSET;
break;
case MERCURY:
case JUPITER:
case VENUS:
case SATURN:
case EARTH:
default:
throw new RuntimeException("Planet not implemented yet");
}
this.planetType = planetType;
initShaderProgram(context);
mColorTextureDataHandle = loadColorTexture(context);
mNormalTextureDataHandle = loadNormalTexture(context);
}
public static Planet GenerateMars(final Context context) {
private class MarsAssets {
private static final String VERTEX_SHADER_ASSET = "shaders/mars.vert";
private static final String FRAGMENT_SHADER_ASSET = "shaders/mars.frag";
private static final int COLOR_TEXTURE_ASSET = R.mipmap.mars_1k_color;
private static final int NORMAL_TEXTURE_ASSET = R.mipmap.mars_1k_normal;
}
private class MoonAssets {
private static final String VERTEX_SHADER_ASSET = "shaders/moon.vert";
private static final String FRAGMENT_SHADER_ASSET = "shaders/moon.frag";
private static final int COLOR_TEXTURE_ASSET = R.mipmap.moonmap2k;
private static final int NORMAL_TEXTURE_ASSET = R.mipmap.moonnormal2k;
}
private String vertexShaderAsset;
private String fragmentShaderAsset;
private int colorTextureAsset;
private int normalTextureAsset;
public static Planet GeneratePlanet(final Context context, PlanetEnum planetEnum) {
float phi = (float) ((1.0d + Math.sqrt(5.0f)) / 2.0d); //1.618
float du = (float) (1.0d / Math.sqrt(phi * phi + 1.0d)); //0.526
float dv = phi * du; // 0.85065
......@@ -76,20 +160,58 @@ public class Planet extends RenderableObject {
vertexList.subdivide(3);
vertexList.fixTextureCoordinates();
return new Planet(
context,
vertexList.generateVertexData(),
vertexList.generateTextCoordData(),
vertexList.generateIndexesData()
);
switch (planetEnum) {
case MOON:
return new Planet(
context,
vertexList.generateVertexData(),
vertexList.generateTextCoordData(),
vertexList.generateIndexesData(),
PlanetEnum.MOON,
MoonAssets.VERTEX_SHADER_ASSET,
MoonAssets.FRAGMENT_SHADER_ASSET,
MoonAssets.COLOR_TEXTURE_ASSET,
MoonAssets.NORMAL_TEXTURE_ASSET
);
case MARS:
return new Planet(
context,
vertexList.generateVertexData(),
vertexList.generateTextCoordData(),
vertexList.generateIndexesData(),
PlanetEnum.MARS,
MarsAssets.VERTEX_SHADER_ASSET,
MarsAssets.FRAGMENT_SHADER_ASSET,
MarsAssets.COLOR_TEXTURE_ASSET,
MarsAssets.NORMAL_TEXTURE_ASSET
);
case MERCURY:
case JUPITER:
case VENUS:
case SATURN:
case EARTH:
default:
throw new RuntimeException("Planet not implemented yet");
}
}
private Planet(final Context context,
float[] vertexArray,
float[] textCoordArray,
short[] indexArray)
short[] indexArray,
final PlanetEnum planetType,
final String vertexShaderAsset,
final String fragmentShaderAsset,
final int colorTextureAsset,
final int normalTextureAsset)
{
super(context, vertexArray, indexArray);
super(vertexArray, indexArray);
this.planetType = planetType;
this.vertexShaderAsset = vertexShaderAsset;
this.fragmentShaderAsset = fragmentShaderAsset;
this.colorTextureAsset = colorTextureAsset;
this.normalTextureAsset = normalTextureAsset;
initShaderProgram(context);
// initialize uv coordinates byte buffer
ByteBuffer bb = ByteBuffer.allocateDirect(
......@@ -106,20 +228,31 @@ public class Planet extends RenderableObject {
// set the buffer to read the first coordinate
textCoordBuffer.position(0);
mColorTextureDataHandle = MyGLRenderer.loadTexture(context, R.mipmap.mars_1k_color);
mNormalTextureDataHandle = MyGLRenderer.loadTexture(context, R.mipmap.mars_1k_normal);
mColorTextureDataHandle = loadColorTexture(context);
mNormalTextureDataHandle = loadNormalTexture(context);
}
@Override
String GetVertexShaderCode(final Context context) {
return getShaderCodeFromAsset(context, "shaders/mars.vert");
return getShaderCodeFromAsset(context, vertexShaderAsset);
}
@Override
String GetFragmentShaderCode(final Context context) {
return getShaderCodeFromAsset(context, "shaders/mars.frag");
return getShaderCodeFromAsset(context, fragmentShaderAsset);
}
private int loadColorTexture(final Context context)
{
return MyGLRenderer.loadTexture(context, colorTextureAsset);
}
private int loadNormalTexture(final Context context)
{
return MyGLRenderer.loadTexture(context, normalTextureAsset);
}
@Override
protected void populateHandles() {
// attributes
......@@ -133,8 +266,8 @@ public class Planet extends RenderableObject {
mNormalTextureUniformHandle = GLES20.glGetUniformLocation(mProgram, "u_NormalTexture");
}
static final int COORDS_PER_TEXTURE_COORD = 2;
protected final int textCoordStride = COORDS_PER_TEXTURE_COORD * 4; // 4 bytes per float
private static final int COORDS_PER_TEXTURE_COORD = 2;
private final int textCoordStride = COORDS_PER_TEXTURE_COORD * 4; // 4 bytes per float
@Override
public void draw(float[] MVMatrix, float[] MVPMatrix, float[] NormalMatrix) {
......
......@@ -22,7 +22,9 @@ abstract public class RenderableObject {
protected final FloatBuffer vertexBuffer;
protected final ShortBuffer drawListBuffer;
protected final int mProgram;
protected int mVertexShader = -1;
protected int mFragmentShader = -1;
protected int mProgram = -1;
String getShaderCodeFromAsset(final Context context, String assetName)
{
......@@ -40,12 +42,13 @@ abstract public class RenderableObject {
// Set color with red, green, blue and alpha values
float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };
public RenderableObject(final Context context, float[] vertexCoordinates) {
this(context, vertexCoordinates, null);
public RenderableObject(float[] vertexCoordinates)
{
this(vertexCoordinates, null);
}
public RenderableObject(final Context context, float[] vertexCoordinates, short[] drawOrder) {
public RenderableObject(float[] vertexCoordinates, short[] drawOrder)
{
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(
// (number of coordinates values * 4 bytes per float)
......@@ -77,15 +80,29 @@ abstract public class RenderableObject {
drawListBuffer = null;
vertexCount = vertexCoordinates.length / COORDS_PER_VERTEX;
}
}
protected void initShaderProgram(final Context context)
{
if (mVertexShader < 0) {
GLES20.glDeleteShader(mVertexShader);
}
if (mFragmentShader < 0) {
GLES20.glDeleteShader(mFragmentShader);
}
int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, GetVertexShaderCode(context));
int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, GetFragmentShaderCode(context));
mVertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, GetVertexShaderCode(context));
mFragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, GetFragmentShaderCode(context));
if (mProgram > 0) {
GLES20.glDeleteProgram(mProgram);
}
// create empty OpenGL ES Program
mProgram = GLES20.glCreateProgram();
if (mProgram != 0) {
GLES20.glAttachShader(mProgram, vertexShader);
GLES20.glAttachShader(mProgram, fragmentShader);
GLES20.glAttachShader(mProgram, mVertexShader);
GLES20.glAttachShader(mProgram, mFragmentShader);
GLES20.glLinkProgram(mProgram);
int[] linkStatus = new int[1];
......
......@@ -116,7 +116,8 @@ public class SkyMap extends RenderableObject
String skyMapResString
)
{
super(context, vertexArray, indexArray);
super(vertexArray, indexArray);
initShaderProgram(context);
// initialize uv coordinates byte buffer
ByteBuffer bb = ByteBuffer.allocateDirect(
......
......@@ -16,7 +16,8 @@ public class Triangle extends RenderableObject {
};
public Triangle(final Context context) {
super(context, TriangleCoords);
super(TriangleCoords);
initShaderProgram(context);
}
@Override
......
......@@ -14,4 +14,22 @@
<item>3k (18MB ram)</item>
<item>4k (32MB ram)</item>
</string-array>
<string-array name="planet_names">
<item>Moon</item>
<item>Mars</item>
<!--<item>Mercury</item>-->
<!--<item>Jupiter</item>-->
<!--<item>Venus</item>-->
<!--<item>Saturn</item>-->
<!--<item>Earth</item>-->
</string-array>
<string-array name="planets">
<item>MOON</item>
<item>MARS</item>
<!--<item>MERCURY</item>-->
<!--<item>JUPITER</item>-->
<!--<item>VENUS</item>-->
<!--<item>SATURN</item>-->
<!--<item>EARTH</item>-->
</string-array>
</resources>
\ No newline at end of file
......@@ -16,6 +16,8 @@
<string name="third_party_licenses">Third Party Licenses</string>
<string name="donate">Donate</string>
<string name="donation_paypal_item">Donation</string>
<string name="pref_planet">Planet</string>
<string name="pref_planet_summary">Choose the celestial body you prefer (Mars is the default)</string>
<string-array name="donation_google_catalog_values">
<item>1 Euro</item>
<item>2 Euros</item>
......
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Wallpaper Settings">
<ListPreference
android:title="@string/pref_planet"
android:summary="@string/pref_planet_summary"
android:key="planet"
android:entries="@array/planet_names"
android:entryValues="@array/planets"
android:defaultValue="MARS" />
<CheckBoxPreference
android:defaultValue="true"
android:key="offset"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment