Author Topic: Skin Viewer 3D  (Read 1270 times)

Offline AeroShark333

  • long
  • ***
  • Posts: 149
    • View Profile
Skin Viewer 3D
« on: January 19, 2015, 11:01:58 am »
Hey guys,

I made an app called: Skin Viewer 3D.
It uses jPCT-AE for the 3D viewer and the live wallpaper! :)

This is how it looks like:


I don't think most of you people here would be interested in a Minecraft app but make sure to check it out if you are! :)

Some code I wanted to share (how I added rain and snow to the app):
(I don't know if this is neat code or not, but I hope it might be useful to some people. I am a beginning Java programmer ;p)

SkinActivity.java (incomplete!)
Code: [Select]
package com.aeroshark333.skinviewer;

import com.aeroshark333.skinviewer.weather.Rain;
import com.aeroshark333.skinviewer.weather.Snow;
import com.aeroshark333.skinviewer.weather.WeatherManager;

import com.threed.jpct.FrameBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView;

public class SkinActivity extends Activity {

class ViewerRenderer implements GLSurfaceView.Renderer {
private FrameBuffer frameBuffer;
private final WeatherManager wM;

public ViewerRenderer() {
this.wM = new WeatherManager();
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
if (frameBuffer != null) {
// don't reset.
return;
}
if (Build.VERSION.SDK_INT < 8) {
this.frameBuffer = new FrameBuffer(gl, width, height);
} else {
this.frameBuffer = new FrameBuffer(width, height);
}

//to add snow:
this.wM.setGroup(new Snow(this.frameBuffer
.getWidth(), this.frameBuffer.getHeight()));

//to add rain:
this.wM.setGroup(new Rain(this.frameBuffer
.getWidth(), this.frameBuffer.getHeight()));

//to reset:
this.wM.resetGroup();
}

@Override
public void onDrawFrame(GL10 gl) {
frameBuffer.clear();
wM.update(frameBuffer);
frameBuffer.display();
}

}
}

WeatherManager.java (complete)
Code: [Select]
package com.aeroshark333.skinviewer.weather;

import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Texture;

public class WeatherManager {
private Weather group;

public WeatherManager() {

}

public void update(final FrameBuffer frameBuffer) {
if (group == null) {
return;
}

final Texture t1 = group.getTexture();
final int sWidth = frameBuffer.getWidth();
final int sHeight = frameBuffer.getHeight();

if (group instanceof Snow) {
// snow
for (int[] particle : group.getParticles()) {
frameBuffer.blit(t1, 50, 50, particle[0], particle[1], 50, 50,
particle[2], particle[2], -1, false);
particle[1] = particle[1] + 1;
final double random = Math.random();
if (random < 0.25d) {
particle[0] = particle[0] + 1;
}
if (random > 0.75d) {
particle[0] = particle[0] - 1;
}
if (particle[0] > sWidth) {
particle[0] = sWidth / 2;
}
if (particle[0] < 0) {
particle[0] = sWidth / 2;
}
if (particle[1] > sHeight) {
particle[1] = 0;
}
}
} else {
// rain
for (int[] particle : group.getParticles()) {
frameBuffer.blit(t1, 50, 50, particle[0], particle[1], 50, 50,
(particle[2] / 3) + 1, particle[2] * 3, -1, false);
particle[1] = particle[1] + 6;
final double random = Math.random();
particle[0] = particle[0] - 1;
if (random < 0.50d) {
particle[0] = particle[0] - 1;
}
if (random < 0.125d) {
particle[0] = particle[0] - 1;
}
if (particle[0] < 0) {
particle[0] = sWidth - 2;
}
if (particle[1] > sHeight) {
particle[1] = 0;
}
}
}
}

public void setGroup(Weather weathergroup) {
this.group = weathergroup;
}

public boolean hasGroup() {
if (this.group != null) {
return true;
}
return false;

}

public void resetGroup() {
this.group = null;
}
}

Weather.java (complete)
Code: [Select]
package com.aeroshark333.skinviewer.weather;

import java.util.ArrayList;

import com.threed.jpct.Texture;

public interface Weather {
public ArrayList<int[]> getParticles();
public void addParticles(final int amount);
public Texture getTexture();
}

Snow.java (complete)
Code: [Select]
package com.aeroshark333.skinviewer.weather;

import java.util.ArrayList;

import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;

public class Snow implements Weather {
// int[]{posX,posY,size}
final private ArrayList<int[]> snowParts;
final int width, height;
final Texture texture;

public Snow(int w, int h) {
snowParts = new ArrayList<int[]>();
this.height = h;
this.width = w;
this.texture = TextureManager.getInstance().getTexture("snow");

addParticles((h * w / 9999) * 6);

}

@Override
public void addParticles(final int amount) {

for (int x = 0; amount > x; x++) {
final int posX = width - ((int)(Math.random() * (double)width));
final int posY = height - ((int)(Math.random() * (double)height));
final int size = (int) ((Math.random()*(double)height * (double) width / 225000d)+ 2.25d);
snowParts.add(new int[] { posX, posY, size });
}
}
@Override
public ArrayList<int[]> getParticles() {
return snowParts;
}

@Override
public Texture getTexture() {
return this.texture;
}

}

Rain.java (complete)
Code: [Select]
package com.aeroshark333.skinviewer.weather;

import java.util.ArrayList;

import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;

public class Rain implements Weather {
// int[]{posX,posY,size}
final private ArrayList<int[]> rainParts;
final int width, height;
final Texture texture;

public Rain(int w, int h) {
rainParts = new ArrayList<int[]>();
this.height = h;
this.width = w;
this.texture = TextureManager.getInstance().getTexture("rain");

addParticles((h * w / 9999) * 9);

}

@Override
public void addParticles(final int amount) {

for (int x = 0; amount > x; x++) {
final int posX = width - ((int) (Math.random() * (double) width));
final int posY = height - ((int) (Math.random() * (double) height));
final int size = (int) ((Math.random() * (double) height
* (double) width / 225000d) + 2.25d);
rainParts.add(new int[] { posX, posY, size });
}
}

@Override
public ArrayList<int[]> getParticles() {
return rainParts;
}

@Override
public Texture getTexture() {
return this.texture;
}

}

Feel free to use this code.
This is not 3D snow though!

Cheers,
Abiram
« Last Edit: January 19, 2015, 11:10:15 am by AeroShark333 »