Skip to content

Commit

Permalink
Just get it running (lots of work to do)
Browse files Browse the repository at this point in the history
  • Loading branch information
i-make-robots committed Jun 26, 2024
1 parent a07eb8f commit c57261f
Show file tree
Hide file tree
Showing 21 changed files with 344 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class DrawingHelper {
* @param radius radius
*/
public static void drawCircle(GL3 gl, float x, float y, float radius) {
// TODO implement me
/*
gl.glTranslatef(x, y, 0);
gl.glBegin(GL3.GL_LINE_LOOP);
for (float f = 0; f < 2.0 * Math.PI; f += 0.3f) {
Expand All @@ -24,7 +26,7 @@ public static void drawCircle(GL3 gl, float x, float y, float radius) {
Math.sin(f) * radius);
}
gl.glEnd();
gl.glTranslatef(-x, -y, 0);
gl.glTranslatef(-x, -y, 0);*/
}

/**
Expand All @@ -37,6 +39,8 @@ public static void drawCircle(GL3 gl, float x, float y, float radius) {
* @param a2 end angle
*/
public static void drawArc(GL3 gl, float x, float y, float radius, float a1, float a2) {
// TODO implement me
/*
gl.glBegin(GL3.GL_LINE_STRIP);
int steps = 10;
float delta = (a2 - a1) / (float) steps;
Expand All @@ -47,7 +51,7 @@ public static void drawArc(GL3 gl, float x, float y, float radius, float a1, flo
y + Math.sin(f) * radius);
f += delta;
}
gl.glEnd();
gl.glEnd();*/
}

/**
Expand All @@ -59,12 +63,14 @@ public static void drawArc(GL3 gl, float x, float y, float radius, float a1, flo
* @param left left coordinate
*/
public static void drawRectangle(GL3 gl, double top, double right, double bottom, double left) {
// TODO implement me
/*
gl.glBegin(GL3.GL_QUADS);
gl.glVertex2d(left, top);
gl.glVertex2d(right, top);
gl.glVertex2d(right, bottom);
gl.glVertex2d(left, bottom);
gl.glEnd();
gl.glEnd();*/
}

/**
Expand All @@ -76,6 +82,8 @@ public static void drawRectangle(GL3 gl, double top, double right, double bottom
* @param height height of the texture
*/
public static void paintTexture(GL3 gl, TextureWithMetadata texture, double x, double y, double width, double height) {
// TODO implement me
/*
texture.use(gl);
gl.glColor4d(1, 1, 1, 1);
gl.glEnable(GL3.GL_TEXTURE_2D);
Expand All @@ -89,6 +97,6 @@ public static void paintTexture(GL3 gl, TextureWithMetadata texture, double x, d
gl.glTexCoord2d(0, 1);
gl.glVertex2d(x, y + height);
gl.glEnd();
gl.glDisable(GL3.GL_TEXTURE_2D);
gl.glDisable(GL3.GL_TEXTURE_2D);*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,11 @@ public static Matrix4d orthographicMatrix4d(double left, double right, double bo
ortho.setOrtho(left, right, bottom, top, near, far).get(list);
return new Matrix4d(list);
}

public static Matrix4d perspectiveMatrix4d(double fovY, double aspect, double near, double far) {
double [] list = new double[16];
org.joml.Matrix4d perspective = new org.joml.Matrix4d();
perspective.setPerspective(Math.toRadians(fovY), aspect, near, far).get(list);
return new Matrix4d(list);
}
}
37 changes: 21 additions & 16 deletions src/main/java/com/marginallyclever/makelangelo/Camera.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class Camera {
public static final double ZOOM_STEP_SIZE = 0.15;

// scale + position
private double offsetX = 0.0;
private double offsetY = 0.0;
private double px = 0.0;
private double py = 0.0;
private double zoom = 1.0;

// window size (for aspect ratio?)
Expand All @@ -30,13 +30,14 @@ public Camera() {}
*/
public void moveRelative(double dx, double dy) {
double scale = PreferencesHelper.getPreferenceNode(PreferencesHelper.MakelangeloPreferenceKey.GRAPHICS).getInt("dragSpeed", 1);
offsetX += dx * scale / zoom;
offsetY += dy * scale / zoom;
// TODO moving camera with these scale factors is pretty close but could be better.
px += dx * scale * zoom/height;
py += dy * scale * zoom/height;
}

private void limitCameraZoom() {
if(zoom< CAMERA_ZOOM_MIN) zoom= CAMERA_ZOOM_MIN;
if(zoom> CAMERA_ZOOM_MAX) zoom= CAMERA_ZOOM_MAX;
if(zoom < CAMERA_ZOOM_MIN) zoom = CAMERA_ZOOM_MIN;
if(zoom > CAMERA_ZOOM_MAX) zoom = CAMERA_ZOOM_MAX;
}

// scale the picture of the robot to fake a zoom.
Expand All @@ -51,8 +52,9 @@ public void zoom(int amount) {
*/
public Point2D screenToWorldSpace(Point2D input) {
Point2D output = new Point2D();
output.x = input.x/zoom + offsetX;
output.y = input.y/zoom + offsetY;
// TODO this is not quite right.
output.x = px + input.x * zoom/width;
output.y = py + input.y * zoom/width;
return output;
}

Expand All @@ -63,29 +65,32 @@ public Point2D screenToWorldSpace(Point2D input) {
*/
public void zoom(int amount, Point2D cursor) {
Point2D before = screenToWorldSpace(cursor);
zoom -= (double)amount * ZOOM_STEP_SIZE;
//zoom -= (double)amount * ZOOM_STEP_SIZE;
double zoomScale = (double)amount * ZOOM_STEP_SIZE;
zoom = zoom * (1.0 + zoomScale);

limitCameraZoom();
Point2D after = screenToWorldSpace(cursor);

offsetX -= after.x - before.x;
offsetY -= after.y - before.y;
px -= after.x - before.x;
py -= after.y - before.y;
}

// scale the picture of the robot to fake a zoom.
public void zoomToFit(double w,double h) {
offsetX = 0;
offsetY = 0;
zoom = Math.max(w/h, h/w);
px = 0;
py = 0;
zoom = Math.max(w,h) * Math.cos(Math.toRadians(60))*2;

limitCameraZoom();
}

public double getX() {
return offsetX;
return px;
}

public double getY() {
return offsetY;
return py;
}

public double getZoom() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ private boolean confirmClose() {
int result = JOptionPane.showConfirmDialog(mainFrame, Translator.get("ConfirmQuitQuestion"),
Translator.get("ConfirmQuitTitle"), JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
previewPanel.removeListener(myPlotter);
// TODO previewPanel.removeListener(myPlotter);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myPlotter.getSettings().save();
plotterSettingsManager.setLastSelectedProfile(myPlotter.getSettings().getUID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class PreviewPanel extends JPanel implements GLEventListener {
private int mouseLastZoomDirection = 0;

private final Plotter myPlotter;
private final Paper myPaper;
private PlotterRenderer myPlotterRenderer;
private final TurtleRenderFacade myTurtleRenderer = new TurtleRenderFacade();
private final DoubleRangeSlider rangeSlider = new DoubleRangeSlider();
Expand All @@ -84,6 +85,7 @@ public PreviewPanel() {
public PreviewPanel(Paper paper, Plotter plotter) {
super(new BorderLayout());
myPlotter = plotter;
myPaper = paper;

add(toolBar, BorderLayout.NORTH);
add(glCanvas, BorderLayout.CENTER);
Expand Down Expand Up @@ -214,12 +216,12 @@ private void buildToolBar() {
buttonZoomIn.addActionListener((e) -> camera.zoom(-1));
buttonZoomIn.setIcon(new ImageIcon(Objects.requireNonNull(getClass().getResource("/com/marginallyclever/makelangelo/icons8-zoom-in-16.png"))));
toolBar.add(buttonZoomIn);
/*

JButton buttonZoomToFit = new JButton(Translator.get("MenuView.zoomFit"));
//buttonZoomToFit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0, SHORTCUT_CTRL));
buttonZoomToFit.addActionListener((e) -> camera.zoomToFit(app.getPaper().getPaperWidth(),app.getPaper().getPaperHeight()));
buttonZoomToFit.addActionListener((e) -> camera.zoomToFit(myPaper.getPaperWidth(),myPaper.getPaperHeight()));
toolBar.add(buttonZoomToFit);
*/

JCheckBox checkboxShowPenUpMoves = new JCheckBox(Translator.get("GFXPreferences.showPenUp"), GFXPreferences.getShowPenUp());
//checkboxShowPenUpMoves.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, SHORTCUT_CTRL));//"ctrl M"
checkboxShowPenUpMoves.addActionListener((e) -> {
Expand Down Expand Up @@ -290,8 +292,8 @@ public void init(GLAutoDrawable glAutoDrawable) {

try {
shader = new ShaderProgram(gl3,
ResourceHelper.readResource(this.getClass(), "/com/marginallyclever/ro3/apps/viewport/default.vert"),
ResourceHelper.readResource(this.getClass(), "/com/marginallyclever/ro3/apps/viewport/default.frag"));
ResourceHelper.readResource(this.getClass(), "default.vert"),
ResourceHelper.readResource(this.getClass(), "default.frag"));
} catch(Exception e) {
logger.error("Failed to load shader", e);
}
Expand Down Expand Up @@ -340,20 +342,25 @@ public void display(GLAutoDrawable glautodrawable) {
// turn on blending
gl3.glEnable(GL3.GL_BLEND);
gl3.glBlendFunc(GL3.GL_SRC_ALPHA, GL3.GL_ONE_MINUS_SRC_ALPHA);


gl3.glDisable(GL3.GL_CULL_FACE);
gl3.glDisable(GL3.GL_DEPTH_TEST);

paintBackground(gl3);

shader.use(gl3);
//shader.setVector3d(gl3,"lightPos",cameraWorldPos); // Light position in world space
shader.setColor(gl3,"lightColor", Color.WHITE);
shader.setColor(gl3,"diffuseColor",Color.WHITE);
shader.setColor(gl3,"specularColor",Color.BLACK);
shader.setColor(gl3,"specularColor",Color.WHITE);
shader.setColor(gl3,"ambientColor",Color.BLACK);
shader.set1i(gl3,"useVertexColor",0);
shader.set1i(gl3,"useVertexColor",1);
shader.set1i(gl3,"useLighting",0);
shader.set1i(gl3,"diffuseTexture",0);
shader.set1i(gl3,"useTexture",1);
shader.set1i(gl3,"useTexture",0);

shader.setMatrix4d(gl3,"projectionMatrix", MatrixHelper.createIdentityMatrix4());
shader.setMatrix4d(gl3,"viewMatrix", MatrixHelper.createIdentityMatrix4());
shader.setMatrix4d(gl3,"modelMatrix", MatrixHelper.createIdentityMatrix4());

paintCamera(gl3);
Expand All @@ -366,26 +373,25 @@ public void display(GLAutoDrawable glautodrawable) {
}

/**
* Set up the correct modelview so the robot appears where it should.
* Set up the correct model view so the robot appears where it should.
*
* @param gl3 OpenGL context
*/
private void paintCamera(GL3 gl3) {
//gl.glScaled(camera.getZoom(),camera.getZoom(),1);
//gl.glTranslated(-camera.getX(), camera.getY(),0);

Matrix4d inverseCamera = new Matrix4d();
inverseCamera.setIdentity();
// scale
inverseCamera.m00 = camera.getZoom();
inverseCamera.m11 = camera.getZoom();
Matrix4d inverseCamera = MatrixHelper.createIdentityMatrix4();
// translate
inverseCamera.setTranslation(new Vector3d(-camera.getX(),-camera.getY(),0));
//inverseCamera.setTranslation(new Vector3d(-camera.getX(),camera.getY(),5));
inverseCamera.setTranslation(new Vector3d(camera.getX(),-camera.getY(),camera.getZoom()));
inverseCamera.invert();
inverseCamera.transpose();

shader.setMatrix4d(gl3,"viewMatrix",inverseCamera);
shader.setMatrix4d(gl3,"projectionMatrix",getOrthographicMatrix(camera.getWidth(),camera.getHeight()));
Vector3d cameraWorldPos = new Vector3d(camera.getX(),-camera.getY(),0);
shader.setVector3d(gl3,"cameraPos",cameraWorldPos); // Camera position in world space
//shader.setMatrix4d(gl3,"projectionMatrix",getOrthographicMatrix(camera.getWidth(),camera.getHeight()));
shader.setMatrix4d(gl3,"projectionMatrix",getPerspectiveFrustum(camera.getWidth(),camera.getHeight()));

// only needed with useLighting=1
//Vector3d cameraWorldPos = new Vector3d(-camera.getX(),camera.getY(),-5);
//shader.setVector3d(gl3,"cameraPos",cameraWorldPos); // Camera position in world space
}

/**
Expand All @@ -394,7 +400,13 @@ private void paintCamera(GL3 gl3) {
public Matrix4d getOrthographicMatrix(int width,int height) {
double h = 5.0; // why 5?
double w = h * (double)width / (double)height;
return MatrixHelper.orthographicMatrix4d(-w,w,-h,h,0.001,10);
return MatrixHelper.orthographicMatrix4d(-w,w,-h,h,1,100);
}


public Matrix4d getPerspectiveFrustum(int width,int height) {
double aspect = (double)width / (double)height;
return MatrixHelper.perspectiveMatrix4d(60,aspect,1,1000);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.marginallyclever.makelangelo.apps.previewpanel;

import com.jogamp.opengl.GL3;
import com.marginallyclever.convenience.helpers.OpenGLHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Loading

0 comments on commit c57261f

Please sign in to comment.