【1】Learn Opengl 复习向 入门-你好窗口

本教程为初学者介绍了如何使用OpenGL创建窗口并设置渲染环境。通过简单的示例代码,读者可以了解OpenGL的基本用法,包括初始化环境、处理输入事件以及设置渲染回调等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://learnopengl-cn.github.io/
入门-你好窗口

#include<glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include<stdio.h>
#include<math.h>

#pragma region 函数_变量声明
GLFWwindow* window;								//窗口指针
const unsigned int SCR_WIDTH = 800;				//屏幕初始宽度设定
const unsigned int SCR_HEIGHT = 600;			//屏幕初始高度设定

void init();
void processInput(GLFWwindow* window);
void mouse_callback(GLFWwindow* window, double xPos, double yPos);
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
void framebuffer_size_callback(GLFWwindow* window, int width, int height);

#pragma endregion

int main()
{
	init();

	//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);//画图模式设置边框or填充 GL_LINE

	//渲染循环(Render Loop)
	while (!glfwWindowShouldClose(window))
	{
		processInput(window);					 //有事件触发的输入

		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		
		
		glfwSwapBuffers(window);				 //交换颜色缓冲
		glfwPollEvents();						 //检查触发事件
	}

	glfwTerminate();								//释放
	return 0;
}

#pragma region 初始化
void init()
{
	//initialize
	glfwInit();//initialize glfw
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);//VERSION
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);//MODE
	
	#ifdef __APPLE__//mac系统需要
		glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
	#endif

	//创建窗口
	window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);//creat window指针前面全局定义
	
	//检验窗口														  												
	if (window == NULL)
	{
		printf("err:windows is NULL");
		glfwTerminate();
		return;
	}
	//绑定上下文和窗口
	glfwMakeContextCurrent(window);
	//注册回调函数
	glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);						 //鼠标输入模式
	glfwSetMouseButtonCallback(window, mouse_button_callback);						 //鼠标点击反馈
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);				 //窗口大小反馈

	//检验glad							   
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
	{
		printf("err:glad  is NULL ");
		glfwTerminate();
		return;
	}

}
#pragma endregion


#pragma region 事件响应
void processInput(GLFWwindow* window)
{
	if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
	{
		glfwSetWindowShouldClose(window, true);
	}
}


void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
	if (action == GLFW_PRESS) switch(button)
	{
	case GLFW_MOUSE_BUTTON_LEFT:
		glfwSetCursorPosCallback(window, mouse_callback);
		break;
	default:
		return;
	}
	if (action != GLFW_PRESS)
	{
	}
	return;
}

void mouse_callback(GLFWwindow* window, double xPos, double yPos)
{

}


void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
	glViewport(0, 0, width, height);
}
#pragma endregion
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值