forked from peterderivaz/penguinspuzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.c
219 lines (198 loc) · 5.42 KB
/
model.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
/*
Copyright (c) 2012, Peter de Rivaz
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* WebGL version at http://penguinspuzzle.appspot.com
*
* This contains code to load and draw 3d models.
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "global.h"
#include "shaders.h"
#include "buffer.h"
#include "model.h"
#include "texture.h"
#include "embed_file.h"
enum {TRIS,FAN,STRIP};
MODEL_T *models[NUMMODELS];
// Allocates a model object to use the given texture.
// All points are scaled by scale after being loaded.
// Assigns the model to the given id in an array of models
MODEL_T *model_new(char *name, int texid, int id, float scale)
{
int numpts;
int numfaces;
EMBED_FILE *fid;
MODEL_T *m;
NEW(m);
fid=embed_fopen(name,"rb");
if (!fid)
{
//fprintf(stderr,"Cannot find %s",name);
exit(-1);
}
assert(fid);
embed_fread(&numpts,sizeof(int),1,fid);
embed_fread(&numfaces,sizeof(int),1,fid);
float pts[numpts][3];
float tex[numpts][3];
short faces[numpts][3];
embed_fread(pts,sizeof(float),3*numpts,fid);
embed_fread(tex,sizeof(float),3*numpts,fid);
embed_fread(faces,sizeof(short),3*numfaces,fid);
for(int i=0;i<numpts;i++)
for(int j=0;j<3;j++)
pts[i][j]*=scale;
m->buf = buffer_new(pts, tex, faces, numpts, numfaces);
m->tex = texid;
m->type = TRIS;
assert(id<NUMMODELS);
models[id]=m;
embed_fclose(fid);
return m;
}
void model_draw(MODEL_T *m,SHADER_T *s)
{
assert(m);
texture_select(m->tex);
if (m->type==TRIS)
buffer_draw(m->buf,s);
else if (m->type==FAN)
buffer_drawfan(m->buf,s);
else if (m->type==STRIP)
buffer_drawstrip(m->buf,s);
else
assert(0);
}
void model_drawid(int id,SHADER_T *s)
{
model_draw(models[id],s);
}
// Generate a model consisting of a triangle fan for the ocean
MODEL_T *model_sea(int t,float sz,float depth,float texscale)
{
int numpts=30; // Number of points around circumference
float pts[numpts+2][3];
float tex[numpts+2][3];
short faces[1][3];
MODEL_T *m;
NEW(m);
m->tex=t;
pts[0][0]=0;
pts[0][1]=0;
pts[0][2]=depth;
tex[0][0]=0;
tex[0][0]=0;
tex[0][0]=1.0;
for(int i=0;i<=numpts;i++)
{
float angle=2*3.1416f*i/numpts;
float x = cos(angle);
float y = sin(angle);
pts[i+1][0] = x*sz+400;
pts[i+1][1] = y*sz+400;
pts[i+1][2] = depth;
tex[i+1][0] = x*texscale;
tex[i+1][1] = y*texscale;
tex[i+1][2] = 1.0f;
}
m->buf = buffer_new(pts,tex,faces,numpts+2,0);
m->type = FAN;
return m;
}
// Generate a model consisting of a triangle strip for the sky
MODEL_T *model_sky(int t,float sz,float depth,float texscale)
{
int numpts=30; // Number of points around circumference
float pts[2*(numpts+1)][3];
float tex[2*(numpts+1)][3];
short faces[1][3];
MODEL_T *m;
NEW(m);
m->tex=t;
for(int i=0;i<=numpts;i++)
{
float angle=2*3.1416f*i/numpts;
float x = cos(angle);
float y = sin(angle);
pts[2*i][0] = x*sz+400;
pts[2*i][1] = y*sz+400;
pts[2*i][2] = depth;
tex[2*i][0] = i*texscale/numpts;
tex[2*i][1] = 1.0f;
tex[2*i][2] = 1.0f;
pts[2*i+1][0] = x*sz+400;
pts[2*i+1][1] = y*sz+400;
pts[2*i+1][2] = sz;
tex[2*i+1][0] = i*texscale/numpts;
tex[2*i+1][1] = 0.0f;
tex[2*i+1][2] = 1.0f;
}
m->buf = buffer_new(pts,tex,faces,2*(numpts+1),0);
m->type = STRIP;
return m;
}
MODEL_T *model_explosion(int t)
{
int numpts=300; // Number of points around circumference
float pts[4*numpts][3];
float tex[4*numpts][3];
short faces[4*numpts][3];
float deltas[4][2] = {{0,0},{1,0},{1,1},{0,1}};
MODEL_T *m;
NEW(m);
m->tex=t;
for(int i=0;i<numpts;i++)
{
float x = (rand()&31)-16;
float y = (rand()&31)-16;
float z = (rand()&31);
int N=i*4;
for(int j=0;j<4;j++)
{
pts[N+j][0] = x;
pts[N+j][1] = y;
pts[N+j][2] = z;
tex[N+j][0] = deltas[j][0];
tex[N+j][1] = deltas[j][1];
tex[N+j][2] = 1.0f;
}
faces[N][0]=N;
faces[N][1]=N+1;
faces[N][2]=N+2;
faces[N+1][0]=N;
faces[N+1][1]=N+2;
faces[N+1][2]=N+3;
faces[N+2][0]=N;
faces[N+2][1]=N+2;
faces[N+2][2]=N+1;
faces[N+3][0]=N;
faces[N+3][1]=N+3;
faces[N+3][2]=N+2;
}
m->buf = buffer_new(pts,tex,faces,numpts*4,numpts*4);
return m;
}