-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·112 lines (94 loc) · 2.84 KB
/
main.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
112
#include <iostream>
// Include standard headers
#include <glad/glad.h>
// Include GLFW
#include <GLFW/glfw3.h>
#include <fstream>
#include <memory>
#include <streambuf>
#include <drawer/3DObjectDrawer.h>
#include <drawer/cameraDrawer.h>
#include <file_loader/stl.h>
#include <file_loader/obj.h>
#include <models/3DObject.h>
#include <models/camera.h>
#include <ui/inputDevice.h>
#include <shaders/shader.h>
#include <shaders/program.h>
struct destroyWindow
{
void operator()(GLFWwindow *ptr) { glfwDestroyWindow(ptr); }
};
using unique_glfwwindow = std::unique_ptr<GLFWwindow, destroyWindow>;
unique_glfwwindow initWindow()
{
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
return unique_glfwwindow(nullptr);
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,
GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE,
GLFW_OPENGL_CORE_PROFILE); // We don't want the old OpenGL
// Open a window and create its OpenGL context
unique_glfwwindow window(
glfwCreateWindow(1024, 768, "Tutorial 01", NULL, NULL));
if (window == nullptr)
{
fprintf(stderr,
"Failed to open GLFW window. If you have an Intel GPU, they are "
"not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
glfwTerminate();
return {};
}
glfwMakeContextCurrent(window.get()); // Initialize GLEW
gladLoadGL();
glfwSetInputMode(window.get(), GLFW_STICKY_KEYS, GL_TRUE);
return window;
};
int main()
{
auto window = initWindow();
if (!window)
return 0;
auto program = Program::create(
Shader::createVertexShader("./shaders/vertex.shader"),
Shader::createFragmentShader("./shaders/fragment.shader"));
if (!program.verify())
{
return 0;
}
std::vector<C3DObject> vec;
// vec.push_back(*Stl::load("./files/cube_ascii.stl"));
vec.push_back(*Obj::load("./files/rabbit.obj"));
// vec.push_back(*Stl::load("./files/triangle.stl"));
// vec.push_back(*Stl::load("./files/20mm_cube.stl"));
Camera camera;
camera.setDrawer(CameraDrawer(program));
for (auto &v : vec)
{
v.setDrawer(C3DObjectDrawer(program));
}
InputDevice inputDevice(camera, *window.get());
do
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program.id());
camera.draw();
for (auto &v : vec)
{
v.draw();
}
glfwSwapBuffers(window.get());
glfwPollEvents();
inputDevice.processInput();
} // Check if the ESC key was pressed or the window was closed
while (glfwGetKey(window.get(), GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window.get()) == 0);
return 0;
}