opengl 键盘和鼠标

glfw库键盘,鼠标点击和滚轮事件处理 https://blog.csdn.net/ynsenyu/article/details/39423493 阅读详情
opengl 键盘和鼠标
还是针对之前的那个立方体,只是键盘和鼠标输入时,实时修改它的透视矩阵和视图矩阵。

#include "glew.h"
#include <glfw3.h>
#include "common/loadShader.h"
#include "common/controls.h"

#include "glm.hpp"
#include "ext.hpp"
GLFWwindow* window;
int main(void)
{
	//GLFWwindow* window;

	/* Initialize the library */
	if (!glfwInit())
		return -1;

	/* Create a windowed mode window and its OpenGL context */
	window = glfwCreateWindow(480, 320, "Hello World", NULL, NULL);
	if (!window)
	{
		glfwTerminate();
		return -1;
	}

	/* Make the window's context current */
	glfwMakeContextCurrent(window);

	// Needed in core profile
	if( glewInit() != GLEW_OK)
	{
		glfwTerminate();
		return -1;
	}

	// Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
	// A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
	static const GLfloat g_vertex_buffer_data[] = {
		-1.f, 1.f, 1.f,
		-1.f,-1.f, 1.f,
		 1.f,-1.f, 1.f,

		 1.f,-1.f, 1.f,
		 1.f, 1.f, 1.f,
		 -1.f, 1.f, 1.f,


		 1.f, 1.f, 1.f,
		 1.f,-1.f, 1.f,
		 1.f,-1.f,-1.f,

		 1.f,-1.f,-1.f,
		 1.f, 1.f,-1.f,
		 1.f, 1.f, 1.f,


		-1.f, 1.f,-1.f,
		-1.f, 1.f, 1.f,
		 1.f, 1.f, 1.f,

		 1.f, 1.f, 1.f,
	     1.f, 1.f,-1.f,
		 -1.f, 1.f,-1.f,


		-1.f,-1.f,-1.f,
		-1.f,-1.f, 1.f,
		 1.f,-1.f, 1.f,

		 1.f,-1.f, 1.f,
		 1.f,-1.f,-1.f,
		 -1.f,-1.f,-1.f,


		-1.f, 1.f,-1.f,
		-1.f,-1.f,-1.f,
		 1.f,-1.f,-1.f,

		 1.f,-1.f,-1.f,
		 1.f, 1.f,-1.f,
		 -1.f, 1.f,-1.f,


		 -1.f, 1.f, 1.f,
		 -1.f, 1.f,-1.f,
		 -1.f,-1.f,-1.f,

		 -1.f,-1.f,-1.f,
		 -1.f,-1.f, 1.f,
		-1.f, 1.f, 1.f, 		
 		
	};

	
	//This will identify our vertex buffer
	GLuint vertexbuffer;

	
	//Generate 1 buffer,put the resulting identifier in vertexbuffer
	glGenBuffers(1,&vertexbuffer);		

	//The following commands will talk about our 'vertexbuffer' buffer
	glBindBuffer(GL_ARRAY_BUFFER,vertexbuffer);

	//Give our vertices to OpenGL.
	glBufferData(GL_ARRAY_BUFFER,sizeof(g_vertex_buffer_data),g_vertex_buffer_data,GL_STATIC_DRAW);


	GLuint Texture = loadBMP_custom("./resource/uvtemplate.bmp");
	// One color for each vertex. They were generated randomly.
	static const GLfloat g_uv_buffer_data[] = {
		0.0f, 1.0f,
		0.0f, 0.0f,
		1.0f, 0.0f,

		1.0f, 0.0f,
		1.0f, 1.0f,
		0.0f, 1.0f,


		0.0f, 1.0f,
		0.0f, 0.0f,
		1.0f, 0.0f,

		1.0f, 0.0f,
		1.0f, 1.0f,
		0.0f, 1.0f,



		0.0f, 1.0f,
		0.0f, 0.0f,
		1.0f, 0.0f,

		1.0f, 0.0f,
		1.0f, 1.0f,
		0.0f, 1.0f,


		0.0f, 1.0f,
		0.0f, 0.0f,
		1.0f, 0.0f,

		1.0f, 0.0f,
		1.0f, 1.0f,
		0.0f, 1.0f,


		0.0f, 1.0f,
		0.0f, 0.0f,
		1.0f, 0.0f,

		1.0f, 0.0f,
		1.0f, 1.0f,
		0.0f, 1.0f,


		0.0f, 1.0f,
		1.0f, 1.0f,
		1.0f, 0.0f,

		1.0f, 0.0f,
		0.0f, 0.0f,
		0.0f, 1.0f		
 		
	};
	GLuint uvbuffer;
	glGenBuffers(1,&uvbuffer);
	glBindBuffer(GL_ARRAY_BUFFER,uvbuffer);
	glBufferData(GL_ARRAY_BUFFER,sizeof(g_uv_buffer_data),g_uv_buffer_data,GL_STATIC_DRAW);


	GLuint programID = LoadShaders("./shader/vertex.shader","./shader/fragment.shader");
	
	glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
	/* Loop until the user closes the window */

	// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit  100 units
	//glm::mat4 Projection = glm::ortho(-4.0f/3.0f, 4.0f/3.0f, -1.0f, 1.0f, 0.1f, 100.0f);
	
	
		
	
		// Get a handle for our "MVP" uniform.
		// Only at initialisation time.
		GLuint MatrixID = glGetUniformLocation(programID,"MVP");
	
		// Send our transformation to the currently bound shader,
		// in the "MVP" uniform
		// For each model you render, since the MVP will be different (at least the M part)
		

		GLuint TextureID = glGetUniformLocation(programID,"myTextureSampler");
		int width,height;	
		glfwGetWindowSize(window,&width,&height);
		float centerX = width;
		float centerY = height;
		centerX /= 2;
		centerY /= 2;
		glfwSetCursorPos(window,centerX,centerY);
		
	while (glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && !glfwWindowShouldClose(window))
	{
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glUseProgram(programID);
		computeMatricesFromInputs();
		glm::mat4 Projection = getProjectionMatrix();
		glm::mat4 View = getViewMatrix();
		//Model matrix : an identity matrix (model will be at the origin)
		glm::mat4 Model = glm::mat4(1.0f);

		// Our ModelViewProjection : multiplication of our 3 matrices
		glm::mat4 MVP = Projection * View * Model;// Remember, matrix multiplication is the other way around
		glUniformMatrix4fv(MatrixID,1,GL_FALSE,&MVP[0][0]);


		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D,Texture);
		glUniform1i(TextureID, 0);
		

		glEnableVertexAttribArray(0);
		glBindBuffer(GL_ARRAY_BUFFER,vertexbuffer);
		glVertexAttribPointer(
			0,			// attribute 0. No particular reason for 0, but must match the layout in the shader.
			3,			// size
			GL_FLOAT,	// type
			GL_FALSE,	// normalized?
			0,			// stride
			(void*)0	// array buffer offset
			);

		

		glEnableVertexAttribArray(1);
		glBindBuffer(GL_ARRAY_BUFFER,uvbuffer);	
		glVertexAttribPointer(
			1,			// attribute 0. No particular reason for 0, but must match the layout in the shader.
			2,			// size
			GL_FLOAT,	// type
			GL_FALSE,	// normalized?
			0,			// stride
			(void*)0	// array buffer offset
			);

		glEnable(GL_DEPTH_TEST);
		glDepthFunc(GL_LESS);
		glDrawArrays(GL_TRIANGLES,0,12*3);// Starting from vertex 0; 3 vertices total -> 1 triangle
		
			
		

		
		

		
		glDisableVertexAttribArray(0);
		glDisableVertexAttribArray(1);
		/* Swap front and back buffers */
		glfwSwapBuffers(window);

		/* Poll for and process events */
		glfwPollEvents();
	}

	glfwTerminate();
	return 0;
}

