Roi Atalla avatar Roi Atalla committed 543753d

I am so impressed with myself that I'm speechless. Sproingie....run Test. WASD to move, Space to jump, and mouse to aim.

Comments (0)

Files changed (3)

src/main/java/com/ra4king/opengl/GLProgram.java

 				Display.sync(fps);
 			}
 		}
+		catch(Exception exc) {
+			exc.printStackTrace();
+		}
 		finally {
 			destroy();
 		}

src/main/java/com/ra4king/opengl/test/Test.java

 import java.nio.ShortBuffer;
 
 import org.lwjgl.BufferUtils;
+import org.lwjgl.input.Keyboard;
+import org.lwjgl.input.Mouse;
 
 import com.ra4king.opengl.GLProgram;
 import com.ra4king.opengl.util.ShaderProgram;
 import com.ra4king.opengl.util.math.Matrix4;
+import com.ra4king.opengl.util.math.Vector3;
 
 public class Test extends GLProgram {
 	public static void main(String[] args) {
 	}
 	
 	public Test() {
-		super("Test",800,600,true);
+		super(true);//"Test",800,600,true);
+		
+		Mouse.setGrabbed(true);
 	}
 	
 	private ShaderProgram program;
 		program.end();
 	}
 	
+	private Vector3 position = new Vector3();
+	private float angle, vy;
+	private final float gravity = -100;
+	
+	private float angleY;
+	
+	@Override
+	public void update(long deltaTime) {
+		final float delta = deltaTime / (float)1e9;
+		
+		float turnSpeed = (float)Math.PI * delta;
+		float moveSpeed = (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) ? 100 : 50) * delta;
+		
+		angle += turnSpeed * Mouse.getDX() / 20f;
+		angleY -= turnSpeed * Mouse.getDY() / 20f;
+		
+		if(Keyboard.isKeyDown(Keyboard.KEY_A))
+			position.sub((float)Math.cos(angle+Math.PI)*moveSpeed, 0, (float)Math.sin(angle+Math.PI)*moveSpeed);
+		if(Keyboard.isKeyDown(Keyboard.KEY_D))
+			position.add((float)Math.cos(angle+Math.PI)*moveSpeed, 0, (float)Math.sin(angle+Math.PI)*moveSpeed);
+		
+		if(Keyboard.isKeyDown(Keyboard.KEY_W))
+			position.add((float)Math.cos(angle+Math.PI/2)*moveSpeed, 0, (float)Math.sin(angle+Math.PI/2)*moveSpeed);
+		if(Keyboard.isKeyDown(Keyboard.KEY_S))
+			position.sub((float)Math.cos(angle+Math.PI/2)*moveSpeed, 0, (float)Math.sin(angle+Math.PI/2)*moveSpeed);
+		
+		if(Keyboard.isKeyDown(Keyboard.KEY_R))
+			position.reset();
+		
+		if(Keyboard.isKeyDown(Keyboard.KEY_SPACE) && position.y() == 0)
+			vy = 50;
+		
+		vy += gravity * delta;
+		position.add(0, vy * delta, 0);
+		
+		if(position.y() < 0) {
+			position.y(0);
+			vy = -vy/3f;
+		}
+	}
+	
 	@Override
 	public void render() {
 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 		
 		glBindVertexArray(vao);
 		for(int a = -100; a <= 100; a++) {
-			if(Math.abs(a) < 2)
-				continue;
-			
 			for(int b = -100; b <= 0; b++) {
-				glUniformMatrix4(modelViewMatrixUniform, false, modelViewMatrix.clearToIdentity()/*.rotate(20*(float)Math.PI/180,1,0,0)*/.translate(a*10, -20, b*10-20).getBuffer());				
+				glUniformMatrix4(modelViewMatrixUniform, false, modelViewMatrix.clearToIdentity().rotate(angleY, 1, 0, 0).rotate(angle,0,1,0).translate(a*10 + position.x(), -20 - position.y(), b*10-20 + position.z()).getBuffer());				
 				glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
 			}
 		}

src/main/java/com/ra4king/opengl/util/math/Vector3.java

 	}
 	
 	public Vector3(Vector3 vec) {
-		x = vec.x;
-		y = vec.y;
-		z = vec.z;
+		set(vec);
 	}
 	
 	public float x() {
 		return this;
 	}
 	
+	public Vector3 set(float x, float y, float z) {
+		this.x = x;
+		this.y = y;
+		this.z = z;
+		return this;
+	}
+	
+	public Vector3 set(Vector3 vec) {
+		return set(vec.x(),vec.y(),vec.z());
+	}
+	
+	public Vector3 reset() {
+		x = y = z = 0;
+		return this;
+	}
+	
 	public float length() {
 		return (float)Math.sqrt(x*x + y*y + z*z);
 	}
 	}
 	
 	public Vector3 add(Vector3 vec) {
-		x += vec.x;
-		y += vec.y;
-		z += vec.z;
-		return this;
+		return add(vec.x(),vec.y(),vec.z());
 	}
 	
 	public Vector3 sub(float x, float y, float z) {
-		this.x += x;
-		this.y += y;
-		this.z += z;
+		this.x -= x;
+		this.y -= y;
+		this.z -= z;
 		return this;
 	}
 	
 	public Vector3 sub(Vector3 vec) {
-		x -= vec.x;
-		y -= vec.y;
-		z -= vec.z;
-		return this;
+		return sub(vec.x(),vec.y(),vec.z());
 	}
 	
 	public Vector3 mul(float f) {
-		x *= f;
-		y *= f;
-		z *= f;
-		return this;
+		return mul(f,f,f);
 	}
 	
 	public Vector3 mul(float x, float y, float z) {
 	}
 	
 	public Vector3 mul(Vector3 vec) {
-		x *= vec.x;
-		y *= vec.y;
-		z *= vec.z;
-		return this;
+		return mul(vec.x(),vec.y(),vec.z());
 	}
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.