-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathApplication.cpp
111 lines (92 loc) · 3.6 KB
/
Application.cpp
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
// has to be included first
#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;
}
// opengl rendering context is created here
// make the window's context current
glfwMakeContextCurrent(window);
// initiating GLEW
if (glewInit() != GLEW_OK)
{
std::cout << "GLEW could not be initialized!" << std::endl;
}
std::cout << glGetString(GL_VERSION) << std::endl;
//////////////////////////
// give OpenGL the data //
//////////////////////////
// vertices of a triangle
float positions[6] = {
-0.5f, -0.5f,
0.0f, 0.5f,
0.5f, -0.5f
};
// create buffer and its id
unsigned int buffer;
glGenBuffers(1, &buffer);
// selecting the buffer with its id
glBindBuffer(GL_ARRAY_BUFFER, buffer);
// <- size of the buffer in bytes
// <- source
// <- STREAM -> modified once, used few times; STATIC -> modified once, used often, DYNAMIC -> modified repeatedly, used often
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);
///////////////////////////////////
// define the layout of the data //
///////////////////////////////////
glEnableVertexAttribArray(0);
// each vertex can have multiple attributes, one of which can be the position
// define the layout of the data
// defining 1 attribute, the position
// <- index: index of this attribute in the each vertex, first attribute, second, third...
// <- size: how many values are representing this attribute; 1, 2, 3, 4 are possible
// <- type of the values in this attribute
// <- normalized: when true converts int (0-255) to float (0.0f - 1.0f)
// <- stride: amount of bytes between each vertex start
// <- pointer: inside the space of a vertex, the start of this attribute, first attribute -> 0, second attribute -> >0
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, (const void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// games loop
// loop until the user closes the window
while (!glfwWindowShouldClose(window))
{
// Render here
glClear(GL_COLOR_BUFFER_BIT);
// using the currently bound buffer
// <- point in the array at which to start
// <- number of vertices
glDrawArrays(GL_TRIANGLES, 0, 3);
// swap front and back buffers
glfwSwapBuffers(window);
// poll for and process events
glfwPollEvents();
}
glfwTerminate();
return 0;
}
/*
* rendering pipeline -> data to rendered image on screen
*
* vertex shader:
* gets called first in pipeline (when draw command is issued)
* called for each vertex
* -> tell OpenGL where the vertex gos (on the screen)
* takes in vertex attributes
* fragment shader (pixel shader):
* gets called second in pipeline (many things in between)
* gets called for each pixel
* decides color for each pixel
*
* there are others (like tessellation, geometry shaders, compute shaders)
*/