Skip to content

Commit

Permalink
Use Opengl 4.1+
Browse files Browse the repository at this point in the history
  • Loading branch information
GhostFoxSledgehammer committed Jun 4, 2021
1 parent 6547f13 commit 9e3c775
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 99 deletions.
24 changes: 0 additions & 24 deletions src/main/java/PanoViewer/Utils/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*
Expand All @@ -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();
Expand All @@ -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;
}
}
37 changes: 25 additions & 12 deletions src/main/java/PanoViewer/gui/PhotoSphere.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,9 +84,7 @@ void zoom(int zoomAmount) {
instance.repaint();
}
};
this.addMouseListener(listener);
this.addMouseMotionListener(listener);
this.addMouseWheelListener(listener);
enableZoomPan();
fov = IDEAL_FOV;
}

Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
}
6 changes: 2 additions & 4 deletions src/main/java/PanoViewer/gui/ZoomPanLis.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/PanoViewer/math/Math.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/PanoViewer/math/Sphere.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 0 additions & 44 deletions src/main/java/PanoViewer/math/UVMapping.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/PanoViewer/settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 9e3c775

Please sign in to comment.