OpenGL学习笔记第二课--Hello Window(1)

1.导入头文件

#define GLEW_STATIC
#include <Gl/glew.h>
#include <Glfw/glfw3.h>

2.

int main()
{
    glfwInit();  // 初始化函数库


    //给三个提示 前两个给出版本号3.3,第一行给出主版本号3,第二行给出次版本号3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    //有很多编程方法可以使用,以CORE_PROFILE流水线最为精简
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);


    //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);  //此行为mac系统专用
  
    return 0;
}

3.创建一个窗口

Next we're required to create a window object. This window object holds all the windowing data and is required by most of GLFW's other functions.

下面我们要创建一个窗口对象

	//Open GLFW Window
	GLFWwindow* window = glfwCreateWindow(800,600,"My OpenGL Game",NULL,NULL);
	if (window == NULL)
	{
		std::cout << "Open window failed." << std::endl;
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window); // 假如window不是空指针,使用这个 window为当前上下文


	// Init GLEW
	glewExperimental = true; // 固定写法,因为有些特性是试验阶段
	if (glewInit() != GLEW_OK)  // GLEW的操作如果成功的话,返回GLEW_OK关键字
	{
		std::cout << "Init GLEW failed" << std::endl;
		glfwTerminate();
		return -1;
	}

4.Viewport

渲染之前的最后一步

glViewport(0, 0, 800, 600);

The first two parameters of glViewport set the location of the lower left corner of the window. The third and fourth parameter set the width and height of the rendering window in pixels, which we set equal to GLFW's window size

前两个参数设置window左下角的位置,后两个参数设置渲染窗口的宽高像素值,这里设置为跟glfw窗口大小一样

 自适应窗口大小

参见完整版代码

5.Ready your engines

We don't want the application to draw a single image and then immediately quit and close the window.写个死循环,不然窗口一闪而过

 

We want the application to keep drawing images and handling user input until the program has been explicitly told to stop我们想要应用一直画图和处理输入,直到程序被很明确的告诉停止

while(!glfwWindowShouldClose(window))
{
    glfwSwapBuffers(window);
    glfwPollEvents(); //获取用户的按键等行为   
}

双重缓冲区避免屏幕撕裂

闪烁跟撕裂是两个现象,都属于artifacts

Double buffer
When an application draws in a single buffer the resulting image may display flickering issues.假如应用只在一个缓冲区中绘制的话,可能会造成画面闪烁

This is because the resulting output image is not drawn in an instant, but drawn pixel by pixel and usually from left to right and top to bottom.这是因为输出图像不是一瞬间绘制完成的,通常是从左到右从上到下一个像素一个像素的绘制 

Because this image is not displayed at an instant to the user while still being rendered to, the result may contain artifacts.因为图象不是立即像用户展示的,结果中可能会有残留的缓存

To circumvent these issues, windowing applications apply a double buffer for rendering.为了克服这个困难,窗口应用两个缓冲区做渲染

The front buffer contains the final output image that is shown at the screen, while all the rendering commands draw to the back buffer.前缓冲包含最终要显示在屏幕上的图像,相对的,所有的渲染工作在后缓冲区进行

As soon as all the rendering commands are finished we swap the back buffer to the front buffer so the image can be displayed without still being rendered to, removing all the aforementioned artifacts当所有的渲染指令都完成的时候,我们交换后缓冲到前缓冲,所以图像可以立即展示,消除了屏幕撕裂和闪烁

6.最后一步 One last thing

As soon as we exit the render loop we would like to properly clean/delete all of GLFW's resources that were allocated. 当我们退出渲染循环的时候,我们要把我们分配的资源清理掉

We can do this via the glfwTerminate function that we call at the end of the main function.我们可以通过在main函数的最后调用glfwTerminate这个函数完成清理工作。

glfwTerminate();
return 0;

附:完整版代码及运行结果

#define GLEW_STATIC
#include <Gl/glew.h>
#include <Glfw/glfw3.h>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
int main()
{
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	//Open GLFW Window
	GLFWwindow* window = glfwCreateWindow(800,600,"My OpenGL Game",NULL,NULL);
	if (window == NULL)
	{
		std::cout << "Open window failed." << std::endl;
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window); // 假如window不是空指针,使用这个 window为当前上下文


	// Init GLEW
	glewExperimental = true; // 固定写法,因为有些特性是试验阶段
	if (glewInit() != GLEW_OK)  // GLEW的操作如果成功的话,返回GLEW_OK关键字
	{
		std::cout << "Init GLEW failed" << std::endl;
		glfwTerminate();
		return -1;
	}

	glViewport(0, 0, 800, 600);
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);


	//
	while (!glfwWindowShouldClose(window))
	{
		glfwSwapBuffers(window);
		glfwPollEvents();
	}
	glfwTerminate();

	return 0;


}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
	glViewport(0, 0, width, height);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值