【OpenGL】(step1)开个窗口

用OpenGL开一个窗口

建立步骤

1:本菜鸟用的IDE是visual studio 2019 RC,至于OpenGL的环境配置我就不多说了,网上有很多教程。
2:配置好OpenGL环境之后建立一个c++project,然后贴上检测代码,运行看是否能出现窗口,如果有错误注意查看include directories等是否正确。
3:以上没有问题就可以考虑构建Window类(往后可能还会调整)。

构建Window类

窗口标题:m_Title
窗口宽:m_Width
窗口高:m_Height
窗口对象:m_Window
glClear() 函数准备放在clear()函数当中,这个函数的作用是用当前缓冲区清除值,
closed():确定窗口是否要关闭,
init():初始化函数窗口,
update():轮询事件(glfwPollEvents())和交换双缓冲函数(glfwSwapBuffers())
具体函数的作用可以参考glfw的官方网站或者google

//Window.h
#pragma once
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <string>
#include <iostream>

class Window
{
private:
	int m_Width, m_Height;
	const char* m_Title;
	GLFWwindow* m_Window;
public:
	Window(const char* title,int width,int height);
	~Window();
	void clear()const;
	bool closed()const;
	void update()const;
	friend void processInput(GLFWwindow* window);
private:
	bool init();
	friend void framebuffer_size_callback(GLFWwindow* window, int width, int height);
};

下面是具体实现过程

//Window.cpp
#include "Window.h"

void processInput(GLFWwindow* window) {
	Window* win = (Window*)glfwGetWindowUserPointer(window);
	if (glfwGetKey(win->m_Window, GLFW_KEY_ESCAPE)==GLFW_PRESS)
		glfwSetWindowShouldClose(win->m_Window, GL_TRUE);
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
	Window* win = (Window*)glfwGetWindowUserPointer(window);
	glfwGetFramebufferSize(win->m_Window, &win->m_Width, &win->m_Height);
	glViewport(0, 0, win->m_Width, win->m_Height);
}

Window::Window(const char* title, int width, int height)
	:m_Width(width),m_Height(height),m_Title(title)
{
	if (!init())
		std::cout << "Failed to initialize window!" << std::endl;
	else std::cout << "Success!" << std::endl;
}

Window::~Window() {
	glfwTerminate();
}

bool Window::closed()const {
	return glfwWindowShouldClose(m_Window)==1;
}

bool Window::init(){
	//初始化GLFW
	if (!glfwInit()) {
		std::cout << "Failed to initialize the window!" << std::endl;
		return false;
	}
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	//创建一个窗口和它OpenGL上下文
	m_Window = glfwCreateWindow(m_Width, m_Height, m_Title, nullptr, nullptr);
	if (!m_Window) {
		std::cout << "Failed to create the window!" << std::endl;
		glfwTerminate();
		return false;
	}
	glfwMakeContextCurrent(m_Window);
	//此函数设置指定窗口的用户定义指针。保留当前值,直到窗口被销毁。
	glfwSetWindowUserPointer(m_Window, this);
	glfwSetFramebufferSizeCallback(m_Window, framebuffer_size_callback);
	//初始化glad
	if (!gladLoadGLLoader(GLADloadproc(glfwGetProcAddress))) {
		std::cout << "Failed to initialize GLAD!" << std::endl;
		return false;
	}
	return true;
}
void Window::clear() const{
	glclear(GL_COLOR_BUFFER_BIT);
}
void Window::update()const {
	glfwSwapBuffers(m_Window);
	glfwPollEvents();
}

接下来是主函数

//Main.cpp
#include "Window.h"

int main() {
	Window window("william", 1200, 800);
	while (!window.closed()) {
		window.clear();
		window.update();
	}
	glfwTerminate();
	return 0;
}

这样就可以创造一个窗口。如果想让窗口有颜色可以在循环前加入glClearColor()函数,此函数有4个参数,对应rgba(红,绿,蓝,α值),α值指的是透明度。

幸甚至哉,歌以咏志。欢迎来喷。
本菜鸟github:https://github.com/project-William

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值