[英]Making a triangle rotate in LWJGL3 with keycallback
因此,如何按上,下,左或右旋轉三角形。 我有一個鍵盤類,可以讀取鍵並創建事件。 但是我不知道在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()中發生,而觸發旋轉的按鍵回調應該在update()中發生
隨着LWJGL 3的引入,您必須使用着色器旋轉,縮放和變換對象。 您可以在Internet上找到許多資源來展示如何執行此操作,其中之一就是OpenGL編程WikiBook。 具體來說, 在教程四中 ,它處理了現代OpenGL中的旋轉對象
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.