2D engine

This commit is contained in:
Antonin Boyon 2021-03-02 18:31:57 +01:00
parent 764a0fe710
commit 9a156f8d11
8 changed files with 496 additions and 72 deletions

17
src/lsystem/Main2D.java Normal file
View File

@ -0,0 +1,17 @@
package lsystem;
import lsystem.screen.MainFrame;
import lsystem.screen.SwingGLCanvas;
import lsystem.screen.SwingGLCanvas2D;
public class Main2D {
public static void main(String[] args) {
MainFrame frame = new MainFrame();
frame.setVisible(true);
SwingGLCanvas2D canvas = new SwingGLCanvas2D();
}
}

View File

@ -11,6 +11,12 @@ public class DrawHelper {
gl.glRotatef(canvas.camera[5], 0f, 0f, 1f); gl.glRotatef(canvas.camera[5], 0f, 0f, 1f);
} }
public static void placeCamera(GL2 gl, SwingGLCanvas2D canvas) {
gl.glRotatef(canvas.camera[4], 1f, 0f, 0f);
gl.glRotatef(canvas.camera[3], 0f, 1f, 0f);
gl.glRotatef(canvas.camera[5], 0f, 0f, 1f);
}
public static void drawAxes(GL2 gl, GLUT glut) { public static void drawAxes(GL2 gl, GLUT glut) {
gl.glRasterPos3f(0f, 0f, 0f); gl.glRasterPos3f(0f, 0f, 0f);
gl.glColor3f(255f, 0f, 0f); gl.glColor3f(255f, 0f, 0f);
@ -41,7 +47,8 @@ public class DrawHelper {
glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, 'Z'); // draw the z axis glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, 'Z'); // draw the z axis
} }
public static void drawRectangularPrism(GL2 gl, float leftBottomX, float leftBottomY, float leftBottomZ, float rightUpX, float rightUpY, float rightUpZ) { public static void drawRectangularPrism(GL2 gl, float leftBottomX, float leftBottomY, float leftBottomZ,
float rightUpX, float rightUpY, float rightUpZ) {
gl.glPushMatrix(); gl.glPushMatrix();
gl.glBegin(GL2.GL_QUADS); gl.glBegin(GL2.GL_QUADS);
gl.glVertex3f(leftBottomX, leftBottomY, leftBottomZ); gl.glVertex3f(leftBottomX, leftBottomY, leftBottomZ);
@ -71,6 +78,89 @@ public class DrawHelper {
gl.glRasterPos3f(canvas.camera[0], canvas.camera[1], canvas.camera[2] - 1); gl.glRasterPos3f(canvas.camera[0], canvas.camera[1], canvas.camera[2] - 1);
gl.glColor3f(1f, 1f, 1f); gl.glColor3f(1f, 1f, 1f);
glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18, glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18,
"x=" + canvas.camera[0] + ", y=" + canvas.camera[1] + ", z=" + canvas.camera[2]+"\n yaw = " + canvas.camera[3] + " pitch = " + canvas.camera[4] + " roll = " + canvas.camera[5]); "x=" + canvas.camera[0] + ", y=" + canvas.camera[1] + ", z=" + canvas.camera[2] + "\n yaw = "
+ canvas.camera[3] + " pitch = " + canvas.camera[4] + " roll = " + canvas.camera[5]);
} }
public static void drawDebugInformation(GL2 gl, GLUT glut, SwingGLCanvas2D canvas) {
gl.glRasterPos3f(canvas.camera[0], canvas.camera[1], canvas.camera[2] - 1);
gl.glColor3f(1f, 1f, 1f);
glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18,
"x=" + canvas.camera[0] + ", y=" + canvas.camera[1] + ", z=" + canvas.camera[2] + "\n yaw = "
+ 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) {
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.glVertex2f(x, y);
gl.glVertex2f(x + (echelle * newX), y + (echelle * newY));
gl.glEnd();
/* System.out.println("X : " +(x + (echelle * newX)));
System.out.println("Y : " +(y + (echelle * newY)));
System.out.println("angle : " +angle);
System.out.println("newX : " +newX);
System.out.println("newY : " +newY);*/
}
} }

View File

