michael.james.90475 -3 Newbie Poster
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.*;

import com.sun.opengl.util.Animator;
import com.sun.opengl.util.FPSAnimator;


public class Object3D implements GLEventListener, KeyListener {

    GLProfile glp;
    GLCapabilities caps;
    GLCanvas canvas;
    GLU glu;
    float rotX = 0.0f;  // Rotate screen on x axis 
    float rotY = 0.0f;  // Rotate screen on y axis
    float rotZ = 0.0f;  // Rotate screen on z axis
    float X = 0.0f;     // Translate screen to x direction (left or right)
    float Y = 0.0f;     // Translate screen to y direction (up or down)
    float Z = 0.0f;     // Translate screen to z direction (zoom in or out)
    float rotLx = 0.0f;   // Translate screen by using  the glulookAt function (left or right)
    float rotLy = 0.0f;   // Translate screen by using  the glulookAt function (up or down)
    float rotLz = 0.0f; 
    float a=10;
    float b=10;
    float c=10;
    float d=0;
    float e=0;
    float f=0;
    float g=0;
    float h=1;
    float i=0;
    double sx = 0.59;
    double sy =0.59;
    double sz = 0.59;
    Boolean rotatingX=false;
    Boolean rotatingY=false;
    Boolean rotatingZ=false;
    Boolean scaling=false;
    int count=0;

    public Object3D()
    {
        glp = GLProfile.getDefault();
        caps = new GLCapabilities(glp);
        canvas = new GLCanvas(caps);
        glu = new GLU();

        Frame frame = new Frame("AWT Window Test");
        frame.setSize(500, 500);
        frame.add(canvas);
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        canvas.addGLEventListener(this);
        canvas.addKeyListener(this);
        canvas.requestFocus();
        Animator animator = new FPSAnimator(canvas,60);
        animator.add(canvas);
        animator.start();

    }

    public static void main(String[] args) {

        Object3D obj3d = new Object3D();

    }



    @Override
    public void display(GLAutoDrawable drawable) {
         //update(drawable);
            GL2 gl = drawable.getGL().getGL2(); 
            GLU glu = new GLU();

            gl.glClear(GL.GL_COLOR_BUFFER_BIT);

            //make sure we are in model_view mode
            gl.glMatrixMode(GL2.GL_MODELVIEW);
            gl.glLoadIdentity();
               glu.gluLookAt(a,b,c,d,e,f,g, h, i);

              if(rotatingX){
             gl.glRotatef(rotX, 1.0f, 0.0f, 0.0f);

            System.out.println("rotated x");}

              if(rotatingY){
             gl.glRotatef(rotY, 0.0f, 1.0f, 0.0f); }
              if(rotatingZ){
             gl.glRotatef(rotZ, 0.0f, 0.0f, 1.0f); }

            gl.glTranslatef(X, Y, Z);   
            if(scaling){
            gl.glScalef( 0.50f,0.25f,0.50f );
                }
            if(!scaling){
                gl.glScalef( 1.0f,1.0f,1.0f );
                    }

        gl.glColor3f(1, 1, 1);
        gl.glBegin(gl.GL_LINE_LOOP);
        gl.glVertex3f(3,4,3);
        gl.glVertex3f(-3,4,3);
        gl.glVertex3f(-3,0,3);
        gl.glVertex3f(3,0,3);
            gl.glEnd();
      gl.glBegin(gl.GL_LINE_LOOP);
        gl.glVertex3f(3,4,-1);
        gl.glVertex3f(-3,4,-1);
        gl.glVertex3f(-3,0,-1);
        gl.glVertex3f(3,0,-1);
            gl.glEnd();


            //bottom right
      gl.glBegin(gl.GL_LINE_LOOP);
          gl.glVertex3f(3,0,-1);
          gl.glVertex3f(3,0,3);
            gl.glEnd();

            //bottom left
      gl.glBegin(gl.GL_LINE_LOOP);
          gl.glVertex3f(-3,0,3);
          gl.glVertex3f(-3,0,-1);
            gl.glEnd();


                gl.glBegin(gl.GL_LINE_LOOP);
                gl.glVertex3f(3,4,-1);
                gl.glVertex3f(1,5,-1);
              gl.glVertex3f(3,4,3);

                    gl.glEnd();
                gl.glBegin(gl.GL_LINE_LOOP);
                gl.glVertex3f(-3,4,-1);
                gl.glVertex3f(-5,5,-1);
              gl.glVertex3f(-3,4,3);


                    gl.glEnd();
                    gl.glBegin(gl.GL_LINE_LOOP);

                    gl.glVertex3f(-5,5,-1);
                   gl.glVertex3f(1,5,-1);


                        gl.glEnd();
             gl.glPopMatrix();  


    }

    public void update(GLAutoDrawable drawable)
    {





    }
    @Override
    public void dispose(GLAutoDrawable drawable) {
        // TODO Auto-generated method stub

    }

    @Override
    public void init(GLAutoDrawable drawable) { 

    }


    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width,
            int height) {
        // TODO Auto-generated method stub
        GL2 gl = drawable.getGL().getGL2();
        GLU glu = new GLU();

        if (height < 1) { // avoid a divide by zero error!   
            height = 1;
        }

        float aspect = (float) width / (float) height;
        gl.glViewport(0, 0, width, height);
        //set up the camera
        gl.glMatrixMode(gl.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f, aspect, 1.0, 30.0);

                    // Set the viewport
                                    // Set the Matrix mode
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
        glu.gluLookAt (rotLx, rotLy, 15.0 + rotLz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 
    }

    public void keyPressed(KeyEvent arg0) {



        if(arg0.getKeyCode() == KeyEvent.VK_R)
        {


            System.out.println("Pressed r");
        }

        if(arg0.getKeyCode() == KeyEvent.VK_X)
        {

            rotX -= 1.9f;
            rotatingX=true;
            System.out.println("Pressed X");
        }
        if(arg0.getKeyCode() == KeyEvent.VK_Y)
        {
            rotatingY=true;
            rotY -= 1.9f;
            System.out.println("Pressed y");
        }
        if(arg0.getKeyCode() == KeyEvent.VK_Z)
        {
            rotatingZ=true;
            rotZ -= 1.9f;
            System.out.println("Pressed z");

        }


        if(arg0.getKeyCode() == KeyEvent.VK_S)
        {

            count++;
            if(count % 2 !=0)
            {
                    scaling=true;
            }
            else {
                    scaling = false;
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }



}

I can rotate the house when I press x, y or z. Depending on what I press will rotate it along that axis. I can scale also by pressing S, press it scales smaller, press again it scales bigger....the issue I am having is that from what I did, I am confused as to why It does not repeatedly rotate or scale...once I set scaling to true, the display method should keep scaling while it remains true, but it only scales/ rotates once per button press, this makes no sense to me...why is this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.