Skip to content

Commit

Permalink
Still stumbling...
Browse files Browse the repository at this point in the history
Need to quit reinventing the wheel here. I think a back port of the renderscript Matrix?f classes may be more suitable.
  • Loading branch information
bryanl committed Feb 6, 2012
1 parent 402ba90 commit 13fde07
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 18 deletions.
Binary file modified art/basic_texture.acorn
Binary file not shown.
11 changes: 11 additions & 0 deletions assets/shaders/billboard.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
precision highp float;
varying vec2 v_texcoord;

uniform sampler2D n_sampler;

void main() {
vec4 v_tex = texture2D(n_sampler, v_texcoord);
if(v_tex.x > .5 && v_tex.y > .5 && v_tex.z > .5)
discard;
gl_FragColor = v_tex;
}
17 changes: 17 additions & 0 deletions assets/shaders/billboard.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
precision highp float;

attribute vec3 v_pos;
attribute vec2 v_tex;

varying vec2 v_texcoord;

uniform mat4 t_modelview_projection;

uniform vec3 v_up, v_right;

void main() {
vec3 v_corner = v_pos + (v_tex.x * 2.0 - 1.0) * v_right + (1.0 - v_tex.y * 2.0) * v_up;
gl_Position = t_modelview_projection * vec4(v_corner, 1.0);

v_texcoord = v_tex;
}
File renamed without changes.
File renamed without changes.
Binary file modified res/raw/basic_texture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/com/osesm/randy/framework/FPSCounter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public FPSCounter(Simulation simulation) {
public void logFrame() {
frames++;
if (System.nanoTime() - startTime >= 1000000000) {
simulation.setStatus("frames per second: " + frames);
simulation.setStatus("fps: " + frames);
frames = 0;
startTime = System.nanoTime();
}
Expand Down
3 changes: 1 addition & 2 deletions src/com/osesm/randy/framework/ParametricSurface.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import com.osesm.randy.framework.math.Vector2;
import com.osesm.randy.framework.math.Vector3;
import com.osesm.randy.lab.Shape;

public abstract class ParametricSurface implements Surface {

Expand Down Expand Up @@ -177,6 +176,6 @@ Vector2 computeDomain(float x, float y) {

public abstract Vector3 evaluate(Vector2 computeDomain);

public abstract Shape toShape(Simulation simulation);
public abstract WorldObject toShape(Simulation simulation);

}
3 changes: 1 addition & 2 deletions src/com/osesm/randy/framework/Sphere.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import com.osesm.randy.framework.math.Vector2;
import com.osesm.randy.framework.math.Vector3;
import com.osesm.randy.lab.Shape;

public class Sphere extends ParametricSurface {

Expand Down Expand Up @@ -33,7 +32,7 @@ public Vector3 evaluate(Vector2 computeDomain) {
}

@Override
public Shape toShape(Simulation simulation) {
public WorldObject toShape(Simulation simulation) {
// TODO Auto-generated method stub
return null;
}
Expand Down
15 changes: 15 additions & 0 deletions src/com/osesm/randy/framework/WorldObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public static long getNextObjectId() {

private String vertexShader;
private String fragmentShader;
private long id;

public WorldObject() {
this.id = getNextObjectId();
}

public void setMesh(Mesh mesh) {
this.mesh = mesh;
Expand All @@ -45,6 +50,10 @@ public void setVertexShader(String vertexShader) {
this.vertexShader = vertexShader;
}

/**
*
* @return file name of
*/
public String getFragmentShader() {
return fragmentShader;
}
Expand All @@ -53,7 +62,13 @@ public void setFragmentShader(String fragmentShader) {
this.fragmentShader = fragmentShader;
}

public long getId() {
return id;
}

public abstract void update();

public abstract void draw();


}
6 changes: 1 addition & 5 deletions src/com/osesm/randy/framework/gl/Mesh.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,7 @@ public void prepare(Matrix4 viewProjectionMatrix) {
mMVPMatrix = viewProjectionMatrix.multiplyByMatrix(modelMatrix);
}

public void setModelMatrix(Matrix4 matrix) {
modelMatrix = matrix;
}

public Matrix4 getModelMatrix() {
public Matrix4 modelMatrix() {
return modelMatrix;
}

Expand Down
27 changes: 26 additions & 1 deletion src/com/osesm/randy/framework/math/Matrix4.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,37 @@ public class Matrix4 {
private final float tmp[] = new float[16];

public Matrix4() {

identity();
}

public Matrix4(float[] values) {
this.set(values);
}

public Matrix4 identity() {
this.values[0] = 1;
this.values[1] = 0;
this.values[2] = 0;
this.values[3] = 0;

this.values[4] = 0;
this.values[5] = 1;
this.values[6] = 0;
this.values[7] = 0;

this.values[8] = 0;
this.values[9] = 0;
this.values[10] = 1;
this.values[11] = 0;

this.values[12] = 0;
this.values[13] = 0;
this.values[14] = 0;
this.values[15] = 1;

return this;
}


public void set(float[] values) {
this.values[M00] = values[M00];
Expand Down
10 changes: 3 additions & 7 deletions src/com/osesm/randy/lab/Shape.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@
public class Shape extends WorldObject {

private float angle;
private long id;

public Shape(Mesh mesh, String vertexShader, String fragmentShader) {
super();

setFragmentShader(fragmentShader);
setVertexShader(vertexShader);

setMesh(mesh);
this.id = getNextObjectId();
}

public long getId() {
return id;
}

public void setAngle(float angle) {
Expand All @@ -37,7 +33,7 @@ public void setMesh(Mesh mesh) {

@Override
public void update() {
getMesh().getModelMatrix().rotate(angle, 0, 0, 1.0f);
getMesh().modelMatrix().rotate(angle, 0, 0, 1.0f);
getMesh().prepare(getProjectionMatrix());
}

Expand Down
63 changes: 63 additions & 0 deletions src/com/osesm/randy/lab/VBODemo.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.osesm.randy.lab;

import java.util.Arrays;

import android.opengl.GLES20;
import android.opengl.Matrix;

import com.osesm.randy.framework.FPSCounter;
import com.osesm.randy.framework.Scene;
import com.osesm.randy.framework.Simulation;
import com.osesm.randy.framework.WorldObject;
import com.osesm.randy.framework.gl.Camera;
import com.osesm.randy.framework.gl.Mesh;
import com.osesm.randy.framework.gl.Texture;
import com.osesm.randy.framework.math.Matrix4;

public class VBODemo extends Simulation {

Expand All @@ -21,6 +26,8 @@ class VBOScene extends Scene {
private FPSCounter fpscounter;
private WorldObject shape;
private Camera camera;
private Texture texture;
private WorldObject shape2;

public VBOScene(Simulation simulation) {
super(simulation);
Expand All @@ -45,6 +52,46 @@ public void present(float deltaTime) {
shape.update();
shape.draw();

// draw a billboard
{

shape2.setProjectionMatrix(camera.getViewProjectionMatrix());
shape2.update();
shape2.draw();

// texture.bind();
//
// // set the up direction
// float up_x = 0, up_y = 1, up_z = 0;
//
// // get the vector from the model view
// float right_x = modelview[0];
// float right_y = modelview[4];
// float right_z = modelview[8];
//
// right_y = 0;
// float right_len = FloatMath.sqrt(right_x * right_x + right_z
// * right_z);
// right_x /= right_len;
// right_z /= right_len;
//
// // shade
//
// // show the texture
// GLES20.glVertexAttribPointer(0, 3, GLES20.GL_FLOAT, false, 5
// * 4,
// p_bboard_vertices_buff.position(2));
// GLES20.glVertexAttribPointer(1, 2, GLES20.GL_FLOAT, false, 5
// * 4,
// p_bboard_vertices_buff.position(0));
// GLES20.glEnableVertexAttribArray(0);
// GLES20.glEnableVertexAttribArray(1);
// GLES20.glDrawElements(GLES20.GL_TRIANGLES, billboard_count *
// 6,
// GLES20.GL_UNSIGNED_INT, p_bboard_indices_buff);

}

fpscounter.logFrame();
}

Expand All @@ -69,11 +116,27 @@ private void initTriangle() {
short triangleIndices[] = { 0, 1, 2 };
float triangleCoords[] = { -0.7f, -0.25f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.3f, -0.25f, 0.25f, 0.0f, 0.0f, 1.0f, 1.0f, -0.2f, 0.559016994f, 0.2f, 1.0f, 0.0f, 0.0f, 1.0f };

float triangle1Coords[] = { -0.5f, -0.25f, 0.5f, 0f, 0f, 0.5f, -0.25f, 0.5f, 1f, 0f, 0.0f, 0.559016994f, 0.5f, 0.5f, 1f };

Mesh mesh = new Mesh(simulation, 21, 6, true, false, false);
mesh.setVertices(triangleCoords, 0, triangleCoords.length);
mesh.setIndices(triangleIndices, 0, triangleIndices.length);
shape = new Shape(mesh, "simple.vert", "simple.frag");

texture = new Texture(simulation, R.raw.basic_texture);
mesh = new Mesh(simulation, 15, 6, false, true, false);
mesh.setVertices(triangle1Coords, 0, triangle1Coords.length);
mesh.setIndices(triangleIndices, 0, triangleIndices.length);
mesh.setTexture(texture);

simulation.debug("translate: " + Arrays.toString(mesh.modelMatrix().values()));
float[] values = mesh.modelMatrix().values();
Matrix.translateM(values, 0, 0, 1, 0f);
simulation.debug("translate: " + Arrays.toString(values));

// shape2 = new Shape(mesh, "billboard.vert", "billboard.frag");
shape2 = new Shape(mesh, "texture.vert", "texture.frag");

}

}
Expand Down

0 comments on commit 13fde07

Please sign in to comment.