-
Notifications
You must be signed in to change notification settings - Fork 0
/
38_skinning.cpp
214 lines (171 loc) · 4.42 KB
/
38_skinning.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include "utils/utils.h"
#include "utils/skybox.h"
#include "utils/camera.h"
#include "utils/pipeline.h"
#include "utils/lights_common.h"
#include "utils/skinning_program.h"
#include "utils/skinned_only_mesh.h"
#include "utils/skeletal_animation.h"
#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 768
const char* WINDOW_NAME = "Tutorial 38";
PersProjInfo projInfo;
Camera* camera = NULL;
DirectionLight directionLight;
SkinningProgram* program = NULL;
SkinnedOnlyMesh* mesh = NULL;
SkeletalAnimation* anims[4];
std::string AnimNames[] = {
"content/assault_combat_idle.FBX",
"content/assault_combat_run.FBX",
"content/assault_combat_shoot_burst.FBX",
"content/assault_combat_shoot.FBX"
};
static float scale = 0.0f;
static long long m_startTime = 0;
static uint animIndex = 0;
float GetRunningTime()
{
float RunningTime = (float)((double)GetCurrentTimeMillis() - (double)m_startTime) / 1000.0f;
return RunningTime;
}
void Render ()
{
camera->OnRender();
scale += 0.1f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float RunningTime = GetRunningTime();
std::vector<Matrix4f> Transforms;
anims[animIndex]->BoneTransform(RunningTime, mesh, Transforms);
// printf("set Bones: %f %lu\n", RunningTime, Transforms.size());
for (uint i = 0 ; i < Transforms.size() ; i++) {
program->SetBoneTransform(i, Transforms[i]);
}
Pipeline p;
p.Pos(0.0f, 0.0f, 6.0f);
p.Scale(0.1f, 0.1f, 0.1f);
p.Rotate(0.0f, 90.0f, 0.0f);
p.SetCamera(*camera);
p.SetPerspectiveProj(projInfo);
program->SetWVP(p.GetTrans());
program->SetWorld(p.GetWorldTrans());
mesh->Render();
}
void createProgram ()
{
program = new SkinningProgram();
if (!program->Init()) {
exit(1);
}
program->AddShader(GL_VERTEX_SHADER, "shaders/skinning_shader.vs");
program->AddShader(GL_FRAGMENT_SHADER, "shaders/skinning_shader.fs");
program->Compile();
program->Link();
program->SetDirectionLight(directionLight);
program->Enable();
}
SDL_Window *window;
void Init()
{
if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, "Unable to init SDL, error: '%s'\n", SDL_GetError());
exit(1);
}
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
window = SDL_CreateWindow(WINDOW_NAME, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
if (window == NULL)
{
fprintf(stderr, "Unable to create SDL Context");
exit(1);
}
glewExperimental = GL_TRUE;
GLenum res = glewInit();
if (res != GLEW_OK)
{
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
exit(1);
}
}
void InitLights ()
{
directionLight.AmbientIntensity = 0.55f;
directionLight.Color = COLOR_YELLOW;
directionLight.DiffuseIntensity = 0.9f;
directionLight.Direction = Vector3f(1.0f, 0.0f, 0.0f);
}
int main (int argc, char *argv[])
{
Init();
InitLights();
Vector3f Pos(0.0f, 3.0f, -1.0f);
Vector3f Target(0.0f, 0.0f, 1.0f);
Vector3f Up(0.0, 1.0f, 0.0f);
camera = new Camera(WINDOW_WIDTH, WINDOW_HEIGHT, Pos, Target, Up);
projInfo.Width = WINDOW_WIDTH;
projInfo.Height = WINDOW_HEIGHT;
createProgram();
mesh = new SkinnedOnlyMesh();
if (!mesh->LoadMesh("content/ToonSoldier.FBX")) {
return 1;
}
for (uint i = 0; i < 4; i++) {
anims[i] = new SkeletalAnimation();
anims[i]->Load(AnimNames[i]);
}
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
m_startTime = GetCurrentTimeMillis();
bool running = true;
while(running)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
switch(event.type)
{
case SDL_QUIT:
running = false;
break;
case SDL_MOUSEMOTION:
camera->OnMouse(event.motion.x, event.motion.y);
break;
case SDL_KEYDOWN:
{
camera->OnKeyboard(event.key.keysym.sym);
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
running = false;
break;
case SDLK_z:
animIndex++;
if (animIndex >= 4)
animIndex = 0;
break;
}
}
break;
}
}
Render();
SDL_GL_SwapWindow(window);
}
SDL_Quit();
return 0;
}