Login  Register

Distortion Along y axis

Posted by John Johns on Oct 10, 2016; 4:44am
URL: https://forum.jogamp.org/Distortion-Along-y-axis-tp4037298.html

Hey, I am trying to create a basic environment to move a player on a 3D plane, but there is a large distortion along the y axis. I am uncertain, but it may have something to do with gluPerspective or such. This is a link to a stackoverflow post I made that has screenshots of the problem if that helps:

https://stackoverflow.com/questions/39951045/opengl-perspective-distorting-scene-much-more-than-expected-along-y-axis

It is fairly obvious that the object does not look the way it is supposed to, but I can not find where the distortion is occurring. Any help would be greatly appreciated.

==================CODE====================
I have stared the lines where I would guess the problem is created, but I am not certain

Driver class:
import java.awt.Cursor;
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import com.jogamp.opengl.GL;
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 Driver implements GLEventListener,KeyListener,MouseListener{
        private GLU glu=new GLU();
        private int rotX=0,rotY=0,rotZ=0;
        private static boolean fullscreen=false;
        private static Point center;
        private boolean rX=false,rY=false,rZ=false;
        private GameHandler gh;
        private int forward=KeyEvent.VK_W;
        private int backward=KeyEvent.VK_S;
        private int strafel=KeyEvent.VK_A;
        private int strafer=KeyEvent.VK_D;
        private int down=KeyEvent.VK_CONTROL;
        private int jump=KeyEvent.VK_SPACE;
        private int w=1024;
        private int h=768;
        public static void main(String[]args)
        {
                Frame frame = new Frame("JOGL Events");
                Toolkit t=Toolkit.getDefaultToolkit();
                Image img=new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
                Cursor pointer=t.createCustomCursor(img, new Point(0,0), "none");
                Driver m=new Driver();
                GLCanvas canvas = new GLCanvas();
                canvas.addGLEventListener(m);
                canvas.addKeyListener(m);
                canvas.addMouseListener(m);
                canvas.setFocusable(true);
                canvas.requestFocus();
               
                frame.add(canvas);
                frame.setUndecorated(true);
                frame.setSize(1024, 768);
                frame.setLocationRelativeTo(null);
                frame.setCursor(pointer);
                frame.setVisible(true);
                GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
                if(fullscreen){
                        ge.getDefaultScreenDevice().setFullScreenWindow(frame);
                }
                final Animator animator = new Animator(canvas);
                animator.setRunAsFastAsPossible(true);
                animator.start();
                Rectangle r=frame.getBounds();
                center=new Point(r.x+r.width/2, r.y+r.height/2);
        }
        public void init(GLAutoDrawable drawable) {
                final GL2 gl = drawable.getGL().getGL2();
*    gl.glShadeModel( GL2.GL_SMOOTH );
*    gl.glClearColor( 0f, 0f, 0f, 0f );
*    gl.glClearDepth( 1.0f );
*    gl.glEnable( GL2.GL_DEPTH_TEST );
*    gl.glDepthFunc( GL2.GL_LEQUAL );
*    gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST );
            gh=new GameHandler(gl);
            gh.setMouseCenter(center);
        }
        public void dispose(GLAutoDrawable drawable) {
        }
        public void display(GLAutoDrawable drawable) {
                final GL2 gl = drawable.getGL().getGL2();
* gl.glShadeModel( GL2.GL_SMOOTH );
*    gl.glClearColor( 0f, 0f, 0f, 0f );
*    gl.glClearDepth( 1.0f );
*    gl.glEnable( GL2.GL_DEPTH_TEST );
*    gl.glDepthFunc( GL2.GL_LEQUAL );
*    gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
* gl.glClear (GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT );      
                // Clear The Screen And The Depth Buffer
* gl.glLoadIdentity();
                gh.run();
                gl.glFlush();
        }
        public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
                final GL2 gl = drawable.getGL().getGL2();
                if(height <=0)
                height =1;
                final float h = ( float ) width / ( float ) height;
