2D engine

This commit is contained in:
Antonin Boyon 2021-03-16 18:16:55 +01:00
parent 32303c22ba
commit 84db074e0b
5 changed files with 182 additions and 68 deletions

View File

@ -1,9 +1,22 @@
package lsystem; package lsystem;
import java.util.ArrayList;
import lsystem.screen.gl2d.SwingGLCanvas2D; import lsystem.screen.gl2d.SwingGLCanvas2D;
import lsystem.screen.main.MainFrame; import lsystem.screen.main.MainFrame;
import lsystem.utils.Pair;
public class Main2D { public class Main2D {
/*public static MainFrame mainFrame;
public static SwingGLCanvas2D joglFrame;
public static void main(String[] args) {
new Thread(() -> {
mainFrame = new MainFrame();
mainFrame.setVisible(true);
}).start();
new Thread(() -> joglFrame = new SwingGLCanvas2D()).start();
} */
public static void main(String[] args) { public static void main(String[] args) {
@ -11,7 +24,14 @@ public class Main2D {
frame.setVisible(true); frame.setVisible(true);
SwingGLCanvas2D canvas = new SwingGLCanvas2D(); SwingGLCanvas2D canvas = new SwingGLCanvas2D();
canvas.setVisible(true); canvas.setVisible(true);
new Thread(() ->{
SwingGLCanvas2D joglFrame = new SwingGLCanvas2D();
String axiom="X";
ArrayList<Pair<String, String>> rules = new ArrayList<>();
rules.add(new Pair<>("X=Y+[[X]-X]-Y[-YX]+X", "Y=YY"));
joglFrame.setLSystem(axiom,rules,5);
joglFrame.setVisible(true);
}).start();
} }
} }

View File

@ -18,5 +18,9 @@ public class Element {
this.parent = parent; this.parent = parent;
this.rotation = rotation; this.rotation = rotation;
} }
public int getRotation2D () {
return (int) rotation[0];
}
} }

View File

