Skip to content

Commit

Permalink
add walls
Browse files Browse the repository at this point in the history
  • Loading branch information
alimg committed Jan 7, 2018
1 parent 3914c52 commit 2bcc1ca
Show file tree
Hide file tree
Showing 17 changed files with 779 additions and 309 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ buildscript {


dependencies {
testCompile 'junit:junit:4.12'

compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

Expand Down
21 changes: 17 additions & 4 deletions src/main/java/bilkent/cs565/paper/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class Main implements GLEventListener, KeyListener {
private final World world;
private Thread thread;
private AtomicBoolean running = new AtomicBoolean(true);
private Vec4 cam = new Vec4(0, -3, -40, 0);
private Vec4 cam = new Vec4(10, -5, -30, 0);
private Vec3 camV = new Vec3();
private Mat4x4 camProj = new Mat4x4();
private Vec3 camDir = new Vec3(0,0,1);
Expand All @@ -60,7 +60,9 @@ public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}

@Override
public void mouseExited(MouseEvent e) {}
public void mouseExited(MouseEvent e) {
camV = new Vec3();
}

@Override
public void mousePressed(MouseEvent e) {
Expand Down Expand Up @@ -127,6 +129,7 @@ private interface Buffer {
private FloatBuffer matBufferProj = GLBuffers.newDirectFloatBuffer(16);

private Program program;
private Program programLine;

private long start;

Expand Down Expand Up @@ -161,13 +164,15 @@ public void windowDestroyed(WindowEvent e) {
public void init(GLAutoDrawable drawable) {

GL3 gl = drawable.getGL().getGL3();
gl.getExtension("OES_standard_derivatives");

initBuffers(gl);

initProgram(gl);

gl.glEnable(GL_DEPTH_TEST);


world.initBuffers(gl);
world.initVertexArray(gl);
start = System.currentTimeMillis();
Expand Down Expand Up @@ -210,6 +215,14 @@ private void initProgram(GL3 gl) {
}
gl.glUniformBlockBinding(program.name, globalMatricesBI, Constants.Uniform.GLOBAL_MATRICES);


programLine = new Program(gl, getClass(), "gl3/shaders", "line.vert", "line.frag", "model");
globalMatricesBI = gl.glGetUniformBlockIndex(program.name, "GlobalMatrices");
if (globalMatricesBI == -1) {
System.err.println("block index 'GlobalMatrices' not found!");
}
gl.glUniformBlockBinding(programLine.name, globalMatricesBI, Constants.Uniform.GLOBAL_MATRICES);

checkError(gl, "initProgram");
}

Expand Down Expand Up @@ -244,8 +257,7 @@ public void display(GLAutoDrawable drawable) {
gl.glClearBufferfv(GL_COLOR, 0, clearColor.put(0, 0.66f).put(1, .66f).put(2, 0.66f).put(3, 1f));
gl.glClearBufferfv(GL_DEPTH, 0, clearDepth.put(0, 1f));

gl.glUseProgram(program.name);
world.draw(gl, program);
world.draw(gl, program, programLine);

gl.glBindVertexArray(0);
gl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Expand Down Expand Up @@ -276,6 +288,7 @@ public void dispose(GLAutoDrawable drawable) {
GL3 gl = drawable.getGL().getGL3();

gl.glDeleteProgram(program.name);
gl.glDeleteProgram(programLine.name);
gl.glDeleteVertexArrays(1, vertexArrayName);
gl.glDeleteBuffers(Buffer.MAX, bufferName);

Expand Down
44 changes: 31 additions & 13 deletions src/main/java/bilkent/cs565/paper/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.List;

import static com.jogamp.opengl.GL.*;
import static com.jogamp.opengl.GL.GL_FLOAT;
Expand All @@ -27,13 +26,14 @@
import static uno.gl.GlErrorKt.checkError;

public class World {
public static final float STEP_SIZE = 0.5f/120f;
public static final float STEP_SIZE = 1f/120f;
private final ArrayList<Wall> walls = new ArrayList<>();
private int elementCount;
private ShortBuffer surfaceBuffer;
private int frames;
private int framesD;

private interface Buffer {
public interface Buffer {
int VERTEX = 0;
int ELEMENT = 1;
int ELEMENT2 = 2;
Expand All @@ -42,7 +42,7 @@ private interface Buffer {
}

private final Paper paper;
private final Vec3 gravity = new Vec3(0, 0, -0.9);
private final Vec3 gravity = new Vec3(0, 0, -2.9);
private final Stepper stepper;
private long time;
private FloatBuffer vertexBuffer;
Expand All @@ -59,8 +59,8 @@ public World() {
//paper = Paper.createFlat(12, 12, 1.5f, 1.5f);
paper = Paper.createFromModel();
//paper = Paper.createFlat(8, 8, 6, 6);
List<Wall> walls = new ArrayList<>();
walls.add(new Wall(new Vec3(0, -50, 0), new Vec3(0, 1, 0)));
walls.add(new Wall(new Vec3(0, -40, 0), new Vec3(0, 1, 0), new Vec3(10, 0, 0)));
walls.add(new Wall(new Vec3(0, 0, -40), new Vec3(0, 0, 1), new Vec3(10, 0, 0)));
PaperPhysics paperP = new PaperPhysics(paper, walls, gravity);
stepper = new Stepper(paperP);

Expand Down Expand Up @@ -134,7 +134,6 @@ public void initBuffers(GL3 gl) {
}

public void initVertexArray(GL3 gl) {

gl.glGenVertexArrays(1, vertexArrayName);
gl.glBindVertexArray(vertexArrayName.get(0));
{
Expand All @@ -155,15 +154,18 @@ public void initVertexArray(GL3 gl) {
gl.glVertexAttribPointer(Constants.Attr.NORMAL, Vec3.length, GL_FLOAT, false, stride, offset);
}
gl.glBindBuffer(GL_ARRAY_BUFFER, 0);

gl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferName.get(Buffer.ELEMENT));
}
gl.glBindVertexArray(0);

for (Wall w: walls) {
w.initVertexArray(gl);
}

gl.glCullFace(GL_FRONT_AND_BACK);
checkError(gl, "initVao");
}

public void draw(GL3 gl, Program program) {
public void draw(GL3 gl, Program program, Program programLine) {
synchronized(stepper) {
for (Particle p : paper.getParticles())
{
Expand All @@ -183,23 +185,39 @@ public void draw(GL3 gl, Program program) {
vertexBuffer.put(p.id * 9 * 2 + 9 + 5, 1);
}
}

gl.glUseProgram(program.name);
gl.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.VERTEX));
gl.glBufferSubData(GL_ARRAY_BUFFER, 0, vertexBuffer.capacity() * Float.BYTES, vertexBuffer);
gl.glBindBuffer(GL_ARRAY_BUFFER, 0);


Mat4x4 model = new Mat4x4();
model.scale(0.50f).to(matBuffer);
model.to(matBuffer);
gl.glUniformMatrix4fv(program.get("model"), 1, false, matBuffer);


gl.glUseProgram(programLine.name);
gl.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.VERTEX));
gl.glBufferSubData(GL_ARRAY_BUFFER, 0, vertexBuffer.capacity() * Float.BYTES, vertexBuffer);
gl.glBindBuffer(GL_ARRAY_BUFFER, 0);
gl.glUniformMatrix4fv(program.get("model"), 1, false, matBuffer);

gl.glBindVertexArray(vertexArrayName.get(0));
gl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferName.get(Buffer.ELEMENT));
gl.glDrawElements(GL_LINES, elementBuffer.capacity(), GL_UNSIGNED_SHORT, 0);
gl.glUseProgram(program.name);
gl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferName.get(Buffer.ELEMENT2));
gl.glDrawElements(GL_TRIANGLES, surfaceBuffer.capacity(), GL_UNSIGNED_SHORT, 0);
gl.glBindVertexArray(0);


model = new Mat4x4();
model.to(matBuffer);
gl.glUniformMatrix4fv(program.get("model"), 1, false, matBuffer);
for (Wall wall: walls) {
wall.draw(gl);
}

gl.glBindVertexArray(0);
gl.glBindBuffer(GL_ARRAY_BUFFER, 0);
}

Expand Down
45 changes: 23 additions & 22 deletions src/main/java/bilkent/cs565/paper/model/Paper.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
package bilkent.cs565.paper.model;

import bilkent.cs565.paper.model.particle.Edge;
import bilkent.cs565.paper.model.particle.Force;
import bilkent.cs565.paper.model.particle.HingeForce;
import bilkent.cs565.paper.model.particle.Particle;
import bilkent.cs565.paper.model.particle.Spring;
import bilkent.cs565.paper.model.particle.Surface;
import bilkent.cs565.paper.model.particle.*;
import glm.vec._3.Vec3;
import org.smurn.jply.*;
import org.smurn.jply.Element;
import org.smurn.jply.ElementReader;
import org.smurn.jply.ElementType;
import org.smurn.jply.PlyReaderFile;
import org.smurn.jply.util.NormalMode;
import org.smurn.jply.util.NormalizingPlyReader;
import org.smurn.jply.util.TesselationMode;
import org.smurn.jply.util.TextureMode;

import java.io.IOException;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Paper {

private static final float MODEL_SCALE = 0.55f;
private final List<Force> forces;
private final List<Particle> particles;
private final List<Spring> springForces = new ArrayList<>();
Expand All @@ -30,30 +27,31 @@ public class Paper {
public Paper(ArrayList<Particle> particles, ArrayList<Force> forces) {
this.particles = particles;
this.forces = forces;
forces.forEach((f)-> {
forces.forEach((f) -> {
if (f instanceof Spring) {
springForces.add((Spring) f);
} else if (f instanceof Surface){
} else if (f instanceof Surface) {
surfaces.add((Surface) f);
}
});
}

public static Paper createFromModel() {
String modelFile = "model/plane3.ply";
//String modelFile = "model/plane2.ply";
//String modelFile = "model/basic.ply";

ArrayList<Particle> particles = new ArrayList<>();
ArrayList<Force> forces = new ArrayList<>();
HashMap<Edge, Particle> hingeEdges = new HashMap<>();
try {
NormalizingPlyReader ply = new NormalizingPlyReader(new PlyReaderFile(
ClassLoader.getSystemResourceAsStream("model/plane2.ply")),
ClassLoader.getSystemResourceAsStream(modelFile)),
TesselationMode.TRIANGLES,
NormalMode.ADD_NORMALS_CCW,
TextureMode.XY
);

int vertexCount = ply.getElementCount("vertex");
int triangleCount = ply.getElementCount("face");

ElementReader reader = ply.nextElementReader();
while (reader != null) {
ElementType type = reader.getElementType();
Expand All @@ -63,7 +61,7 @@ public static Paper createFromModel() {
Particle p = new Particle(particles.size());
p.pos = new Vec3(element.getDouble("x"),
element.getDouble("y"),
element.getDouble("z"));
element.getDouble("z")).times(MODEL_SCALE);
particles.add(p);
element = reader.readElement();
}
Expand Down Expand Up @@ -114,10 +112,10 @@ public static Paper createFlat(int n, int m, float X, float Y) {
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
Particle p = new Particle(particles.size());
p.pos = new Vec3(x*X/n, y*Y/m, Math.random()*0.11 + x*0.02*X/n+ 4);
p.pos = new Vec3(x * X / n, y * Y / m, Math.random() * 0.11 + x * 0.02 * X / n + 4);
p.norm = new Vec3(0, 0, 1);
p.dir = new Vec3(0, -1, 0);
p.vel = new Vec3(0.2f,0.1f,0);
p.vel = new Vec3(0.2f, 0.1f, 0);
particles.add(p);
}
}
Expand Down Expand Up @@ -180,10 +178,13 @@ public static Paper createFlat(int n, int m, float X, float Y) {
}

private static void addHinges(
final Surface f, final HashMap<Edge, Particle> hingeEdges, final ArrayList<Force> forces) {
for (int i = 0; i<3; i++) {
int j = (i+1) % 3;
int k = (i+2) % 3;
final Surface f, final HashMap<Edge, Particle> hingeEdges, final ArrayList<Force> forces) {
if (f.particles.length != 3) {
return;
}
for (int i = 0; i < 3; i++) {
int j = (i + 1) % 3;
int k = (i + 2) % 3;

final Edge edge = new Edge(f.particles[i], f.particles[j]);
final Particle h = hingeEdges.get(edge);
Expand Down
Loading

0 comments on commit 2bcc1ca

Please sign in to comment.