forked from raduprv/Eternal-Lands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcal.c
322 lines (246 loc) · 9.46 KB
/
cal.c
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <stdlib.h>
#include "global.h"
#include "cal.h"
void cal_actor_set_anim(int id,struct cal_anim anim)
{
struct CalMixer *mixer;
int i;
if (actors_list[id]->calmodel==NULL) return;
if (actors_list[id]->cur_anim.anim_index==anim.anim_index) return;
mixer=CalModel_GetMixer(actors_list[id]->calmodel);
//Stop previous animation if needed
if (actors_list[id]->IsOnIdle!=1) {
if ((actors_list[id]->cur_anim.anim_index!=-1)&&(actors_list[id]->cur_anim.kind==0)) {
CalMixer_ClearCycle(mixer,actors_list[id]->cur_anim.anim_index,0.05);
}
if ((actors_list[id]->cur_anim.anim_index!=-1)&&(actors_list[id]->cur_anim.kind==1)) {
CalMixer_RemoveAction(mixer,actors_list[id]->cur_anim.anim_index);
}
}
if (actors_list[id]->IsOnIdle==1) {
for (i=0;i<actors_defs[actors_list[id]->actor_type].group_count;++i) {
CalMixer_ClearCycle(mixer,actors_list[id]->cur_idle_anims[i].anim_index,0.05);
}
}
if (anim.kind==0)
CalMixer_BlendCycle(mixer,anim.anim_index,1.0,0.05);
else
CalMixer_ExecuteAction_Stop(mixer,anim.anim_index,0.0,0.0);
actors_list[id]->cur_anim=anim;
actors_list[id]->anim_time=0.0;
CalModel_Update(actors_list[id]->calmodel,0.0001);//Make changes take effect now
if (actors_list[id]->cur_anim.anim_index==-1) actors_list[id]->busy=0;
actors_list[id]->IsOnIdle=0;
}
struct cal_anim cal_load_anim(actor_types *act, char *str)
{
char fname[255]={0};
struct cal_anim res={-1,0,0};
char temp[255]={0};
struct CalCoreAnimation *coreanim;
if(sscanf(str,"%s %d",fname,&res.kind)!=2) return res;
res.anim_index=CalCoreModel_LoadCoreAnimation(act->coremodel,fname);
coreanim=CalCoreModel_GetCoreAnimation(act->coremodel,res.anim_index);
if (coreanim) {
CalCoreAnimation_Scale(coreanim,act->scale);
res.duration=CalCoreAnimation_GetDuration(coreanim);
} else {
snprintf(temp,sizeof(temp),"No Anim: %s\n",fname);
log_error(temp);
}
return res;
}
void cal_render_bones(actor *act)
{
float lines[1024][2][3];
float points[1024][3];
int nrLines;
int nrPoints;
int currLine;
int currPoint;
struct CalSkeleton *skel;
skel=CalModel_GetSkeleton(act->calmodel);
nrLines = CalSkeleton_GetBoneLines(skel,&lines[0][0][0]);
glLineWidth(3.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_LINES);
for(currLine = 0; currLine < nrLines; currLine++) {
glVertex3f(lines[currLine][0][0], lines[currLine][0][1], lines[currLine][0][2]);
glVertex3f(lines[currLine][1][0], lines[currLine][1][1], lines[currLine][1][2]);
}
glEnd();
glLineWidth(1.0f);
// draw the bone points
nrPoints = CalSkeleton_GetBonePoints(skel,&points[0][0]);
glPointSize(4.0f);
glBegin(GL_POINTS);
for(currPoint = 0; currPoint < nrPoints; currPoint++) {
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(points[currPoint][0], points[currPoint][1], points[currPoint][2]);
}
glEnd();
}
__inline__ void render_submesh(int meshId, int submeshCount, struct CalRenderer * pCalRenderer, float meshVertices[30000][3], float meshNormals[30000][3], float meshTextureCoordinates[30000][2], CalIndex meshFaces[50000][3])
{
int submeshId;
int vertexCount=0;
int textureCoordinateCount=0;
int faceCount=0;
for(submeshId = 0; submeshId < submeshCount; submeshId++) {
// select mesh and submesh for further data access
if(CalRenderer_SelectMeshSubmesh(pCalRenderer,meshId, submeshId)) {
// get the transformed vertices of the submesh
vertexCount = CalRenderer_GetVertices(pCalRenderer,&meshVertices[0][0]);
// get the transformed normals of the submesh
CalRenderer_GetNormals(pCalRenderer,&meshNormals[0][0]);
// get the texture coordinates of the submesh
textureCoordinateCount = CalRenderer_GetTextureCoordinates(pCalRenderer,0,&meshTextureCoordinates[0][0]);
// get the faces of the submesh
faceCount = CalRenderer_GetFaces(pCalRenderer, &meshFaces[0][0]);
// set the vertex and normal buffers
glVertexPointer(3, GL_FLOAT, 0, &meshVertices[0][0]);
glNormalPointer(GL_FLOAT, 0, &meshNormals[0][0]);
// draw the submesh
glTexCoordPointer(2, GL_FLOAT, 0, &meshTextureCoordinates[0][0]);
if(sizeof(CalIndex)==2)
glDrawElements(GL_TRIANGLES, faceCount * 3, GL_UNSIGNED_SHORT, &meshFaces[0][0]);
else
glDrawElements(GL_TRIANGLES, faceCount * 3, GL_UNSIGNED_INT, &meshFaces[0][0]);
}
}
}
void cal_render_actor(actor *act)
{
struct CalRenderer *pCalRenderer;
int meshCount,meshId,submeshCount/*,submeshId, vertexCount*/;
//int textureCoordinateCount,faceCount;
float points[1024][3];
static float meshVertices[30000][3];
static float meshNormals[30000][3];
static float meshTextureCoordinates[30000][2];
static CalIndex meshFaces[50000][3];
struct CalSkeleton *skel;
struct CalMesh *_mesh;
struct CalCoreMesh *_coremesh;
struct CalCoreMesh *_weaponmesh;
struct CalCoreMesh *_shieldmesh;
int boneid=-1;
float reverse_scale;
//char str[255];
int glow=-1;
if(act->calmodel==NULL) {
return;//Wtf!?
}
skel=CalModel_GetSkeleton(act->calmodel);
//glDisable(GL_TEXTURE_2D);
glPushMatrix();
//glScalef(1.2,1.2,1.2);
// get the renderer of the model
#ifdef DEBUG
if (render_mesh) {
#endif
pCalRenderer = CalModel_GetRenderer(act->calmodel);
// begin the rendering loop
if(CalRenderer_BeginRendering(pCalRenderer)){
// set global OpenGL states
// we will use vertex arrays, so enable them
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// get the number of meshes
meshCount = CalRenderer_GetMeshCount(pCalRenderer);
// render all meshes of the model
for(meshId = 0; meshId < meshCount; meshId++){
// get the number of submeshes
submeshCount = CalRenderer_GetSubmeshCount(pCalRenderer,meshId);
glPushMatrix();
//Special treatment for weapons and shields only for enhanced models
boneid=-1;
glow=-1;
if (act->is_enhanced_model) {
_mesh=CalModel_GetAttachedMesh(act->calmodel,meshId);//Get current rendered mesh
_coremesh=CalMesh_GetCoreMesh(_mesh);//Get the coremesh
if(actors_defs[act->actor_type].weapon[act->cur_weapon].mesh_index!=-1)_weaponmesh=CalCoreModel_GetCoreMesh(actors_defs[act->actor_type].coremodel,actors_defs[act->actor_type].weapon[act->cur_weapon].mesh_index);
else _weaponmesh=NULL;
if(act->body_parts->shield_meshindex!=-1)_shieldmesh=CalCoreModel_GetCoreMesh(actors_defs[act->actor_type].coremodel,act->body_parts->shield_meshindex);
else _shieldmesh=NULL;
if (_coremesh==_weaponmesh) boneid=26;//If it's a weapon snap to WeaponR bone
if (_coremesh==_shieldmesh) boneid=21;//If it's a shield snap to WeaponL bone
if (boneid!=-1) {
reverse_scale=1.0/actors_defs[act->actor_type].skel_scale;
CalSkeleton_GetBonePoints(skel,&points[0][0]);
glTranslatef(points[boneid][0],points[boneid][1],points[boneid][2]);
glScalef(reverse_scale,reverse_scale,reverse_scale);
glTranslatef(-points[boneid][0],-points[boneid][1],-points[boneid][2]);
switch(boneid){
case 26:
if(actors_defs[act->actor_type].weapon[act->cur_weapon].glow>0){
glow=actors_defs[act->actor_type].weapon[act->cur_weapon].glow;
}
break;
case 21:
if(actors_defs[act->actor_type].shield[act->cur_shield].glow>0){
glow=actors_defs[act->actor_type].shield[act->cur_shield].glow;
}
default:
break;
}
}
}
if(glow>0){
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE,GL_SRC_ALPHA);
glDisable(GL_LIGHTING);
if(use_shadow_mapping){
glPushAttrib(GL_TEXTURE_BIT|GL_ENABLE_BIT);
ELglActiveTextureARB(shadow_unit);
glDisable(depth_texture_target);
disable_texgen();
ELglActiveTextureARB(GL_TEXTURE0);
}
glColor4f(glow_colors[glow].r, glow_colors[glow].g, glow_colors[glow].b, 0.5f);
glPushMatrix();
glScalef(0.99f, 0.99f, 0.99f);
render_submesh(meshId, submeshCount, pCalRenderer, meshVertices, meshNormals, meshTextureCoordinates, meshFaces);
glPopMatrix();
glColor4f(glow_colors[glow].r, glow_colors[glow].g, glow_colors[glow].b, 0.85f);
render_submesh(meshId, submeshCount, pCalRenderer, meshVertices, meshNormals, meshTextureCoordinates, meshFaces);
glColor4f(glow_colors[glow].r, glow_colors[glow].g, glow_colors[glow].b, 0.99f);
glPushMatrix();
glScalef(1.01f, 1.01f, 1.01f);
render_submesh(meshId, submeshCount, pCalRenderer, meshVertices, meshNormals, meshTextureCoordinates, meshFaces);
glPopMatrix();
if(use_shadow_mapping){
glPopAttrib();
}
glColor3f(1.0f, 1.0f, 1.0f);
glDisable(GL_COLOR_MATERIAL);
glDisable(GL_BLEND);
glEnable(GL_LIGHTING);
} else render_submesh(meshId, submeshCount, pCalRenderer, meshVertices, meshNormals, meshTextureCoordinates, meshFaces);
glPopMatrix();
}
// clear vertex array state
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
// end the rendering
CalRenderer_EndRendering(pCalRenderer);
}
#ifdef DEBUG
}
#endif
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glDisable(GL_TEXTURE_2D);
glColor3f(1,1,1);
#ifdef DEBUG
if (render_skeleton) cal_render_bones(act);
#endif
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glPopMatrix();
//glEnable(GL_TEXTURE_2D);
}