-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAssimpViewer.cpp
162 lines (130 loc) · 7.45 KB
/
AssimpViewer.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "stdafx.h"
#include "Import.h"
#include "AnimatedModel.h"
#include "Shader.h"
#include "Renderer.h"
#include <string>
#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <glm\glm.hpp>
#include <glm\gtc\matrix_transform.hpp>
glm::vec3 camera_vel;
float camera_rot_vel = 0;
int window_width = 640, window_height = 480;
void FramebufferResizeCallback(GLFWwindow* window, int width, int height)
{
window_width = width;
window_height = height;
glViewport(0, 0, width, height);
}
void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_UP) {
if (action == GLFW_PRESS) {
camera_vel.y = -1;
}
if (action == GLFW_RELEASE) {
camera_vel.y = 0;
}
}
if (key == GLFW_KEY_RIGHT) {
if (action == GLFW_PRESS) {
camera_vel.x = -1;
}
if (action == GLFW_RELEASE) {
camera_vel.x = 0;
}
}
if (key == GLFW_KEY_DOWN) {
if (action == GLFW_PRESS) {
camera_vel.y = 1;
}
if (action == GLFW_RELEASE) {
camera_vel.y = 0;
}
}
if (key == GLFW_KEY_LEFT) {
if (action == GLFW_PRESS) {
camera_vel.x = 1;
}
if (action == GLFW_RELEASE) {
camera_vel.x = 0;
}
}
if (key == GLFW_KEY_PERIOD) {
if (action == GLFW_PRESS) {
camera_rot_vel = 1;
}
if (action == GLFW_RELEASE) {
camera_rot_vel = 0;
}
}
if (key == GLFW_KEY_COMMA) {
if (action == GLFW_PRESS) {
camera_rot_vel = -1;
}
if (action == GLFW_RELEASE) {
camera_rot_vel = 0;
}
}
}
void ScrollCallback(GLFWwindow* window, double xoffset, double yoffset)
{
camera_vel.z = static_cast<float>(yoffset * 100);
}
GLFWwindow* Initialize() {
glfwInit();
GLFWwindow* window = glfwCreateWindow(window_width, window_height, "AssimpViewer", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, FramebufferResizeCallback);
glfwSetKeyCallback(window, KeyCallback);
glfwSetScrollCallback(window, ScrollCallback);
GLenum err = glewInit();
if (err != GLEW_OK) {
std::cout << glewGetErrorString(err) << std::endl;
}
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
return window;
}
int main(int argc, const char * argv[])
{
if (argc == 1) {
std::cout << "Usage: AssimpViewer <filename> " << std::endl;
return -1;
}
GLFWwindow* window = Initialize();
std::string filename = argv[1];
std::cout << "Loading " << filename << std::endl;
Shader* shader = new Shader();
shader->AddComponent("shaders/fs.glsl", GL_FRAGMENT_SHADER);
shader->AddComponent("shaders/skeletal_vs.glsl", GL_VERTEX_SHADER);
shader->Link();
AnimatedModel* m = Import::LoadAnimatedFile(filename);
m->SetAnimation("Take 001");
m->shader = shader;
glm::vec3 camera_position = glm::vec3(0.0f, 0.0f, 0.0f);
float camera_rotation = 0.0f;
double last_frame_time = glfwGetTime();
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera_position += (camera_vel * 0.005f);
camera_rotation += (camera_rot_vel * 0.005f);
glm::mat4 view_matrix = glm::translate(glm::mat4(1), camera_position);
view_matrix = glm::rotate(view_matrix, camera_rotation, glm::vec3(0, 1, 0));
glm::mat4 projection_matrix = glm::perspective(glm::radians(90.0f), (float)window_width / window_height, 0.1f, 100.0f);
shader->SetMat4("model_matrix", glm::mat4(1));
shader->SetMat4("view_matrix", view_matrix);
shader->SetMat4("projection_matrix", projection_matrix);
double current_time = glfwGetTime();
m->Update(current_time - last_frame_time);
Renderer::Render(*m);
camera_vel.z *= 0.5;
last_frame_time = current_time;
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}