* gl.glViewport( 0, 0, width, height );
* gl.glMatrixMode( GL2.GL_PROJECTION );
* gl.glLoadIdentity();
* glu.gluPerspective( 68.0f, h, 0.1f, 20.0f);
* gl.glMatrixMode( GL2.GL_MODELVIEW );
* gl.glLoadIdentity();
* gl.glLoadIdentity();
                center=new Point(x+width/2, y+height/2);
                gh.setMouseCenter(center);
        }
        public void keyTyped(KeyEvent e) {
        }
        public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==forward)
                {
                        gh.setFord(true);
                        System.out.println("fdgdfg");
                }
                if(e.getKeyCode()==backward)
                        gh.setback(true);
                if(e.getKeyCode()==strafel)
                        gh.setstrafel(true);
                if(e.getKeyCode()==strafer)
                        gh.setstrafer(true);
                if(e.getKeyCode()==KeyEvent.VK_ESCAPE)
                        System.exit(0);
                if(e.getKeyCode()==jump)
                        gh.setJump(true);
                if(e.getKeyCode()==down)
                        gh.setFall(true);
        }
        public void keyReleased(KeyEvent e) {
                if(e.getKeyCode()==forward)
                        gh.setFord(false);
                if(e.getKeyCode()==backward)
                        gh.setback(false);
                if(e.getKeyCode()==strafel)
                        gh.setstrafel(false);
                if(e.getKeyCode()==strafer)
                        gh.setstrafer(false);
                if(e.getKeyCode()==jump)
                        gh.setJump(false);
                if(e.getKeyCode()==down)
                        gh.setFall(false);
        }
        public void mouseClicked(MouseEvent e) {
        }
        public void mousePressed(MouseEvent e) {
        }
        public void mouseReleased(MouseEvent e) {
        }
        public void mouseEntered(MouseEvent e) {
        }
        public void mouseExited(MouseEvent e) {
        }
}

GameHandler Class:

import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.util.ArrayList;
import com.jogamp.opengl.GL2;
public class GameHandler {
        private ArrayList<Cube> c=new ArrayList<Cube>();
        private boolean ford=false,back=false,strafel=false,strafer=false,jump=false,down=false;
        private Point mouseCenter;
        private Robot robot;
        private final float headSens=0.02f;
        private final float pitchSens=0.01f;
        private long lastTime=0;
        private float dx, dy;
        private Player player;
        private GL2 gl;
        public GameHandler(GL2 gl)
        {
                this.gl=gl;
                Level.loadCoords();
                player=new Player();
                for(Coord co:Level.CORDS)
                        c.add(new Cube(co.getX(),co.getY(),co.getZ()));
                Robot r=null;
                try{
                        r=new Robot();
                }
                catch(final AWTException e){
                        System.out.println("Trouble stating Robot");
                }
                this.robot=r;
                if(robot==null)
                        System.out.println("Error setting up robot");
        }
        public void checkEvents()
        {
                long now=System.nanoTime();
                float period=(float)((now-lastTime)*0.000005);
                lastTime=now;
                dx=MouseInfo.getPointerInfo().getLocation().x;
                dy=MouseInfo.getPointerInfo().getLocation().y;
                float head=(mouseCenter.x-dx)/3;
                float pit=(mouseCenter.y-dy)/3;
                if(head!=0)
                        player.setHeading(head*headSens);
                if(pit!=0)
                        player.setPitch(pit*pitchSens);
                if(ford)
                        player.setFord((float)period);
                if(back)
                        player.setBack((float)period);
                if(strafel)
                        player.setStrafel((float)period);
                if(strafer)
                        player.setStrafer((float)period);
                if(jump)
                        player.jump("up");
                if(down)
                        player.jump("down");
                player.set();
               
        }
        public void run()
        {
                checkEvents();
                if(robot!=null)
                        robot.mouseMove(mouseCenter.x, mouseCenter.y);
                player.draw(gl);
                for(Cube c:getCubes())
                        c.draw(gl);

        }
        public void setMouseCenter(Point center)
        {
                this.mouseCenter=center;
                System.out.print(center);
        }
        public ArrayList<Cube> getCubes()
        {
                return c;
        }
        public void setFord(boolean temp){
                ford=temp;
        }
        public void setback(boolean temp){
                back=temp;
        }
        public void setstrafel(boolean temp){
                strafel=temp;
        }
        public void setstrafer(boolean temp){
                strafer=temp;
        }
        public void setJump(boolean temp)
        {
                jump=temp;
        }
        public void setFall(boolean temp)
        {
                down=temp;
        }
}

Player Class:

import java.nio.FloatBuffer;

import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.GL2;

