Skip to content

Instantly share code, notes, and snippets.

@alicanalbayrak
Created June 16, 2016 11:39
Show Gist options
  • Save alicanalbayrak/489f80759460621c4623dffef112bf09 to your computer and use it in GitHub Desktop.
Save alicanalbayrak/489f80759460621c4623dffef112bf09 to your computer and use it in GitHub Desktop.
Test: Java opengl (jogl) double precision
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import static com.jogamp.opengl.GL.GL_COLOR_BUFFER_BIT;
import static com.jogamp.opengl.GL.GL_TRIANGLES;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.FPSAnimator;
@SuppressWarnings("serial")
public class DoubleTest extends GLCanvas implements GLEventListener {
private static String TITLE = "Double test";
private static final int CANVAS_WIDTH = 320;
private static final int CANVAS_HEIGHT = 240;
private static final int FPS = 60;
private double left = 99.9999d;
private double right = 100.001d;
private double bottom = -5d;
private double top = 400d;
public DoubleTest() {
this.addGLEventListener(this);
}
@Override
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL2 gl = drawable.getGL().getGL2();
gl.glViewport(0, 0, width, height);
gl.glOrtho(left, right, bottom, top, -1, 1);
}
@Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL_COLOR_BUFFER_BIT);
gl.glEnable(GL2.GL_LINE_SMOOTH);
gl.glEnable(GL2.GL_BLEND);
gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
gl.glHint(GL2.GL_LINE_SMOOTH_HINT, GL2.GL_DONT_CARE);
gl.glLoadIdentity();
gl.glOrtho(left, right, bottom, top, -1, 1);
gl.glBegin(GL_TRIANGLES);
gl.glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
gl.glVertex2d(left, bottom);
gl.glVertex2d(100.00045, top);
gl.glVertex2d(right, bottom);
gl.glEnd();
gl.glBegin(GL2.GL_LINE_STRIP);
int j = -5;
double offset = (0.001 / 8000);
for (int i = 0; i < 8000; i++) {
int y = j++;
double x = 100 + (offset * i);
gl.glColor3f(0f, 1f, 1f);
gl.glVertex2d(x, y);
if (j == 200) {
j = -5;
}
}
gl.glEnd();
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glOrtho(0d, right - left, bottom, top, -1, 1);
gl.glBegin(GL2.GL_LINE_STRIP);
j = 200;
offset = (0.001 / 8000);
for (int i = 0; i < 8000; i++) {
int y = j++;
double x = 100 + (offset * i);
// int remove = BigDecimal.valueOf(x).intValue();
gl.glColor3f(1f, 1f, 0f);
gl.glVertex2d(x - left, y);
if (j == 400) {
j = 200;
}
}
gl.glEnd();
gl.glPopMatrix();
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glPopMatrix();
}
@Override
public void dispose(GLAutoDrawable drawable) {
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GLCanvas canvas = new DoubleTest();
canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
final FPSAnimator animator = new FPSAnimator(canvas, FPS, true);
final JFrame frame = new JFrame();
frame.getContentPane().add(canvas);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
new Thread() {
@Override
public void run() {
if (animator.isStarted()) animator.stop();
System.exit(0);
}
}.start();
}
});
frame.setTitle(TITLE);
frame.pack();
frame.setVisible(true);
animator.start();
}
});
}
}
@IratzsMicheal
Copy link

Hello please will you like to show me some help on my application PLEASE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment