Rotating model vertices

Started by Onisama, July 09, 2013, 02:57:22

Previous topic - Next topic

Onisama

Right now I'm trying to create methods for rotating the vertices of a model by 1 degree every time a call to my model class is made, but no matter what I do, I can't seem to get the algorithm right and whenever I try to look for how to do this on Google, all the links say to use glRotated() despite the fact that seems to rotate the camera and not the model itself. If anyone can tell me how to actually rotate the vertices of a model, I'd be grateful.

Oh, right, this is the code for the application:

public class BasicLWJGLtest {
    
    final float ASPECT_RATIO = Toolkit.getDefaultToolkit().getScreenSize().width / 
            Toolkit.getDefaultToolkit().getScreenSize().height; //for some reason it keeps rounding it to an integer when it's called.
    
    public void load(Model model){
        String intermediate;
        try{
            BufferedReader reader = new BufferedReader(new FileReader(new File("Gangster.obj")));
            while((intermediate = reader.readLine()) != null){
                if(intermediate.startsWith("v ")){
                    Point3d pointIntermediate = new Point3d();
                    pointIntermediate.x = Double.valueOf(intermediate.split(" ")[1]);
                    pointIntermediate.y = Double.valueOf(intermediate.split(" ")[2]);
                    pointIntermediate.z = Double.valueOf(intermediate.split(" ")[3]);
                    model.addPoint(pointIntermediate);
                }
                else if(intermediate.startsWith("f ")){
                    int IDa = Integer.valueOf(intermediate.split(" ")[1]) - 1;
                    int IDb = Integer.valueOf(intermediate.split(" ")[2]) - 1;
                    int IDc = Integer.valueOf(intermediate.split(" ")[3]) - 1;
                    Point3d a = new Point3d(model.listOfPoints.get(IDa));
                    Point3d b = new Point3d(model.listOfPoints.get(IDb));
                    Point3d c = new Point3d(model.listOfPoints.get(IDc));
                    model.addFace(new Face(a, b, c, IDa, IDb, IDc));
                }
            }
        }
        catch(IOException e){
            JOptionPane.showMessageDialog(null, e.toString());
            e.printStackTrace();
            System.exit(0);
        }
    }
    
    public void render(Model model, int keyMovement){
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        model.updateModel();
        glBegin(GL_TRIANGLES);
        for (Face face : model.listOfFaces){
            glVertex3d(face.getA().x - 3.5, face.getA().y, face.getA().z);
            glVertex3d(face.getB().x - 3.5, face.getB().y, face.getB().z);
            glVertex3d(face.getC().x - 3.5, face.getC().y, face.getC().z);
        }
        glEnd();
    }
    
    public static void main(String[] args) {
        int keyMovement = 0;
        BasicLWJGLtest methods = new BasicLWJGLtest();
        Model model = new Model();
        methods.load(model);
        
        try{
            Display.setFullscreen(true);
            Display.create();
        }
        catch(LWJGLException e){
            JOptionPane.showMessageDialog(null, e.toString());
            e.printStackTrace();
            Display.destroy();
            System.exit(0);
        }
        
        GLU.gluPerspective(45f, 1.6f, 0.005f, 1000f);
        glTranslated(0.0, 0.0, -10);
        
        while(!Display.isCloseRequested()){
            if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
                try{
                    Display.setFullscreen(false);
                }
                catch(LWJGLException e){
                    JOptionPane.showMessageDialog(null, e.toString());
                    e.printStackTrace();
                    Display.destroy();
                    System.exit(0);
                }
            }
            else if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)){
                glTranslated(0,0,-0.1);
            }
            else if (Keyboard.isKeyDown(Keyboard.KEY_UP)){
                glTranslated(0,0,0.1);
            }
            else if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){
                keyMovement = Keyboard.KEY_RIGHT;
            }
            else if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)){
                keyMovement = Keyboard.KEY_LEFT;
            }
            methods.render(model, keyMovement);
            Display.sync(60);
            Display.update();
        }
        Display.destroy();
    }
}


And this is the code for the Model class:

public class Model {
    ArrayList<Point3d> listOfPoints;
    ArrayList<Face> listOfFaces;
    int angleTurned;
    
    public Model(){
        listOfPoints = new ArrayList<Point3d>();
        listOfFaces = new ArrayList<Face>();
    }
    
    public void setListOfPoints(ArrayList<Point3d> inLOP){
        listOfPoints = inLOP;
    }
    
    public void setListOfFaces(ArrayList<Face> inLOF){
        listOfFaces = inLOF;
    }
    
    public void addPoint(Point3d newPoint){
        listOfPoints.add(newPoint);
    }
    
    public void addFace(Face newFace){
        listOfFaces.add(newFace);
    }
    
    public void translateX(double speed){
        for(Point3d point : listOfPoints){
            point.x += speed;
        }
    }
    
    public void translateY(double speed){
        for(Point3d point : listOfPoints){
            point.y += speed;
        }
    }
    
    public void translateZ(double speed){
        for(Point3d point : listOfPoints){
            point.z += speed;
        }
    }
    
    //this is the method I need to try and get to work.
    public void rotateZ(double speed){ 
        for(Point3d point : listOfPoints){
            point.x *= Math.cos(Math.toRadians(speed));
            point.y *= Math.sin(Math.toRadians(speed));
        }
    }
    
    public void updateModel(){
        for (Face face : listOfFaces){
            face.setA(listOfPoints.get(face.getIDa()));
            face.setB(listOfPoints.get(face.getIDb()));
            face.setC(listOfPoints.get(face.getIDc()));
        }
    }
}

Fool Running

I think your problem is that you are doing the rotation after you are doing the translation. All the matrix operations build on each other so if you translate the object then rotate you are rotating around the wrong axis.
Try code closer to the following:
import java.awt.Toolkit;
import java.io.*;