public class Player {
        private static final float _90=(float)Math.toRadians(90);
        private static final float _maxPitch=(float)Math.toRadians(90);
        private float heading=0.0f;
        private float pitch=0.0f;
        private float cosa, cosb, cosz, sina, sinb, sinz;
        private float cosc=1.0f;
        private float sinc=0.0f;
        private float x,y,z;
        private float[] mat={ 1,0,0,0,
                        0,1,0,0,
                        0,0,1,0,
                        0,0,0,1};
        private FloatBuffer matrix;
        public Player()
        {
                matrix=Buffers.newDirectFloatBuffer(mat.length);
                matrix.put(mat);
                x=y=z=0;
        }
        public void setHeading(float amount){
                heading-=amount;
                cosb=(float)Math.toRadians(Math.cos(heading));
                sinb=(float)Math.toRadians(Math.sin(heading));
                cosz=(float)Math.toRadians(Math.cos(heading+_90));
                sinz=(float)Math.toRadians(Math.sin(heading+_90));
        }
        public void setPitch(float amount){
                pitch-=amount;
                if(pitch>_maxPitch)pitch=_maxPitch;
                if(pitch<-_maxPitch)pitch=-_maxPitch;
                cosa=(float)Math.cos(pitch);
                sina=(float)Math.sin(pitch);
        }
        public void setFord(float amount){
                x+=cosz*cosa*amount*2;
                z+=sinz*cosa*amount*2;
                y+=Math.toRadians(sina)*2;
        }
        public void setBack(float amount){
                x-=cosz*cosa*amount*2;
                z-=sinz*cosa*amount*2;
                y-=Math.toRadians(sina)*2;
        }
        public void setStrafel(float amount){
                x+=cosb*amount*2;
                z+=sinb*amount*2;
        }
        public void setStrafer(float amount){
                x-=cosb*amount*2;
                z-=sinb*amount*2;
        }
        public void set(){
* matrix.put(0, cosc*cosb-sinc*sina*sinb);
* matrix.put(1, sinc*cosb+cosc*sina*sinb);
* matrix.put(2, -cosa*sinb);
* matrix.put(4, -sinc*cosa);
* matrix.put(5, cosc*cosa);
* matrix.put(6, sina);
* matrix.put(8, cosc*sinb+sinc*sina*cosb);
* matrix.put(9, sinc*sinb-cosc*sina*cosb);
* matrix.put(10, cosa*cosb);
* matrix.put(12, matrix.get(0)*x+matrix.get(4)*y+matrix.get(8)*z);
* matrix.put(13, matrix.get(1)*x+matrix.get(5)*y+matrix.get(9)*z);
* matrix.put(14, matrix.get(2)*x+matrix.get(6)*y+matrix.get(10)*z);
        }
        public void jump(String dir)
        {
                if(dir.equals("up"))
                        y-=.03;
                else if(dir.equals("down"))
                        y+=.03;
        }
        public void draw(GL2 gl){
                gl.glLoadIdentity();
                gl.glBegin(gl.GL_QUADS);
                gl.glColor3f(5.0f, 0.0f, 3.0f);
                gl.glVertex3f(1.0f, 1.0f, 0.0f);
                gl.glVertex3f(2.0f, 1.0f, 0.0f);
                gl.glVertex3f(2.0f, 1.0f, -5.0f);
                gl.glVertex3f(1.0f, 1.0f, -5.0f);
                gl.glEnd();
* matrix.rewind();
* gl.glLoadMatrixf(matrix);
        }
}

Cube class:

import com.jogamp.opengl.GL2;

public class Cube {

        int x,y,z;
       
        public Cube(int x,int y,int z)
        {
                this.x=x;
                this.y=y;
                this.z=z;
        }
       
