/
2_hello_triangle.h
70 lines (62 loc) · 1.78 KB
/
2_hello_triangle.h
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
#pragma once
#include "../common.h"
// draw a triangle
static int tutorial2() {
// config opengl
GLFWwindow* window = nullptr;
if (commonInit(&window) < 0) {
return -1;
}
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n{"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n{"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\0";
// create shader program
unsigned int shaderProgram = createShaderProgram(vertexShaderSource, fragmentShaderSource);
// vertex data
float vertices[] = {
-1.0f, -0.5f, 0.0f, // left
0.0f, -0.5f, 0.0f, // right
-0.5f, 0.5f, 0.0f // top
};
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// bind the vertex array object
glBindVertexArray(VAO);
// bind the vertex buffer object
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// fill data uage:GL_STATIC_DRAW, GL_DYNAMIC_DRAW, GL_STREAM_DRAW
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// tell opengl how it should interpret the vertex data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// unbind VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
// unbind VAO
glBindVertexArray(0);
// render loop
while (!glfwWindowShouldClose(window)) {
// handle input event
processInput(window);
// render
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// draw triangle
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
return 0;
}