Skip to content

Commit

Permalink
Added and implemented window resize events
Browse files Browse the repository at this point in the history
  • Loading branch information
david-palm committed Sep 22, 2022
1 parent 1e40b38 commit 30c6e69
Show file tree
Hide file tree
Showing 16 changed files with 221 additions and 52 deletions.
24 changes: 11 additions & 13 deletions app/src/main/assets/shaders/ui/card.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@ const float EPSILON = 0.02;
in vec2 fragmentPosition;
out vec4 fragColor;

uniform mat4 uModel;
uniform float uAspectRatio;
uniform vec4 uColor;
uniform mat4 uModel;
// All corner radii sorted from left to right, top to bottom. Radii are given in percentages
uniform vec2 uRoundCorners[4];
uniform vec2 uRoundedCorners[4];

bool isClose(float a, float b){
return (abs(a - b) < EPSILON);
}
void main(){
fragColor = vec4(0.0, 0.0, 0.0, 0.0);

// Calculating corner points. Radii are multiplied by 0.5 to convert percentage into coordinate
// Calculating corner points
vec2 corners[4];
corners[0] = vec2(-1.0 + uRoundCorners[0].x, 1.0 - uRoundCorners[0].y);
corners[1] = vec2(1.0 - uRoundCorners[1].x, 1.0 - uRoundCorners[1].y);
corners[2] = vec2(-1.0 + uRoundCorners[2].x, -1.0 + uRoundCorners[2].y);
corners[3] = vec2(1.0 - uRoundCorners[3].x, -1.0 + uRoundCorners[3].y);
corners[0] = vec2(-1.0 + uRoundedCorners[0].x * uModel[1][1], 1.0 - uRoundedCorners[0].y * uModel[0][0] * uAspectRatio);
corners[1] = vec2(1.0 - uRoundedCorners[1].x * uModel[1][1], 1.0 - uRoundedCorners[1].y * uModel[0][0] * uAspectRatio);
corners[2] = vec2(-1.0 + uRoundedCorners[2].x * uModel[1][1], -1.0 + uRoundedCorners[2].y * uModel[0][0] * uAspectRatio);
corners[3] = vec2(1.0 - uRoundedCorners[3].x * uModel[1][1], -1.0 + uRoundedCorners[3].y * uModel[0][0] * uAspectRatio);

// Coloring everything except the corners
if( fragmentPosition.x > corners[0].x && fragmentPosition.x < corners[1].x){
Expand All @@ -36,10 +34,10 @@ void main(){
for(int i = 0; i < 4; i++){
// Transposing position into "corner space"
vec2 positionsEllipseSpace = fragmentPosition - corners[i];
float x = pow(positionsEllipseSpace.x, 2.0) / pow(corners[i].x, 2.0);
float y = pow(positionsEllipseSpace.y, 2.0) / pow(corners[i].y, 2.0);
float x = pow(positionsEllipseSpace.x, 2.0) / pow(uRoundedCorners[i].x * uModel[1][1], 2.0);
float y = pow(positionsEllipseSpace.y, 2.0) / pow(uRoundedCorners[i].y * uModel[0][0] * uAspectRatio, 2.0);
if(x + y <= 1.0){
fragColor = vec4(0.8, 0.8, 0.8, 1.0);
fragColor = uColor;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.view.ScaleGestureDetector;

import com.example.opengl3renderer.events.Event;
import com.example.opengl3renderer.events.WindowResizeEvent;
import com.example.opengl3renderer.layers.Layer;
import com.example.opengl3renderer.math.Vec2;
import com.example.opengl3renderer.math.Vec4;
Expand All @@ -22,7 +23,7 @@
import java.util.ArrayList;
import java.util.List;

public class AppRenderer extends ScaleGestureDetector.SimpleOnScaleGestureListener implements GLSurfaceView.Renderer {
public class AppRenderer extends ScaleGestureDetector.SimpleOnScaleGestureListener implements GLSurfaceView.Renderer{

int width;
int height;
Expand All @@ -36,18 +37,18 @@ public AppRenderer(Context context){

@Override
public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig){
layerStack.add(new Scene(new Vec2(width, height), context));
layerStack.add(new Scene(context));
// Creating UI test
UI ui = new UI(context);
ui.addToLayerStack(layerStack);

GLES32.glEnable(GLES32.GL_DEPTH_TEST);
}

@Override
public void onSurfaceChanged(GL10 glUnused, int width, int height){
this.width = width;
this.height = height;
onEvent(new WindowResizeEvent(width, height));
// Set Viewport
GLES32.glViewport(0,0, width, height);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public enum Type{
MULTITOUCH_DOWN,
// ACTION_POINTER_UP
MULTITOUCH_UP,
// onSurfaceChange
WINDOW_RESIZE,

WINDOW_EVENT,
APPLICATION_EVENT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.example.opengl3renderer.events;

import android.util.Log;

public class EventDispatcher {
private Event event;

Expand All @@ -12,7 +10,6 @@ public EventDispatcher(Event event){
public void dispatch(Event.Type type, EventHandler eventHandler){
// Event is not dispatched, if it was already handled
if(event.handled){
Log.d("Event Dispatcher", "Event already handled!");
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.opengl3renderer.events;

public class WindowResizeEvent extends Event{
float x;
float y;
public WindowResizeEvent(float x, float y){
super(Type.WINDOW_RESIZE);
this.x = x;
this.y = y;
}

public float getX() {
return x;
}

public float getY() {
return y;
}
}
28 changes: 28 additions & 0 deletions app/src/main/java/com/example/opengl3renderer/math/Mat4.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,34 @@ public static Mat4 rotation(Vec3 axis, float alpha){
return new Mat4(Mat3.rotation(axis, alpha));
}

public static Mat4 scale(float x, float y){
Mat4 mat = new Mat4();

mat.x1 = x;
mat.y2 = y;

return mat;
}

public static Mat4 scale(Vec2 scale){
Mat4 mat = new Mat4();

mat.x1 = scale.x;
mat.y2 = scale.y;

return mat;
}

public static Mat4 scale(float x, float y, float z){
Mat4 mat = new Mat4();

mat.x1 = x;
mat.y2 = y;
mat.z3 = z;

return mat;
}

public static Mat4 scale(Vec3 scale){
Mat4 mat = new Mat4();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public float getFov(){
}
public void setFov(float fov){
this.fov = fov;
projection = projection = Mat4.perspective((float)Math.toRadians(fov), aspectRatio, 0.1f, 100);
projection = Mat4.perspective((float)Math.toRadians(fov), aspectRatio, 0.1f, 100);
}
public void setAspectRatio(float aspectRatio){
this.aspectRatio = aspectRatio;
projection = Mat4.perspective((float)Math.toRadians(fov), aspectRatio, 0.1f, 100);
}
public Vec3 getPosition(){
return position;
Expand Down
23 changes: 20 additions & 3 deletions app/src/main/java/com/example/opengl3renderer/scene/Scene.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.example.opengl3renderer.events.TouchDownEvent;
import com.example.opengl3renderer.events.TouchMoveEvent;
import com.example.opengl3renderer.events.TouchUpEvent;
import com.example.opengl3renderer.events.WindowResizeEvent;
import com.example.opengl3renderer.layers.Layer;
import com.example.opengl3renderer.math.Vec2;
import com.example.opengl3renderer.math.Vec3;
Expand All @@ -21,14 +22,15 @@
import com.example.opengl3renderer.scene.object3d.StandardObject3DShader;

public class Scene extends Layer {
float aspectRatio;
Camera camera;
PointLight[] pointLights;
Vec3 ambientLight;
Object3D object;

public Scene(Vec2 aspectRatio, Context context){
public Scene(Context context){
// Creating camera and lights
camera = new Camera(new Vec3( 0.0f, 0.0f, -5.0f), 60.0f, 9.0f / 16.0f);
camera = new Camera(new Vec3( 0.0f, 0.0f, -5.0f), 60.0f, 1.0f);
pointLights = new PointLight[1];
pointLights[0] = new PointLight(new Vec3(1.0f, 0.0f, 1.0f), new Vec3(1.0f, 1.0f, 1.0f), 1.25f);
ambientLight = new Vec3(0.25f, 0.25f, 0.25f);
Expand All @@ -50,12 +52,21 @@ public Scene(Vec2 aspectRatio, Context context){
object = new Object3D(mesh, material);
}

public float getAspectRatio(){
return aspectRatio;
}

public void setAspectRatio(float aspectRatio){
this.aspectRatio = aspectRatio;
}

public void onEvent(Event event){
EventDispatcher dispatcher = new EventDispatcher(event);
dispatcher.dispatch(Event.Type.TOUCH_DOWN, (Event e) -> (onTouchDownEvent((TouchDownEvent) e)));
dispatcher.dispatch(Event.Type.TOUCH_MOVE, (Event e) -> (onTouchMoveEvent((TouchMoveEvent) e)));
dispatcher.dispatch(Event.Type.TOUCH_UP, (Event e) -> (onTouchUpEvent((TouchUpEvent) e)));
dispatcher.dispatch(Event.Type.SCALE_GESTURE, (Event e) -> (onScaleEvent((ScaleEvent) e)));
dispatcher.dispatch(Event.Type.WINDOW_RESIZE, (Event e) -> (onWindowResizeEvent((WindowResizeEvent) e)));
}

public void onRender(){
Expand All @@ -79,9 +90,15 @@ public boolean onScaleEvent(ScaleEvent e){
return true;
}

public boolean onWindowResizeEvent(WindowResizeEvent e){
aspectRatio = e.getX() / e.getY();
camera.setAspectRatio(aspectRatio);
return false;
}

public void rotateObject(float dX, float dY){
float rotationSpeed = 150.0f;
float degToRad = (float) (Math.PI/180.0);
float degToRad = (float) (Math.PI / 180.0);
float rotX = dX * rotationSpeed * degToRad;
float rotY = -dY * rotationSpeed * degToRad;
object.rotate(new Vec4(0.0f, 1.0f, 0.0f, rotX));
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/example/opengl3renderer/ui/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.example.opengl3renderer.events.EventDispatcher;
import com.example.opengl3renderer.events.TouchDownEvent;
import com.example.opengl3renderer.events.TouchMoveEvent;
import com.example.opengl3renderer.events.WindowResizeEvent;
import com.example.opengl3renderer.layers.Layer;
import com.example.opengl3renderer.math.Mat4;
import com.example.opengl3renderer.ui.object2d.Object2D;

import java.util.ArrayList;
Expand Down Expand Up @@ -39,6 +41,7 @@ public void onEvent(Event event){
EventDispatcher dispatcher = new EventDispatcher(event);
dispatcher.dispatch(Event.Type.TOUCH_DOWN, (Event e) -> (onTouchDownEvent((TouchDownEvent) e)));
dispatcher.dispatch(Event.Type.TOUCH_MOVE, (Event e) -> (onTouchMoveEvent((TouchMoveEvent) e)));
dispatcher.dispatch(Event.Type.WINDOW_RESIZE, (Event e) ->(onWindowResizeEvent((WindowResizeEvent) e)));
}

@Override
Expand Down Expand Up @@ -67,4 +70,13 @@ public boolean onTouchMoveEvent(TouchMoveEvent e){
}
return false;
}

public boolean onWindowResizeEvent(WindowResizeEvent e){
float aspectRatio = e.getX() / e.getY();
for(Object2D object : objects){
object.getMaterial().getShader().bind();
object.getMaterial().getShader().setAspectRatio(aspectRatio);
}
return false;
}
}
9 changes: 6 additions & 3 deletions app/src/main/java/com/example/opengl3renderer/ui/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
import java.util.ArrayList;
import java.util.List;

public class UI {
public class UI{
public ArrayList<Component> components;

public UI(Context context){
components = new ArrayList<Component>();
CardMaterial material = new CardMaterial(context, new Vec4(0.8f, 0.8f, 0.8f, 0.2f));
Card card = new Card(material, new Vec2(0.0f, -0.8f), new Vec2(1.0f, 0.2f), 0.0f, 0.5f);

// Initialize Card
CardMaterial material = new CardMaterial(context, new Vec4(0.2f, 0.2f, 0.2f, 1.0f));
Vec2[] roundedCorners = new Vec2[]{ new Vec2(0.4f, 0.4f), new Vec2(0.4f, 0.4f) };
Card card = new Card(material, new Vec2(0.0f, -0.8f), new Vec2(1.0f, 0.2f), 0.0f, roundedCorners);
Component menu = new Component(card);
components.add(menu);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.opengl3renderer.ui.object2d;

import android.content.Context;

import com.example.opengl3renderer.math.Vec4;

public class BasicMaterial2D extends Material2D{
BasicObject2DShader shader;
Vec4 color;

public BasicMaterial2D(Context context) {
this(context, new Vec4());
}

public BasicMaterial2D(Context context, Vec4 color){
shader = new BasicObject2DShader(context);
this.color = color;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.opengl3renderer.ui.object2d;

import android.content.Context;

import com.example.opengl3renderer.math.Vec4;
import com.example.opengl3renderer.renderer.Uniform;

public class BasicObject2DShader extends Object2DShader{
Uniform color;
public BasicObject2DShader(Context context){
super(context, "assets/ui/BasicShader.vs", "assets/ui/BasicShader.fs");
color = new Uniform("uColor", shaderProgramId);
}

public void setColor(Vec4 color){
this.color.setObject(color);
this.color.sendToShader();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@

public class Object2DShader extends Shader {
protected Uniform model;
protected Uniform aspectRatio;
public Object2DShader(String vertexShaderSource, String fragmentShaderSource){
super(vertexShaderSource, fragmentShaderSource);
model = new Uniform("uModel", shaderProgramId);
aspectRatio = new Uniform("uAspectRatio", shaderProgramId);
}

public Object2DShader(Context context, String vertexShaderPath, String fragmentShaderPath){
super(context, vertexShaderPath, fragmentShaderPath);
model = new Uniform("uModel", shaderProgramId);
aspectRatio = new Uniform("uAspectRatio", shaderProgramId);
}

public void setModel(Mat4 model){
this.model.setObject(model);
this.model.sendToShader();
}

public void setAspectRatio(float aspectRatio){
this.aspectRatio.setObject(aspectRatio);
this.aspectRatio.sendToShader();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.opengl3renderer.ui.object2d.arrow;

import android.content.Context;

import com.example.opengl3renderer.math.Mat4;
import com.example.opengl3renderer.math.Vec2;
import com.example.opengl3renderer.ui.object2d.BasicMaterial2D;
import com.example.opengl3renderer.ui.object2d.Object2D;
import com.example.opengl3renderer.ui.object2d.Rectangle;

public class ArrowHead extends Object2D {
BasicMaterial2D material;
float angle;
float length;
float thickness;
Mat4[] models;

public ArrowHead(Context context){
this(context, new Vec2(), (float) Math.PI/6, 0.1f, 0.02f);
}

public ArrowHead(Context context, Vec2 position, float angle, float length, float thickness){
super(new Rectangle(), new BasicMaterial2D(context));
this.material = new BasicMaterial2D(context);
this.position = position;
this.angle = angle;
this.length = length;
this.thickness = thickness;
models = new Mat4[2];
}

@Override
public boolean isInside(float x, float y) {
return false;
}
}
Loading

0 comments on commit 30c6e69

Please sign in to comment.