Skip to content

Commit

Permalink
1. add sky box background
Browse files Browse the repository at this point in the history
2.make sky box translation with user touch event
  • Loading branch information
AlbertSnow committed Jul 12, 2017
1 parent 1dfc7c4 commit 6f9324f
Show file tree
Hide file tree
Showing 16 changed files with 236 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.graphics.Color;
import android.opengl.GLES20;
import android.util.Log;

import com.example.albertsnow.myapplication.data.VertexArray;
import com.example.albertsnow.myapplication.programs.ParticleShaderProgram;
Expand Down Expand Up @@ -69,11 +68,6 @@ public void addParticle(Geometry.Point position, int color, Geometry.Vector dire
particles[currentOffset++] = particleStartTime;

vertexArray.updateBuffer(particles, particleOffset, TOTAL_COMPONENT_COUNT);

Log.i(TAG, "add particles, size: " + nextParticle);
Log.i(TAG, "add particles, x: " + position.x);
Log.i(TAG, "add particles, y: " + position.y);
Log.i(TAG, "add particles, z: " + position.z);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.example.albertsnow.myapplication.objects;

import android.opengl.GLES20;

import com.example.albertsnow.myapplication.data.VertexArray;
import com.example.albertsnow.myapplication.programs.SkyBoxShaderProgram;

import java.nio.ByteBuffer;

/**
* Created by albertsnow on 7/7/17.
*/

public class SkyBox {
private static final int POSITION_COMPONENT_COUNT = 3;
private final VertexArray vertexArray;
private final ByteBuffer indexArray;

public SkyBox() {
//create a unit cube.
vertexArray = new VertexArray(new float[] {
-1, 1, 1, // (0) Top-left near
1, 1, 1, // (1) Top-right near
-1, -1, 1, // (2) Bottom-left near
1, -1, 1, // (3) Bottom-right near
-1, 1, -1, // (4) Top-left far
1, 1, -1, // (5) Top-right far
-1, -1, -1, // (6) Bottom-left far
1, -1, -1, // (7) Bottom-right far
});

indexArray = ByteBuffer.allocate(6 * 6)
.put(new byte[] {
// Front
1, 3, 0,
0, 3, 2,

// Back
4, 6, 5,
5, 6, 7,

// Left
0, 2, 4,
4, 2, 6,

// Right
5, 7, 1,
1, 7, 3,

// Top
5, 1, 4,
4, 1, 0,

// Bottom
6, 2, 7,
7, 2, 3
});
indexArray.position(0);
}

public void bindData(SkyBoxShaderProgram skyBoxShaderProgram) {
vertexArray.setVertexAttribPointer(0,
skyBoxShaderProgram.getPositionAttributeLocation(),
POSITION_COMPONENT_COUNT, 0);
}

public void draw() {
GLES20.glDrawElements(GLES20.GL_TRIANGLES, 36, GLES20.GL_UNSIGNED_BYTE, indexArray);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;

import com.example.albertsnow.myapplication.view.ParticlesRenderer;

Expand All @@ -14,10 +16,41 @@ public class ParticleActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_particle);
final ParticlesRenderer renderer = new ParticlesRenderer();
surfaceView = new GLSurfaceView(this);
surfaceView.setEGLContextClientVersion(2);
surfaceView.setRenderer(new ParticlesRenderer());
surfaceView.setRenderer(renderer);
surfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
surfaceView.setOnTouchListener(new View.OnTouchListener() {
float previousX, previousY;

@Override
public boolean onTouch(View v, MotionEvent event) {
if (event != null) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
previousX = event.getX();
previousY = event.getY();
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
final float deltaX = event.getX() - previousX;
final float deltaY = event.getY() - previousY;

previousX = event.getX();
previousY = event.getY();

surfaceView.queueEvent(new Runnable() {
@Override
public void run() {
renderer.handleTouchDrag(
deltaX, deltaY
);
}
});
}
return true;
}
return false;
}
});

setContentView(surfaceView);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.albertsnow.myapplication.programs;

import android.opengl.GLES20;

import com.example.albertsnow.myapplication.MyApplication;
import com.example.albertsnow.myapplication.R;

/**
* Created by albertsnow on 7/7/17.
*/

