-
Notifications
You must be signed in to change notification settings - Fork 0
/
example1.c
159 lines (133 loc) · 4.3 KB
/
example1.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
#include "raylib.h"
typedef struct
{
int ReferenceCount;
char *Name;
Model ModelResource;
} Resource_t;
typedef struct Node_t
{
char *Name;
int ResourceIndex;
Matrix localMatrix;
Matrix worldMatrix;
struct Node_t *Parent;
} Node_t;
typedef struct
{
int nResources;
int nNodes;
Resource_t *Resources;
Node_t *Nodes;
} Scene_T;
// search backwards from the current Node.
// parent is usually not far up the chain
Node_t *FindNode(Scene_T *scene, Node_t *cNode, const char *parent)
{
while(cNode!=&scene->Nodes[0])
{
cNode--;
if (cNode->Name!=NULL)
{
if (stricmp(cNode->Name,parent)==0)
{
return cNode;
}
}
}
return NULL;
}
void UnLoadAscii3D(Scene_T *scene)
{
for (int q=0;q<scene->nNodes;q++)
{
Node_t *node = &scene->Nodes[q];
RL_FREE(node->Name);
}
for (int q=0;q<scene->nResources;q++)
{
UnloadModel(scene->Resources[q].ModelResource);
RL_FREE(scene->Resources[q].Name);
}
RL_FREE(scene->Nodes);
RL_FREE(scene->Resources);
RL_FREE(scene);
}
Scene_T *LoadAscii3D(const char *fname)
{
char linebuffer[128];
FILE *fp = fopen(fname,"rt");
Scene_T *scene = RL_CALLOC(1,sizeof(Scene_T));
fscanf(fp,"resources:%d\n",&scene->nResources);
printf("%d\n",scene->nResources);
scene->Resources = RL_CALLOC(scene->nResources,sizeof(Resource_t));
for (int q=0;q<scene->nResources;q++)
{
fgets(linebuffer,sizeof(linebuffer),fp);
linebuffer[strlen(linebuffer)-1]=0;
scene->Resources[q].ReferenceCount = 0;
scene->Resources[q].ModelResource = LoadModel(linebuffer);
scene->Resources[q].Name = strdup(linebuffer);
}
fscanf(fp,"nodes:%d\n",&scene->nNodes);
scene->Nodes = RL_CALLOC(scene->nNodes,sizeof(Node_t));
for (int q=0;q<scene->nNodes;q++)
{
Node_t *cNode = &scene->Nodes[q];
fgets(linebuffer,sizeof(linebuffer),fp);
linebuffer[strlen(linebuffer)-1]=0;
char *ClassName = strtok(linebuffer,",");
char *NodeName = strtok(NULL,",");
char *ParentName = strtok(NULL,",");
int ResourceID = atoi(strtok(NULL,","));
cNode->localMatrix = MatrixIdentity();
cNode->localMatrix.m0 = strtod(strtok(NULL,","),NULL);
cNode->localMatrix.m1 = strtod(strtok(NULL,","),NULL);
cNode->localMatrix.m2 = strtod(strtok(NULL,","),NULL);
cNode->localMatrix.m4 = strtod(strtok(NULL,","),NULL);
cNode->localMatrix.m5 = strtod(strtok(NULL,","),NULL);
cNode->localMatrix.m6 = strtod(strtok(NULL,","),NULL);
cNode->localMatrix.m8 = strtod(strtok(NULL,","),NULL);
cNode->localMatrix.m9 = strtod(strtok(NULL,","),NULL);
cNode->localMatrix.m10 = strtod(strtok(NULL,","),NULL);
cNode->localMatrix.m12 = strtod(strtok(NULL,","),NULL);
cNode->localMatrix.m13 = strtod(strtok(NULL,","),NULL);
cNode->localMatrix.m14 = strtod(strtok(NULL,","),NULL);
cNode->ResourceIndex = ResourceID;
cNode->Name = strdup(NodeName);
cNode->Parent = FindNode(scene,cNode,ParentName);
if (stricmp(ClassName,"OmniLight3D")==0)
{
char *nxt = strtok(NULL,",");
// uint32_t = (uint32_t)strtol(&nxt[1], NULL, 16);
// nxt is $RRGGBBAA hex value
}
}
return scene;
}
// we need to draw a mesh with a direct matrix
// instead of trashing the ModelResource matrix every drawcall
void DrawModelMatrix(Model ModelResource, Matrix matTransform, Color tint)
{
for (int i = 0; i < ModelResource.meshCount; i++)
{
DrawMesh(ModelResource.meshes[i], ModelResource.materials[ModelResource.meshMaterial[i]], matTransform);
}
}
void DrawAscii3D(Scene_T *scene)
{
for (int q=0;q<scene->nNodes;q++)
{
Node_t *node = &scene->Nodes[q];
Matrix m = node->localMatrix;
if (node->Parent!=NULL)
{
m = MatrixMultiply(m,node->Parent->worldMatrix);
}
node->worldMatrix = m;
if (node->ResourceIndex!=-1)
{
DrawModelMatrix(scene->Resources[scene->Nodes[q].ResourceIndex].ModelResource,m,WHITE);
}
}
}