done somes optimization with 3d + fixed potential rewriter issue

This commit is contained in:
Quentin Legot 2021-02-23 00:23:42 +01:00
parent 1eda03f965
commit 3fe3b481d8
4 changed files with 47 additions and 84 deletions

View File

@ -21,7 +21,7 @@ public class Rewrite {
String toRewrite = rewritted;
for(int j = 0; j < rules.size(); ++j){
Pair<String, String> pair = rules.get(j);
toRewrite = toRewrite.replace(pair.getLeft(), "$" + j);
toRewrite = toRewrite.replace(pair.getLeft(), "${" + j + "}");
}
return toRewrite;
}
@ -29,7 +29,7 @@ public class Rewrite {
private String replaceIDByRuleApplication(final String toRewrite) {
String rewritted = toRewrite;
for(int j = 0; j < rules.size(); ++j){
rewritted = rewritted.replace("$" + j, rules.get(j).getRight());
rewritted = rewritted.replace("${" + j + "}", rules.get(j).getRight());
}
return rewritted;
}

View File

@ -1,7 +1,6 @@
package lsystem.screen;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.gl2.GLUT;
public class DrawHelper {
@ -12,11 +11,9 @@ public class DrawHelper {
gl.glRotatef(canvas.camera[5], 0f, 0f, 1f);
}
public static void drawAxes(GL2 gl, GLUT glut, SwingGLCanvas canvas){
gl.glDisable(GL2.GL_LIGHTING);
gl.glDisable(GL2.GL_LIGHT0);
public static void drawAxes(GL2 gl, GLUT glut){
gl.glRasterPos3f(0f, 0f, 0f);
gl.glColor3f(1f, 0f, 0f);
gl.glColor3f(255f, 0f, 0f);
gl.glBegin(GL2.GL_LINES);
gl.glVertex3f(0f, 0f, 0f);
gl.glVertex3f(1f, 0f, 0f);
@ -27,13 +24,13 @@ public class DrawHelper {
gl.glVertex3f(0f, 0f, 0f);
gl.glVertex3f(0f, 0f, 1f);
gl.glColor3f(1f, 1f, 1f);
for(int i = -50; i < 51; i++) {
gl.glVertex3f(-50f, 0f, i);
gl.glVertex3f(50f, 0f, i);
gl.glColor3f(255f, 255f, 255f);
for(int i = -25; i < 26; i++) {
gl.glVertex3f(-25f, 0f, i);
gl.glVertex3f(25f, 0f, i);
gl.glVertex3f(i, 0, -50f);
gl.glVertex3f(i, 0, 50f);
gl.glVertex3f(i, 0, -25f);
gl.glVertex3f(i, 0, 25f);
}
gl.glEnd();
gl.glRasterPos3f(1.1f, 0.0f, 0.0f);
@ -44,37 +41,25 @@ public class DrawHelper {
glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, 'Z'); //draw the z axis
}
public static void drawRectangular0Prism(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.glBegin(GL2.GL_TRIANGLES);
gl.glBegin(GL2.GL_QUADS);
gl.glVertex3f(leftBottomX, leftBottomY, leftBottomZ);
gl.glVertex3f(rightUpX, leftBottomY, leftBottomZ);
gl.glVertex3f(rightUpX, rightUpY, leftBottomZ);
gl.glVertex3f(leftBottomX, rightUpY, leftBottomZ);
gl.glVertex3f(leftBottomX, leftBottomY, leftBottomZ);
gl.glVertex3f(leftBottomX, rightUpY, leftBottomZ);
gl.glVertex3f(rightUpX, rightUpY, leftBottomZ);
gl.glVertex3f(leftBottomX, leftBottomY, leftBottomZ);
gl.glVertex3f(leftBottomX, rightUpY, leftBottomZ);
gl.glVertex3f(leftBottomX, leftBottomY, rightUpZ);
gl.glVertex3f(leftBottomX, rightUpY, leftBottomZ);
gl.glVertex3f(leftBottomX, rightUpY, rightUpZ);
gl.glVertex3f(leftBottomX, leftBottomY, rightUpZ);
gl.glVertex3f(leftBottomX, rightUpY, leftBottomZ);
gl.glVertex3f(rightUpX, leftBottomY, leftBottomZ);
gl.glVertex3f(rightUpX, rightUpY, leftBottomZ);
gl.glVertex3f(rightUpX, leftBottomY, rightUpZ);
gl.glVertex3f(rightUpX, rightUpY, leftBottomZ);
gl.glVertex3f(rightUpX, rightUpY, rightUpZ);
gl.glVertex3f(rightUpX, leftBottomY, rightUpZ);
gl.glVertex3f(rightUpX, rightUpY, leftBottomZ);
gl.glVertex3f(leftBottomX, leftBottomY, rightUpZ);
gl.glVertex3f(leftBottomX, rightUpY, rightUpZ);
gl.glVertex3f(rightUpX, leftBottomY, rightUpZ);
gl.glVertex3f(rightUpX, leftBottomY, rightUpZ);
gl.glVertex3f(rightUpX, rightUpY, rightUpZ);
gl.glVertex3f(leftBottomX, rightUpY, rightUpZ);
@ -82,26 +67,10 @@ public class DrawHelper {
gl.glPopMatrix();
}
public static void drawDebugInformation(GL2 gl, GLU glu, GLUT glut, SwingGLCanvas canvas, int window_height, int window_width) {
public static void drawDebugInformation(GL2 gl, GLUT glut, SwingGLCanvas 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]);
gl.glDepthMask(true);
}
public static void prepareDraw2D(GL2 gl, GLUT glut, SwingGLCanvas canvas) {
gl.glDisable(GL2.GL_LIGHTING);
gl.glDisable(GL2.GL_LIGHT0);
}
public static void prepareDraw3D(GL2 gl, GLUT glut, SwingGLCanvas canvas) {
gl.glEnable(GL2.GL_LIGHTING);
gl.glEnable(GL2.GL_LIGHT0);
gl.glEnable(GL2.GL_DEPTH_TEST);
}
private static double distance(double x1, double y1, double z1, double x2, double y2, double z2) {
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2) + Math.pow(z1 - z2, 2));
}
}

View File

@ -9,15 +9,11 @@ import com.jogamp.opengl.util.gl2.GLUT;
import lsystem.screen.listener.JoglEventListener;
import lsystem.screen.listener.JoglMouseListener;
import lsystem.screen.listener.KeyboardListener;
import lsystem.utils.Pair;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.LinkedList;
import java.util.Random;
public class SwingGLCanvas {
public final GLCanvas glCanvas;
@ -25,7 +21,6 @@ public class SwingGLCanvas {
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 LinkedList<Pair<Integer, Integer>> prismPosition = new LinkedList<>();
public SwingGLCanvas() {
GLProfile glProfile = GLProfile.getDefault();
@ -49,14 +44,6 @@ public class SwingGLCanvas {
jframe.getContentPane().add(glCanvas, BorderLayout.CENTER);
jframe.setSize(Constants.WIDTH, Constants.HEIGHT);
for(int i = -50; i < 51; ++i) {
for(int j = -50; j < 51; ++j) {
if(new Random().nextFloat() < 0.05) {
prismPosition.add(new Pair<Integer, Integer>(i, j));
}
}
}
animator.start();
jframe.setVisible(true);
}

View File

@ -1,30 +1,30 @@
package lsystem.screen.listener;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;
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.utils.Pair;
import java.util.LinkedList;
import java.util.Random;
public class JoglEventListener implements GLEventListener {
private final SwingGLCanvas 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 float[] material_specular = {0.8f, 0.8f, 0.8f, 0.8f};
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 width;
private int height;
private int fps;
public JoglEventListener(SwingGLCanvas swingGLCanvas) {
@ -54,6 +54,14 @@ public class JoglEventListener implements GLEventListener {
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){
@ -67,7 +75,6 @@ public class JoglEventListener implements GLEventListener {
}
}
}).start();
}
@Override
@ -82,13 +89,7 @@ public class JoglEventListener implements GLEventListener {
gl.glLoadIdentity();
gl.glTranslatef(0, 0, 0);
DrawHelper.placeCamera(gl, canvas);
// x, y, z, x of where the camera looks at, y of where the camera looks at, z of where the camera looks at
glu.gluLookAt(canvas.camera[0], canvas.camera[1], canvas.camera[2], canvas.camera[0], canvas.camera[1], canvas.camera[2] - 1, 0f, 1f, 0f);
DrawHelper.prepareDraw3D(gl, glut, canvas);
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.glPushMatrix();
gl.glTranslatef(0f, 0f, -4f);
@ -104,23 +105,24 @@ public class JoglEventListener implements GLEventListener {
glut.glutSolidSphere(0.75f, 20, 20);
gl.glPopMatrix();
angle += 0.1f;
angle %= 360f;
angle = (angle + 0.1f) % 360;
gl.glColor3f(0, 144, 255);
canvas.prismPosition.forEach(pair ->
DrawHelper.drawRectangular0Prism(gl, pair.getLeft(), 0f, pair.getRight(), pair.getLeft() + 1, 1f, pair.getRight() + 1));
gl.glColor3f(0f, 0.5f, 1f);
gl.glPushMatrix();
for(Pair<Integer, Integer> pair : prismPosition) {
DrawHelper.drawRectangularPrism(gl, pair.getLeft(), 0f, pair.getRight(), pair.getLeft() + 1, 1f, pair.getRight() + 1);
}
gl.glPopMatrix();
gl.glTranslatef(0f, 0f, 0f);
DrawHelper.drawAxes(gl, glut, canvas);
DrawHelper.drawDebugInformation(gl, glu, glut, canvas, glAutoDrawable.getSurfaceHeight(), glAutoDrawable.getSurfaceWidth());
DrawHelper.drawAxes(gl, glut);
DrawHelper.drawDebugInformation(gl, glut, canvas);
gl.glFlush();
fps++;
}
@Override
public void reshape(GLAutoDrawable glAutoDrawable, int x, int y, int width, int height) {
this.width = width;
this.height = height;
GL2 gl = glAutoDrawable.getGL().getGL2();
gl.glViewport(x, y, width, height);
@ -128,6 +130,11 @@ public class JoglEventListener implements GLEventListener {
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);
}
}