【问题标题】:Making a triangle rotate in LWJGL3 with keycallback使用 keycallback 在 LWJGL3 中旋转三角形
【发布时间】:2015-08-21 23:02:42
【问题描述】:

那么,如何通过按上、下、左或右来旋转三角形。我有一个可以读取键和创建事件的键盘类。但我不知道 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();
}

}

还有键盘处理程序

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() 中

【问题讨论】:

  • 问题总结:如何在 GLFWKeyCallback 的帮助下旋转 LWJGL3 中的三角形
  • 通过上下旋转。我指的是 glRotatef(theta, 0.0f, 0.0f, 1.0f);

标签: java opengl graphics lwjgl


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多