来看键盘和鼠标检测
#include "controls.h"
#include <glfw3.h>
#include "glm.hpp"
#include "ext.hpp"
glm::mat4 ProjectionMatrix ;
glm::mat4 ViewMatrix;
extern GLFWwindow* window;
float speed = 3.0f; // 3 units / second
float mouseSpeed = 0.005f;
// position
glm::vec3 position = glm::vec3(0,0,5);
// horizontal angle : toward -Z
float horizontalAngle = 3.14f;
//vertical angle : 0, look at the horizon
float verticalAngle = 0.0f;
//Initial Field of View
float initialFov = 45.0f;
double lastXpos = 240;
double lastYpos = 160;
void computeMatricesFromInputs()
{
	

	
	//FoV is the level of zoom. 80° = very wide angle, huge deformations. 60° – 45° : standard. 20° : big zoom.
	double xpos,ypos;
	
	glfwGetCursorPos(window,&xpos,&ypos);

	
	

	static double lastTime = glfwGetTime();
	double currentTime = glfwGetTime();
	float deltaTime = float(currentTime - lastTime);
	lastTime = float(currentTime);

	// Compute new orientation
	horizontalAngle += mouseSpeed  * float(xpos - lastXpos);
	verticalAngle += mouseSpeed  * float(ypos - lastYpos);
	lastXpos = xpos;
	lastYpos = ypos;

	// Direction : Spherical coordinates to Cartesian coordinates conversion
	glm::vec3 direction(
		cos(verticalAngle) * sin(horizontalAngle),
		sin(verticalAngle),
		cos(verticalAngle) * cos(horizontalAngle)
		);

	// Right vector
	glm::vec3 right = glm::vec3(
		sin(horizontalAngle - 3.14f/2.0f),
		0,
		cos(horizontalAngle - 3.14f/2.0f)
		);

	// Up vector : perpendicular to both direction and right
	glm::vec3 up = glm::cross( right, direction );

	// Move forward
	if (glfwGetKey(window, GLFW_KEY_UP ) == GLFW_PRESS){
		position += direction * deltaTime * speed;
	}
	// Move backward
	if (glfwGetKey(window, GLFW_KEY_DOWN ) == GLFW_PRESS){
		position -= direction * deltaTime * speed;
	}
	// Strafe right
	if (glfwGetKey(window, GLFW_KEY_RIGHT ) == GLFW_PRESS){
		position += right * deltaTime * speed;
	}
	// Strafe left
	if (glfwGetKey(window, GLFW_KEY_LEFT ) == GLFW_PRESS){
		position -= right * deltaTime * speed;
	}

	float FoV = 45.0f;//initialFoV - 5 * glfwGetMouseButton();

	ProjectionMatrix = glm::perspective(FoV, 4.0f / 3.0f, 0.1f, 100.0f);
	ViewMatrix       = glm::lookAt(
		position,           // Camera is here
		position+direction, // and looks here : at the same position, plus "direction"
		up      
		);
	// For the next frame, the "last time" will be "now"
	lastTime = currentTime;
}

