Skip to content

Instantly share code, notes, and snippets.

@ronnyworm
Created March 25, 2017 20:45
Show Gist options
  • Save ronnyworm/c3c2103b169daf969224f006929e942f to your computer and use it in GitHub Desktop.
Save ronnyworm/c3c2103b169daf969224f006929e942f to your computer and use it in GitHub Desktop.
public class Camera {
float pos_x;
float pos_y;
float pos_z;
float rot_x;
float rot_y;
float rot_z;
float change_pos_x;
float change_pos_y;
float change_pos_z;
float change_rot_x;
float change_rot_y;
float change_rot_z;
float pos_speed;
float rot_speed;
public Camera(){
pos_speed = 0.02f;
rot_speed = 0.1f;
pos_z = -10;
rot_x = 45;
rot_y = 10;
}
}
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
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.glu.GLU;
import com.jogamp.opengl.util.Animator;
public class Functions3D implements GLEventListener, MouseMotionListener, MouseWheelListener, KeyListener {
public Camera cam = new Camera();
private int swapInterval;
private int oldMouseX;
private int oldMouseY;
private boolean showGrid = true;
private int colorStyle = 1;
public static void main(String[] args) {
java.awt.Frame frame = new java.awt.Frame("Functions3D");
frame.setSize(800, 800);
frame.setLayout(new java.awt.BorderLayout());
final Animator animator = new Animator();
frame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
// Run this on another thread than the AWT event queue to
// make sure the call to Animator.stop() completes before
// exiting
new Thread(new Runnable() {
public void run() {
animator.stop();
System.exit(0);
}
}).start();
}
});
GLCanvas canvas = new GLCanvas();
animator.add(canvas);
final Functions3D demo = new Functions3D();
canvas.addGLEventListener(demo);
canvas.addMouseWheelListener((java.awt.event.MouseWheelListener)demo);
canvas.addMouseMotionListener((java.awt.event.MouseMotionListener)demo);
canvas.addKeyListener((java.awt.event.KeyListener)demo);
frame.add(canvas, java.awt.BorderLayout.CENTER);
frame.validate();
frame.setVisible(true);
animator.start();
}
public Functions3D(){
this.swapInterval = 1;
}
public static void rotate(GL2 gl, float x, float y, float z){
gl.glRotatef(x, 1, 0, 0);
gl.glRotatef(y, 0, 1, 0);
gl.glRotatef(z, 0, 0, 1);
}
public void colorFunc(GL2 gl, double span, double i, double j){
switch(colorStyle % 4){
case 0:gl.glColor3f((float)(((i/span)+1)/2.0), (float)(1.0-(((j/span)+1)/2.0)), (float)(1.0-(((i/span)+1)/2.0)));break;
case 1:gl.glColor3f((float)((((i/span)+1)/2.0)), (float)(((i/span)+1)/2.0), (float)(((i/span)+1)/2.0));break;
case 2:gl.glColor3f((float)(0.7-((i/span)+1)/3.0), (float)(1.0-(((j/span)+1)/2.0)), (float)(0.5-(((i/span)+1)/6.0)));break;
case 3:gl.glColor3f((float)(1.0-((i/span)+1)/3.0), (float)(((j/span)+1)/2.0), (float)(1.0-(((i/span)+1)/6.0)));break;
}
}
@Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT|GL2.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glPushMatrix();
{
gl.glTranslatef(cam.pos_x, cam.pos_y, cam.pos_z);
rotate(gl, cam.rot_x, cam.rot_y, cam.rot_z);
gl.glColor3f(0.0f, 0.0f, 0.0f);
float intervalPlane=0.1f;
float intervalGrid=0.2f;
float span=10.0f;
float stauchung=1.0f;
float gridAbhebung=span/100.0f;
for(float i=-span;i<span;i+=intervalPlane){
for(float j=-span;j<span;j+=intervalPlane){
colorFunc(gl, span,i,j);
gl.glBegin(GL2.GL_QUADS);
gl.glVertex3f(i,func(i,j)*stauchung,j);
gl.glVertex3f(i+intervalPlane,func(i+intervalPlane,j)*stauchung,j);
gl.glVertex3f(i+intervalPlane,func(i+intervalPlane,j+intervalPlane)*stauchung,j+intervalPlane);
gl.glVertex3f(i,func(i,j+intervalPlane)*stauchung,j+intervalPlane);
gl.glEnd();
}
}
if(showGrid){
for(float i=-span;i<span;i+=intervalGrid)
for(float j=-span;j<span;j+=intervalGrid){
gl.glColor3f(0.0f, 0.0f, 0.0f);
gl.glBegin(GL2.GL_LINES);
gl.glVertex3f(i,(func(i,j)+gridAbhebung)*stauchung,j);
gl.glVertex3f(i+intervalGrid,(func(i+intervalGrid,j)+gridAbhebung)*stauchung,j);
gl.glVertex3f(i,(func(i,j)+gridAbhebung)*stauchung,j);
gl.glVertex3f(i,(func(i,j+intervalGrid)+gridAbhebung)*stauchung,j+intervalGrid);
gl.glEnd();
}
}
}
gl.glPopMatrix();
}
@Override
public void dispose(GLAutoDrawable arg0) {
}
@Override
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glEnable(GL2.GL_COLOR_MATERIAL);
gl.glClearColor(1.0f,1.0f,1.0f,1.0f);
gl.glBlendFunc(GL2.GL_SRC_ALPHA,GL2.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL2.GL_BLEND);
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
GL2 gl = drawable.getGL().getGL2();
GLU glu = new GLU();
gl.setSwapInterval(swapInterval);
gl.glViewport(0,0,w,h);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0,(double)w/(double)h,0.01,200.0);
}
public float func(double x,double y){
return (float)(Math.sin(0.5*x)*Math.sin(0.3*y));
//return (float)(x*x*x - 3*x*x*y + 3*x*y*y + y*y*y - 3*x - 2*y);
}
@Override
public void mouseDragged(MouseEvent e) {
if(oldMouseX - e.getX() > -20 && oldMouseX - e.getX() < 20 &&
e.getY() - oldMouseY > -20 && e.getY() - oldMouseY < 20){
cam.rot_y += (oldMouseX - e.getX()) % 360.0f;
cam.rot_x += (e.getY() - oldMouseY) % 360.0f;
}
oldMouseX = e.getX();
oldMouseY = e.getY();
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
cam.pos_z -= e.getWheelRotation();
if(cam.pos_z > -5)
cam.pos_z = -5;
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int kc = e.getKeyCode();
switch(kc){
case KeyEvent.VK_G:
showGrid = !showGrid;break;
case KeyEvent.VK_1:
colorStyle = 1;break;
case KeyEvent.VK_2:
colorStyle = 2;break;
case KeyEvent.VK_3:
colorStyle = 3;break;
case KeyEvent.VK_4:
colorStyle = 4;break;
}
/*if(KeyEvent.VK_LEFT == kc) {
view_roty -= 1;
} else if(KeyEvent.VK_RIGHT == kc) {
view_roty += 1;
} else if(KeyEvent.VK_UP == kc) {
view_rotx -= 1;
} else if(KeyEvent.VK_DOWN == kc) {
view_rotx += 1;
}*/
}
@Override
public void keyReleased(KeyEvent e) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment