[Modern OpenGL系列(二)]创建OpenGL窗口

在博主的上一篇文章中已经介绍了OpenGL开发环境的搭建,本篇博文将验证开发环境是否搭建成功。同时,也是迈出OpenGL开发的第一步。我们使用上一篇文章中新建的项目,创建一个OpenGL窗口。

  1. 添加头文件display.h

    
    #include <SDL2\SDL.h>
    
    
    #include <string>
    
    
    class Display
    {
    public:
        Display(int width, int height, const std::string& title);
    
        void Clear(float r, float g, float b, float a);
        void Update();
        bool IsClosed();
    
        virtual ~Display();
    
    protected:
    private:
        Display(const Display& other) {}
        Display& operator=(const Display& other) {}
    
        SDL_Window* m_window;
        SDL_GLContext m_glContext;
        bool m_isClosed;
    };
  2. 添加显示类display.cpp.

    
    #include "display.h"
    
    
    #include <GL\glew.h>
    
    
    #include <iostream>
    
    
    Display::Display(int width, int height, const std::string& title)
    {
        SDL_Init(SDL_INIT_EVERYTHING);
    
        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);// 设置颜色的四个分量所占用内存为8bit
        SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    
        m_window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
        m_glContext = SDL_GL_CreateContext(m_window);
    
        GLenum status = glewInit();
    
        if (status != GLEW_OK)
        {
            std::cerr << "Glew failed to initialize" << std::endl;
        }
    
        m_isClosed = false;
    }
    
    Display::~Display()
    {
        SDL_GL_DeleteContext(m_glContext);
        SDL_DestroyWindow(m_window);
        SDL_Quit();
    }
    
    void Display::Clear(float r, float g, float b, float a)
    {
        glClearColor(r, g, b, a);// 设置窗口背景色
        glClear(GL_COLOR_BUFFER_BIT);// 清除颜色缓冲
    }
    
    bool Display::IsClosed()
    {
        return m_isClosed;
    }
    
    void Display::Update()
    {
        SDL_GL_SwapWindow(m_window);
    
        SDL_Event e;
    
        while (SDL_PollEvent(&e))
        {
            if (e.type == SDL_QUIT)
            {
                m_isClosed = true;
            }
        }
    }
  3. main函数中调用显示方法。具体代码如下:

    
    #include <GL\glew.h>
    
    
    #include "display.h"
    
    
    int main(int argc, char** argv)
    {
        Display display(400, 300, "hello world!");
    
        while (!display.IsClosed())
        {
            display.Clear(0.0f, 1.0f, 0.0f, 1.0f);
    
            display.Update();// 刷新
        }
    
        return 0;
    }
  4. 运行项目。可以使用快捷键F5来运行项目。运行后会显示一个绿色的OpenGL窗口。如图:这里写图片描述
  • 注意:如果main方法没有参数,则会报错:SDL2main.lib(SDL_windows_main.obj) : error LNK2019: 无法解析的外部符号 _SDL_main,该符号在函数 _main_utf8 中被引用,所以main方法的签名必须是int main(int argc, char** argv)
  • 本文中的项目使用的是VS2015,建议是用VS2015打开。点此下载源码
  • 本文整理自YouTube视频教程#2 Intro to Modern OpenGL Tutorial: OpenGL Windows
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值