/
object.cpp
executable file
·274 lines (235 loc) · 9.69 KB
/
object.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
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
/*
* ---------------- www.spacesimulator.net --------------
* ---- Space simulators and 3d engine tutorials ----
*
* Author: Damiano Vitulli <info@spacesimulator.net>
*
* ALL RIGHTS RESERVED
*
*
* Object functions
*
*/
#include <math.h>
#include "load_3ds.h"
#include "load_bmp.h"
#include "imageloader.h"
#include "object.h"
/**********************************************************
*
* VARIABLES DECLARATION
*
*********************************************************/
obj_type object[MAX_OBJECTS]; //Now the object is generic, the cube has annoyed us a little bit, or not?
int obj_qty=0; //Number of objects in our world
int obj_control=0; //Number of the object that we can control
/**********************************************************
*
* FUNCTION ObjLoad(char *p_object_name, char *p_texture_name, float p_pos_x, float p_pos_y, float p_pos_z, float p_rot_x, float p_rot_y, float p_rot_z)
*
* This function loads an object and set some object's data
*
* Parameters: p_object_name = object name
* p_texture_name = texture name
* p_pos_x = starting position x coordinate
* p_pos_y = starting position y coordinate
* p_pos_z = starting position z coordinate
* p_rot_x = starting orientation x axis
* p_rot_y = starting orientation y axis
* p_rot_z = starting orientation z axis
*
* Return value: (char) 1 if the object was loaded correctly, 0 otherwise
*
*********************************************************/
char ObjLoad(char *p_object_name, char *p_texture_name, float p_pos_x, float p_pos_y, float p_pos_z, int p_rot_x, int p_rot_y, int p_rot_z)
{
if (Load3DS (&object[obj_qty],p_object_name)==0) return(0); //Object loading
object[obj_qty].id_texture=LoadBMP(p_texture_name); // The Function LoadBitmap() returns the current texture ID
ObjCalcNormals(&object[obj_qty]); //Once we have all the object data we need to calculate all the normals of the object's vertices
MatrIdentity_4x4(object[obj_qty].matrix); //Object matrix init
ObjPosition(&object[obj_qty], p_pos_x, p_pos_y, p_pos_z); // Object initial position
ObjRotate(&object[obj_qty], p_rot_x, p_rot_y, p_rot_z); // Object initial rotation
obj_qty++; // Let's increase the object number and get ready to load another object!
return (1); // If all is ok then return 1
}
/**********************************************************
*
* SUBROUTINE ObjCalcNormals(obj_type_ptr p_object)
*
* This function calculate all the polygons and vertices' normals of the specified object
*
* Input parameters: p_object = object
*
*********************************************************/
void ObjCalcNormals(obj_type_ptr p_object)
{
int i;
p3d_type l_vect1,l_vect2,l_vect3,l_vect_b1,l_vect_b2,l_normal; //Some local vectors
int l_connections_qty[MAX_VERTICES]; //Number of poligons around each vertex
// Resetting vertices' normals...
for (i=0; i<p_object->vertices_qty; i++)
{
p_object->normal[i].x = 0.0;
p_object->normal[i].y = 0.0;
p_object->normal[i].z = 0.0;
l_connections_qty[i]=0;
}
for (i=0; i<p_object->polygons_qty; i++)
{
l_vect1.x = p_object->vertex[p_object->polygon[i].a].x;
l_vect1.y = p_object->vertex[p_object->polygon[i].a].y;
l_vect1.z = p_object->vertex[p_object->polygon[i].a].z;
l_vect2.x = p_object->vertex[p_object->polygon[i].b].x;
l_vect2.y = p_object->vertex[p_object->polygon[i].b].y;
l_vect2.z = p_object->vertex[p_object->polygon[i].b].z;
l_vect3.x = p_object->vertex[p_object->polygon[i].c].x;
l_vect3.y = p_object->vertex[p_object->polygon[i].c].y;
l_vect3.z = p_object->vertex[p_object->polygon[i].c].z;
// Polygon normal calculation
VectCreate (&l_vect1, &l_vect2, &l_vect_b1); // Vector from the first vertex to the second one
VectCreate (&l_vect1, &l_vect3, &l_vect_b2); // Vector from the first vertex to the third one
VectDotProduct (&l_vect_b1, &l_vect_b2, &l_normal); // Dot product between the two vectors
VectNormalize (&l_normal); //Normalizing the resultant we obtain the polygon normal
l_connections_qty[p_object->polygon[i].a]+=1; // For each vertex shared by this polygon we increase the number of connections
l_connections_qty[p_object->polygon[i].b]+=1;
l_connections_qty[p_object->polygon[i].c]+=1;
p_object->normal[p_object->polygon[i].a].x+=l_normal.x; // For each vertex shared by this polygon we add the polygon normal
p_object->normal[p_object->polygon[i].a].y+=l_normal.y;
p_object->normal[p_object->polygon[i].a].z+=l_normal.z;
p_object->normal[p_object->polygon[i].b].x+=l_normal.x;
p_object->normal[p_object->polygon[i].b].y+=l_normal.y;
p_object->normal[p_object->polygon[i].b].z+=l_normal.z;
p_object->normal[p_object->polygon[i].c].x+=l_normal.x;
p_object->normal[p_object->polygon[i].c].y+=l_normal.y;
p_object->normal[p_object->polygon[i].c].z+=l_normal.z;
}
for (i=0; i<p_object->vertices_qty; i++)
{
if (l_connections_qty[i]>0)
{
p_object->normal[i].x /= l_connections_qty[i]; // Let's now average the polygons' normals to obtain the vertex normal!
p_object->normal[i].y /= l_connections_qty[i];
p_object->normal[i].z /= l_connections_qty[i];
}
}
}
/**********************************************************
*
* SUBROUTINE ObjPosition (obj_type_ptr p_object,float p_x,float p_y,float p_z)
*
* Object positioning relative to the world
*
* Input parameters: p_object = object to move
* p_x = x coordinate
* p_y = y coordinate
* p_z = z coordinate
*
*********************************************************/
void ObjPosition (obj_type_ptr p_object,float p_x,float p_y,float p_z)
{
//The position fields in the object matrix are filled with the new values
p_object->matrix[3][0]=p_x;
p_object->matrix[3][1]=p_y;
p_object->matrix[3][2]=p_z;
}
/**********************************************************
*
* SUBROUTINE ObjTranslate (obj_type_ptr p_object,float p_x,float p_y,float p_z)
*
* Object translation relative to the point of view
*
* Input parameters: p_object = object to translate
* p_x = x coordinate
* p_y = y coordinate
* p_z = z coordinate
*
*********************************************************/
void ObjTranslate (obj_type_ptr p_object,float p_x,float p_y,float p_z)
{
int j,k;
matrix_4x4_type l_matrix, l_res;
MatrIdentity_4x4(l_matrix);
l_matrix[3][0]=p_x;
l_matrix[3][1]=p_y;
l_matrix[3][2]=p_z;
//The object matrix is multiplied by a translation matrix
MatrMul_4x4_4x4(l_matrix,p_object->matrix,l_res);
for(j=0;j<4;j++)
for(k=0;k<4;k++)
p_object->matrix[j][k]=l_res[j][k];
}
/**********************************************************
*
* SUBROUTINE ObjTranslateW (obj_type_ptr p_object,float p_x,float p_y,float p_z)
*
* Object translation relative to the world
*
* Input parameters: p_object = object to translate
* p_x = x coordinate
* p_y = y coordinate
* p_z = z coordinate
*
*********************************************************/
void ObjTranslateW (obj_type_ptr p_object,float p_x,float p_y,float p_z)
{
p_object->matrix[3][0]+=p_x;
p_object->matrix[3][1]+=p_y;
p_object->matrix[3][2]+=p_z;
}
/**********************************************************
*
* SUBROUTINE ObjRotate (obj_type_ptr p_object,int p_angle_x,int p_angle_y,int p_angle_z)
*
* Object rotation relative to the point of view
* Angles are expressed in 1/10 degree
*
* Input parameters: p_object = object to rotate
* p_angle_x = x rotation angle
* p_angle_y = y rotation angle
* p_angle_z = z rotation angle
*
*********************************************************/
void ObjRotate (obj_type_ptr p_object,int p_angle_x,int p_angle_y,int p_angle_z)
{
matrix_4x4_type l_matrix, l_res;
//Range control
if (p_angle_x<0) p_angle_x=3600+p_angle_x;
if (p_angle_y<0) p_angle_y=3600+p_angle_y;
if (p_angle_z<0) p_angle_z=3600+p_angle_z;
if (p_angle_x<0 || p_angle_x>3600) p_angle_x=0;
if (p_angle_y<0 || p_angle_y>3600) p_angle_y=0;
if (p_angle_z<0 || p_angle_z>3600) p_angle_z=0;
if (p_angle_x)
{
//The object matrix is multiplied by the X rotation matrix
MatrIdentity_4x4(l_matrix);
l_matrix[1][1]=(matr_cos_table[p_angle_x]);
l_matrix[1][2]=(matr_sin_table[p_angle_x]);
l_matrix[2][1]=(-matr_sin_table[p_angle_x]);
l_matrix[2][2]=(matr_cos_table[p_angle_x]);
MatrMul_4x4_4x4(l_matrix,p_object->matrix,l_res);
MatrCopy_4x4(p_object->matrix,l_res);
}
if (p_angle_y)
{
// ...by the Y rotation matrix
MatrIdentity_4x4(l_matrix);
l_matrix[0][0]=(matr_cos_table[p_angle_y]);
l_matrix[0][2]=(-matr_sin_table[p_angle_y]);
l_matrix[2][0]=(matr_sin_table[p_angle_y]);
l_matrix[2][2]=(matr_cos_table[p_angle_y]);
MatrMul_4x4_4x4(l_matrix,p_object->matrix,l_res);
MatrCopy_4x4(p_object->matrix,l_res);
}
if (p_angle_z)
{
// ...by the Z rotation matrix
MatrIdentity_4x4(l_matrix);
l_matrix[0][0]=(matr_cos_table[p_angle_z]);
l_matrix[0][1]=(matr_sin_table[p_angle_z]);
l_matrix[1][0]=(-matr_sin_table[p_angle_z]);
l_matrix[1][1]=(matr_cos_table[p_angle_z]);
MatrMul_4x4_4x4(l_matrix,p_object->matrix,l_res);
MatrCopy_4x4(p_object->matrix,l_res);
}
}