首页 OpenGL学习(一)
文章
取消

OpenGL学习(一)

一、前言

OpenGL能够实现跨平台的图形显示API调用。其能够使用C++进行编写,效率高,扩展强,跨平台。无论是进行图形显示、引擎开发、驱动开发、显卡开发等等工作都需要进行学习和熟练使用。

二、下载

1、GLFW下载

一个开源的多平台库。官方网站为:https://www.glfw.org/

include和lib文件夹是真正要用的。

2、GLEW下载

一个开源的OpenGL扩展库。官方网站为:https://glew.sourceforge.net/

同样的,下载后include和lib文件夹是真正要用的。

doc文件夹是网页形式的离线开发文档。

3、VS下载安装

目前所使用的版本是Visual Studio2017,更新的也可以。

三、环境配置

1、平台

这里我使用的debug平台使用的版本是32(x86)。当然你使用x64也可以的。

2、配置

1、创建工程后,需要在C/C++的General里的include中,包含GLFW和GLEW的include文件夹。

2、在C/C++的Preprocessor第一行添加GLEW_STATIC关键字。

3、在Linker的General里的lib中的Additional Library Directories中,添加GLFW和GLEW的lib文件夹。

4、在Linker的Input里的Additional Dependencies中,添加glfw3.lib、opengl32.lib、glew32s.lib。

四、第一个脚本编写和调试

这个脚本作为入门脚本,功能是测试GLEW和GLFW是否配置成功,以及在屏幕上显示一个二维的等边三角。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
int main(void)
{
	GLFWwindow* window;

	/* Initialize the library */
	if (!glfwInit())
		return -1;

	/* Create a windowed mode window and its OpenGL context */
	window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
	if (!window)
	{
		glfwTerminate();
		return -1;
	}

	/* Make the window's context current */
	glfwMakeContextCurrent(window);

    /* GLEW init and test */
	if (glewInit() != GLEW_OK)
		std::cout << "ERROR!" << std::endl;
	std::cout << glGetString(GL_VERSION) << std::endl;
	unsigned int a;
	glGenBuffers(1, &a);

	/* Loop until the user closes the window */
	while (!glfwWindowShouldClose(window))
	{
		/* Render here */
		glClear(GL_COLOR_BUFFER_BIT);

		glBegin(GL_TRIANGLES);
		glVertex2f(-0.5f, -0.5f);
		glVertex2f( 0.0f,  0.5f);
		glVertex2f( 0.5f, -0.5f);
		glEnd();

		/* Swap front and back buffers */
		glfwSwapBuffers(window);

		/* Poll for and process events */
		glfwPollEvents();
	}

	glfwTerminate();
	return 0;
}

debug时,可以在glew.h或glfw3.h文件中进行寻找错误原因。

五、参考

https://glew.sourceforge.net/

https://www.glfw.org/

https://www.bilibili.com/video/BV1MJ411u7Bc/

https://www.bilibili.com/video/BV11W411N7b9/

本文由作者按照 CC BY 4.0 进行授权