        public void draw(GL2 gl)
        {
                gl.glBegin(GL2.GL_QUADS);
                 gl.glColor3f(1f,0f,0f);
                 gl.glVertex3f(x+-.5f, y+.5f, z+-.5f);
                 gl.glVertex3f(x+.5f, y+.5f, z+-.5f);
                 gl.glVertex3f(x+.5f, y+-.5f, z+-.5f);
                 gl.glVertex3f(x+-.5f, y+-.5f, z+-.5f);
                 
                 gl.glColor3f(0f,1f,0f);
                 gl.glVertex3f(x+-.5f, y+.5f, z+.5f);
                 gl.glVertex3f(x+.5f, y+.5f, z+.5f);
                 gl.glVertex3f(x+.5f, y+-.5f, z+.5f);
                 gl.glVertex3f(x+-.5f, y+-.5f, z+.5f);
                 
                 gl.glColor3f(1f,0f,0f);
                 gl.glVertex3f(x+-.5f, y+-.5f, z+.5f);
                 gl.glVertex3f(x+-.5f, y+-.5f, z+-.5f);
                 gl.glVertex3f(x+-.5f, y+.5f, z+-.5f);
                 gl.glVertex3f(x+-.5f,y+.5f, z+.5f);

                 
                 gl.glColor3f(0f,0f,1f);
                 gl.glVertex3f(x+.5f, y+.5f, z+-.5f);
                 gl.glVertex3f(x+.5f, y+-.5f, z+-.5f);
                 gl.glVertex3f(x+.5f, y+-.5f, z+.5f);
                 gl.glVertex3f(x+.5f, y+.5f, z+.5f);
                 
                 gl.glColor3f(1f,0f,1f);
                 gl.glVertex3f(x+-.5f, y+.5f, z+-.5f);
                 gl.glVertex3f(x+-.5f, y+.5f, z+.5f);
                 gl.glVertex3f(x+.5f, y+.5f, z+.5f);
                 gl.glVertex3f(x+.5f, y+.5f, z+-.5f);
                 
                 gl.glColor3f(1f,.5f,1f);
                 gl.glVertex3f(x+-.5f, y+-.5f, z+-.5f);
                 gl.glVertex3f(x+-.5f, y+-.5f, z+.5f);
                 gl.glVertex3f(x+.5f, y+-.5f, z+.5f);
                 gl.glVertex3f(x+.5f, y+-.5f, z+-.5f);

                 gl.glEnd();
                 
                 
                 
                 gl.glBegin(GL2.GL_LINES);
                 gl.glColor3f(1f, 1f, 1f);
                 gl.glVertex3f(x+-.5f, y+.5f, z+-.5f);
                 gl.glVertex3f(x+.5f, y+.5f, z+-.5f);
                 gl.glEnd();
                 
                 gl.glBegin(GL2.GL_LINES);
                 gl.glColor3f(1f, 1f, 1f);
                 gl.glVertex3f(x+.5f, y+.5f, z+-.5f);
                 gl.glVertex3f(x+.5f, y+-.5f, z+-.5f);
                 gl.glEnd();
                 
                 gl.glBegin(GL2.GL_LINES);
                 gl.glColor3f(1f, 1f, 1f);
                 gl.glVertex3f(x+.5f, y+-.5f, z+-.5f);
                 gl.glVertex3f(x+-.5f, y+-.5f, z+-.5f);
                 gl.glEnd();
                 
                 gl.glBegin(GL2.GL_LINES);
                 gl.glColor3f(1f, 1f, 1f);
                 gl.glVertex3f(x+-.5f, y+.5f, z+-.5f);
                 gl.glVertex3f(x+-.5f, y+-.5f, z+-.5f);
                 gl.glEnd();
                 
                 gl.glBegin(GL2.GL_LINES);
                 gl.glColor3f(1f, 1f, 1f);
                 gl.glVertex3f(x+-.5f, y+.5f, z+.5f);
                 gl.glVertex3f(x+.5f, y+.5f, z+.5f);
                 gl.glEnd();
                 
                 gl.glBegin(GL2.GL_LINES);
                 gl.glColor3f(1f, 1f, 1f);
                 gl.glVertex3f(x+.5f, y+.5f, z+.5f);
                 gl.glVertex3f(x+.5f, y+-.5f, z+.5f);
                 gl.glEnd();
                 
                 gl.glBegin(GL2.GL_LINES);
                 gl.glColor3f(1f, 1f, 1f);
                 gl.glVertex3f(x+.5f, y+-.5f, z+.5f);
                 gl.glVertex3f(x+-.5f, y+-.5f, z+.5f);
                 gl.glEnd();
                 
                 gl.glBegin(GL2.GL_LINES);
                 gl.glColor3f(1f, 1f, 1f);
                 gl.glVertex3f(x+-.5f, y+.5f, z+.5f);
                 gl.glVertex3f(x+-.5f, y+-.5f, z+.5f);
                 gl.glEnd();
                 
                 gl.glBegin(GL2.GL_LINES);
                 gl.glColor3f(1f, 1f, 1f);
                 gl.glVertex3f(x+-.5f, y+-.5f, z+-.5f);
                 gl.glVertex3f(x+-.5f, y+-.5f, z+.5f);
                 gl.glEnd();
                 
                 gl.glBegin(GL2.GL_LINES);
                 gl.glColor3f(1f, 1f, 1f);
                 gl.glVertex3f(x+.5f, y+-.5f, z+-.5f);
                 gl.glVertex3f(x+.5f, y+-.5f, z+.5f);
                 gl.glEnd();
                 
                 gl.glBegin(GL2.GL_LINES);
                 gl.glColor3f(1f, 1f, 1f);
                 gl.glVertex3f(x+-.5f, y+.5f, z+-.5f);
                 gl.glVertex3f(x+-.5f, y+.5f, z+.5f);
                 gl.glEnd();
                 
                 gl.glBegin(GL2.GL_LINES);
                 gl.glColor3f(1f, 1f, 1f);
                 gl.glVertex3f(x+.5f, y+.5f, z+-.5f);
                 gl.glVertex3f(x+.5f, y+.5f, z+.5f);
                 gl.glEnd();
        }
}


There are a few more, but they have nothing to do with the drawing of the items