glm::mat4 getProjectionMatrix()
{
	return ProjectionMatrix;
}

glm::mat4 getViewMatrix()
{
	return ViewMatrix;
}



最全opengl鼠标键盘控制事件 opengl前后滚轮,鼠标左右中键,键盘f1f2f3等等,可以直接使用,有问题留言,帮助解决,本资源不是那种发起者自己跑都跑不了的 立即下载

相关推荐

opengl鼠标和键盘交互

用opengl实现鼠标和键盘交互,关键是掌握那两个函数,也可以通过MFC实现

Computer Graphics(8)

Bezier曲线绘制 这次作业实现的整体架构与之前使用Bresenham算法绘制直线类似:先通过算法所要绘制的点集,然后进行绘制即可。 STEP1 Bezier算法的实现 由于作业中要求是根据用户输入的四个控制点绘制出对应的拟合曲线,所以我们需要实现的是三阶贝塞尔曲线绘制,通过老师的课件我们可以得知三阶贝塞尔曲线对应的公式如下图所示: 其中Pi代表控制点i,系数中的t与我们所选取的步距...

leprechaun_的博客 321

OpenGL的鼠标键盘事件

OpenGL的鼠标键盘事件机制,简单的例子代码!

glfw 鼠标事件处理

glfw 鼠标事件

代码波士 2320

HFSS实战:手把手教你设计一款小型化圆极化微带天线(附仿真文件)

本文详细介绍了使用HFSS设计小型化圆极化微带天线的完整流程,包括工程初始化、辐射贴片设计、仿真设置与优化等关键步骤。通过实战案例,帮助工程师掌握圆极化天线设计技巧,解决轴比控制和耦合馈电匹配等核心问题,适用于物联网终端和便携式设备应用。

weixin_32770423的博客 345

Anroid8.1 强制修改系统可以adb remount