public class SkyBoxShaderProgram extends ShaderProgram {

private final int uMatrixLocation;
private final int uTextureUnitLocation;
private final int aPositionLocation;


public SkyBoxShaderProgram() {
super(MyApplication.getApplication()
, R.raw.skybox_vertex_shader, R.raw.skybox_fragment_shader);

uMatrixLocation = GLES20.glGetUniformLocation(program, U_MATRIX);
uTextureUnitLocation = GLES20.glGetUniformLocation(program, U_TEXTURE_UNIT);
aPositionLocation = GLES20.glGetAttribLocation(program, A_POSITION);
}

public void setUniforms(float[] matrix, int textureId) {
GLES20.glUniformMatrix4fv(uMatrixLocation, 1, false, matrix, 0);

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_CUBE_MAP, textureId);
GLES20.glUniform1i(uTextureUnitLocation, 0);
}

public int getPositionAttributeLocation() {
return aPositionLocation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,26 @@ public static int loadCubeMap(Context context, int[] cubeResource) {
GLES20.glDeleteTextures(1, textureObjectIds, 0);
return 0;
}
}

GLES20.glBindTexture(GLES20.GL_TEXTURE_CUBE_MAP, textureObjectIds[0]);

GLES20.glTexParameteri(GLES20.GL_TEXTURE_CUBE_MAP, GLES20.GL_TEXTURE_MIN_FILTER,
GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_CUBE_MAP, GLES20.GL_TEXTURE_MAG_FILTER,
GLES20.GL_LINEAR);
GLES20.glBindTexture(GLES20.GL_TEXTURE_CUBE_MAP, textureObjectIds[0]);

GLUtils.texImage2D(GLES20.GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, cubeBitmaps[0], 0);
GLUtils.texImage2D(GLES20.GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, cubeBitmaps[1], 0);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_CUBE_MAP, GLES20.GL_TEXTURE_MIN_FILTER,
GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_CUBE_MAP, GLES20.GL_TEXTURE_MAG_FILTER,
GLES20.GL_LINEAR);

GLUtils.texImage2D(GLES20.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, cubeBitmaps[2], 0);
GLUtils.texImage2D(GLES20.GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, cubeBitmaps[3], 0);
GLUtils.texImage2D(GLES20.GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, cubeBitmaps[0], 0);
GLUtils.texImage2D(GLES20.GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, cubeBitmaps[1], 0);

GLUtils.texImage2D(GLES20.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, cubeBitmaps[4], 0);
GLUtils.texImage2D(GLES20.GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, cubeBitmaps[5], 0);
GLUtils.texImage2D(GLES20.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, cubeBitmaps[2], 0);
GLUtils.texImage2D(GLES20.GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, cubeBitmaps[3], 0);

GLUtils.texImage2D(GLES20.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, cubeBitmaps[4], 0);
GLUtils.texImage2D(GLES20.GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, cubeBitmaps[5], 0);

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);

}
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);

for (Bitmap bitmap : cubeBitmaps) {
bitmap.recycle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;

import com.example.albertsnow.myapplication.MyApplication;
import com.example.albertsnow.myapplication.R;
import com.example.albertsnow.myapplication.objects.ParticleShooter;
import com.example.albertsnow.myapplication.objects.ParticleSystem;
import com.example.albertsnow.myapplication.objects.SkyBox;
import com.example.albertsnow.myapplication.programs.ParticleShaderProgram;
import com.example.albertsnow.myapplication.programs.SkyBoxShaderProgram;
import com.example.albertsnow.myapplication.util.Geometry;
import com.example.albertsnow.myapplication.util.LoggerConfig;
import com.example.albertsnow.myapplication.util.MatrixHelper;
Expand All @@ -34,23 +37,35 @@ public class ParticlesRenderer implements GLSurfaceView.Renderer {
private ParticleShooter greenParticleShooter;
private ParticleShooter blueParticleShooter;

private SkyBoxShaderProgram skyBoxShaderProgram;
private SkyBox skyBox;
private int skyboxTexture;

private long globalStartTime;
private float angleVarianceInDegree = 5f;
private float speedVariance = 1f;
private int texture;
private int particleTexture;

private float xRotation, yRotation;


@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE);
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

particleShaderProgram = new ParticleShaderProgram();
particleSystem = new ParticleSystem(10000);
globalStartTime = System.nanoTime();
LoggerConfig.i(TAG, "Time is: " + globalStartTime);

skyBoxShaderProgram = new SkyBoxShaderProgram();
skyBox = new SkyBox();
skyboxTexture = TextureHelper.loadCubeMap(MyApplication.getApplication(),
new int[] { R.drawable.left, R.drawable.right,
R.drawable.bottom, R.drawable.top,
R.drawable.front, R.drawable.back});



final Geometry.Vector particleDirection = new Geometry.Vector(0f, 0.5f, 0f);

Expand Down Expand Up @@ -78,7 +93,7 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
speedVariance
);

