0

OpenGL では、3D 空間全体が見えるようにカメラを回転させようとしています。翻訳ポイントで空間を回転させることを調査しましたが、得られるのはカメラが中心を中心に回転することだけです。

ビューを回転させるために使用しているコードは次のとおりです(Javaで):

ゲームループ

public Game(){
    try {
        Display.setDisplayMode(new DisplayMode(1280, 720));
        Display.setTitle("Survive the Planets");
        Display.create();
    } catch (LWJGLException e) {
        e.printStackTrace();
    }

    //Initialize OpenGL
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(50f, 1280f / 720f, 1.0f, 10000);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_TEXTURE_2D);

    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    Planet planet = Planet.generateCreative();
    planet.viewPlanet();

    while(!Display.isCloseRequested()){
        //render
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        planet.render();

        //update
        if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
            Display.destroy();
            System.exit(0);
            return;
        }

        Camera.rotate(1, 1, 1);

        Display.update();
        Display.sync(240);
    }

    Display.destroy();
}

カメラクラス

public class Camera {

    private static float x, y, z, rX, rY, rZ;

    public static void translate(float x, float y, float z){
        Camera.x += x;
        Camera.y += y;
        Camera.z += z;
        glTranslatef(x, y, z);
    }

    public static void rotate(float x, float y, float z){
        glRotatef(x, 1, 0, 0);
        glRotatef(y, 0, 1, 0);
        glRotatef(z, 0, 0, 1);

        Camera.rX += x;
        Camera.rY += y;
        Camera.rZ += z;
    }

    public static void setLocation(float x, float y, float z){
        float dx = x - Camera.x;
        float dy = y - Camera.y;
        float dz = z - Camera.z;
        glTranslatef(dx, dy, dz);

        Camera.x = x;
        Camera.y = y;
        Camera.z = z;
    }

    public static void setRotation(float x, float y, float z){
        float dx = x - Camera.rX;
        float dy = y - Camera.rY;
        float dz = z - Camera.rZ;
        glRotatef(dx, 1, 0, 0);
        glRotatef(dy, 0, 1, 0);
        glRotatef(dz, 0, 0, 1);

        Camera.rX = x;
        Camera.rY = y;
        Camera.rZ = z;
    }

}

惑星クラス:

public class Planet {

    private List<Block> blocks;
    private PlanetReference ref;

    private Planet(){ }

    public void render() {
        for(Block block : blocks){
            block.render();
        }
    }

    public static Planet generate(PlanetReference ref){
        Planet p = new Planet();
        p.ref = ref;
        List<Block> blocks = new CopyOnWriteArrayList<Block>();

        System.out.println("Genrating planet " + ref.getName() + "... ");

        //world generation
        int r = ref.getRadius() * 32;
        int i = 0;
        for(int x = -r; x != r; x += 32){
            for(int y = -r; y != r; y += 32){
                for(int z = -r; z != r; z += 32){
                    i += 1;
                    Cube cube = new Cube(x, y, z, 32);
                    if(Game.getDistance(cube.getCenter()) < r){
                        Block block = new Block(p, Material.DIRT, cube.getLocation());
                        blocks.add(block);
                    }
                }
            }
        }

        p.blocks = blocks;
        System.out.println(blocks.size() + " blocks where generated. " + i);

        return p;
    }

    public void viewPlanet(){
        Camera.setLocation(0, 0, -(ref.getRadius() * 96));
    }

    public static Planet generateCreative() {
        return Planet.generate(new PlanetReference("Creative", 16, 9, 64));
    }

}

私が何をすべきかについての提案はありますか?もうコードが必要な場合は、コメントを残してください。

4

0 に答える 0