1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 现代OpenGL教程(六):鼠标和键盘(imgui+OpenGL3.3)

现代OpenGL教程(六):鼠标和键盘(imgui+OpenGL3.3)

时间:2021-07-10 22:37:52

相关推荐

现代OpenGL教程(六):鼠标和键盘(imgui+OpenGL3.3)

前言:该系列教程主要参考自网站www.opengl-,基于开源GUI框架imgui v1.61实现,imgui自带的例子里面直接集成了glfw+gl3w环境,本系列教程将gl3w换成了glew,glew具体环境配置可参考:OpenGL环境配置教程:VS + GLEW + GLFW + GLM。

教程目录(持续更新中):

现代OpenGL教程(一):绘制三角形(ImGui+OpenGL3.3)

现代OpenGL教程(二):矩阵变换(ImGui+OpenGL3.3)

现代OpenGL教程(三):绘制彩色立方体(ImGui+OpenGL3.3)

现代OpenGL教程(四):立方体纹理贴图(ImGui+OpenGL3.3)

现代OpenGL教程(五):obj模型加载(ImGui+OpenGL3.3)

现代OpenGL教程(六):鼠标和键盘(ImGui+OpenGL3.3)

现代OpenGL教程(七):基础光照——颜色(ImGui+OpenGL3.3)

现代OpenGL教程(八):基础光照——Phong光照模型(ImGui+OpenGL3.3)

现代OpenGL教程(九):基础光照——材质(ImGui+OpenGL3.3)

本节教程在上一节(现代OpenGL教程(五):obj模型加载(imgui+OpenGL3.3))的基础上,增加了鼠标和键盘控制视图的功能。完整代码下载地址:/maijiaquan/blog-code/tree/master/opengl-control

运行效果

第一步:鼠标和键盘控制相机view矩阵

glm::mat4 GetView(){static double lastTime = glfwGetTime();double currentTime = glfwGetTime();float deltaTime = float(currentTime - lastTime);// Get mouse positionif (canMouseRotate){horizontalAngle -= mouseSpeed * ImGui::GetIO().MouseDelta.x;verticalAngle -= mouseSpeed * ImGui::GetIO().MouseDelta.y;}// Direction : Spherical coordinates to Cartesian coordinates conversionglm::vec3 direction(cos(verticalAngle) * sin(horizontalAngle),sin(verticalAngle),cos(verticalAngle) * cos(horizontalAngle));// Right vectorglm::vec3 right = glm::vec3(sin(horizontalAngle - 3.14f / 2.0f),0,cos(horizontalAngle - 3.14f / 2.0f));// Up vectorglm::vec3 up = glm::cross(right, direction);// Move forwardif (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS){position += direction * deltaTime * speed;}// Move backwardif (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS){position -= direction * deltaTime * speed;}// Strafe rightif (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS){position += right * deltaTime * speed;}// Strafe leftif (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS){position -= right * deltaTime * speed;}float FoV = initialFoV ; glm::mat4 ViewMatrix = glm::lookAt(position, // Camera is hereposition + direction, // and looks here : at the same position, plus "direction"up// Head is up (set to 0,-1,0 to look upside-down));lastTime = currentTime;return ViewMatrix;}

第二步:将view矩阵传给OpenGL

glm::mat4 ViewMatrix = GetView();glm::mat4 MVP = Projection * ViewMatrix * Model;

参考资料:

第六课:键盘和鼠标 opengl-tutorial

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。