@ -73,19 +73,21 @@ public class Jogl implements GLEventListener {
// display() sera appelée en boucle tout au long de l'application par la classe Animator. // display() sera appelée en boucle tout au long de l'application par la classe Animator.
// C'est dans cette fonction qu'on fera tout ce qui doit être affiché // C'est dans cette fonction qu'on fera tout ce qui doit être affiché
public void display(GLAutoDrawable drawable) { public void display(GLAutoDrawable drawable) {
final GL2 gl = drawable.getGL().getGL2(); GL2 gl = drawable.getGL().getGL2();
gl.glBegin(GL2.GL_LINES); float xDefault = -1.0f, yDefault = -1.0f;
// 2eme argument : angle
gl.glVertex2f(-0.25f, -0.25f); /*gl.glBegin(GL2.GL_LINES);
gl.glVertex2f(0.5f, 0.15f); gl.glVertex2f(xDefault, yDefault);
gl.glVertex2f(xDefault + (0.1f * 1), yDefault + (0.1f * 1));
gl.glEnd(); gl.glEnd();
gl.glBegin(GL2.GL_LINES); gl.glBegin(GL2.GL_LINES);
gl.glVertex2f(0.5f, 0.15f); gl.glVertex2f(xDefault + (0.1f * 1), -1.1f);
gl.glVertex2f(0.8f, 0.8f); gl.glVertex2f(xDefault + (0.1f * 1) + (0.2f*1), yDefault + (0.1f * 1) + (0.2f*-1));
gl.glEnd(); gl.glEnd();*/
DrawHelper.drawStick(gl, 0.1f, xDefault, yDefault, 0);
DrawHelper.drawStick(gl, 0.2f, -0.9f, -0.9f, 90);
} }
/** /**

View File

@ -0,0 +1,53 @@
package lsystem.screen;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.FPSAnimator;
import com.jogamp.opengl.util.gl2.GLUT;
import lsystem.screen.listener.JoglEventListener;
import lsystem.screen.listener.JoglEventListener2D;
import lsystem.screen.listener.JoglMouseListener;
import lsystem.screen.listener.JoglMouseListener2D;
import lsystem.screen.listener.KeyboardListener;
import lsystem.screen.listener.KeyboardListener2D;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class SwingGLCanvas2D {
public final GLCanvas glCanvas;
public float[] camera = {0f, 1f, 5f, // camera pos x,y, z
0f, 0f, 0f}; // camera rotation yaw(x-axis), pitch(y-axis), roll(z-axis)
public GLU glu = new GLU();
public GLUT glut = new GLUT();
public SwingGLCanvas2D() {
GLProfile glProfile = GLProfile.getDefault();
GLCapabilities glCapabilities = new GLCapabilities(glProfile);
this.glCanvas = new GLCanvas(glCapabilities);
glCanvas.addGLEventListener(new JoglEventListener2D(this));
JoglMouseListener2D mouse = new JoglMouseListener2D(this);
glCanvas.addMouseListener(mouse);
glCanvas.addMouseMotionListener(mouse);
glCanvas.addMouseWheelListener(mouse);
glCanvas.addKeyListener(new KeyboardListener2D(this));
final JFrame jframe = new JFrame("L-System");
final FPSAnimator animator = new FPSAnimator(glCanvas, 60);
jframe.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
jframe.dispose();
}
});
jframe.getContentPane().add(glCanvas, BorderLayout.CENTER);
jframe.setSize(Constants.WIDTH, Constants.HEIGHT);
animator.start();
jframe.setVisible(true);
}
}

View File

@ -0,0 +1,113 @@
package lsystem.screen.listener;
import com.jogamp.opengl.*;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.gl2.GLUT;
import lsystem.screen.DrawHelper;
import lsystem.screen.SwingGLCanvas;
import lsystem.screen.SwingGLCanvas2D;
import lsystem.utils.Pair;
import java.util.LinkedList;
import java.util.Random;
public class JoglEventListener2D implements GLEventListener {
private final SwingGLCanvas2D canvas;
private final float[] light_0_ambient = {0.01f, 0.01f, 0.01f, 0.01f};
private final float[] light_0_diffuse = {1.0f, 1.0f, 1.0f, 1.0f};
private final float[] light_0_specular = {1.0f,1.0f, 1.0f, 1.0f};
private final float[] light_0_position = {1000f, 1000f, 1000f, 1f};
private final float[] material_specular = {0.8f, 0.8f, 0.8f, 0.8f};
private float angle = 0f;
private final LinkedList<Pair<Integer, Integer>> prismPosition = new LinkedList<>();
private final GLU glu;
private final GLUT glut;
private int fps;
public JoglEventListener2D(SwingGLCanvas2D swingGLCanvas) {
this.canvas = swingGLCanvas;
this.glu = canvas.glu;
this.glut = canvas.glut;
}
@Override
public void init(GLAutoDrawable glAutoDrawable) {
GL2 gl = glAutoDrawable.getGL().getGL2();
gl.glClearColor(0f, 0f, 0f, 1.0f);
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glEnable(GL2.GL_LIGHT0);
gl.glEnable(GL2.GL_LIGHTING);
gl.glEnable(GL2.GL_NORMALIZE);
gl.glEnable(GL2.GL_COLOR_MATERIAL);
gl.glClearDepth(1.0f);
gl.glShadeModel(GL2.GL_SMOOTH);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, light_0_ambient, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, light_0_diffuse, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_0_specular, 0);
gl.glDepthFunc(GL2.GL_LEQUAL);
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
for(int i = -50; i < 51; ++i) {
for(int j = -50; j < 51; ++j) {
if(new Random().nextFloat() < 0.05) {
prismPosition.add(new Pair<>(i, j));
}
}
}
System.out.println(prismPosition.size() * 8);
new Thread(() -> {
while (true) {
synchronized (this){
try {
wait(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(fps);
fps = 0;
}
}
}).start();
}
@Override
public void dispose(GLAutoDrawable glAutoDrawable) {
}
@Override
public void display (GLAutoDrawable glAutoDrawable) {
GL2 gl = glAutoDrawable.getGL().getGL2();
float xDefault = -1.0f, yDefault = -1.0f;
DrawHelper.drawStick(gl, 0.1f, xDefault, yDefault, 0);
DrawHelper.drawStick(gl, 0.2f, 1.1f, 1.1f, 90);
}
@Override
public void reshape(GLAutoDrawable glAutoDrawable, int x, int y, int width, int height) {
GL2 gl = glAutoDrawable.getGL().getGL2();
gl.glViewport(x, y, width, height);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(60.0f, (float) width / height, 0.1f, 1000.0f);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_0_position, 0);
gl.glColorMaterial(GL2.GL_FRONT_AND_BACK, GL2.GL_AMBIENT_AND_DIFFUSE);
gl.glMateriali(GL2.GL_FRONT_AND_BACK, GL2.GL_SHININESS, 90);
gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, material_specular, 0);
gl.glMatrixMode(GL2.GL_MODELVIEW);
}
}

View File

@ -0,0 +1,90 @@
package lsystem.screen.listener;
import com.jogamp.opengl.glu.GLU;
import lsystem.screen.SwingGLCanvas;
import lsystem.screen.SwingGLCanvas2D;
import java.awt.*;
import java.awt.event.*;
public class JoglMouseListener2D implements MouseListener, MouseMotionListener, MouseWheelListener {
private final GLU glu;
private final SwingGLCanvas2D canvas;
private int button = 0;
private Point origine;
public JoglMouseListener2D(SwingGLCanvas2D canvas) {
this.canvas = canvas;
this.glu = canvas.glu;
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
if(button == 0) {
button = e.getButton();
origine = e.getPoint();
} else {
button = 0;
origine = null;
}
}
@Override
public void mouseReleased(MouseEvent e) {
button = 0;
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("exited " + canvas.camera[0] + ", " + canvas.camera[1]);
}
@Override
public void mouseDragged(MouseEvent e) {
if(origine != null) {
double xDiff = origine.getX() - e.getPoint().getX();
double yDiff = origine.getY() - e.getPoint().getY();
/* if(button == 2) {
canvas.camera[0] += Math.cos(canvas.camera[3]) * xDiff * 0.01;
canvas.camera[1] += Math.cos(canvas.camera[4]) * yDiff * 0.01;
canvas.camera[2] += Math.sin(canvas.camera[3]) * xDiff * 0.01;
} */
if(button == 3) {
canvas.camera[3] += xDiff * 0.1;
canvas.camera[4] += yDiff * 0.1;
}
origine = e.getPoint();
}
for (int i = 0; i < canvas.camera.length; i++) {
canvas.camera[i] = canvas.camera[i] % 360;
}
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if( 45 > Math.abs(canvas.camera[3]) && 45>Math.abs(canvas.camera[4]))
canvas.camera[0] += e.getWheelRotation() * Math.tan(Math.toRadians(-canvas.camera[3])) * 0.25;
else
canvas.camera[0] += e.getWheelRotation() * Math.tan(Math.toRadians(canvas.camera[3])) * 0.25;
canvas.camera[1] += e.getWheelRotation() * Math.tan(Math.toRadians(canvas.camera[4])) * 0.25;
if( 45 < Math.abs(canvas.camera[3]) || 45 < Math.abs(canvas.camera[4]))
canvas.camera[2] += -e.getWheelRotation()*0.25;
else
canvas.camera[2] += e.getWheelRotation()*0.25;
}
}

View File

@ -0,0 +1,59 @@
package lsystem.screen.listener;
import lsystem.screen.SwingGLCanvas;
import lsystem.screen.SwingGLCanvas2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class KeyboardListener2D implements KeyListener {
private final SwingGLCanvas2D canvas;
public KeyboardListener2D(SwingGLCanvas2D swingGLCanvas) {
this.canvas = swingGLCanvas;
}
@Override
public void keyTyped(KeyEvent e) {
switch (e.getKeyChar()) {
case 'z':
canvas.camera[2] -= 0.1f;
break;
case 's':
canvas.camera[2] += 0.1f;
break;
case 'q':
canvas.camera[0] -= 0.1f;
break;
case 'd':
canvas.camera[0] += 0.1f;
break;
case 'a':
canvas.camera[3] -= 1;
break;
case 'e':
canvas.camera[3] += 1;
break;
case 'w':
canvas.camera[1] += 0.1f;
break;
case 'x':
canvas.camera[1] -= 0.1f;
break;
}
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}