@ -1,11 +1,21 @@
package lsystem.screen.gl2d; package lsystem.screen.gl2d;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import com.jogamp.opengl.GL2; import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable; import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener; import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.glu.GLU; import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.gl2.GLUT; import com.jogamp.opengl.util.gl2.GLUT;
import javafx.scene.control.Tab;
import lsystem.engine.Element;
import lsystem.engine.ElementProperties;
import lsystem.screen.gl3d.DrawHelper; import lsystem.screen.gl3d.DrawHelper;
import lsystem.screen.gl3d.GLCanvas;
import lsystem.utils.Pair;
public class JoglEventListener2D implements GLEventListener { public class JoglEventListener2D implements GLEventListener {
@ -43,15 +53,37 @@ public class JoglEventListener2D implements GLEventListener {
@Override @Override
public void dispose(GLAutoDrawable glAutoDrawable) { public void dispose(GLAutoDrawable glAutoDrawable) {
} }
public void drawAll (GL2 gl, Element actual, lsystem.screen.gl2d.Point origin) {
if (actual.property == ElementProperties.DRAW) {
System.out.println("DESSIN");
lsystem.screen.gl2d.Point newOrigin = new lsystem.screen.gl2d.Point (origin, actual.getRotation2D());
DrawHelper.drawStick(gl, origin, newOrigin);
}
System.out.println(actual.children.isEmpty());
for (Element children : actual.children) {
System.out.println("CHILD");
drawAll(gl, children, new lsystem.screen.gl2d.Point (origin, actual.getRotation2D()));
}
}
@Override @Override
public void display (GLAutoDrawable glAutoDrawable) { public void display (GLAutoDrawable glAutoDrawable) {
GL2 gl = glAutoDrawable.getGL().getGL2(); /* Element str = new Element(ElementProperties.DRAW, null, new int[]{0, 0, 0});
float xDefault = -1.0f, yDefault = -1.0f; Element child1 = new Element(ElementProperties.DRAW, str, new int[]{0, 0, 0});
Element child2 = new Element(ElementProperties.DRAW, str, new int[]{45, 0, 0});
str.children.add(child1);
str.children.add(child2);
Element child11 = new Element(ElementProperties.DRAW, child1, new int[]{225, 0, 0});
Element child12 = new Element(ElementProperties.DRAW, child1, new int[]{270, 0, 0});
child1.children.add(child11);
child1.children.add(child12);*/
GL2 gl = glAutoDrawable.getGL().getGL2();
drawAll (gl, canvas.getLSystem(), new lsystem.screen.gl2d.Point(-1.0f, -1.0f));
DrawHelper.drawStick(gl, 0.1f, xDefault, yDefault, 0);
DrawHelper.drawStick(gl, 0.2f, 1.1f, 1.1f, 90);
} }
@Override @Override

View File

@ -0,0 +1,114 @@
package lsystem.screen.gl2d;
public class Point {
private float x;
private float y;
public Point () {
}
public Point (float x, float y) {
this.x = x;
this.y = y;
}
public Point (Point old, int angle) {
angle = compactAngle(angle);
//Point a = new Point();
//x = getNewOrigin(old, angle, 0.1f);
getNewOrigin(old, angle, 0.1f);
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public void getNewOrigin (Point old, int angle, float echelle) {
int newX=0, newY=0;
switch (angle) {
case 0:
newX = 1;
newY = 1;
break;
case 45:
newX = 1;
newY = 0;
break;
case 90:
newX = 1;
newY = -1;
break;
case 135:
newX = 0;
newY = -1;
break;
case 180:
newX = -1;
newY = -1;
break;
case 225:
newX = -1;
newY = 0;
break;
case 270:
newX = -1;
newY = 1;
break;
case 315:
newX = 0;
newY = 1;
break;
}
this.x = old.getX() + echelle * newX;
this.y+= old.getY() + echelle * newY;
//return old;
}
public String toString () {
return "("+this.x+";"+this.y+")";
}
public static int compactAngle (int angle) {
angle = angle - ((angle / 360) * 360);
switch (angle) {
case -315:
angle = 45;
break;
case -270:
angle = 90;
break;
case -225:
angle = 135;
break;
case -180:
angle = 180;
break;
case -135:
angle = 225;
break;
case -90:
angle = 270;
break;
case -45:
angle = 315;
break;
}
return angle;
}
}

View File

@ -2,6 +2,8 @@ package lsystem.screen.gl3d;
import com.jogamp.opengl.GL2; import com.jogamp.opengl.GL2;
import com.jogamp.opengl.util.gl2.GLUT; import com.jogamp.opengl.util.gl2.GLUT;
import lsystem.screen.gl2d.Point;
import lsystem.screen.gl2d.SwingGLCanvas2D; import lsystem.screen.gl2d.SwingGLCanvas2D;
public class DrawHelper { public class DrawHelper {
@ -101,72 +103,14 @@ public class DrawHelper {
+ canvas.camera[3] + " pitch = " + canvas.camera[4] + " roll = " + canvas.camera[5]); + canvas.camera[3] + " pitch = " + canvas.camera[4] + " roll = " + canvas.camera[5]);
} }
public static void drawStick(GL2 gl, float echelle, float x, float y, int angle) { public static void drawStick(GL2 gl, Point origin, Point newOrigin) {
angle = angle - ((angle / 360) * 360);
switch (angle) {
case -315:
angle = 45;
;
case -270:
angle = 90;
;
case -225:
angle = 135;
;
case -180:
angle = 180;
;
case -135:
angle = 225;
;
case -90:
angle = 270;
;
case -45:
angle = 315;
;
}
// Direction
int newX=0, newY=0;
switch (angle) {
case 0:
newX = 1;
newY = 1;
break;
case 45:
newX = 1;
newY = 0;
break;
case 90:
newX = 1;
newY = -1;
break;
case 135:
newX = 0;
newY = -1;
break;
case 180:
newX = -1;
newY = -1;
break;
case 225:
newX = -1;
newY = 0;
break;
case 270:
newX = -1;
newY = 1;
break;
case 315:
newX = 0;
newY = 1;
break;
}
gl.glBegin(GL2.GL_LINES); gl.glBegin(GL2.GL_LINES);
gl.glVertex2f(x, y); gl.glVertex2f(origin.getX(), origin.getY());
gl.glVertex2f(x + (echelle * newX), y + (echelle * newY)); gl.glVertex2f(newOrigin.getX(), newOrigin.getY());
gl.glEnd(); gl.glEnd();
} }
} }