这是我的FPS风格相机的基础代码片段.如您所见,我正在沿着屏幕中心沿X和Y轴计算鼠标位置的增量.

您可以在我的System.out.println中看到我为增量计算的值,以及它应按此量旋转的事实.然后,我将这些值乘以我的灵敏度,然后将相机旋转这个量.

import org.lwjgl.BufferUtils;

import org.lwjgl.Sys;

import org.lwjgl.glfw.*;

import org.lwjgl.opengl.*;

import java.nio.ByteBuffer;

import java.nio.DoubleBuffer;

import java.nio.FloatBuffer;

import static org.lwjgl.glfw.Callbacks.*;

import static org.lwjgl.glfw.GLFW.*;

import static org.lwjgl.glfw.GLFW.glfwGetCursorPos;

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

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

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

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

import static org.lwjgl.system.MemoryUtil.*;

public class Main {

// We need to strongly reference callback instances.

private GLFWErrorCallback errorCallback;

private GLFWKeyCallback keyCallback;

// The window handle

private long window;

public void run() {

System.out.println("Hello LWJGL " + Sys.getVersion() + "!");

try {

init();

loop();

// Release window and window callbacks

glfwDestroyWindow(window);

keyCallback.release();

} finally {

// Terminate GLFW and release the GLFWerrorfun

glfwTerminate();

errorCallback.release();

}

}

private void init() {

// Setup an error callback. The default implementation

// will print the error message in System.err.

glfwSetErrorCallback(errorCallback = errorCallbackPrint(System.err));

// Initialize GLFW. Most GLFW functions will not work before doing this.

if ( glfwInit() != GL11.GL_TRUE )

throw new IllegalStateException("Unable to initialize GLFW");

// Configure our window

glfwDefaultWindowHints(); // optional, the current window hints are already the default

glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // the window will stay hidden after creation

glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // the window will be resizable

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);

glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

int WIDTH = 800;

int HEIGHT = 600;

// Create the window

window = glfwCreateWindow(WIDTH, HEIGHT, "Hello World!", NULL, NULL);

if ( window == NULL )

throw new RuntimeException("Failed to create the GLFW window");

// Setup a key callback. It will be called every time a key is pressed, repeated or released.

glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {

@Override

public void invoke(long window, int key, int scancode, int action, int mods) {

if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )

glfwSetWindowShouldClose(window, GL_TRUE); // We will detect this in our rendering loop

}

});

// Get the resolution of the primary monitor

ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());

// Center our window

glfwSetWindowPos(

window,

(GLFWvidmode.width(vidmode) - WIDTH) / 2,

(GLFWvidmode.height(vidmode) - HEIGHT) / 2

);

// Make the OpenGL context current

glfwMakeContextCurrent(window);

// Enable v-sync

glfwSwapInterval(1);

// Make the window visible

glfwShowWindow(window);

}

