计算机图形学-实验

实验

实验体系

GLFW

glfw

GLAD

glad

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//渲染
while (!glfwWindowShouldClose(window))

{

//glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClearColor(0.0f, 0.34f, 0.57f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

//交换颜色缓存,开始绘制
glfwSwapBuffers(window);
//检查触发事件
glfwPollEvents();

}

双缓冲的运用

VAO 顶点数组对象

VAO

VBO 顶点缓冲对象

VBO

实验一:绘制三角形

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>

using namespace std;

const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

int main()
{

//cout << SCR_HEIGHT << endl;

/*

初始化OpenGL
1)初始化GLFW
2)创建窗口
3)初始化GLAD
4)创建视口

*/

// 1)初始化GLFW
glfwInit();
//设置主版本号、次版本号为3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
//使用核心模式,即只能使用openGL功能的一个子集(没有向后兼容特性)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//Mac OS需要加上这句 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
//不可改变窗口大小
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);


// 2)设置GLFW窗口对象
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);

if (window == NULL)

{
//若未创建成功,终止程序
std::cout << "Failed to create GLFW window" << std::endl;

glfwTerminate();

return -1;

}

//设置GLFW上下文(状态)为当前线程的上下文
glfwMakeContextCurrent(window);

//GLAD管理OpenGL函数指针
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{

std::cout << "Failed to initialize GLAD" << std::endl;
return -1;

}

//指定当前视口尺寸(左下角坐标,视口宽高)
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);


/*
数据处理
*/
//1)三角形三个顶点输入(标准化,0-1之间)
const float triangle[] = {
-0.5f,-0.5f,0.0f, //左下
0.5f,-0.5f,0.0f, //右下
0.0f,0.5f,0.0f //正上
};

//2)VAO(顶点数组对象) VBO(顶点缓冲对象)
GLuint vertex_array_object;
glGenVertexArrays(1, &vertex_array_object);
glBindVertexArray(vertex_array_object);

GLuint vertex_buffer_object;
glGenBuffers(1, &vertex_buffer_object);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_object);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle), triangle, GL_STATIC_DRAW);

//设置顶点属性指针
//顶点着色器的位置,顶点属性为3分量向量,是否标准化,连续顶点属性之间的间隔,数据偏移量
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);

// 3) 生成并编译着色器
const char *vertex_shader_source =
"#version 330 core\n"
"layout(location = 0 ) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos, 1.0);\n"
"}\n\0";

const char *fragment_shader_source =
"#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0" ;

//状态标识量
int success;
char info_log[512];

//顶点着色器
int vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL);
glCompileShader(vertex_shader);
//检查编译是否成功
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success);
if (!success) {

glGetShaderInfoLog(vertex_shader, 512, NULL, info_log);
cout<<"error 1:"<< info_log << endl;
}
//片段着色器
int fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL);
glCompileShader(fragment_shader);
//检查编译是否成功
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success);
if (!success) {

glGetShaderInfoLog(fragment_shader, 512, NULL, info_log);
cout << "error 2:" << info_log << endl;
}
//链接顶点和片段着色器至一个着色器程序
int shader_program = glCreateProgram();
glAttachShader(shader_program, vertex_shader);
glAttachShader(shader_program, fragment_shader);
glLinkProgram(shader_program);
//检查链接是否成功
glGetProgramiv(shader_program, GL_LINK_STATUS, &success);
if (!success) {

glGetProgramInfoLog(fragment_shader, 512, NULL, info_log);
cout << "error 3:" << info_log << endl;
}


//渲染
while (!glfwWindowShouldClose(window))

{
//渲染指令
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
//清空颜色缓冲后,填充蓝色
//glClearColor(0.0f, 0.34f, 0.57f, 1.0f);
//清除颜色缓冲
glClear(GL_COLOR_BUFFER_BIT);

//使用着色器程序
glUseProgram(shader_program);
//绘制三角形
glBindVertexArray(vertex_array_object);
glDrawArrays(GL_TRIANGLES, 0, 3);
//解除绑定
glBindVertexArray(0);

//交换颜色缓存,开始绘制
glfwSwapBuffers(window);
//检查触发事件
glfwPollEvents();

}

//删除VAO VBO
glDeleteVertexArrays(1,&vertex_array_object);
glDeleteBuffers(1, &vertex_buffer_object);

//释放资源
glfwTerminate();

return 0;

}
---------------- 本文结束 ----------------

本文标题:计算机图形学-实验

文章作者:Pabebe

发布时间:2019年10月14日 - 17:57:16

最后更新:2020年06月16日 - 18:24:34

原始链接:https://pabebezz.github.io/article/9200fd3/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%