网络建设网站有关知识,wordpress去除版权,黄骅港招聘贴吧最新招聘,免费ppt模板下载 简约9.2 OpenGL调用窗口#xff0c;方向键和鼠标
9.2.1 opengl调用窗口
OpenGL调用窗口步骤#xff1a;
第一步#xff1a;初始化 GLFW#xff0c;初始化OpenGL,初始化窗口#xff0c;初始化上下文
第二步#xff1a;设置窗口大小和位置#xff0c;设置输入输出
第三步…9.2 OpenGL调用窗口方向键和鼠标
9.2.1 opengl调用窗口
OpenGL调用窗口步骤
第一步初始化 GLFW初始化OpenGL,初始化窗口初始化上下文
第二步设置窗口大小和位置设置输入输出
第三步循环渲染
第四步终止
示例代码
#include glew.h
#include glfw3.h
#include iostreamvoid processInput(GLFWwindow* window)
{if(glfwGetKey(window, GLFW_KEY_ESCAPE) GLFW_PRESS)glfwSetWindowShouldClose(window, true);
}int main()
{//第一步初始化glfw,glfwInit();//初始化glfw//glfwWindowHint初始化glfw的版本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);//mac上使用//初始化窗口GLFWwindow* window glfwCreateWindow(800, 600, learnOpenGL, NULL, NULL);if (window NULL){std::cout Failed to create GLFW window std::endl;glfwTerminate();return -1;}//初始化上下文glfwMakeContextCurrent(window); //将主线程设置为当前渲染环境//Init GLEWglewExperimental true;if(glewInit() ! GLEW_OK){printf(Init GLEW failed.);glfwTerminate();return -1;}//第二步设置窗口大小和位置glViewport(0, 0, 800, 600);//前两个参数窗口左下角的位置。后两个渲染窗口的宽度和高度while (!glfwWindowShouldClose(window)){processInput(window); ///设置输入输出glClearColor(0.2f, 0.3f, 0.3f, 1.0f);//前面三个参数是RGB后面一个参数是透明度glClear(GL_COLOR_BUFFER_BIT);glfwSwapBuffers(window);//函数在每次循环迭代开始时检查 GLFW 是否已被指示关闭glfwPollEvents();//函数检查是否触发了任何事件}glfwTerminate();//第四步清理return 0;
}9.2.2 opengl 调用方向键
调用方向键主要是考虑按键和移动速度修改的办法是在上面调用窗口的设置函数processInput上进行修改
void processInput(GLFWwindow *window)
{...const float cameraSpeed 0.05f; // adjust accordinglyif (glfwGetKey(window, GLFW_KEY_W) GLFW_PRESS)//按下W键cameraPos cameraSpeed * cameraFront;//cameraFront相机超向if (glfwGetKey(window, GLFW_KEY_S) GLFW_PRESS)//按下S键cameraPos - cameraSpeed * cameraFront;if (glfwGetKey(window, GLFW_KEY_A) GLFW_PRESS)//按下A键cameraPos - glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;if (glfwGetKey(window, GLFW_KEY_D) GLFW_PRESS)//按下D键cameraPos glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
}9.2.3 opengl调用鼠标
OpenGL 调用鼠标主要考虑鼠标的移动和缩放主要是使用下面3个函数
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); //隐藏并捕获光标
void mouse_callback(GLFWwindow* window, double xpos, double ypos);//监听鼠标移动事件xpos和ypos代表当前鼠标位置
glfwSetCursorPosCallback(window, mouse_callback)计算鼠标光标步骤
计算鼠标自上一帧以来的偏移量。将偏移值添加到相机的偏航和俯仰值中。为最小/最大音高值添加一些约束。计算方向向量。
代码
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{if (firstMouse)// initially set to true保证初次使用不会大幅度跳跃{lastX xpos;lastY ypos;firstMouse false;}float xoffset xpos - lastX;float yoffset lastY - ypos; lastX xpos;lastY ypos;float sensitivity 0.1f; //乘以灵敏度值降低鼠标移动太剧烈xoffset * sensitivity;yoffset * sensitivity;yaw xoffset;//偏移值加入俯仰pitch yoffset;//偏移值加入偏航if(pitch 89.0f)///加入约束pitch 89.0f;if(pitch -89.0f)pitch -89.0f;glm::vec3 direction;//计算实际方向向量direction.x cos(glm::radians(yaw)) * cos(glm::radians(pitch));direction.y sin(glm::radians(pitch));direction.z sin(glm::radians(yaw)) * cos(glm::radians(pitch));cameraFront glm::normalize(direction);
}
2鼠标缩放
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{fov - (float)yoffset;if (fov 1.0f)fov 1.0f;if (fov 45.0f)fov 45.0f;
}projection glm::perspective(glm::radians(fov), 800.0f / 600.0f, 0.1f, 100.0f);
glfwSetScrollCallback(window, scroll_callback);