OpenGL入门系列- 窗口和GLContext

GLX是图形窗口和OpenGL的媒介,起着胶水的作用,使得OpenGL绘制结果能够显示到图形窗口上,以及维护一个GLContext来保存OpenGL的各种状态。

#pragma once

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <cmath>

#include <GL/glew.h>
#include <GL/glxew.h>
#include <GLFW/glfw3.h>

#include <GL/gl.h>
#include <GL/glext.h>
class GLApplication
{
public:
    GLApplication();
    virtual ~GLApplication();
    virtual void run(GLApplication* app);
    virtual void init();
    virtual void startup();
    virtual void render(double currentTime);
    virtual void shutdown();
&n

### 创建第一个 OpenGL 窗口 #### 初始化环境 为了创建一个基本的 OpenGL 应用程序,需要先设置开发环境。这通常涉及到安装必要的库文件配置编译器。 对于 Linux 用户,在终端执行如下命令来安装所需软件包[^2]: ```bash sudo apt-get install libglu1-mesa-dev freeglut3-dev mesa-common-dev ``` 而对于 Windows 用户,则可以通过 MinGW 或 Visual Studio 来构建项目,并下载预编译版本的 GLFW GLAD 库。 #### 加载 OpenGL 函数指针 Glad 是用于管理 OpenGL 函数指针的重要工具。在调用任何 OpenGL API 之前,必须通过 Glad 进行初始化操作以确保可以正常使用这些函数[^3]. #### 使用 GLFW 创建窗口 GLFW 提供了一个简单易用的方式来创建 OpenGL 上下文及窗口对象。下面是一个完整的 C++ 示例代码展示如何实现这一点: ```cpp #include <iostream> #include <GL/glew.h> // GLEW library header file #include <GLFW/glfw3.h> // GLFW library header file int main() { // Initialize the glfw library before creating a windowed mode window. if (!glfwInit()) { std::cerr << "Failed to initialize GLFW\n"; return -1; } // Create a windowed mode window and its associated OpenGL context with specified width, height, title. GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); if (window == NULL){ std::cerr << "Failed to create GLFW window\n"; glfwTerminate(); return -1; } // Make the window's context current so that all subsequent calls will affect this particular window. glfwMakeContextCurrent(window); // Load all available OpenGL function pointers into memory using gladLoadGLLoader(). if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){ std::cout << "Failed to initialize GLAD" << std::endl; return -1; } while (!glfwWindowShouldClose(window)) { // Render here // Swap front and back buffers after rendering is done. glfwSwapBuffers(window); // Poll for events such as keyboard input or mouse movement. glfwPollEvents(); } // Clean up resources when finished. glfwDestroyWindow(window); glfwTerminate(); return 0; } ``` 这段代码展示了如何利用 GLFW 创建一个名为 “LearnOpenGL” 的固定大小为 800×600 像素的新窗口,并且设置了该窗口作为当前活动窗口以便后续对其进行绘制工作。同时,也包含了错误处理逻辑以及循环等待直到用户关闭应用程序为止[^4].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑不溜秋的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值