zoukankan      html  css  js  c++  java
  • [Modern OpenGL系列(二)]创建OpenGL窗口

    本文已同步发表在CSDN:http://blog.csdn.net/wenxin2011/article/details/51295663


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

    1. 添加头文件display.h
      #include <SDL2SDL.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 <GLglew.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 <GLglew.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

    声明:本文欢迎转载和分享,但是请尊重作者的劳动成果,转载分享时请注明出处:http://www.cnblogs.com/davidsheh/p/5452336.html 。同时,码字实在不易,如果你觉得笔者分享的笔记对你有点用处,请顺手点击下方的推荐,谢谢!

  • 相关阅读:
    .csproj文件
    堆栈
    数据库操作(一)
    Math数学函数
    SSM框架下各个层的解释说明
    MyBatis DAO层传递参数到mapping.xml
    Spring MVC3在controller和视图之间传递参数的方法
    注册/登陆界面验证码的作用及代码实现
    input中name和id的区别
    <mvc:default-servlet-handler/>的作用
  • 原文地址:https://www.cnblogs.com/davidsheh/p/5452336.html
Copyright © 2011-2022 走看看