texture = TextureHelper.loadTexture(R.drawable.particle_texture);
particleTexture = TextureHelper.loadTexture(R.drawable.particle_texture);
}

@Override
Expand All @@ -95,18 +110,55 @@ public void onSurfaceChanged(GL10 gl, int width, int height) {
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GLES20.GL_COLOR_BUFFER_BIT);
drawSkybox();
drawParticles();
}

private void drawParticles() {
float currentTime = (System.nanoTime() - globalStartTime) / 1000000000f;

redParticleShooter.addParticles(particleSystem, currentTime, 5);
greenParticleShooter.addParticles(particleSystem, currentTime, 5);
blueParticleShooter.addParticles(particleSystem, currentTime, 5);

Matrix.setIdentityM(viewMatrix, 0);
Matrix.rotateM(viewMatrix, 0, -yRotation, 1f, 0f, 0f);
Matrix.rotateM(viewMatrix, 0, -xRotation, 0f, 1f, 0f);
Matrix.translateM(viewMatrix, 0, 0f, -1.5f, -5f);
Matrix.multiplyMM(viewProjectionMatrix, 0, projectionMatrix, 0, viewMatrix, 0);
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE);

particleShaderProgram.useProgram();
particleShaderProgram.setUniform(viewProjectionMatrix, currentTime, texture);
particleShaderProgram.setUniform(viewProjectionMatrix, currentTime, particleTexture);
particleSystem.bindData(particleShaderProgram);
particleSystem.draw();

GLES20.glDisable(GLES20.GL_BLEND);
}

private void drawSkybox() {
Matrix.setIdentityM(viewMatrix, 0);
Matrix.rotateM(viewMatrix, 0, -yRotation, 1f, 0f, 0f);
Matrix.rotateM(viewMatrix, 0, -xRotation, 0f, 1f, 0f);
Matrix.multiplyMM(viewProjectionMatrix, 0, projectionMatrix, 0, viewMatrix, 0);

skyBoxShaderProgram.useProgram();
skyBoxShaderProgram.setUniforms(viewProjectionMatrix, skyboxTexture);
skyBox.bindData(skyBoxShaderProgram);
skyBox.draw();
}


public void handleTouchDrag(float deltaX, float deltaY) {
xRotation += deltaX / 16f;
yRotation += deltaY / 16f;

if (yRotation < -99) {
yRotation = -90;
} else if (yRotation > 90) {
yRotation = 90;
}
}

}
Binary file added app/src/main/res/drawable-nodpi/back.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-nodpi/bottom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-nodpi/front.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-nodpi/left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified app/src/main/res/drawable-nodpi/particle_texture.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-nodpi/right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-nodpi/top.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions app/src/main/res/raw/skybox_fragment_shader.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
precision mediump float;

uniform samplerCube u_TextureUnit;
varying vec3 v_Position;

void main()
{
gl_FragColor = textureCube(u_TextureUnit, v_Position);
}
12 changes: 12 additions & 0 deletions app/src/main/res/raw/skybox_vertex_shader.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
uniform mat4 u_Matrix;
attribute vec3 a_Position;
varying vec3 v_Position;

void main()
{
v_Position = a_Position;
v_Position.z = -v_Position.z;

gl_Position = u_Matrix * vec4(a_Position, 1.0);
gl_Position = gl_Position.xyww;
}

0 comments on commit 6f9324f

Please sign in to comment.