加入键盘鼠标控制的代码
以下是简单代码实例,着色器就不放了,比较简单
#define GLEW_STATIC
#define STB_IMAGE_IMPLEMENTATION#include
#include
#include
#include"shader.h"
#include
#include
#include
#include#include
using namespace std;bool firstmouse = true;
bool keys[1024];//视角
GLfloat pitch = 0.0f;
GLfloat yaw = -90.0f;
GLfloat lastx = 400.0f;
GLfloat lasty = 300.0f;
GLfloat fov=45.0f;GLfloat deltaTime = 0.0f; // 当前帧和上一帧之间的时间差
GLfloat lastFrame = 0.0f; // 上一帧时间//camera
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -2.0f);//方向向量=原点-pos
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);//事件回调函数
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
void do_moment();
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void roll_callback(GLFWwindow* window, double xoffset, double yoffset);int main()
{//glfw initialif (!glfwInit()){cout << "Fail to initial glfw!" << endl;return -1;}glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);//windowGLFWwindow *window = glfwCreateWindow(800, 600, "hello triangle", nullptr, nullptr);if (window == NULL){cout << "Fail to create window!" << endl;glfwTerminate();return -1;}//创建窗口的context指定为当前contextglfwMakeContextCurrent(window);//注册事件回调函数glfwSetKeyCallback(window, key_callback);glfwSetCursorPosCallback(window, mouse_callback); //监听鼠标函数glfwSetScrollCallback(window, roll_callback);//监听鼠标滚轮函数//glew initialglewExperimental = GL_TRUE;if (glewInit() != GLEW_OK){cout << "Fail to initial glew!" << endl;glfwTerminate();return -1;}//viewportglViewport(0, 0, 800, 600);//dotfloat vertices[] = {-0.5f, -0.5f, -0.5f, 0.0f, 0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 0.0f, 1.0f};unsigned int VBO, VAO;glGenVertexArrays(1, &VAO);glGenBuffers(1, &VBO);glBindVertexArray(VAO);glBindBuffer(GL_ARRAY_BUFFER, VBO);glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);// position attributeglVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);glEnableVertexAttribArray(0);// texture coord attributeglVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));glEnableVertexAttribArray(1);//texture GB//texture 1unsigned int texture1,texture2;glGenTextures(1, &texture1);glBindTexture(GL_TEXTURE_2D, texture1);// set the texture wrapping parametersglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);// set texture filtering parametersglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);//loadint height, width, nrChannel;unsigned char *date = stbi_load("container.jpg", &width, &height, &nrChannel, 0);//checkif (date){glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, date);glGenerateMipmap(GL_TEXTURE_2D);}else{cout << "Failed to load texture" << endl;}stbi_image_free(date);//texture 2glGenTextures(1, &texture2);glBindTexture(GL_TEXTURE_2D, texture2);// set the texture wrapping parametersglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);// set texture filtering parametersglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);date = stbi_load("awesomeface.png", &width, &height, &nrChannel, 0);//checkif (date){glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, date);RGBAglGenerateMipmap(GL_TEXTURE_2D);}else{cout << "Failed to load texture2" << endl;}stbi_image_free(date);//positionglm::vec3 cubePositions[] = {glm::vec3(0.0f, 0.0f, 0.0f),glm::vec3(2.0f, 5.0f, -15.0f),glm::vec3(-1.5f, -2.2f, -2.5f),glm::vec3(-3.8f, -2.0f, -12.3f),glm::vec3(2.4f, -0.4f, -3.5f),glm::vec3(-1.7f, 3.0f, -7.5f),glm::vec3(1.3f, -2.0f, -2.5f),glm::vec3(1.5f, 2.0f, -2.5f),glm::vec3(1.5f, 0.2f, -1.5f),glm::vec3(-1.3f, 1.0f, -1.5f)};//准备着色器Shader shader("t.vert", "t.frag");GLint modelLoc = glGetUniformLocation(shader.programId, "model");GLint viewLoc = glGetUniformLocation(shader.programId, "view");GLint projectionLoc = glGetUniformLocation(shader.programId, "projection");//解除绑定glBindBuffer(GL_ARRAY_BUFFER, 0);glBindVertexArray(0);//开启深度测试glEnable(GL_DEPTH_TEST);//隐藏鼠标glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);//显示主循环while (!glfwWindowShouldClose(window)){//平衡设备速度GLfloat currentFrame = glfwGetTime();deltaTime = currentFrame - lastFrame;lastFrame = currentFrame;//记录键盘鼠标等glfwPollEvents();do_moment();//colorglClearColor(0.5f, 0.0f, 1.0f, 1.0f);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//shadershader.use();//active textureglActiveTexture(GL_TEXTURE0);glBindTexture(GL_TEXTURE_2D, texture1);glActiveTexture(GL_TEXTURE1);glBindTexture(GL_TEXTURE_2D, texture2);glUniform1i(glGetUniformLocation(shader.programId, "ourTexture"), 0);glUniform1i(glGetUniformLocation(shader.programId, "ourTexture0"), 1);//camera//GLfloat camerax = sin((GLfloat)glfwGetTime())*10.0f;//(glm::radians(10.0f));//GLfloat cameraz = cos((GLfloat)glfwGetTime())*10.0f;// (glm::radians(10.0f));glm::mat4 view;view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);glm::mat4 projection;projection = glm::perspective(fov, (GLfloat)800/(GLfloat)600, 0.1f, 100.0f);//3DglUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));glBindVertexArray(VAO);for (GLuint i = 0; i < 10; i++){glm::mat4 model;//对于每一个model是会变换的,所以每次渲染的时候需要重新申请modelmodel = glm::translate(model, cubePositions[i]);GLfloat angle = 20.0f * i*(glfwGetTime()*50.0f/20);model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));glDrawArrays(GL_TRIANGLES, 0, 36);}glBindVertexArray(0);glUseProgram(0);//交换缓存glfwSwapBuffers(window);}glDeleteVertexArrays(1, &VAO);glDeleteBuffers(1, &VBO);glfwTerminate();return 0;
}void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{if (action == GLFW_PRESS)keys[key] = true;else if (action == GLFW_RELEASE)keys[key] = false;if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS){glfwSetWindowShouldClose(window, GL_TRUE); // 关闭窗口}
}
void do_moment()
{// 摄像机控制GLfloat cameraSpeed = 5.0f * deltaTime;if (keys[GLFW_KEY_W])cameraPos += cameraSpeed * cameraFront;if (keys[GLFW_KEY_S])cameraPos -= cameraSpeed * cameraFront;if (keys[GLFW_KEY_A])cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;if (keys[GLFW_KEY_D])cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{if (firstmouse){xpos = lastx;ypos = lasty;firstmouse = false;}GLfloat offsetx = xpos - lastx;GLfloat offsety = lasty - ypos;lastx = xpos;lasty = ypos;GLfloat sensitivity = 0.05;offsetx *= sensitivity;offsety *= sensitivity;yaw += offsetx;pitch += offsety;if (pitch > 89.9)pitch = 89.9;if (pitch < -89.9)pitch = -89.9;glm::vec3 front;front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));front.y = sin(glm::radians(pitch));front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));cameraFront = glm::normalize(front);
}
void roll_callback(GLFWwindow* window, double xoffset, double yoffset)
{if (fov >= 1.0f && fov <= 45.0f)fov -= yoffset;if (fov <= 1.0f)fov = 1.0f;if (fov >= 45.0f)fov = 45.0f;
}
加入键盘鼠标控制的代码相关推荐
- Cesium 键盘鼠标控制相机漫游(源码+原理讲解)
Cesium 键盘鼠标控制相机漫游(源码+原理讲解) 在各大博客平台上,Cesium使用键盘控制相机漫游的源码已经有不少人贴出源码,本人在浏览这些源码的过程中发现大家采用的方式基本一致,大部分代码都是 ...
- 【Unity3D】键盘鼠标控制视角的移动
在3D游戏里面,视角是一个很重要的因素,部分玩家对锁视角嗤之以鼻,因此视角也是游戏制作里面需要努力的方面.其实也不难,换到Unity3D里面是想办法控制摄像机的移动.下面举一个简单的例子说明在Unit ...
- VR项目添加键盘鼠标控制
VR项目添加键盘鼠标控制 使用VR模版创建的项目,程序运行后,只能使用VR头盔进行交互场景,不便于调试工作,为此,在场景中添加键盘.鼠标控制功能. 1 .键盘控制添加: 键盘控制主要为ASDW 键控制 ...
- python键盘控制_Python 键盘/鼠标控制
键盘控制 需要用到keyboard模块(pip install keyboard) 按键字符 #字符'1''a'...#控制'ctrl''alt''shift''enter''esc''f1'...# ...
- Python之OpenGL笔记(17):键盘鼠标控制摄像机移动旋转
一.目的 1.键盘控制摄像机自由移动: 2.鼠标控制摄像机旋转: 3.鼠标滚轮放大缩小视野. 二.程序运行结果 三.自由移动 view = lookAt(cameraPos, cameraPos + ...
- 无界鼠标:使用一套键盘鼠标控制多台电脑
在办公室需要同时使用自己的电脑和公司的电脑,然而来回切换鼠标是真的麻烦,于是在网上找到并整合和相关方法,供大家参考使用: 无界鼠标百度云:链接:https://pan.baidu.com/s/1GI5 ...
- java写键盘鼠标录制器,易语言仿按键精灵录制键盘鼠标操作的代码
DLL命令表 .版本 2 .DLL命令 取按键状态, 短整数型, "user32.dll", "GetAsyncKeyState" .参数 鼠标键, 整数型 . ...
- 虚幻4键盘鼠标控制输入事件操作笔记
虚幻的键盘输入命令设置是在工程设置里面进行.设置完成以后,在蓝图编辑器里面就可以进行调用了,鼠标点击事件之间在蓝图编辑界面调用,就不需要再工程设置里面设置了
- 使用Synergy控制实现一套键盘鼠标控制多台电脑(windows+linux)
一,下载安装 Synergy windows版本下载地址:http://www.3322.cc/soft/14996.html Synergy linux版本下载地址:https://www.jb51 ...
最新文章
- shell实行mysql语句_【Mysql】shell运行mysql的sql语句_MySQL
- python list 和矩阵的切片
- Python3--批量爬取数据之调用有道api进行翻译
- java 转账 锁_Java多线程 多个人转账发生死锁
- 初识-Android之智能短信项目相关技术整理
- canvas动画简单操作
- 优秀自我简介200字_全球战疫 翰墨传情——东方盛世杯网络公益书画展优秀作品【二】...
- python 物理学中的应用_大学物理中Python的应用
- android 编译sdk,android编译sdk
- 中奖名单出炉,本期“开发者大调查”的奖品花落谁家?
- html文件如何转php文件,怎么把动态的php文件转换成静态的html文件,html文件是php文件…...
- sqlserver management studio 2014 升级2016_画质提升音质更佳:Insta360影石ONE R迎来最强升级...
- Linux Shell常用命令学习(1)
- [小说连载]张小庆,在路上(2)- 兰博和威震天
- ubuntu1604安装网卡驱动 联想g510
- HMC——Hamiltonian Monte Carlo笔记
- python获取当前日期所在的是本月第几周
- 自己捣鼓的小程序实现订单代付的功能
- benchmark在postgresql上的安装及使用
- ChunJun 1.16 Release版本即将发布,bug 捉虫活动邀您参与!