在Android6.0 (Android M)user版本上,发现使用adb root后再adb remount系统之后会提示这样信息:remount of /system failed: Read-only file system,这是不能对system分区进行操作,提示没有写权限,为只读文件系统Read-only file system,因为系统本身在user就不支持可写操作,我们就不能adb push来调试apk,如果想调试apk就需要修改system、vendor权限问题。 如下现象: C:\Use

wq892373445的博客 1411

[OpengGL] 摄像机[10]

  在上一章中,我们讨论了视图矩阵以及如何使用视图矩阵在场景中移动(我们向后移动了一点)。 OpenGL 本身并不熟悉相机的概念,但我们可以尝试通过反向移动场景中的所有对象来模拟一个相机,给人一种我们正在移动的错觉。  在本章中,我们将讨论如何在 OpenGL 中设置相机。 我们将讨论一种可让您在 3D 场景中自由移动的飞行式相机。 我们还将讨论键盘和鼠标输入,并以自定义相机类结束。  当我们谈论相机/观察空间时,我们谈论的是从相机的角度看作为场景原点的所有顶点坐标:视图矩阵将所有世界坐标转换为相对于相机位

unity尧明的博客 780

cMake编译 VS2026+opencv4.12.0+opencv_contrib-4.12.0

本文提供了OpenCV 4.12.0集成CUDA、cuDNN和ONNX Runtime的详细编译指南。主要步骤包括:准备Visual Studio 2026、CMake 4.2.0、CUDA 12.6和cuDNN 9.16.0;设置无空格和中文的路径结构;配置关键CMake变量(如开启CUDA支持、指定路径等);以及最终的编译安装过程。注意事项包括:版本一致性检查、cuDNN文件复制、国内用户需手动获取.cache文件等。文章还提供了常见问题解决方案,如通过设置CUDA_NVCC_FLAGS参数解决VS版本

weixin_46624734的博客 1389

GLFW按键及鼠标事件等

GLFW按键及鼠标事件等,事件回调包括键盘、鼠标、滚动、游标、游戏手柄等

sq8706的专栏 1778

【Stata双重差分模型】双重差分DID的具体操作步骤

本案例将详细阐述如何使用双重差分法进行实证分析,并展示具体的STATA操作代码。

a519573917的博客 2万+

MAX31865三线PT100测温实战:配置、接线与代码全解析

本文详细解析MAX31865三线PT100测温的配置、接线与代码实现。通过实战经验分享,介绍如何利用MAX31865芯片实现高精度温度测量,重点讲解三线制接法的自动误差补偿功能,并提供完整的寄存器配置、SPI通信和温度转换算法,帮助开发者快速解决工业测温中的常见问题。

ss78901的博客 761

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

