From 9e3c77574b3a5c24b08edb233e280f040571c40b Mon Sep 17 00:00:00 2001 From: Kishan Tripathi Date: Fri, 4 Jun 2021 23:04:06 +0530 Subject: [PATCH] Use Opengl 4.1+ --- src/main/java/PanoViewer/Utils/IOUtils.java | 24 ---------- src/main/java/PanoViewer/gui/PhotoSphere.java | 37 +++++++++++----- src/main/java/PanoViewer/gui/ZoomPanLis.java | 6 +-- src/main/java/PanoViewer/math/Math.java | 13 ------ src/main/java/PanoViewer/math/Sphere.java | 2 +- src/main/java/PanoViewer/math/UVMapping.java | 44 ------------------- src/main/java/PanoViewer/settings.java | 2 +- 7 files changed, 29 insertions(+), 99 deletions(-) delete mode 100644 src/main/java/PanoViewer/math/Math.java delete mode 100644 src/main/java/PanoViewer/math/UVMapping.java diff --git a/src/main/java/PanoViewer/Utils/IOUtils.java b/src/main/java/PanoViewer/Utils/IOUtils.java index 1f5891d..3f06a23 100644 --- a/src/main/java/PanoViewer/Utils/IOUtils.java +++ b/src/main/java/PanoViewer/Utils/IOUtils.java @@ -3,15 +3,12 @@ */ package PanoViewer.Utils; -import java.awt.image.BufferedImage; import java.io.File; -import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; -import javax.imageio.ImageIO; /** * @@ -20,25 +17,19 @@ public class IOUtils { public static InputStream getFileFromResourceAsStream(String fileName) { - - // The class loader that loaded the class Class currentClass = new Object() { }.getClass().getEnclosingClass(); ClassLoader classLoader = currentClass.getClassLoader(); InputStream inputStream = classLoader.getResourceAsStream(fileName); - - // the stream holding the file content if (inputStream == null) { return null; } else { return inputStream; } - } public static File getFileFromResource(String fileName) { File file = null; - // The class loader that loaded the class Class currentClass = new Object() { }.getClass().getEnclosingClass(); ClassLoader classLoader = currentClass.getClassLoader(); @@ -50,19 +41,4 @@ public static File getFileFromResource(String fileName) { } return file; } - - public static BufferedImage getBufferedImage(String fileName) { - BufferedImage img; - try { - Class currentClass = new Object() { - }.getClass().getEnclosingClass(); - ClassLoader classLoader = currentClass.getClassLoader(); - URL resource = classLoader.getResource(fileName); - img = ImageIO.read(resource); - } catch (IOException e) { - System.err.println("Error reading '" + fileName + '"'); - throw new RuntimeException(e); - } - return img; - } } diff --git a/src/main/java/PanoViewer/gui/PhotoSphere.java b/src/main/java/PanoViewer/gui/PhotoSphere.java index 83d4b2a..5ef0038 100644 --- a/src/main/java/PanoViewer/gui/PhotoSphere.java +++ b/src/main/java/PanoViewer/gui/PhotoSphere.java @@ -19,9 +19,8 @@ import static com.jogamp.opengl.GL.GL_TEXTURE0; import static com.jogamp.opengl.GL.GL_TEXTURE_2D; import static com.jogamp.opengl.GL.GL_TRIANGLES; -import com.jogamp.opengl.GL3; +import com.jogamp.opengl.GL4; import com.jogamp.opengl.GLAutoDrawable; -import com.jogamp.opengl.GLContext; import com.jogamp.opengl.GLEventListener; import com.jogamp.opengl.awt.GLCanvas; import com.jogamp.opengl.util.texture.Texture; @@ -85,9 +84,7 @@ void zoom(int zoomAmount) { instance.repaint(); } }; - this.addMouseListener(listener); - this.addMouseMotionListener(listener); - this.addMouseWheelListener(listener); + enableZoomPan(); fov = IDEAL_FOV; } @@ -98,13 +95,18 @@ public static PhotoSphere getInstance() { return instance; } + public static void destroyInstance() { + instance.destroy(); + instance = null; + } + public void replaceImage(BufferedImage image) { textureData = getTextureData(image); updateImage = true; instance.repaint(); } - private void replaceTextureData(GL3 gl) { + private void replaceTextureData(GL4 gl) { texture.updateImage(gl, textureData); textureData = null; updateImage = false; @@ -113,7 +115,7 @@ private void replaceTextureData(GL3 gl) { @Override public void init(GLAutoDrawable glad) { rendering_program = createShaderProgram("Shaders/vertex.shader", "Shaders/frag.shader"); - setupVertices(); + setupVertices(glad.getGL().getGL4()); aspect = (float) getWidth() / (float) getHeight(); pMat.setPerspective((float) Math.toRadians(fov), aspect, 0.1f, 1000.0f); vMat = camera.getViewMatrix(); @@ -123,7 +125,7 @@ public void init(GLAutoDrawable glad) { @Override public void dispose(GLAutoDrawable glad) { - GL3 gl = (GL3) GLContext.getCurrentGL().getGL3(); + GL4 gl = glad.getGL().getGL4(); gl.glDeleteProgram(rendering_program); gl.glDeleteVertexArrays(vao.length, vao, 0); gl.glDeleteBuffers(vbo.length, vbo, 0); @@ -132,7 +134,7 @@ public void dispose(GLAutoDrawable glad) { @Override public void display(GLAutoDrawable glad) { - GL3 gl = (GL3) GLContext.getCurrentGL(); + GL4 gl = glad.getGL().getGL4(); if (updateImage) { replaceTextureData(gl); } @@ -172,7 +174,7 @@ public void display(GLAutoDrawable glad) { @Override public void reshape(GLAutoDrawable glad, int x, int y, int widht, int height) { /*http://forum.jogamp.org/canvas-not-filling-frame-td4040092.html#a4040138*/ - GL3 gl = GLContext.getCurrentGL().getGL3(); + GL4 gl = glad.getGL().getGL4(); double dpiScalingFactor = ((Graphics2D) getGraphics()).getTransform().getScaleX(); widht = (int) (widht * dpiScalingFactor); height = (int) (height * dpiScalingFactor); @@ -181,9 +183,8 @@ public void reshape(GLAutoDrawable glad, int x, int y, int widht, int height) { pMat.setPerspective((float) Math.toRadians(70), aspect, 0.1f, 1000.0f); } - private void setupVertices() { + private void setupVertices(GL4 gl) { Sphere sphere = new Sphere(getPrecision()); - GL3 gl = GLContext.getCurrentGL().getGL3(); numVerts = sphere.getIndices().length; int[] indices = sphere.getIndices(); Vector3f[] vertices = sphere.getVertices(); @@ -212,4 +213,16 @@ private void setupVertices() { FloatBuffer texBuff = Buffers.newDirectFloatBuffer(texValue); gl.glBufferData(GL_ARRAY_BUFFER, texBuff.limit() * 4, texBuff, GL_STATIC_DRAW); } + + private void enableZoomPan() { + this.addMouseListener(listener); + this.addMouseMotionListener(listener); + this.addMouseWheelListener(listener); + } + + private void disableZoomPan() { + this.removeMouseListener(listener); + this.removeMouseMotionListener(listener); + this.removeMouseWheelListener(listener); + } } diff --git a/src/main/java/PanoViewer/gui/ZoomPanLis.java b/src/main/java/PanoViewer/gui/ZoomPanLis.java index 069b0a0..08ee49a 100644 --- a/src/main/java/PanoViewer/gui/ZoomPanLis.java +++ b/src/main/java/PanoViewer/gui/ZoomPanLis.java @@ -4,17 +4,15 @@ package PanoViewer.gui; import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; -import java.awt.event.MouseMotionListener; import java.awt.event.MouseWheelEvent; -import java.awt.event.MouseWheelListener; import static PanoViewer.settings.*; +import java.awt.event.MouseAdapter; /** * * @author kshan */ -public abstract class ZoomPanLis implements MouseListener, MouseMotionListener, MouseWheelListener { +public abstract class ZoomPanLis extends MouseAdapter { private int lastX; private int lastY; diff --git a/src/main/java/PanoViewer/math/Math.java b/src/main/java/PanoViewer/math/Math.java deleted file mode 100644 index 6e40c9b..0000000 --- a/src/main/java/PanoViewer/math/Math.java +++ /dev/null @@ -1,13 +0,0 @@ -/* - * - */ -package PanoViewer.math; - -import org.joml.Matrix3f; - -/** - * - * @author kshan - */ -public class Math { -} diff --git a/src/main/java/PanoViewer/math/Sphere.java b/src/main/java/PanoViewer/math/Sphere.java index 1711011..67ec807 100644 --- a/src/main/java/PanoViewer/math/Sphere.java +++ b/src/main/java/PanoViewer/math/Sphere.java @@ -14,7 +14,7 @@ */ public class Sphere { - private int numVertices, numIndices, horizontalP; // prec = precision + private int numVertices, numIndices, horizontalP; private int[] indices; private Vector3f[] vertices; private Vector2f[] texCoords; diff --git a/src/main/java/PanoViewer/math/UVMapping.java b/src/main/java/PanoViewer/math/UVMapping.java deleted file mode 100644 index affdd06..0000000 --- a/src/main/java/PanoViewer/math/UVMapping.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * - */ -package PanoViewer.math; - -import java.awt.geom.Point2D; -import org.joml.Vector3d; - -public class UVMapping { - - /** - * Returns the point of the texture image that is mapped to the given point in 3D space - * (given as {@link Vector3D}) See - * the Wikipedia article on UV - * mapping. - * - * @param vector the vector to which the texture point is mapped - * @return a point on the texture image somewhere in the rectangle between (0, 0) and - * (1, 1) - */ - public static Point2D.Double getTextureCoordinate(double x, double y, double z) { - final double u = 0.5 + (Math.atan2(x, z) / (2 * Math.PI)); - final double v = 0.5 + (Math.asin(y) / Math.PI); - return new Point2D.Double(u, v); - } - - /** - * For a given point of the texture (i.e. the image), return the point in 3D space where - * the point of the texture is mapped to (as {@link Vector3D}). - * - * @param u x-coordinate of the point on the texture (in the range between 0 and 1, from - * left to right) - * @param v y-coordinate of the point on the texture (in the range between 0 and 1, from - * top to bottom) - * @return the vector from the origin to where the point of the texture is mapped on the - * sphere - */ - public static Vector3d getVector(final double u, final double v) { - final double vectorY = Math.cos(v * Math.PI); - return new Vector3d(-Math.sin(2 * Math.PI * u) * Math.sqrt(1 - vectorY * vectorY), - -vectorY, -Math.cos(2 * Math.PI * u) * Math.sqrt(1 - vectorY * vectorY) - ); - } -} diff --git a/src/main/java/PanoViewer/settings.java b/src/main/java/PanoViewer/settings.java index 8278ed4..2c2b1ab 100644 --- a/src/main/java/PanoViewer/settings.java +++ b/src/main/java/PanoViewer/settings.java @@ -37,7 +37,7 @@ static GLProfile getMaxProfile() { } public static boolean checkMinimumVersion() { - return GLProfile.isAvailable(GLProfile.GL3); + return GLProfile.isAvailable(GLProfile.GL4); } public static float getDragSenstivity() {