OpenGL学习(2)

创建一个窗口

由上一篇我们已经创建出一个窗口了,那么其程序是如何的呢?这是我们要讨论的下一个问题。

利用GLFW库创建一个窗口的具体步骤如下:

1.利用glfwInit() 函数初始化GLFE,并判断是否初始化成功;


<span style="white-space:pre">	</span>if (!glfwInit())
	{
		fprintf(stderr,"Failed to the initialized GLFW.\n");
		return;
	}

2.利用函数glfwCreateWindow() 创建窗口,并判断是否创建成功;


<span style="white-space:pre">	</span>m_Window=glfwCreateWindow(m_Width,m_Height,m_Title,NULL,NULL);

	if (m_Width==NULL)
	{
		fprintf(stderr,"Failed to the create window.\n");
		glfwTerminate();
		return ;
	}


3.利用glfwMakeContextCurrent() 函数,创建窗口背景;


<span style="white-space:pre">	</span>glfwMakeContextCurrent(m_Window);

4.利用glewInit() 函数初始化GLEW,并判断是否初始化成功;


<span style="white-space:pre">	</span>glewExperimental=GL_TRUE;

	if (glewInit()!=GLEW_OK)
	{
		fprintf(stderr,"Failed to the initialized the GLEW.\n");
		glfwTerminate();
		return ;
	}

5.进行主循环渲染操作,包括使用glClearColor() 函数设置窗口背景、glClear() 函数清除帧缓冲区、自定义函数画图、glfwSwapBuffers() 函数交换缓冲区和glfwPollEvents() 函数设定事件轮询方式;


<span style="white-space:pre">	</span>do 
	{
		glClearColor(1.0f,1.0f,0.0f,0.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		DrawSomething();

		glfwSwapBuffers(m_Window);
		glfwPollEvents();

	} while (glfwWindowShouldClose(m_Window)==0);

P.S:上述5个步骤的操作是必须的,且顺序不可改变。为使按下Esc键可以实现程序的退出,可以使用glfwSetInputMode() 函数来设置为按键输入模式,使用glfwGetKey() 函数来判断是否Esc键已按下。(完整程序如下)

<span style="white-space:pre">	</span>if (!glfwInit())
	{
		fprintf(stderr,"Failed to the initialized GLFW.\n");
		return;
	}

	m_Window=glfwCreateWindow(m_Width,m_Height,m_Title,NULL,NULL);

	if (m_Width==NULL)
	{
		fprintf(stderr,"Failed to the create window.\n");
		glfwTerminate();
		return ;
	}

	glfwMakeContextCurrent(m_Window);

	glewExperimental=GL_TRUE;

	if (glewInit()!=GLEW_OK)
	{
		fprintf(stderr,"Failed to the initialized the GLEW.\n");
		glfwTerminate();
		return ;
	}

	glfwSetInputMode(m_Window,GLFW_STICKY_KEYS,GL_TRUE);

	do 
	{
		glClearColor(1.0f,1.0f,0.0f,0.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		DrawSomething();

		glfwSwapBuffers(m_Window);
		glfwPollEvents();

	} while (glfwGetKey(m_Window,GLFW_KEY_ESCAPE)!=GLFW_PRESS
		&& glfwWindowShouldClose(m_Window)==0);

	glfwTerminate();

	return ;






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值