使用keycallback在LWJGL3中旋转三角形

时间:2015-08-21 23:02:42

标签: java opengl graphics lwjgl

那么,如何通过按向上,向下,向左或向右旋转三角形。我有一个Keyboard类,可以读取键和创建事件。但我不知道在LWJGL 3中旋转的功能。我认为我熟悉旋转的经典gl.h方式,但由于LWJGL 3很新,所以没有很多关于此的信息。这是Display类和KeyboardHandler类的代码。

显示

public class Driver implements Runnable{

private GLFWKeyCallback keyCallback;
private Thread thread = new Thread();
private boolean running = false;


public long window;

private static final int WIDTH = 600;
private static final int HEIGHT = WIDTH / 12 * 9;

public Driver(){

}

private synchronized void start(){
    thread.start();
    running = true;
}

private synchronized void stop(){
    try {
        thread.join();
        running = false;
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public void run(){
    init();
    while(running){

        render();
        update();

        if(glfwWindowShouldClose(window) == GL_TRUE){
            running = false;
            keyCallback.release();
        }
    }
}

public void init() {

    if(glfwInit() != GL_TRUE){
        System.err.println("Failed to initilaize OpenGL");
    }

    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    window = glfwCreateWindow(WIDTH, HEIGHT, "Endless", NULL, NULL);

    if(window == NULL){
        System.err.println("Could not create window. ");
    }

    glfwSetKeyCallback(window, keyCallback = new KeyboardHandler());

    ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, 100, 100);
    glfwMakeContextCurrent(window);
    glfwShowWindow(window);

}


public void update() {
    if(KeyboardHandler.isKeyDown(GLFW_KEY_LEFT)){
        //event who'll start rotation
    }
    glfwPollEvents();
}

public void render() {

    GLContext.createFromCurrent();

    glBegin(GL_TRIANGLES);
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex2f(-1.0f / 2, -1.0f / 2);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex2f(0.0f / 2, 1.0f / 2);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex2f(1.0f / 2, -1.0f / 2);
    glEnd();

    //rotation of triangle

    glfwSwapBuffers(window);
}


public static void main(String[] args) {
    Driver game = new Driver();
    game.start();
    game.run();
}

}

和KeyboardHandler

public class KeyboardHandler extends GLFWKeyCallback{

private static boolean keys[] = new boolean[65536];

public void invoke(long window, int key, int scancode, int action, int mods)         
{   
keys[key] = action != GLFW_RELEASE;
}

public static boolean isKeyDown(int keycode){
    return keys[keycode];
}

}

我猜旋转本身应该在render()中发生,而触发它的keycallback应该在update()中

1 个答案:

答案 0 :(得分:1)

随着LWJGL 3的推出,您必须使用着色器来旋转,缩放和变换对象。您可以在互联网上找到许多资源,向您展示如何执行此操作,其中之一是OpenGL编程WikiBook。具体来说,in tutorial four,处理现代OpenGL中的旋转对象