File size: 7,764 Bytes
00e6582
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
Copyright (c) 2026 Ethan Mortonson.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef NEW_MODEL_H
#define NEW_MODEL_H
#include "../engine.h"
#include "stdio.h"
#include "../physics/vector.h"
#include "image.h"

#define NUMVERTEXNORMALS        162
#define SHADEDOT_QUANT          16
#define MAX_SE3_VERTS 2048

#pragma pack(push, 1)
// vertex
struct _Animation_File_Animation
{
    char Name[32];
    unsigned char Keyframes[2];
    float Speed;
    bool Loop;
};

struct _Animation_File
{
    //.ANF
     struct _Animation_File_Animation Animations[21];
};

struct Vertex
{
    unsigned short  Coordinates[3];                // compressed vertex (x, y, z) coordinates
    unsigned char  Normal_Index;    // index to a normal vector for the lighting

};

struct Texture_Coordinate
{
    short x;
    short y;
};

struct Frame
{
    float  Frame_Scale[3];       // scale values
    float  Frame_Translation_Vector[3];   // translation vector
    char   Frame_Name[16];       // frame name
    struct Vertex*    First_Vertex;       // first vertex of this frame
};

struct Triangle
{
    short  Triangle_Vertex_IDs[3];    // indexes to triangle's vertices
    short  Triangle_Texture_Vertex_IDs[3];     // indexes to vertices' texture coorinates

};

typedef struct {
    float pos[3];
    float normal[3];
    float uv[2];
} gl_vertex_t;



struct Texture_Triangle
{
    short  Triangle_Texture_Vertex_IDs[3];     // indexes to vertices' texture coorinates

};

struct Stripped_Triangle
{
    short  Triangle_Vertex_IDs[3];    // indexes to triangle's vertices

};

// animation
struct _Animation
{
    int     First_Frame_ID;            // first frame of the animation
    int     Last_Frame_ID;             // number of frames
    int     FPS;                    // number of frames per second

};

// animation state
struct _Animation_State
{
    int     Start_Frame_ID;              // first frame
    int     End_Frame_ID;                // last frame
    int     FPS;                     // frame per second for this animation

    float   Current_Time;               // current time
    float   Old_Time;                // old time
    float   Interpolation_Percentage;                // percent of interpolation

    int     Animation_Type;                    // animation type

    int     Current_Frame_ID;              // current frame
    int     Next_Frame_ID;              // next frame

};

//Populate anorms and anorms dots

struct Texture_Name
{
    char Name[64];
};

/////////////////////////////////////////////////

struct _Draw_Array
{
    float* Triangle_Coordinates;
    float* Texture_Triangle_Coordinates;
    float* Triangle_Normal_Coordinates;
};

struct _Normal_Table_Entry
{
    float x;
    float y;
    float z;
};

struct _Normal_Table
{
    struct _Normal_Table_Entry Entries[240];
};

struct _SE3_Model
{
    struct _Draw_Array* Draw_Arrays;
    gl_vertex_t* glDraw_Vertices;
    struct _Animation_File* Animation_File;
    unsigned char Current_Start_Keyframe;
    unsigned char Current_End_Keyframe;
    unsigned char Current_Keyframe = 0;

    int Frame_Size;
    int Number_Of_Textures;
    int Number_Of_Vertices;
    int Number_Of_Texture_Vertices;
    int Number_Of_Triangles;
    int Number_Of_Commands;
    int Number_Of_Frames;
    unsigned int Texture_ID;
    float Model_Scale;

    struct _Animation_State Animation_State;


    Vector* Vertex_Array;
    int* Command_Array;
    int* Light_Normals;
    struct _Animation* Animation_List[21];







    Texture_Name* Texture_Names;
    struct Texture_Coordinate* Texture_Coordinates;
    struct Frame* Frame_Data;

    struct Triangle* Triangle_Indices;
    struct Texture_Triangle* Texture_Triangle_Indices;
    struct Stripped_Triangle* Stripped_Triangle_Indices;


        // member variables
    static Vector   anorms[ NUMVERTEXNORMALS ];
    static float    anorms_dots[ SHADEDOT_QUANT ][256];





/* Table of precalculated normals */
Vector anorms_table[240] = {
#include "normal_table.h"
};




struct _Normal_Table NT;

//    unsigned int Texture_ID;

    struct _Image* Image;

    int RGBA_Image_Size;
        unsigned char Texture_Made;
        int x;
        int y;
        int Width;
        int Height;
        unsigned char* RGB_Canvas;
        unsigned char* RGBA_Canvas;
        float Vertices[8];
        //float Texture_Vertices[8];
};

struct SE3_glcmd_t
{
  float s;                    /* s texture coord. */
  float t;                    /* t texture coord. */
  int index;                  /* vertex index */
};

struct SE3_vertex_t
{
  unsigned short v[3];         /* position */
  unsigned char normalIndex;  /* normal vector index */
};

struct SE3_frame_t
{
  Vector scale;               /* scale factor */
  Vector translate;           /* translation vector */
  char name[16];              /* frame name */
  struct SE3_vertex_t *verts; /* list of frame's vertices */
};

#pragma pack(pop)

struct _SE3_Model* Create_SE3_Model();
void Load_SE3_Model(struct _SE3_Model* Model, const char* Filename);
void Load_SE3_Texture(struct _SE3_Model* Model, const char* Filename);

void Load_Transparent_SE3_Texture(struct _SE3_Model* Image, const char* Filename, const char* Transparency_Map_Filename);

void Load_SE3_Animation_Array(struct _SE3_Model* Model, const char* Filename);


void Render_SE3_Model (struct _SE3_Model* Model, int n, float x, float y, float z, float x_Rotation, float y_Rotation, float z_Rotation, bool Vertex_Coloring, float R, float G, float B, bool Cull_Back_Faces);

struct _Animation* Register_Animation(int First_Frame_ID, int Last_Frame_ID, int FPS);


void Add_Animation(struct _Animation_File* A, unsigned char Index, char Name[32], unsigned char Keyframe_One, unsigned char Keyframe_Two, float Speed, bool Loop);
void Create_Animation_File(const char* Filename);


void Render_Wrapper(struct _SE3_Model* Model, unsigned char Index, unsigned char Keyframe_One, unsigned char Keyframe_Two, float Speed, bool Loop, int n, float x, float y, float z, float x_Rotation, float y_Rotation, float z_Rotation); //for animations

void Set_Model_Sequeance(struct _SE3_Model* Model, unsigned char Keyframe_One, unsigned char Keyframe_Two);

void Render_SE3_Model_GLDraw(struct _SE3_Model* Model, int n, float x, float y, float z, float x_Rotation, float y_Rotation, float z_Rotation, bool Vertex_Coloring, float R, float G, float B);
void update_gl_vertices_for_frame(struct _SE3_Model* Model, int frame_index);

void Extract_All_Frames_To_Arrays(struct _SE3_Model* Model);

void Render_SE3_Model_Dynamic_Texture(struct _SE3_Model* Model, int n, float x, float y, float z, float x_Rotation, float y_Rotation, float z_Rotation, bool Vertex_Coloring, float R, float G, float B, int Texture_ID);

//void Dump_SE3_


#endif /* NEW_MODEL_H */