private void loop() {

// This line is critical for LWJGL's interoperation with GLFW's

// This line is critical for LWJGL's interoperation with GLFW's

// OpenGL context, or any context that is managed externally.

// LWJGL detects the context that is current in the current thread,

// creates the ContextCapabilities instance and makes the OpenGL

// bindings available for use.

GLContext.createFromCurrent();

// Create a FloatBuffer to hold our vertex data

FloatBuffer vertices = BufferUtils.createFloatBuffer(9);

// Add vertices of the triangle

vertices.put(new float[]

{

0.0f, 0.5f, 0.0f,

0.5f, -0.5f, 0.0f,

-0.5f, -0.5f, 0.0f

});

// Rewind the vertices

vertices.rewind();

int vbo = glGenBuffers();

int vao = glGenVertexArrays();

glBindBuffer (GL_ARRAY_BUFFER, vbo);

glBufferData (GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);

glBindVertexArray(vao);

glEnableVertexAttribArray (0);

glBindBuffer (GL_ARRAY_BUFFER, vbo);

glVertexAttribPointer (0, 3, GL_FLOAT, false, 0, 0);

final String vertex_shader =

"#version 410\n" +

"in vec3 vp;\n" +

"void main () {\n" +

" gl_Position = vec4 (vp, 1.0);\n" +

"}";

final String frag_shader =

"#version 400\n" +

"out vec4 frag_colour;" +

"void main () {" +

" frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);" +

"}";

int shader_programme = glCreateProgram();

int vertexShaderID = glCreateShader(GL_VERTEX_SHADER);

glShaderSource(vertexShaderID, vertex_shader);

glCompileShader (vertexShaderID);

if(glGetShaderi(vertexShaderID, GL_COMPILE_STATUS) == 0){

System.err.println(glGetShaderInfoLog(vertexShaderID, 1024));

System.exit(1);

}

glAttachShader (shader_programme, vertexShaderID);

int fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

glShaderSource(fragmentShaderID, frag_shader);

glCompileShader (fragmentShaderID);

if(glGetShaderi(fragmentShaderID, GL_COMPILE_STATUS) == 0){

System.err.println(glGetShaderInfoLog(fragmentShaderID, 1024));

System.exit(1);

}

glAttachShader (shader_programme, fragmentShaderID);

glLinkProgram (shader_programme);

if(glGetProgrami(shader_programme, GL_LINK_STATUS) == 0){

System.err.println(glGetProgramInfoLog(shader_programme, 1024));

System.exit(1);

}

glValidateProgram(shader_programme);

if(glGetProgrami(shader_programme, GL_VALIDATE_STATUS) == 0){

System.err.println(glGetProgramInfoLog(shader_programme, 1024));

System.exit(1);

}

boolean mouseLocked = false;

double newX = 400;

double newY = 300;

double prevX = 0;

double prevY = 0;

boolean rotX = false;

boolean rotY = false;

glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

while ( glfwWindowShouldClose(window) == GL_FALSE ) {

if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1) == GLFW_PRESS) {

glfwSetCursorPos(window, 800/2, 600/2);

mouseLocked = true;

}

if (mouseLocked){

DoubleBuffer x = BufferUtils.createDoubleBuffer(1);

DoubleBuffer y = BufferUtils.createDoubleBuffer(1);

glfwGetCursorPos(window, x, y);

x.rewind();

y.rewind();

newX = x.get();

newY = y.get();

double deltaX = newX - 400;

double deltaY = newY - 300;

rotX = newX != prevX;

rotY = newY != prevY;

if(rotY) {

System.out.println("ROTATE Y AXIS: " + deltaY);

}

if(rotX) {

System.out.println("ROTATE X AXIS: " + deltaX);

}

prevX = newX;

prevY = newY;

System.out.println("Delta X = " + deltaX + " Delta Y = " + deltaY);

glfwSetCursorPos(window, 800/2, 600/2);

}

// wipe the drawing surface clear

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUseProgram (shader_programme);

glBindVertexArray (vao);

// draw points 0-3 from the currently bound VAO with current in-use shader

glDrawArrays (GL_TRIANGLES, 0, 3);

// update other events like input handling

glfwPollEvents ();

// put the stuff we've been drawing onto the display

glfwSwapBuffers (window);

}

}

public static void main(String[] args) {

new Main().run();

}

}