前言:该系列教程主要参考自网站www.opengl-tutorial.org,基于开源GUI框架imgui v1.61实现,imgui自带的例子里面直接集成了glfw+gl3w环境,本系列教程将gl3w换成了glew,glew具体环境配置可参考:OpenGL环境配置教程:VS2012 + GLEW + GLFW + GLM。 教程目录(持续更新中): 现代OpenGL教程(一):绘制三角形(...

... 4951

Android SystemUI——车载CarSystemUI加载(八)

车载 CarSystemUI 是指专门为汽车环境设计的用户界面和用户体验(UI/UX),它通常与 Android Automotive OS 或其他嵌入式操作系统一起工作,为驾驶者和乘客提供安全、直观且高效的交互方式。Android Automotive OS 是 Google 专门为汽车开发的操作系统版本,它基于 Android 平台,并针对车辆内部的使用进行了优化。

小旭的专栏 2177

OpenGL案例2----keyBoard键盘交互和鼠标交互

键盘按w:向上;按s:向下 ; 按d:向右 按a:向左代码2: 结果: 矩形随鼠标左键点击而移动

liyaoliy的博客 1701

Dify 第 7 篇 Dify 应用介绍 + 聊天助手&Agent 应用关键点说明

本文介绍了Dify应用的核心概念与使用方法。Dify是基于大模型构建的实际场景应用,提供API、WebApp和易用界面。包含5种应用类型:聊天助手、文本生成、Agent、对话流和工作流。创建方式有模板创建、空白应用和DSL文件三种。重点讲解了聊天助手和Agent应用的关键功能:聊天助手支持提示词优化、知识库引用、文件处理等;Agent应用则强调推理能力、工具调用和任务自动化。文章还详细介绍了应用的调试、发布和管理工具,为开发者提供了全面的应用构建指南。

甘蓝的专栏 1891

YOLO-World:从入门到实战的多模态目标检测全指南

想象一下,你给模型一张“公园”的图片,再输入文字“红色的郁金香”,它能直接在图中框出所有红色郁金香的位置——这就是多模态目标检测:结合图像视觉信息和文本语义信息,同时完成“是什么”(分类)和“在哪里”(定位)的任务。传统目标检测(如YOLOv5、YOLOv8)只能识别预定义的类别(比如训练时学过“猫”“狗”),但多模态目标检测摆脱了“类别预定义”的限制,只要文本能描述,它就能检测,这就是“开放词汇检测(Open-Vocabulary Detection)”的魅力。

know_F0727的博客 208

OpenGL学习例程精析(键盘和鼠标的调用)

void keyboard (unsigned char key, int x, int y) { switch (key) { case 's': case 'S': glBindTexture(GL_TEXTURE_2D, texName[0]); glTexSubImage2D(GL_TEXTURE_2D, 0, 12, 44, subImageWidth, subImageHeight

KingsMan666的博客 538

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

openGL 初试 绘制三角形 和添加鼠标键盘事件

code:   #include &lt;gl/glut.h&gt; #include &lt;stdlib.h&gt; void render(void); void keyboard(unsigned char c,int x,int y); void mouse(int button,int state,int x,int y); int main(int arg...

weixin_33827731的博客 265

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

OpenGL随笔十三 opengl 用鼠标和键盘的输入控制相机移动

一、控制相机的视角 //禁用光标 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); //注册鼠标事件回调函数 glfwSetCursorPosCallback(window, mouse_callback); void mouse_callback(GLFWwindow* window, double xpos, double...

yan17iiiiii的博客 1869

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

opengl材质与光属性和键盘鼠标控制的优化

键盘鼠标控制优化,按Q之后才触发将回调函数给window进程 void processInput(GLFWwindow* window) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) //当我们获取了ESC键就退出窗口 glfwSetWindowShouldClose(window, true); if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRES.

wqdqwass的博客 214

计算机图形学(一)——opengl实现三维立方体添加纹理光照与材质、键盘鼠标控制平移旋转和放大缩小

1.实验要求: 用OpenGL和C语言编写一个带纹理和材质的一个立方体的交互式程序。 1)要求生成一个在立方体,并在立方体的六个面上并分别实现不同的纹理映射和材质。纹理的类型为BMP图片纹理,材质类型可以自己设计。例如,三个面上实现纹理映射的头像和生活照,另外三个面上实现材质(金属、塑料等)。 2)可以利用鼠标和键盘进行交互,实现该立方体的旋转、平移和缩放。 2.实验效果图如下: 正常情况下 加...

诶你爱我吗的博客 8832

OpenGL.tutorial06键盘和鼠标

1、这个图是用 Excel画的 简单示意图(单元格边框,视图-->网格线)  1.1、中间的 正立方体 处于 X/Y/Z轴的中心,边长为2   ZC:代码中 原版是 人物在 (0,0,5)处,水平夹角180°(即 horizontalAngle为180°[弧度为π]),相当于 人物向后转了(应该是 绕向量(0,1,0)逆时针旋转的)  1.2、代码为:E:\OpenGL_somet...

weixin_30788731的博客 161

java机器人控制鼠标_[转载]一个用OpenGL绘制通过鼠标和键盘控制的小机器人程序...

//小机器人程序,通过鼠标的 左 右 中 三个键 和键盘//控制小机器人//#include #include //Glaux库的头文件#include //标准输入/输出库的头文件//// 在此处加入程序要求的库到链接器中:#pragma comment( lib, "glaux.lib" ) // 链接时查找glaux.lib#define MAXTEXTURE 3GLuint textu...

weixin_42605397的博客 334

Qt OpenGL 通过鼠标和键盘移动三维物体(设置相机)

顶点着色器: 片段着色器: aaa

wb175208的专栏 1426
上一篇: openg 立方体纹理
下一篇: lua 基础语法(一)
缥缈的云
博客等级 码龄15年 94粉丝 179原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值