-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
138 lines (105 loc) · 4.22 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
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
#include "pch.hpp"
#include "my_gui.hpp"
#include "utils.hpp"
#include "gltf_loader.hpp"
#include "mesh.hpp"
#include "shader.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb_image_resize.h"
#define WIDTH 800
#define GUI_WIDTH 200
#define SCENE_WIDTH (WIDTH - GUI_WIDTH)
#define HEIGHT 600
GLFWwindow* window;
void imgui_init(GLFWwindow* window) {
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
ImGui_ImplGlfw_InitForOpenGL(window,true);
ImGui_ImplOpenGL3_Init("#version 330");
}
void on_window_resize(GLFWwindow* window, int w, int h) {
glViewport(0,0,w,h);
}
int glfw_init() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE,GLFW_FALSE);
window = glfwCreateWindow(WIDTH,HEIGHT,"glTF Viewer",NULL,NULL);
if(window == NULL) {
ERROR("failed to create window");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
ERROR("failed tp load opengl function");
glfwTerminate();
return -1;
}
glViewport(0,0,SCENE_WIDTH,HEIGHT);
glfwSetFramebufferSizeCallback(window,on_window_resize);
return 0;
}
int main() {
if(glfw_init() == -1) { return -1; }
imgui_init(window);
GltfLoader gltf_loader("assets/helmet/DamagedHelmet.gltf");
Mesh mesh(gltf_loader);
glm::mat4 proj = glm::perspective(glm::radians(45.f),(float)SCENE_WIDTH/HEIGHT,0.1f,100.f);
mesh.shader.enable();
mesh.shader.set_mat4x4("proj",glm::value_ptr(proj));
glEnable(GL_DEPTH_TEST);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
if(glfwGetKey(window,GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window,true);
}
mesh.render();
{
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::SetWindowSize(ImVec2(GUI_WIDTH,HEIGHT));
ImGui::SetWindowPos(ImVec2(SCENE_WIDTH,0));
ImGui::Text("Transalte:");
bool translate_updated = MyGui::InputFloat3WithScroll("##translation",&mesh.translate[0],0.1);
if(translate_updated) {
mesh.translation_mat = glm::translate(glm::mat4x4(1.),mesh.translate);
}
ImGui::Text("Rotation:");
bool rotation_updated = MyGui::InputFloat3WithScroll("##rotation",&mesh.rotation[0],5);
if(rotation_updated) {
mesh.rotation_mat = glm::rotate(glm::mat4x4(1.),glm::radians(mesh.rotation.x),glm::vec3(1.,0.,0.));
mesh.rotation_mat = glm::rotate(mesh.rotation_mat,glm::radians(mesh.rotation.y),glm::vec3(0.,1.,0.));
mesh.rotation_mat = glm::rotate(mesh.rotation_mat,glm::radians(mesh.rotation.z),glm::vec3(0.,0.,1.));
}
ImGui::Text("Scale:");
bool scale_updated = MyGui::InputFloat3WithScroll("##scaling",&mesh.scale[0]);
if(scale_updated) {
mesh.scale_mat = glm::scale(glm::mat4x4(1.),mesh.scale);
}
if(translate_updated || rotation_updated || scale_updated) {
auto model = glm::mat4(1.);
model = (mesh.translation_mat * mesh.rotation_mat * mesh.scale_mat) * gltf_loader.main_transformation;
mesh.shader.enable();
mesh.shader.set_mat4x4("model",glm::value_ptr(model));
}
ImGui::EndFrame();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
glfwSwapBuffers(window);
glClearColor(0.f,0.f,0.f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
mesh.shader.free();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}