java lwjgl3_java-LWJGL 3鼠标移动相关推荐

  1. java 中鼠标事件_[Java教程]js中鼠标事件总结

    [Java教程]js中鼠标事件总结 0 2017-07-11 00:00:19 js中鼠标事件主要有onclick,onmousedown,onmouseup,oncontextmenu,ondblc ...

  2. swing jtable显示html,Java Swing:基于鼠标指针下的文本显示JTable中的工具提示

    我有一个JTable,其中显示一些使用html格式化的字符串数据.我需要显示鼠标指针Java Swing:基于鼠标指针下的文本显示JTable中的工具提示 在鼠标移到"线路1"和& ...

  3. java 监听桌面鼠标事件,鼠标事件的监听和使用

    鼠标操作是图形操作系统最常用操作,用户使用鼠标单击,双击,右击,拖动等操作实现与软件的交互. 鼠标事件监听器 鼠标事件监听器由MouseListener接口和MouseMotionListener接口 ...

  4. Java之Cursor改变鼠标形状

    将鼠标设置成手掌形状 import javax.swing.JFrame; import java.awt.Cursor; public class uu {static Cursor cursor= ...

  5. java mouseevent_「mouse_event」Java MouseEvent类(鼠标事件) - seo实验室

    mouse_event 简介 所有组件都能发出鼠标事件,MouseEvent类负责捕获鼠标事件,可以通过为组件添加实现了MouseListener接口的监听器类来处理相应的鼠标事件. MouseLis ...

  6. java rect平移_如何在Java Swing中使用鼠标平移图像

    小编典典 尝试使用scrollRectToVisible(...)method代替JViewport#setViewPosition(...): import java.awt.*; import j ...

  7. Java Selenium Actions模拟鼠标拖动dragAndDrop总结

    鼠标拖动API Actions action = new Actions(webdriver); ##source-要拖动的元素A,target-拖动元素A到达的目标元素 action.dragAnd ...

  8. java怎样自动调用鼠标点击屏幕固定地方_python办公自动化:让PyAutoGUI来帮你干活...

    做不完的工作,写不完的文档,粘贴不完的excel,打不完的 word,讨厌的996,今天我将会带领一个你专属的机器人送给你,让它来代替你做这些重复性的工作,只要你能动脑经把工作分解成若干步可重复的劳动 ...

  9. java 双击按键_java鼠标各按键单击和左键击双击

    下面代码实现了鼠标各按键单击和左键击双击,以及右键弹出菜单 JButton btn = new JButton("鼠标左键.右键.滚轮点击测试"); btn.addMouseLis ...

  10. Java怎么监听鼠标滑轮的转向_javascript监听鼠标滚轮事件浅析

    我们都见到过这些效果,用鼠标滚轮实现某个表单内的数字增加减少操作,或者滚轮控制某个按钮的左右,上下滚动.这些都是通过js对鼠标滚轮的事件监听来实现的.今天这里介绍的是一点简单的js对于鼠标滚轮事件的监 ...

最新文章

  1. 微信小游戏创业,究竟是红海还是死海?
  2. 揭秘华为AI一站式开发平台,3步构建一个AI模型 | 华为昇腾师资培训沙龙西安场...
  3. MFC:怎么将程序窗口最小化到系统托盘
  4. Vue2.x双向数据绑定
  5. Xcode6 itunes完美打包api 方法
  6. uiw 1.2.17 发布,基于 React 16 的组件库
  7. LINUX smb共享
  8. 题库明细 C#语言和SQL Server
  9. BGP——Route-map扩展(讲解+配置)@
  10. HTML5 Geolocation用来定位用户的位置。
  11. java基础知识---IO常用基础操作(二)
  12. 分享一个好用的图片压缩软件
  13. python 伯努利分布
  14. 第三方支付接口申请和开发
  15. Photoshop抠图(用调整边缘命令抠图)
  16. SAP学习之北冥神功
  17. 美联航客机门被冻坏无法起飞 乘客困机上约16小时
  18. hugeng007_tensorflow_demo
  19. 用PL/SQL创建图书表
  20. Spring入门到精通:第二章 IOC容器(XML方式):1.IOC概念

热门文章

  1. 【Java小游戏】小球躲避游戏 图片轮播爆炸效果、边缘反弹、计时功能
  2. Linux中source命令的用法:修改环境变量之后立即生效
  3. Pandas高级教程之:处理缺失数据
  4. 在onelogin中使用OpenId Connect Implicit Flow
  5. Leet Code OJ 26. Remove Duplicates from Sorted Array [Difficulty: Easy]
  6. Leet Code OJ 112. Path Sum [Difficulty: Easy]
  7. IntelliJ IDEA使用(一)基本设置与类、方法模板设置
  8. 【双100%提交】剑指 Offer 09. 用两个栈实现队列
  9. 测试点3错的来:1028 人口普查 (20分)(解题报告)
  10. [Leetcode总结] 98.验证二叉搜索树