import javax.swing.JOptionPane;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.util.glu.GLU;

import static org.lwjgl.opengl.GL11.*;

public class BasicLWJGLtest {
    
    public void load(Model model){
        String intermediate;
        try{
            BufferedReader reader = new BufferedReader(new FileReader(new File("Gangster.obj")));
            while((intermediate = reader.readLine()) != null){
                if(intermediate.startsWith("v ")){
                    Point3d pointIntermediate = new Point3d();
                    pointIntermediate.x = Double.valueOf(intermediate.split(" ")[1]);
                    pointIntermediate.y = Double.valueOf(intermediate.split(" ")[2]);
                    pointIntermediate.z = Double.valueOf(intermediate.split(" ")[3]);
                    model.addPoint(pointIntermediate);
                }
                else if(intermediate.startsWith("f ")){
                    int IDa = Integer.valueOf(intermediate.split(" ")[1]) - 1;
                    int IDb = Integer.valueOf(intermediate.split(" ")[2]) - 1;
                    int IDc = Integer.valueOf(intermediate.split(" ")[3]) - 1;
                    Point3d a = new Point3d(model.listOfPoints.get(IDa));
                    Point3d b = new Point3d(model.listOfPoints.get(IDb));
                    Point3d c = new Point3d(model.listOfPoints.get(IDc));
                    model.addFace(new Face(a, b, c, IDa, IDb, IDc));
                }
            }
        }
        catch(IOException e){
            JOptionPane.showMessageDialog(null, e.toString());
            e.printStackTrace();
            System.exit(0);
        }
    }
    
    public static void main(String[] args) {
        int keyMovement = 0;
        BasicLWJGLtest methods = new BasicLWJGLtest();
        Model model = new Model();
        methods.load(model);
        
        try{
            Display.setFullscreen(true);
            Display.create();
        }
        catch(LWJGLException e){
            JOptionPane.showMessageDialog(null, e.toString());
            e.printStackTrace();
            Display.destroy();
            System.exit(0);
        }
        
        while(!Display.isCloseRequested()){
            if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
                try{
                    Display.setFullscreen(false);
                }
                catch(LWJGLException e){
                    JOptionPane.showMessageDialog(null, e.toString());
                    e.printStackTrace();
                    Display.destroy();
                    System.exit(0);
                }
            }
            else if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)){
            	model.translateZ(-0.1);
            }
            else if (Keyboard.isKeyDown(Keyboard.KEY_UP)){
            	model.translateZ(0.1);
            }
            else if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){
                keyMovement = Keyboard.KEY_RIGHT;
            }
            else if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)){
                keyMovement = Keyboard.KEY_LEFT;
            }

            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glMatrixMode(GL_PROJECTION_MATRIX);
            glLoadIdentity();
            GLU.gluPerspective(45f, Display.getDisplayMode().getWidth() / 
            		(float)Display.getDisplayMode().getHeight(), 0.005f, 1000f);
            glTranslated(0.0, 0.0, -10);
            
            glMatrixMode(GL_MODELVIEW_MATRIX);
            glLoadIdentity();
            
            model.updateModel();

            model.render(keyMovement);
            Display.sync(60);
            Display.update();
        }
        Display.destroy();
    }
}


import java.util.ArrayList;
import static org.lwjgl.opengl.GL11.*;

public class Model {
    ArrayList<Point3d> listOfPoints;
    ArrayList<Face> listOfFaces;
    int angleTurned;
    
    private double offsetX, offsetY, offsetZ;
    private float rotateX, rotateY, rotateZ;
    
    public Model(){
        listOfPoints = new ArrayList<Point3d>();
        listOfFaces = new ArrayList<Face>();
    }
    
    public void setListOfPoints(ArrayList<Point3d> inLOP){
        listOfPoints = inLOP;
    }
    
    public void setListOfFaces(ArrayList<Face> inLOF){
        listOfFaces = inLOF;
    }
    
    public void addPoint(Point3d newPoint){
        listOfPoints.add(newPoint);
    }
    
    public void addFace(Face newFace){
        listOfFaces.add(newFace);
    }
    
    public void translateX(double speed){
    	offsetX += speed;
    }
    
    public void translateY(double speed){
    	offsetY += speed;
    }
    
    public void translateZ(double speed){
    	offsetZ += speed;
    }
    
    //this is the method I need to try and get to work.
    public void rotateZ(float speed){ 
    	rotateZ += speed;
    }
    
    public void updateModel(){
    }
    
    public void render(int keyMovement){
    	glPushMatrix();
    	
    	glRotatef(rotateZ, 0.0f, 0.0f, 1.0f);
    	glRotatef(rotateY, 0.0f, 1.0f, 0.0f);
    	glRotatef(rotateX, 1.0f, 0.0f, 0.0f);
    	
        glTranslated(offsetX, offsetY, offsetZ);

        glBegin(GL_TRIANGLES);
        for (Face face : listOfFaces){
            glVertex3d(face.getA().x - 3.5, face.getA().y, face.getA().z);
            glVertex3d(face.getB().x - 3.5, face.getB().y, face.getB().z);
            glVertex3d(face.getC().x - 3.5, face.getC().y, face.getC().z);
        }
        glEnd();
        
        glPopMatrix();
    }    
}


P.S. Sorry if it doesn't compile or something, I didn't have some of the classes you are using so I tried to remember how to do it without actually being able to run it. :P
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Onisama

It compiles, it just does what it always does when I try pushMatrix and popMatrix. It renders a model that, no matter how far I move the camera's initial starting place back, always covers most of the screen and when I try to move it back during runtime disappears very quickly. I don't know what pushMatrix does, but it really doesn't seem to be what I want.