view3d

view src/scene.c @ 11:1a3799ff443a

merged fullscreen and minor edit
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 04 Sep 2012 05:11:13 +0300
parents 5562a637e5aa
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <float.h>
5 #include <GL/glew.h>
7 #include <assimp/assimp.h>
8 #include <assimp/aiScene.h>
9 #include <assimp/aiPostProcess.h>
11 #include <imago2.h>
13 #include "scene.h"
15 static void setup_light(struct light *lt);
16 static void color(float *dest, float r, float g, float b);
17 static struct mesh *create_mesh(const struct aiScene *aiscn, struct aiMesh *aim);
18 static unsigned int create_buffer(unsigned int type, void *data, size_t sz);
19 static unsigned int load_texture(const char *fname);
21 extern int verbose;
23 int init_scene(struct scene *scn)
24 {
25 scn->meshes = 0;
26 scn->lights = 0;
28 scn->bbox.min[0] = scn->bbox.min[1] = scn->bbox.min[2] = FLT_MAX;
29 scn->bbox.max[0] = scn->bbox.max[1] = scn->bbox.max[2] = -FLT_MAX;
30 return 0;
31 }
33 int load_scene(struct scene *scn, const char *fname)
34 {
35 int i, j;
36 const struct aiScene *aiscn;
37 unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
38 aiProcess_PreTransformVertices | aiProcess_Triangulate |
39 aiProcess_GenSmoothNormals | aiProcess_SortByPType | aiProcess_FlipUVs;
41 if(!(aiscn = aiImportFile(fname, proc_flags))) {
42 fprintf(stderr, "failed to load: %s\n", fname);
43 return -1;
44 }
46 if(verbose) {
47 printf("scene: %s (%d meshes, %d lights)\n", fname, aiscn->mNumMeshes, aiscn->mNumLights);
48 }
50 for(i=0; i<aiscn->mNumLights; i++) {
51 struct light *lt;
52 struct aiLight *ailt = aiscn->mLights[i];
54 if(!(lt = malloc(sizeof *lt))) {
55 perror("failed to allocate light");
56 return -1;
57 }
59 if(verbose) {
60 printf("- light(%s) ", ailt->mName.data);
61 }
63 switch(ailt->mType) {
64 case aiLightSource_POINT:
65 lt->pos[0] = ailt->mPosition.x;
66 lt->pos[1] = ailt->mPosition.y;
67 lt->pos[2] = ailt->mPosition.z;
68 lt->pos[3] = 1.0f;
69 if(verbose) {
70 printf("pos(%.2f %.2f %.2f) ", lt->pos[0], lt->pos[1], lt->pos[2]);
71 }
72 break;
74 case aiLightSource_DIRECTIONAL:
75 lt->pos[0] = ailt->mDirection.x;
76 lt->pos[1] = ailt->mDirection.y;
77 lt->pos[2] = ailt->mDirection.z;
78 lt->pos[3] = 0.0f;
79 if(verbose) {
80 printf("dir(%.2f %.2f %.2f) ", lt->pos[0], lt->pos[1], lt->pos[2]);
81 }
82 break;
84 default:
85 fprintf(stderr, "error loading light: %s, unsupported type\n", ailt->mName.data);
86 continue;
87 }
88 color(lt->color, ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b);
89 if(verbose) {
90 printf("col(%.2f %.2f %.2f) ", lt->color[0], lt->color[1], lt->color[2]);
91 }
93 lt->cone_inner = ailt->mAngleInnerCone;
94 lt->cone_outer = ailt->mAngleOuterCone;
96 lt->att[0] = ailt->mAttenuationConstant;
97 lt->att[1] = ailt->mAttenuationLinear;
98 lt->att[2] = ailt->mAttenuationQuadratic;
99 if(verbose) {
100 printf("att(%.2f %.2f %.2f)\n", lt->att[0], lt->att[1], lt->att[2]);
101 }
103 lt->next = scn->lights;
104 scn->lights = lt;
105 }
107 /* load meshes and calculate bounding box */
108 for(i=0; i<aiscn->mNumMeshes; i++) {
109 struct mesh *m;
111 if(!(m = create_mesh(aiscn, aiscn->mMeshes[i]))) {
112 return -1;
113 }
115 for(j=0; j<3; j++) {
116 if(m->bbox.min[j] < scn->bbox.min[j]) {
117 scn->bbox.min[j] = m->bbox.min[j];
118 }
119 if(m->bbox.max[j] > scn->bbox.max[j]) {
120 scn->bbox.max[j] = m->bbox.max[j];
121 }
122 }
124 m->next = scn->meshes;
125 scn->meshes = m;
126 }
128 aiReleaseImport(aiscn);
129 return 0;
130 }
132 static void color(float *dest, float r, float g, float b)
133 {
134 dest[0] = r;
135 dest[1] = g;
136 dest[2] = b;
137 dest[3] = 1.0;
138 }
140 static struct mesh *create_mesh(const struct aiScene *aiscn, struct aiMesh *aim)
141 {
142 int i, j;
143 struct mesh *m;
144 struct aiMaterial *mat = aiscn->mMaterials[aim->mMaterialIndex];
145 float sstr = 1.0;
146 unsigned int sz, *idxarr, *dptr;
147 struct aiVector3D *vptr;
148 struct aiString tex_name;
150 if(!(m = calloc(1, sizeof *m))) {
151 perror("failed to allocate mesh");
152 return 0;
153 }
155 if(verbose) {
156 printf("- mesh(%s) v:%d f:%d\n", aim->mName.data, aim->mNumVertices, aim->mNumFaces);
157 }
159 /* default material */
160 color(m->mat.kd, 1, 1, 1);
161 color(m->mat.ks, 0, 0, 0);
162 m->mat.shin = 60.0;
164 aiGetMaterialColor(mat, AI_MATKEY_COLOR_DIFFUSE, (void*)m->mat.kd);
165 aiGetMaterialColor(mat, AI_MATKEY_COLOR_SPECULAR, (void*)m->mat.ks);
166 sz = 1;
167 aiGetMaterialFloatArray(mat, AI_MATKEY_SHININESS, &m->mat.shin, &sz);
168 sz = 1;
169 aiGetMaterialFloatArray(mat, AI_MATKEY_SHININESS_STRENGTH, &sstr, &sz);
170 color(m->mat.ks, m->mat.ks[0] * sstr, m->mat.ks[1] * sstr, m->mat.ks[2] * sstr);
172 if(aiGetMaterialString(mat, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0), &tex_name) == 0) {
173 m->mat.tex = load_texture(tex_name.data);
174 }
176 m->num_verts = aim->mNumVertices;
177 m->num_faces = aim->mNumFaces;
179 m->bbox.min[0] = m->bbox.min[1] = m->bbox.min[2] = FLT_MAX;
180 m->bbox.max[0] = m->bbox.max[1] = m->bbox.max[2] = -FLT_MAX;
182 vptr = aim->mVertices;
183 for(i=0; i<m->num_verts; i++) {
184 if(vptr->x < m->bbox.min[0]) m->bbox.min[0] = vptr->x;
185 if(vptr->y < m->bbox.min[1]) m->bbox.min[1] = vptr->y;
186 if(vptr->z < m->bbox.min[2]) m->bbox.min[2] = vptr->z;
187 if(vptr->x > m->bbox.max[0]) m->bbox.max[0] = vptr->x;
188 if(vptr->y > m->bbox.max[1]) m->bbox.max[1] = vptr->y;
189 if(vptr->z > m->bbox.max[2]) m->bbox.max[2] = vptr->z;
190 vptr++;
191 }
193 m->vert_buf = create_buffer(GL_ARRAY_BUFFER, aim->mVertices, m->num_verts * sizeof *aim->mVertices);
194 if(aim->mNormals) {
195 m->norm_buf = create_buffer(GL_ARRAY_BUFFER, aim->mNormals, m->num_verts * sizeof *aim->mNormals);
196 }
197 if(aim->mTextureCoords) {
198 m->tex_buf = create_buffer(GL_ARRAY_BUFFER, aim->mTextureCoords[0], m->num_verts * sizeof *aim->mTextureCoords[0]);
199 }
201 /* indices are scattered all over the fucking place... map the buffer and collect them there directly */
202 m->idx_buf = create_buffer(GL_ELEMENT_ARRAY_BUFFER, 0, m->num_faces * 3 * sizeof *aim->mFaces[0].mIndices);
203 dptr = idxarr = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
205 for(i=0; i<m->num_faces; i++) {
206 for(j=0; j<3; j++) {
207 *dptr++ = aim->mFaces[i].mIndices[j];
208 }
209 }
210 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
211 return m;
212 }
214 static unsigned int create_buffer(unsigned int type, void *data, size_t sz)
215 {
216 unsigned int vbo;
217 glGenBuffers(1, &vbo);
218 glBindBuffer(type, vbo);
219 glBufferData(type, sz, data, GL_STATIC_DRAW);
220 return vbo;
221 }
223 static unsigned int load_texture(const char *fname)
224 {
225 unsigned int tex;
226 void *pixels;
227 int xsz, ysz;
229 if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
230 fprintf(stderr, "failed to load image: %s\n", fname);
231 return 0;
232 }
234 if(verbose) {
235 printf(" - texture: %s (%dx%d)\n", fname, xsz, ysz);
236 }
238 glGenTextures(1, &tex);
239 glBindTexture(GL_TEXTURE_2D, tex);
240 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
241 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
242 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
243 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
244 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
246 img_free_pixels(pixels);
247 return tex;
248 }
250 void render_scene(struct scene *scn)
251 {
252 struct light *lt = scn->lights;
253 int pass = 0;
255 glEnable(GL_BLEND);
256 glDepthFunc(GL_LEQUAL);
258 while(lt || pass == 0) {
259 struct mesh *m;
261 setup_light(lt);
263 if(pass == 0) {
264 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
265 } else {
266 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
267 glDepthMask(0);
268 }
270 m = scn->meshes;
271 while(m) {
272 render_mesh(m, pass);
273 m = m->next;
274 }
275 pass++;
276 if(lt) {
277 lt = lt->next;
278 }
279 }
281 glDisable(GL_BLEND);
282 glDepthMask(1);
283 }
285 static void setup_light(struct light *lt)
286 {
287 if(!lt)
288 return;
289 glLightfv(GL_LIGHT0, GL_POSITION, lt->pos);
290 glLightfv(GL_LIGHT0, GL_DIFFUSE, lt->color);
291 glLightfv(GL_LIGHT0, GL_SPECULAR, lt->color);
292 glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, lt->att[0]);
293 glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, lt->att[1]);
294 glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, lt->att[2]);
295 glEnable(GL_LIGHT0);
296 }
298 void render_mesh(struct mesh *m, int pass)
299 {
300 if(pass > 0) {
301 float black[] = {0, 0, 0, 0};
302 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, black);
303 } else {
304 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, m->mat.kd);
305 }
306 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, m->mat.kd);
307 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, m->mat.ks);
308 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, m->mat.shin);
310 if(m->mat.tex) {
311 glEnable(GL_TEXTURE_2D);
312 glBindTexture(GL_TEXTURE_2D, m->mat.tex);
313 }
315 glBindBuffer(GL_ARRAY_BUFFER, m->vert_buf);
316 glVertexPointer(3, GL_FLOAT, 0, 0);
317 glEnableClientState(GL_VERTEX_ARRAY);
319 if(m->norm_buf) {
320 glBindBuffer(GL_ARRAY_BUFFER, m->norm_buf);
321 glNormalPointer(GL_FLOAT, 0, 0);
322 glEnableClientState(GL_NORMAL_ARRAY);
323 }
324 if(m->tex_buf) {
325 glBindBuffer(GL_ARRAY_BUFFER, m->tex_buf);
326 glTexCoordPointer(3, GL_FLOAT, 0, 0);
327 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
328 }
330 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m->idx_buf);
331 glDrawElements(GL_TRIANGLES, m->num_faces * 3, GL_UNSIGNED_INT, 0);
333 glDisableClientState(GL_VERTEX_ARRAY);
334 glDisableClientState(GL_NORMAL_ARRAY);
335 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
337 if(m->mat.tex) {
338 glDisable(GL_TEXTURE_2D);
339 }
340 }