forked from christopher-besch/opengl_reference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.cpp
87 lines (72 loc) · 2.36 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
// 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;
//////////////////////////
// new //
// 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
// <- 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);
// 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;
}
/*
* vertex buffer:
* buffer in V-RAM
* can be used in the draw call (by the CPU)
* -> executed by the shader (running on the GPU)
*
* OpenGL is a state machine -> can't be treated as an object -> functions don't get the necessary data parsed but OpenGL already known everything it needs
*
* docs.gl is a good reference site for OpenGL
*/