annotate plugins/crazychat/glm.c @ 13119:fcde3faa1f57

[gaim-migrate @ 15481] This adds support for displaying log timestamps in their original timezone. If your OS's definition of struct tm sucks, then the log timestamps will show up in your local timezone, but converted, so the time is accurate. Yay! Anyway, this all works, as I've renamed lots of my log files locally, but currently, there's no code to save new logs in this name format. That's held up on a portability issue and backwards compatibility issue. committer: Tailor Script <tailor@pidgin.im>
author Richard Laager <rlaager@wiktel.com>
date Sat, 04 Feb 2006 20:55:52 +0000
parents 8bcd4d4ccef6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11232
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1 /*
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
2 glm.c
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
3 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
4
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
5
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
6 #include <math.h>
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
7 #include <stdio.h>
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
8 #include <stdlib.h>
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
9 #include <string.h>
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
10 #include <assert.h>
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
11 #include "glm.h"
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
12
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
13
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
14 #define T(x) (model->triangles[(x)])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
15
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
16
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
17 /* _GLMnode: general purpose node
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
18 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
19 typedef struct _GLMnode {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
20 GLuint index;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
21 GLboolean averaged;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
22 struct _GLMnode* next;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
23 } GLMnode;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
24
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
25
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
26 /* glmMax: returns the maximum of two floats */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
27 static GLfloat
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
28 glmMax(GLfloat a, GLfloat b)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
29 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
30 if (b > a)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
31 return b;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
32 return a;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
33 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
34
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
35 /* glmAbs: returns the absolute value of a float */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
36 static GLfloat
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
37 glmAbs(GLfloat f)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
38 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
39 if (f < 0)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
40 return -f;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
41 return f;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
42 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
43
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
44 /* glmDot: compute the dot product of two vectors
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
45 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
46 * u - array of 3 GLfloats (GLfloat u[3])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
47 * v - array of 3 GLfloats (GLfloat v[3])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
48 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
49 static GLfloat
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
50 glmDot(GLfloat* u, GLfloat* v)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
51 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
52 assert(u); assert(v);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
53
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
54 return u[0]*v[0] + u[1]*v[1] + u[2]*v[2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
55 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
56
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
57 /* glmCross: compute the cross product of two vectors
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
58 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
59 * u - array of 3 GLfloats (GLfloat u[3])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
60 * v - array of 3 GLfloats (GLfloat v[3])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
61 * n - array of 3 GLfloats (GLfloat n[3]) to return the cross product in
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
62 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
63 static GLvoid
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
64 glmCross(GLfloat* u, GLfloat* v, GLfloat* n)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
65 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
66 assert(u); assert(v); assert(n);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
67
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
68 n[0] = u[1]*v[2] - u[2]*v[1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
69 n[1] = u[2]*v[0] - u[0]*v[2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
70 n[2] = u[0]*v[1] - u[1]*v[0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
71 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
72
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
73 /* glmNormalize: normalize a vector
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
74 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
75 * v - array of 3 GLfloats (GLfloat v[3]) to be normalized
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
76 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
77 static GLvoid
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
78 glmNormalize(GLfloat* v)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
79 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
80 GLfloat l;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
81
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
82 assert(v);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
83
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
84 l = (GLfloat)sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
85 v[0] /= l;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
86 v[1] /= l;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
87 v[2] /= l;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
88 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
89
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
90 /* glmEqual: compares two vectors and returns GL_TRUE if they are
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
91 * equal (within a certain threshold) or GL_FALSE if not. An epsilon
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
92 * that works fairly well is 0.000001.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
93 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
94 * u - array of 3 GLfloats (GLfloat u[3])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
95 * v - array of 3 GLfloats (GLfloat v[3])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
96 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
97 static GLboolean
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
98 glmEqual(GLfloat* u, GLfloat* v, GLfloat epsilon)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
99 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
100 if (glmAbs(u[0] - v[0]) < epsilon &&
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
101 glmAbs(u[1] - v[1]) < epsilon &&
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
102 glmAbs(u[2] - v[2]) < epsilon)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
103 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
104 return GL_TRUE;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
105 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
106 return GL_FALSE;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
107 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
108
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
109 /* glmWeldVectors: eliminate (weld) vectors that are within an
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
110 * epsilon of each other.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
111 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
112 * vectors - array of GLfloat[3]'s to be welded
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
113 * numvectors - number of GLfloat[3]'s in vectors
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
114 * epsilon - maximum difference between vectors
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
115 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
116 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
117 GLfloat*
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
118 glmWeldVectors(GLfloat* vectors, GLuint* numvectors, GLfloat epsilon)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
119 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
120 GLfloat* copies;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
121 GLuint copied;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
122 GLuint i, j;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
123
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
124 copies = (GLfloat*)malloc(sizeof(GLfloat) * 3 * (*numvectors + 1));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
125 memcpy(copies, vectors, (sizeof(GLfloat) * 3 * (*numvectors + 1)));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
126
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
127 copied = 1;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
128 for (i = 1; i <= *numvectors; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
129 for (j = 1; j <= copied; j++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
130 if (glmEqual(&vectors[3 * i], &copies[3 * j], epsilon)) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
131 goto duplicate;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
132 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
133 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
134
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
135 /* must not be any duplicates -- add to the copies array */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
136 copies[3 * copied + 0] = vectors[3 * i + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
137 copies[3 * copied + 1] = vectors[3 * i + 1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
138 copies[3 * copied + 2] = vectors[3 * i + 2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
139 j = copied; /* pass this along for below */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
140 copied++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
141
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
142 duplicate:
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
143 /* set the first component of this vector to point at the correct
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
144 index into the new copies array */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
145 vectors[3 * i + 0] = (GLfloat)j;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
146 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
147
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
148 *numvectors = copied-1;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
149 return copies;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
150 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
151
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
152 /* glmFindGroup: Find a group in the model
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
153 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
154 GLMgroup*
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
155 glmFindGroup(GLMmodel* model, char* name)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
156 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
157 GLMgroup* group;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
158
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
159 assert(model);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
160
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
161 group = model->groups;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
162 while(group) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
163 if (!strcmp(name, group->name))
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
164 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
165 group = group->next;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
166 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
167
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
168 return group;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
169 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
170
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
171 /* glmAddGroup: Add a group to the model
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
172 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
173 GLMgroup*
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
174 glmAddGroup(GLMmodel* model, char* name)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
175 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
176 GLMgroup* group;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
177
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
178 group = glmFindGroup(model, name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
179 if (!group) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
180 group = (GLMgroup*)malloc(sizeof(GLMgroup));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
181 group->name = strdup(name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
182 group->material = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
183 group->numtriangles = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
184 group->triangles = NULL;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
185 group->next = model->groups;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
186 model->groups = group;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
187 model->numgroups++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
188 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
189
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
190 return group;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
191 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
192
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
193 /* glmFindGroup: Find a material in the model
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
194 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
195 GLuint
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
196 glmFindMaterial(GLMmodel* model, char* name)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
197 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
198 GLuint i;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
199
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
200 /* XXX doing a linear search on a string key'd list is pretty lame,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
201 but it works and is fast enough for now. */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
202 for (i = 0; i < model->nummaterials; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
203 if (!strcmp(model->materials[i].name, name))
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
204 goto found;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
205 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
206
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
207 /* didn't find the name, so print a warning and return the default
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
208 material (0). */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
209 //fprintf(stderr, "glmFindMaterial(): can't find material \"%s\".\n", name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
210 i = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
211
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
212 found:
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
213 return i;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
214 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
215
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
216
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
217 /* glmDirName: return the directory given a path
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
218 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
219 * path - filesystem path
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
220 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
221 * NOTE: the return value should be free'd.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
222 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
223 static char*
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
224 glmDirName(char* path)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
225 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
226 char* dir;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
227 char* s;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
228
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
229 dir = strdup(path);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
230
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
231 s = strrchr(dir, '/');
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
232 if (s)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
233 s[1] = '\0';
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
234 else
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
235 dir[0] = '\0';
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
236
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
237 return dir;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
238 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
239
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
240
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
241 void glmSetMat(GLMmat_str* mats, GLint index){
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
242 GLMmaterial* material;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
243 assert(!(index<0 || index >=mats->num_materials));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
244 material = &mats->materials[index];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
245 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material->ambient);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
246 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material->diffuse);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
247 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material->specular);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
248 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, material->shininess);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
249 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
250
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
251
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
252 GLMmat_str* glmMTL(char* name){
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
253 FILE* file;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
254 char* dir;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
255 char* filename;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
256 char buf[128];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
257 GLuint nummaterials, i;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
258 GLMmat_str* ret;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
259
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
260 file = fopen(name, "r");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
261 if (!file) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
262 fprintf(stderr, "glmReadMTL() failed: can't open material file \"%s\".\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
263 name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
264 exit(1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
265 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
266
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
267 /* count the number of materials in the file */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
268 nummaterials = 1;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
269 while(fscanf(file, "%s", buf) != EOF) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
270 switch(buf[0]) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
271 case '#': /* comment */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
272 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
273 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
274 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
275 case 'n': /* newmtl */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
276 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
277 nummaterials++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
278 sscanf(buf, "%s %s", buf, buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
279 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
280 default:
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
281 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
282 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
283 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
284 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
285 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
286
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
287 rewind(file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
288
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
289 ret = (GLMmat_str*)malloc(sizeof(GLMmat_str));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
290 ret->materials = (GLMmaterial*)malloc(sizeof(GLMmaterial) * nummaterials);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
291 ret->num_materials = nummaterials;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
292
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
293 /* set the default material */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
294 for (i = 0; i < nummaterials; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
295 ret->materials[i].name = NULL;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
296 ret->materials[i].shininess = 65.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
297 ret->materials[i].diffuse[0] = 0.8;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
298 ret->materials[i].diffuse[1] = 0.1;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
299 ret->materials[i].diffuse[2] = 0.1;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
300 ret->materials[i].diffuse[3] = 1.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
301 ret->materials[i].ambient[0] = 0.2;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
302 ret->materials[i].ambient[1] = 0.2;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
303 ret->materials[i].ambient[2] = 0.2;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
304 ret->materials[i].ambient[3] = 1.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
305 ret->materials[i].specular[0] = 0.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
306 ret->materials[i].specular[1] = 0.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
307 ret->materials[i].specular[2] = 0.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
308 ret->materials[i].specular[3] = 1.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
309 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
310 ret->materials[0].name = strdup("default");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
311
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
312 /* now, read in the data */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
313 nummaterials = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
314 while(fscanf(file, "%s", buf) != EOF) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
315 switch(buf[0]) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
316 case '#': /* comment */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
317 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
318 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
319 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
320 case 'n': /* newmtl */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
321 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
322 sscanf(buf, "%s %s", buf, buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
323 nummaterials++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
324 ret->materials[nummaterials].name = strdup(buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
325 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
326 case 'N':
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
327 fscanf(file, "%f", &ret->materials[nummaterials].shininess);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
328 /* wavefront shininess is from [0, 1000], so scale for OpenGL */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
329 ret->materials[nummaterials].shininess /= 1000.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
330 ret->materials[nummaterials].shininess *= 128.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
331 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
332 case 'K':
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
333 switch(buf[1]) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
334 case 'd':
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
335 fscanf(file, "%f %f %f",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
336 &ret->materials[nummaterials].diffuse[0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
337 &ret->materials[nummaterials].diffuse[1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
338 &ret->materials[nummaterials].diffuse[2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
339 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
340 case 's':
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
341 fscanf(file, "%f %f %f",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
342 &ret->materials[nummaterials].specular[0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
343 &ret->materials[nummaterials].specular[1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
344 &ret->materials[nummaterials].specular[2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
345 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
346 case 'a':
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
347 fscanf(file, "%f %f %f",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
348 &ret->materials[nummaterials].ambient[0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
349 &ret->materials[nummaterials].ambient[1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
350 &ret->materials[nummaterials].ambient[2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
351 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
352 default:
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
353 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
354 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
355 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
356 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
357 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
358 default:
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
359 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
360 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
361 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
362 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
363 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
364 return ret;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
365 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
366
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
367 //^^^^charlie^^^^^
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
368
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
369 /* glmReadMTL: read a wavefront material library file
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
370 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
371 * model - properly initialized GLMmodel structure
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
372 * name - name of the material library
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
373 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
374 static GLvoid
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
375 glmReadMTL(GLMmodel* model, char* name)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
376 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
377 FILE* file;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
378 char* dir;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
379 char* filename;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
380 char buf[128];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
381 GLuint nummaterials, i;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
382
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
383 dir = glmDirName(model->pathname);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
384 filename = (char*)malloc(sizeof(char) * (strlen(dir) + strlen(name) + 1));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
385 strcpy(filename, dir);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
386 strcat(filename, name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
387 free(dir);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
388
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
389 file = fopen(filename, "r");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
390 if (!file) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
391 fprintf(stderr, "glmReadMTL() failed: can't open material file \"%s\".\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
392 filename);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
393 exit(1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
394 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
395 free(filename);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
396
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
397 /* count the number of materials in the file */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
398 nummaterials = 1;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
399 while(fscanf(file, "%s", buf) != EOF) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
400 switch(buf[0]) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
401 case '#': /* comment */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
402 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
403 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
404 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
405 case 'n': /* newmtl */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
406 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
407 nummaterials++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
408 sscanf(buf, "%s %s", buf, buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
409 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
410 default:
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
411 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
412 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
413 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
414 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
415 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
416
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
417 rewind(file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
418 model->materials = (GLMmaterial*)malloc(sizeof(GLMmaterial) * nummaterials);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
419 model->nummaterials = nummaterials;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
420
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
421 /* set the default material */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
422 for (i = 0; i < nummaterials; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
423 model->materials[i].name = NULL;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
424 model->materials[i].shininess = 65.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
425 model->materials[i].diffuse[0] = 0.8;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
426 model->materials[i].diffuse[1] = 0.1;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
427 model->materials[i].diffuse[2] = 0.1;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
428 model->materials[i].diffuse[3] = 1.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
429 model->materials[i].ambient[0] = 0.2;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
430 model->materials[i].ambient[1] = 0.2;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
431 model->materials[i].ambient[2] = 0.2;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
432 model->materials[i].ambient[3] = 1.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
433 model->materials[i].specular[0] = 0.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
434 model->materials[i].specular[1] = 0.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
435 model->materials[i].specular[2] = 0.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
436 model->materials[i].specular[3] = 1.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
437 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
438 model->materials[0].name = strdup("default");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
439
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
440 /* now, read in the data */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
441 nummaterials = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
442 while(fscanf(file, "%s", buf) != EOF) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
443 switch(buf[0]) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
444 case '#': /* comment */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
445 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
446 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
447 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
448 case 'n': /* newmtl */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
449 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
450 sscanf(buf, "%s %s", buf, buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
451 nummaterials++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
452 model->materials[nummaterials].name = strdup(buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
453 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
454 case 'N':
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
455 fscanf(file, "%f", &model->materials[nummaterials].shininess);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
456 /* wavefront shininess is from [0, 1000], so scale for OpenGL */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
457 model->materials[nummaterials].shininess /= 1000.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
458 model->materials[nummaterials].shininess *= 128.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
459 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
460 case 'K':
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
461 switch(buf[1]) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
462 case 'd':
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
463 fscanf(file, "%f %f %f",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
464 &model->materials[nummaterials].diffuse[0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
465 &model->materials[nummaterials].diffuse[1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
466 &model->materials[nummaterials].diffuse[2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
467 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
468 case 's':
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
469 fscanf(file, "%f %f %f",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
470 &model->materials[nummaterials].specular[0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
471 &model->materials[nummaterials].specular[1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
472 &model->materials[nummaterials].specular[2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
473 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
474 case 'a':
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
475 fscanf(file, "%f %f %f",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
476 &model->materials[nummaterials].ambient[0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
477 &model->materials[nummaterials].ambient[1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
478 &model->materials[nummaterials].ambient[2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
479 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
480 default:
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
481 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
482 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
483 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
484 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
485 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
486 default:
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
487 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
488 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
489 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
490 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
491 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
492 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
493
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
494 /* glmWriteMTL: write a wavefront material library file
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
495 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
496 * model - properly initialized GLMmodel structure
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
497 * modelpath - pathname of the model being written
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
498 * mtllibname - name of the material library to be written
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
499 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
500 static GLvoid
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
501 glmWriteMTL(GLMmodel* model, char* modelpath, char* mtllibname)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
502 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
503 FILE* file;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
504 char* dir;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
505 char* filename;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
506 GLMmaterial* material;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
507 GLuint i;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
508
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
509 dir = glmDirName(modelpath);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
510 filename = (char*)malloc(sizeof(char) * (strlen(dir)+strlen(mtllibname)));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
511 strcpy(filename, dir);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
512 strcat(filename, mtllibname);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
513 free(dir);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
514
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
515 /* open the file */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
516 file = fopen(filename, "w");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
517 if (!file) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
518 fprintf(stderr, "glmWriteMTL() failed: can't open file \"%s\".\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
519 filename);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
520 exit(1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
521 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
522 free(filename);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
523
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
524 /* spit out a header */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
525 fprintf(file, "# \n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
526 fprintf(file, "# Wavefront MTL generated by GLM library\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
527 fprintf(file, "# \n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
528 fprintf(file, "# GLM library\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
529 fprintf(file, "# Nate Robins\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
530 fprintf(file, "# ndr@pobox.com\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
531 fprintf(file, "# http://www.pobox.com/~ndr\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
532 fprintf(file, "# \n\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
533
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
534 for (i = 0; i < model->nummaterials; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
535 material = &model->materials[i];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
536 fprintf(file, "newmtl %s\n", material->name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
537 fprintf(file, "Ka %f %f %f\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
538 material->ambient[0], material->ambient[1], material->ambient[2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
539 fprintf(file, "Kd %f %f %f\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
540 material->diffuse[0], material->diffuse[1], material->diffuse[2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
541 fprintf(file, "Ks %f %f %f\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
542 material->specular[0],material->specular[1],material->specular[2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
543 fprintf(file, "Ns %f\n", material->shininess / 128.0 * 1000.0);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
544 fprintf(file, "\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
545 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
546 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
547
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
548
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
549 /* glmFirstPass: first pass at a Wavefront OBJ file that gets all the
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
550 * statistics of the model (such as #vertices, #normals, etc)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
551 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
552 * model - properly initialized GLMmodel structure
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
553 * file - (fopen'd) file descriptor
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
554 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
555 static GLvoid
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
556 glmFirstPass(GLMmodel* model, FILE* file)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
557 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
558 GLuint numvertices; /* number of vertices in model */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
559 GLuint numnormals; /* number of normals in model */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
560 GLuint numtexcoords; /* number of texcoords in model */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
561 GLuint numtriangles; /* number of triangles in model */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
562 GLMgroup* group; /* current group */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
563 unsigned v, n, t;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
564 char buf[128];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
565
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
566 /* make a default group */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
567 group = glmAddGroup(model, "default");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
568
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
569 numvertices = numnormals = numtexcoords = numtriangles = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
570 while(fscanf(file, "%s", buf) != EOF) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
571 switch(buf[0]) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
572 case '#': /* comment */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
573 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
574 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
575 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
576 case 'v': /* v, vn, vt */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
577 switch(buf[1]) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
578 case '\0': /* vertex */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
579 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
580 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
581 numvertices++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
582 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
583 case 'n': /* normal */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
584 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
585 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
586 numnormals++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
587 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
588 case 't': /* texcoord */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
589 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
590 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
591 numtexcoords++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
592 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
593 default:
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
594 printf("glmFirstPass(): Unknown token \"%s\".\n", buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
595 exit(1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
596 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
597 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
598 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
599 case 'm':
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
600 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
601 sscanf(buf, "%s %s", buf, buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
602 model->mtllibname = strdup(buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
603 //glmReadMTL(model, buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
604 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
605 case 'u':
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
606 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
607 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
608 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
609 case 'g': /* group */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
610 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
611 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
612 #if SINGLE_STRING_GROUP_NAMES
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
613 sscanf(buf, "%s", buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
614 #else
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
615 buf[strlen(buf)-1] = '\0'; /* nuke '\n' */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
616 #endif
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
617 group = glmAddGroup(model, buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
618 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
619 case 'f': /* face */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
620 v = n = t = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
621 fscanf(file, "%s", buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
622 /* can be one of %d, %d//%d, %d/%d, %d/%d/%d %d//%d */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
623 if (strstr(buf, "//")) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
624 /* v//n */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
625 sscanf(buf, "%d//%d", &v, &n);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
626 fscanf(file, "%d//%d", &v, &n);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
627 fscanf(file, "%d//%d", &v, &n);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
628 numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
629 group->numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
630 while(fscanf(file, "%d//%d", &v, &n) > 0) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
631 numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
632 group->numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
633 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
634 } else if (sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
635 /* v/t/n */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
636 fscanf(file, "%d/%d/%d", &v, &t, &n);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
637 fscanf(file, "%d/%d/%d", &v, &t, &n);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
638 numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
639 group->numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
640 while(fscanf(file, "%d/%d/%d", &v, &t, &n) > 0) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
641 numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
642 group->numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
643 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
644 } else if (sscanf(buf, "%d/%d", &v, &t) == 2) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
645 /* v/t */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
646 fscanf(file, "%d/%d", &v, &t);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
647 fscanf(file, "%d/%d", &v, &t);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
648 numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
649 group->numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
650 while(fscanf(file, "%d/%d", &v, &t) > 0) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
651 numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
652 group->numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
653 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
654 } else {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
655 /* v */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
656 fscanf(file, "%d", &v);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
657 fscanf(file, "%d", &v);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
658 numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
659 group->numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
660 while(fscanf(file, "%d", &v) > 0) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
661 numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
662 group->numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
663 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
664 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
665 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
666
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
667 default:
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
668 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
669 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
670 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
671 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
672 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
673
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
674 /* set the stats in the model structure */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
675 model->numvertices = numvertices;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
676 model->numnormals = numnormals;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
677 model->numtexcoords = numtexcoords;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
678 model->numtriangles = numtriangles;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
679
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
680 /* allocate memory for the triangles in each group */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
681 group = model->groups;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
682 while(group) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
683 group->triangles = (GLuint*)malloc(sizeof(GLuint) * group->numtriangles);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
684 group->numtriangles = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
685 group = group->next;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
686 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
687 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
688
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
689 /* glmSecondPass: second pass at a Wavefront OBJ file that gets all
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
690 * the data.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
691 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
692 * model - properly initialized GLMmodel structure
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
693 * file - (fopen'd) file descriptor
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
694 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
695 static GLvoid
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
696 glmSecondPass(GLMmodel* model, FILE* file)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
697 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
698 GLuint numvertices; /* number of vertices in model */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
699 GLuint numnormals; /* number of normals in model */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
700 GLuint numtexcoords; /* number of texcoords in model */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
701 GLuint numtriangles; /* number of triangles in model */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
702 GLfloat* vertices; /* array of vertices */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
703 GLfloat* normals; /* array of normals */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
704 GLfloat* texcoords; /* array of texture coordinates */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
705 GLMgroup* group; /* current group pointer */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
706 GLuint material; /* current material */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
707 GLuint v, n, t;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
708 char buf[128];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
709
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
710 /* set the pointer shortcuts */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
711 vertices = model->vertices;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
712 normals = model->normals;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
713 texcoords = model->texcoords;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
714 group = model->groups;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
715
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
716 /* on the second pass through the file, read all the data into the
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
717 allocated arrays */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
718 numvertices = numnormals = numtexcoords = 1;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
719 numtriangles = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
720 material = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
721 while(fscanf(file, "%s", buf) != EOF) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
722 switch(buf[0]) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
723 case '#': /* comment */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
724 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
725 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
726 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
727 case 'v': /* v, vn, vt */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
728 switch(buf[1]) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
729 case '\0': /* vertex */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
730 fscanf(file, "%f %f %f",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
731 &vertices[3 * numvertices + 0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
732 &vertices[3 * numvertices + 1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
733 &vertices[3 * numvertices + 2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
734 numvertices++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
735 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
736 case 'n': /* normal */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
737 fscanf(file, "%f %f %f",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
738 &normals[3 * numnormals + 0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
739 &normals[3 * numnormals + 1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
740 &normals[3 * numnormals + 2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
741 numnormals++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
742 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
743 case 't': /* texcoord */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
744 fscanf(file, "%f %f",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
745 &texcoords[2 * numtexcoords + 0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
746 &texcoords[2 * numtexcoords + 1]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
747 numtexcoords++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
748 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
749 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
750 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
751 case 'u':
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
752 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
753 sscanf(buf, "%s %s", buf, buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
754 group->material = material = glmFindMaterial(model, buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
755 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
756 case 'g': /* group */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
757 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
758 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
759 #if SINGLE_STRING_GROUP_NAMES
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
760 sscanf(buf, "%s", buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
761 #else
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
762 buf[strlen(buf)-1] = '\0'; /* nuke '\n' */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
763 #endif
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
764 group = glmFindGroup(model, buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
765 group->material = material;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
766 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
767 case 'f': /* face */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
768 v = n = t = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
769 fscanf(file, "%s", buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
770 /* can be one of %d, %d//%d, %d/%d, %d/%d/%d %d//%d */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
771 if (strstr(buf, "//")) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
772 /* v//n */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
773 sscanf(buf, "%d//%d", &v, &n);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
774 T(numtriangles).vindices[0] = v;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
775 T(numtriangles).nindices[0] = n;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
776 fscanf(file, "%d//%d", &v, &n);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
777 T(numtriangles).vindices[1] = v;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
778 T(numtriangles).nindices[1] = n;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
779 fscanf(file, "%d//%d", &v, &n);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
780 T(numtriangles).vindices[2] = v;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
781 T(numtriangles).nindices[2] = n;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
782 group->triangles[group->numtriangles++] = numtriangles;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
783 numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
784 while(fscanf(file, "%d//%d", &v, &n) > 0) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
785 T(numtriangles).vindices[0] = T(numtriangles-1).vindices[0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
786 T(numtriangles).nindices[0] = T(numtriangles-1).nindices[0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
787 T(numtriangles).vindices[1] = T(numtriangles-1).vindices[2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
788 T(numtriangles).nindices[1] = T(numtriangles-1).nindices[2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
789 T(numtriangles).vindices[2] = v;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
790 T(numtriangles).nindices[2] = n;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
791 group->triangles[group->numtriangles++] = numtriangles;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
792 numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
793 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
794 } else if (sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
795 /* v/t/n */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
796 T(numtriangles).vindices[0] = v;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
797 T(numtriangles).tindices[0] = t;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
798 T(numtriangles).nindices[0] = n;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
799 fscanf(file, "%d/%d/%d", &v, &t, &n);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
800 T(numtriangles).vindices[1] = v;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
801 T(numtriangles).tindices[1] = t;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
802 T(numtriangles).nindices[1] = n;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
803 fscanf(file, "%d/%d/%d", &v, &t, &n);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
804 T(numtriangles).vindices[2] = v;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
805 T(numtriangles).tindices[2] = t;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
806 T(numtriangles).nindices[2] = n;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
807 group->triangles[group->numtriangles++] = numtriangles;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
808 numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
809 while(fscanf(file, "%d/%d/%d", &v, &t, &n) > 0) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
810 T(numtriangles).vindices[0] = T(numtriangles-1).vindices[0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
811 T(numtriangles).tindices[0] = T(numtriangles-1).tindices[0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
812 T(numtriangles).nindices[0] = T(numtriangles-1).nindices[0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
813 T(numtriangles).vindices[1] = T(numtriangles-1).vindices[2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
814 T(numtriangles).tindices[1] = T(numtriangles-1).tindices[2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
815 T(numtriangles).nindices[1] = T(numtriangles-1).nindices[2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
816 T(numtriangles).vindices[2] = v;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
817 T(numtriangles).tindices[2] = t;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
818 T(numtriangles).nindices[2] = n;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
819 group->triangles[group->numtriangles++] = numtriangles;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
820 numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
821 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
822 } else if (sscanf(buf, "%d/%d", &v, &t) == 2) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
823 /* v/t */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
824 T(numtriangles).vindices[0] = v;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
825 T(numtriangles).tindices[0] = t;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
826 fscanf(file, "%d/%d", &v, &t);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
827 T(numtriangles).vindices[1] = v;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
828 T(numtriangles).tindices[1] = t;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
829 fscanf(file, "%d/%d", &v, &t);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
830 T(numtriangles).vindices[2] = v;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
831 T(numtriangles).tindices[2] = t;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
832 group->triangles[group->numtriangles++] = numtriangles;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
833 numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
834 while(fscanf(file, "%d/%d", &v, &t) > 0) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
835 T(numtriangles).vindices[0] = T(numtriangles-1).vindices[0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
836 T(numtriangles).tindices[0] = T(numtriangles-1).tindices[0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
837 T(numtriangles).vindices[1] = T(numtriangles-1).vindices[2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
838 T(numtriangles).tindices[1] = T(numtriangles-1).tindices[2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
839 T(numtriangles).vindices[2] = v;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
840 T(numtriangles).tindices[2] = t;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
841 group->triangles[group->numtriangles++] = numtriangles;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
842 numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
843 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
844 } else {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
845 /* v */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
846 sscanf(buf, "%d", &v);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
847 T(numtriangles).vindices[0] = v;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
848 fscanf(file, "%d", &v);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
849 T(numtriangles).vindices[1] = v;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
850 fscanf(file, "%d", &v);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
851 T(numtriangles).vindices[2] = v;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
852 group->triangles[group->numtriangles++] = numtriangles;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
853 numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
854 while(fscanf(file, "%d", &v) > 0) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
855 T(numtriangles).vindices[0] = T(numtriangles-1).vindices[0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
856 T(numtriangles).vindices[1] = T(numtriangles-1).vindices[2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
857 T(numtriangles).vindices[2] = v;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
858 group->triangles[group->numtriangles++] = numtriangles;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
859 numtriangles++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
860 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
861 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
862 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
863
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
864 default:
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
865 /* eat up rest of line */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
866 fgets(buf, sizeof(buf), file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
867 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
868 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
869 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
870
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
871 #if 0
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
872 /* announce the memory requirements */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
873 printf(" Memory: %d bytes\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
874 numvertices * 3*sizeof(GLfloat) +
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
875 numnormals * 3*sizeof(GLfloat) * (numnormals ? 1 : 0) +
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
876 numtexcoords * 3*sizeof(GLfloat) * (numtexcoords ? 1 : 0) +
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
877 numtriangles * sizeof(GLMtriangle));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
878 #endif
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
879 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
880
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
881
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
882 /* public functions */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
883
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
884
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
885 /* glmUnitize: "unitize" a model by translating it to the origin and
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
886 * scaling it to fit in a unit cube around the origin. Modelurns the
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
887 * scalefactor used.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
888 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
889 * model - properly initialized GLMmodel structure
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
890 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
891 GLfloat
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
892 glmUnitize(GLMmodel* model)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
893 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
894 GLuint i;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
895 GLfloat maxx, minx, maxy, miny, maxz, minz;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
896 GLfloat cx, cy, cz, w, h, d;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
897 GLfloat scale;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
898
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
899 assert(model);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
900 assert(model->vertices);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
901
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
902 /* get the max/mins */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
903 maxx = minx = model->vertices[3 + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
904 maxy = miny = model->vertices[3 + 1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
905 maxz = minz = model->vertices[3 + 2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
906 for (i = 1; i <= model->numvertices; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
907 if (maxx < model->vertices[3 * i + 0])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
908 maxx = model->vertices[3 * i + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
909 if (minx > model->vertices[3 * i + 0])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
910 minx = model->vertices[3 * i + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
911
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
912 if (maxy < model->vertices[3 * i + 1])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
913 maxy = model->vertices[3 * i + 1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
914 if (miny > model->vertices[3 * i + 1])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
915 miny = model->vertices[3 * i + 1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
916
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
917 if (maxz < model->vertices[3 * i + 2])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
918 maxz = model->vertices[3 * i + 2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
919 if (minz > model->vertices[3 * i + 2])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
920 minz = model->vertices[3 * i + 2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
921 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
922
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
923 /* calculate model width, height, and depth */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
924 w = glmAbs(maxx) + glmAbs(minx);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
925 h = glmAbs(maxy) + glmAbs(miny);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
926 d = glmAbs(maxz) + glmAbs(minz);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
927
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
928 /* calculate center of the model */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
929 cx = (maxx + minx) / 2.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
930 cy = (maxy + miny) / 2.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
931 cz = (maxz + minz) / 2.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
932
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
933 /* calculate unitizing scale factor */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
934 scale = 2.0 / glmMax(glmMax(w, h), d);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
935
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
936 /* translate around center then scale */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
937 for (i = 1; i <= model->numvertices; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
938 model->vertices[3 * i + 0] -= cx;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
939 model->vertices[3 * i + 1] -= cy;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
940 model->vertices[3 * i + 2] -= cz;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
941 //charlie, i took this out, i just want to center
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
942 /*
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
943 model->vertices[3 * i + 0] *= scale;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
944 model->vertices[3 * i + 1] *= scale;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
945 model->vertices[3 * i + 2] *= scale;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
946 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
947 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
948
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
949 return scale;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
950 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
951
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
952 /* glmDimensions: Calculates the dimensions (width, height, depth) of
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
953 * a model.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
954 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
955 * model - initialized GLMmodel structure
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
956 * dimensions - array of 3 GLfloats (GLfloat dimensions[3])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
957 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
958 GLvoid
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
959 glmDimensions(GLMmodel* model, GLfloat* dimensions)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
960 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
961 GLuint i;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
962 GLfloat maxx, minx, maxy, miny, maxz, minz;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
963
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
964 assert(model);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
965 assert(model->vertices);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
966 assert(dimensions);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
967
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
968 /* get the max/mins */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
969 maxx = minx = model->vertices[3 + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
970 maxy = miny = model->vertices[3 + 1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
971 maxz = minz = model->vertices[3 + 2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
972 for (i = 1; i <= model->numvertices; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
973 if (maxx < model->vertices[3 * i + 0])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
974 maxx = model->vertices[3 * i + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
975 if (minx > model->vertices[3 * i + 0])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
976 minx = model->vertices[3 * i + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
977
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
978 if (maxy < model->vertices[3 * i + 1])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
979 maxy = model->vertices[3 * i + 1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
980 if (miny > model->vertices[3 * i + 1])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
981 miny = model->vertices[3 * i + 1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
982
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
983 if (maxz < model->vertices[3 * i + 2])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
984 maxz = model->vertices[3 * i + 2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
985 if (minz > model->vertices[3 * i + 2])
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
986 minz = model->vertices[3 * i + 2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
987 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
988
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
989 /* calculate model width, height, and depth */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
990 dimensions[0] = glmAbs(maxx) + glmAbs(minx);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
991 dimensions[1] = glmAbs(maxy) + glmAbs(miny);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
992 dimensions[2] = glmAbs(maxz) + glmAbs(minz);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
993 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
994
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
995 /* glmScale: Scales a model by a given amount.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
996 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
997 * model - properly initialized GLMmodel structure
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
998 * scale - scalefactor (0.5 = half as large, 2.0 = twice as large)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
999 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1000 GLvoid
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1001 glmScale(GLMmodel* model, GLfloat scale)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1002 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1003 GLuint i;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1004
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1005 for (i = 1; i <= model->numvertices; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1006 model->vertices[3 * i + 0] *= scale;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1007 model->vertices[3 * i + 1] *= scale;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1008 model->vertices[3 * i + 2] *= scale;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1009 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1010 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1011
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1012 /* glmReverseWinding: Reverse the polygon winding for all polygons in
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1013 * this model. Default winding is counter-clockwise. Also changes
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1014 * the direction of the normals.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1015 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1016 * model - properly initialized GLMmodel structure
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1017 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1018 GLvoid
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1019 glmReverseWinding(GLMmodel* model)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1020 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1021 GLuint i, swap;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1022
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1023 assert(model);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1024
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1025 for (i = 0; i < model->numtriangles; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1026 swap = T(i).vindices[0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1027 T(i).vindices[0] = T(i).vindices[2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1028 T(i).vindices[2] = swap;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1029
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1030 if (model->numnormals) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1031 swap = T(i).nindices[0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1032 T(i).nindices[0] = T(i).nindices[2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1033 T(i).nindices[2] = swap;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1034 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1035
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1036 if (model->numtexcoords) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1037 swap = T(i).tindices[0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1038 T(i).tindices[0] = T(i).tindices[2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1039 T(i).tindices[2] = swap;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1040 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1041 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1042
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1043 /* reverse facet normals */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1044 for (i = 1; i <= model->numfacetnorms; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1045 model->facetnorms[3 * i + 0] = -model->facetnorms[3 * i + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1046 model->facetnorms[3 * i + 1] = -model->facetnorms[3 * i + 1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1047 model->facetnorms[3 * i + 2] = -model->facetnorms[3 * i + 2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1048 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1049
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1050 /* reverse vertex normals */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1051 for (i = 1; i <= model->numnormals; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1052 model->normals[3 * i + 0] = -model->normals[3 * i + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1053 model->normals[3 * i + 1] = -model->normals[3 * i + 1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1054 model->normals[3 * i + 2] = -model->normals[3 * i + 2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1055 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1056 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1057
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1058 /* glmFacetNormals: Generates facet normals for a model (by taking the
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1059 * cross product of the two vectors derived from the sides of each
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1060 * triangle). Assumes a counter-clockwise winding.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1061 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1062 * model - initialized GLMmodel structure
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1063 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1064 GLvoid
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1065 glmFacetNormals(GLMmodel* model)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1066 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1067 GLuint i;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1068 GLfloat u[3];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1069 GLfloat v[3];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1070
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1071 assert(model);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1072 assert(model->vertices);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1073
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1074 /* clobber any old facetnormals */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1075 if (model->facetnorms)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1076 free(model->facetnorms);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1077
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1078 /* allocate memory for the new facet normals */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1079 model->numfacetnorms = model->numtriangles;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1080 model->facetnorms = (GLfloat*)malloc(sizeof(GLfloat) *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1081 3 * (model->numfacetnorms + 1));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1082
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1083 for (i = 0; i < model->numtriangles; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1084 model->triangles[i].findex = i+1;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1085
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1086 u[0] = model->vertices[3 * T(i).vindices[1] + 0] -
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1087 model->vertices[3 * T(i).vindices[0] + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1088 u[1] = model->vertices[3 * T(i).vindices[1] + 1] -
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1089 model->vertices[3 * T(i).vindices[0] + 1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1090 u[2] = model->vertices[3 * T(i).vindices[1] + 2] -
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1091 model->vertices[3 * T(i).vindices[0] + 2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1092
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1093 v[0] = model->vertices[3 * T(i).vindices[2] + 0] -
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1094 model->vertices[3 * T(i).vindices[0] + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1095 v[1] = model->vertices[3 * T(i).vindices[2] + 1] -
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1096 model->vertices[3 * T(i).vindices[0] + 1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1097 v[2] = model->vertices[3 * T(i).vindices[2] + 2] -
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1098 model->vertices[3 * T(i).vindices[0] + 2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1099
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1100 glmCross(u, v, &model->facetnorms[3 * (i+1)]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1101 glmNormalize(&model->facetnorms[3 * (i+1)]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1102 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1103 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1104
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1105 /* glmVertexNormals: Generates smooth vertex normals for a model.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1106 * First builds a list of all the triangles each vertex is in. Then
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1107 * loops through each vertex in the the list averaging all the facet
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1108 * normals of the triangles each vertex is in. Finally, sets the
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1109 * normal index in the triangle for the vertex to the generated smooth
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1110 * normal. If the dot product of a facet normal and the facet normal
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1111 * associated with the first triangle in the list of triangles the
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1112 * current vertex is in is greater than the cosine of the angle
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1113 * parameter to the function, that facet normal is not added into the
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1114 * average normal calculation and the corresponding vertex is given
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1115 * the facet normal. This tends to preserve hard edges. The angle to
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1116 * use depends on the model, but 90 degrees is usually a good start.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1117 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1118 * model - initialized GLMmodel structure
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1119 * angle - maximum angle (in degrees) to smooth across
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1120 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1121 GLvoid
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1122 glmVertexNormals(GLMmodel* model, GLfloat angle)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1123 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1124 GLMnode* node;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1125 GLMnode* tail;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1126 GLMnode** members;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1127 GLfloat* normals;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1128 GLuint numnormals;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1129 GLfloat average[3];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1130 GLfloat dot, cos_angle;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1131 GLuint i, avg;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1132
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1133 assert(model);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1134 assert(model->facetnorms);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1135
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1136 /* calculate the cosine of the angle (in degrees) */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1137 cos_angle = cos(angle * M_PI / 180.0);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1138
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1139 /* nuke any previous normals */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1140 if (model->normals)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1141 free(model->normals);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1142
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1143 /* allocate space for new normals */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1144 model->numnormals = model->numtriangles * 3; /* 3 normals per triangle */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1145 model->normals = (GLfloat*)malloc(sizeof(GLfloat)* 3* (model->numnormals+1));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1146
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1147 /* allocate a structure that will hold a linked list of triangle
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1148 indices for each vertex */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1149 members = (GLMnode**)malloc(sizeof(GLMnode*) * (model->numvertices + 1));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1150 for (i = 1; i <= model->numvertices; i++)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1151 members[i] = NULL;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1152
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1153 /* for every triangle, create a node for each vertex in it */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1154 for (i = 0; i < model->numtriangles; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1155 node = (GLMnode*)malloc(sizeof(GLMnode));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1156 node->index = i;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1157 node->next = members[T(i).vindices[0]];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1158 members[T(i).vindices[0]] = node;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1159
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1160 node = (GLMnode*)malloc(sizeof(GLMnode));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1161 node->index = i;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1162 node->next = members[T(i).vindices[1]];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1163 members[T(i).vindices[1]] = node;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1164
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1165 node = (GLMnode*)malloc(sizeof(GLMnode));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1166 node->index = i;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1167 node->next = members[T(i).vindices[2]];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1168 members[T(i).vindices[2]] = node;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1169 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1170
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1171 /* calculate the average normal for each vertex */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1172 numnormals = 1;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1173 for (i = 1; i <= model->numvertices; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1174 /* calculate an average normal for this vertex by averaging the
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1175 facet normal of every triangle this vertex is in */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1176 node = members[i];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1177 if (!node)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1178 fprintf(stderr, "glmVertexNormals(): vertex w/o a triangle\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1179 average[0] = 0.0; average[1] = 0.0; average[2] = 0.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1180 avg = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1181 while (node) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1182 /* only average if the dot product of the angle between the two
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1183 facet normals is greater than the cosine of the threshold
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1184 angle -- or, said another way, the angle between the two
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1185 facet normals is less than (or equal to) the threshold angle */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1186 dot = glmDot(&model->facetnorms[3 * T(node->index).findex],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1187 &model->facetnorms[3 * T(members[i]->index).findex]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1188 if (dot > cos_angle) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1189 node->averaged = GL_TRUE;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1190 average[0] += model->facetnorms[3 * T(node->index).findex + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1191 average[1] += model->facetnorms[3 * T(node->index).findex + 1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1192 average[2] += model->facetnorms[3 * T(node->index).findex + 2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1193 avg = 1; /* we averaged at least one normal! */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1194 } else {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1195 node->averaged = GL_FALSE;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1196 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1197 node = node->next;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1198 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1199
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1200 if (avg) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1201 /* normalize the averaged normal */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1202 glmNormalize(average);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1203
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1204 /* add the normal to the vertex normals list */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1205 model->normals[3 * numnormals + 0] = average[0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1206 model->normals[3 * numnormals + 1] = average[1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1207 model->normals[3 * numnormals + 2] = average[2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1208 avg = numnormals;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1209 numnormals++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1210 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1211
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1212 /* set the normal of this vertex in each triangle it is in */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1213 node = members[i];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1214 while (node) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1215 if (node->averaged) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1216 /* if this node was averaged, use the average normal */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1217 if (T(node->index).vindices[0] == i)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1218 T(node->index).nindices[0] = avg;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1219 else if (T(node->index).vindices[1] == i)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1220 T(node->index).nindices[1] = avg;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1221 else if (T(node->index).vindices[2] == i)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1222 T(node->index).nindices[2] = avg;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1223 } else {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1224 /* if this node wasn't averaged, use the facet normal */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1225 model->normals[3 * numnormals + 0] =
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1226 model->facetnorms[3 * T(node->index).findex + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1227 model->normals[3 * numnormals + 1] =
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1228 model->facetnorms[3 * T(node->index).findex + 1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1229 model->normals[3 * numnormals + 2] =
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1230 model->facetnorms[3 * T(node->index).findex + 2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1231 if (T(node->index).vindices[0] == i)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1232 T(node->index).nindices[0] = numnormals;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1233 else if (T(node->index).vindices[1] == i)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1234 T(node->index).nindices[1] = numnormals;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1235 else if (T(node->index).vindices[2] == i)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1236 T(node->index).nindices[2] = numnormals;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1237 numnormals++;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1238 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1239 node = node->next;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1240 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1241 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1242
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1243 model->numnormals = numnormals - 1;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1244
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1245 /* free the member information */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1246 for (i = 1; i <= model->numvertices; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1247 node = members[i];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1248 while (node) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1249 tail = node;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1250 node = node->next;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1251 free(tail);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1252 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1253 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1254 free(members);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1255
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1256 /* pack the normals array (we previously allocated the maximum
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1257 number of normals that could possibly be created (numtriangles *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1258 3), so get rid of some of them (usually alot unless none of the
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1259 facet normals were averaged)) */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1260 normals = model->normals;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1261 model->normals = (GLfloat*)malloc(sizeof(GLfloat)* 3* (model->numnormals+1));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1262 for (i = 1; i <= model->numnormals; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1263 model->normals[3 * i + 0] = normals[3 * i + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1264 model->normals[3 * i + 1] = normals[3 * i + 1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1265 model->normals[3 * i + 2] = normals[3 * i + 2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1266 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1267 free(normals);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1268 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1269
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1270
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1271 /* glmLinearTexture: Generates texture coordinates according to a
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1272 * linear projection of the texture map. It generates these by
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1273 * linearly mapping the vertices onto a square.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1274 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1275 * model - pointer to initialized GLMmodel structure
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1276 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1277 GLvoid
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1278 glmLinearTexture(GLMmodel* model)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1279 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1280 GLMgroup *group;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1281 GLfloat dimensions[3];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1282 GLfloat x, y, scalefactor;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1283 GLuint i;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1284
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1285 assert(model);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1286
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1287 if (model->texcoords)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1288 free(model->texcoords);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1289 model->numtexcoords = model->numvertices;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1290 model->texcoords=(GLfloat*)malloc(sizeof(GLfloat)*2*(model->numtexcoords+1));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1291
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1292 glmDimensions(model, dimensions);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1293 scalefactor = 2.0 /
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1294 glmAbs(glmMax(glmMax(dimensions[0], dimensions[1]), dimensions[2]));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1295
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1296 /* do the calculations */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1297 for(i = 1; i <= model->numvertices; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1298 x = model->vertices[3 * i + 0] * scalefactor;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1299 y = model->vertices[3 * i + 2] * scalefactor;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1300 model->texcoords[2 * i + 0] = (x + 1.0) / 2.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1301 model->texcoords[2 * i + 1] = (y + 1.0) / 2.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1302 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1303
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1304 /* go through and put texture coordinate indices in all the triangles */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1305 group = model->groups;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1306 while(group) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1307 for(i = 0; i < group->numtriangles; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1308 T(group->triangles[i]).tindices[0] = T(group->triangles[i]).vindices[0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1309 T(group->triangles[i]).tindices[1] = T(group->triangles[i]).vindices[1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1310 T(group->triangles[i]).tindices[2] = T(group->triangles[i]).vindices[2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1311 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1312 group = group->next;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1313 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1314
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1315 #if 0
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1316 printf("glmLinearTexture(): generated %d linear texture coordinates\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1317 model->numtexcoords);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1318 #endif
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1319 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1320
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1321 /* glmSpheremapTexture: Generates texture coordinates according to a
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1322 * spherical projection of the texture map. Sometimes referred to as
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1323 * spheremap, or reflection map texture coordinates. It generates
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1324 * these by using the normal to calculate where that vertex would map
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1325 * onto a sphere. Since it is impossible to map something flat
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1326 * perfectly onto something spherical, there is distortion at the
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1327 * poles. This particular implementation causes the poles along the X
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1328 * axis to be distorted.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1329 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1330 * model - pointer to initialized GLMmodel structure
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1331 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1332 GLvoid
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1333 glmSpheremapTexture(GLMmodel* model)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1334 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1335 GLMgroup* group;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1336 GLfloat theta, phi, rho, x, y, z, r;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1337 GLuint i;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1338
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1339 assert(model);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1340 assert(model->normals);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1341
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1342 if (model->texcoords)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1343 free(model->texcoords);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1344 model->numtexcoords = model->numnormals;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1345 model->texcoords=(GLfloat*)malloc(sizeof(GLfloat)*2*(model->numtexcoords+1));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1346
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1347 for (i = 1; i <= model->numnormals; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1348 z = model->normals[3 * i + 0]; /* re-arrange for pole distortion */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1349 y = model->normals[3 * i + 1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1350 x = model->normals[3 * i + 2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1351 r = sqrt((x * x) + (y * y));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1352 rho = sqrt((r * r) + (z * z));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1353
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1354 if(r == 0.0) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1355 theta = 0.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1356 phi = 0.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1357 } else {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1358 if(z == 0.0)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1359 phi = 3.14159265 / 2.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1360 else
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1361 phi = acos(z / rho);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1362
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1363 if(y == 0.0)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1364 theta = 3.141592365 / 2.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1365 else
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1366 theta = asin(y / r) + (3.14159265 / 2.0);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1367 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1368
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1369 model->texcoords[2 * i + 0] = theta / 3.14159265;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1370 model->texcoords[2 * i + 1] = phi / 3.14159265;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1371 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1372
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1373 /* go through and put texcoord indices in all the triangles */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1374 group = model->groups;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1375 while(group) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1376 for (i = 0; i < group->numtriangles; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1377 T(group->triangles[i]).tindices[0] = T(group->triangles[i]).nindices[0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1378 T(group->triangles[i]).tindices[1] = T(group->triangles[i]).nindices[1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1379 T(group->triangles[i]).tindices[2] = T(group->triangles[i]).nindices[2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1380 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1381 group = group->next;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1382 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1383 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1384
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1385 /* glmDelete: Deletes a GLMmodel structure.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1386 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1387 * model - initialized GLMmodel structure
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1388 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1389 GLvoid
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1390 glmDelete(GLMmodel* model)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1391 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1392 GLMgroup* group;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1393 GLuint i;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1394
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1395 assert(model);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1396
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1397 if (model->pathname) free(model->pathname);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1398 if (model->mtllibname) free(model->mtllibname);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1399 if (model->vertices) free(model->vertices);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1400 if (model->normals) free(model->normals);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1401 if (model->texcoords) free(model->texcoords);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1402 if (model->facetnorms) free(model->facetnorms);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1403 if (model->triangles) free(model->triangles);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1404 if (model->materials) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1405 for (i = 0; i < model->nummaterials; i++)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1406 free(model->materials[i].name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1407 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1408 free(model->materials);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1409 while(model->groups) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1410 group = model->groups;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1411 model->groups = model->groups->next;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1412 free(group->name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1413 free(group->triangles);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1414 free(group);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1415 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1416
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1417 free(model);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1418 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1419
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1420 /* glmReadOBJ: Reads a model description from a Wavefront .OBJ file.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1421 * Modelurns a pointer to the created object which should be free'd with
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1422 * glmDelete().
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1423 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1424 * filename - name of the file containing the Wavefront .OBJ format data.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1425 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1426 GLMmodel*
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1427 glmReadOBJ(char* filename)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1428 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1429 GLMmodel* model;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1430 FILE* file;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1431 printf("*");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1432 fflush(NULL);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1433
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1434 /* open the file */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1435 file = fopen(filename, "r");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1436 if (!file) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1437 fprintf(stderr, "glmReadOBJ() failed: can't open data file \"%s\".\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1438 filename);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1439 exit(1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1440 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1441
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1442 /* allocate a new model */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1443 model = (GLMmodel*)malloc(sizeof(GLMmodel));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1444 model->pathname = strdup(filename);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1445 model->mtllibname = NULL;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1446 model->numvertices = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1447 model->vertices = NULL;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1448 model->numnormals = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1449 model->normals = NULL;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1450 model->numtexcoords = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1451 model->texcoords = NULL;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1452 model->numfacetnorms = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1453 model->facetnorms = NULL;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1454 model->numtriangles = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1455 model->triangles = NULL;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1456 model->nummaterials = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1457 model->materials = NULL;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1458 model->numgroups = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1459 model->groups = NULL;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1460 model->position[0] = 0.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1461 model->position[1] = 0.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1462 model->position[2] = 0.0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1463
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1464 /* make a first pass through the file to get a count of the number
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1465 of vertices, normals, texcoords & triangles */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1466 glmFirstPass(model, file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1467
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1468 /* allocate memory */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1469 model->vertices = (GLfloat*)malloc(sizeof(GLfloat) *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1470 3 * (model->numvertices + 1));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1471 model->triangles = (GLMtriangle*)malloc(sizeof(GLMtriangle) *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1472 model->numtriangles);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1473 if (model->numnormals) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1474 model->normals = (GLfloat*)malloc(sizeof(GLfloat) *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1475 3 * (model->numnormals + 1));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1476 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1477 if (model->numtexcoords) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1478 model->texcoords = (GLfloat*)malloc(sizeof(GLfloat) *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1479 2 * (model->numtexcoords + 1));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1480 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1481
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1482 /* rewind to beginning of file and read in the data this pass */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1483 rewind(file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1484
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1485 glmSecondPass(model, file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1486
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1487 /* close the file */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1488 fclose(file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1489
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1490 return model;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1491 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1492
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1493 /* glmWriteOBJ: Writes a model description in Wavefront .OBJ format to
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1494 * a file.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1495 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1496 * model - initialized GLMmodel structure
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1497 * filename - name of the file to write the Wavefront .OBJ format data to
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1498 * mode - a bitwise or of values describing what is written to the file
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1499 * GLM_NONE - render with only vertices
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1500 * GLM_FLAT - render with facet normals
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1501 * GLM_SMOOTH - render with vertex normals
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1502 * GLM_TEXTURE - render with texture coords
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1503 * GLM_COLOR - render with colors (color material)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1504 * GLM_MATERIAL - render with materials
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1505 * GLM_COLOR and GLM_MATERIAL should not both be specified.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1506 * GLM_FLAT and GLM_SMOOTH should not both be specified.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1507 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1508 GLvoid
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1509 glmWriteOBJ(GLMmodel* model, char* filename, GLuint mode)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1510 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1511 GLuint i;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1512 FILE* file;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1513 GLMgroup* group;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1514
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1515 assert(model);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1516
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1517 /* do a bit of warning */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1518 if (mode & GLM_FLAT && !model->facetnorms) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1519 printf("glmWriteOBJ() warning: flat normal output requested "
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1520 "with no facet normals defined.\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1521 mode &= ~GLM_FLAT;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1522 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1523 if (mode & GLM_SMOOTH && !model->normals) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1524 printf("glmWriteOBJ() warning: smooth normal output requested "
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1525 "with no normals defined.\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1526 mode &= ~GLM_SMOOTH;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1527 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1528 if (mode & GLM_TEXTURE && !model->texcoords) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1529 printf("glmWriteOBJ() warning: texture coordinate output requested "
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1530 "with no texture coordinates defined.\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1531 mode &= ~GLM_TEXTURE;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1532 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1533 if (mode & GLM_FLAT && mode & GLM_SMOOTH) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1534 printf("glmWriteOBJ() warning: flat normal output requested "
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1535 "and smooth normal output requested (using smooth).\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1536 mode &= ~GLM_FLAT;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1537 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1538 if (mode & GLM_COLOR && !model->materials) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1539 printf("glmWriteOBJ() warning: color output requested "
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1540 "with no colors (materials) defined.\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1541 mode &= ~GLM_COLOR;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1542 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1543 if (mode & GLM_MATERIAL && !model->materials) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1544 printf("glmWriteOBJ() warning: material output requested "
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1545 "with no materials defined.\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1546 mode &= ~GLM_MATERIAL;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1547 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1548 if (mode & GLM_COLOR && mode & GLM_MATERIAL) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1549 printf("glmWriteOBJ() warning: color and material output requested "
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1550 "outputting only materials.\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1551 mode &= ~GLM_COLOR;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1552 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1553
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1554
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1555 /* open the file */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1556 file = fopen(filename, "w");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1557 if (!file) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1558 fprintf(stderr, "glmWriteOBJ() failed: can't open file \"%s\" to write.\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1559 filename);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1560 exit(1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1561 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1562
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1563 /* spit out a header */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1564 fprintf(file, "# \n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1565 fprintf(file, "# Wavefront OBJ generated by GLM library\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1566 fprintf(file, "# \n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1567 fprintf(file, "# GLM library\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1568 fprintf(file, "# Nate Robins\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1569 fprintf(file, "# ndr@pobox.com\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1570 fprintf(file, "# http://www.pobox.com/~ndr\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1571 fprintf(file, "# \n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1572
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1573 if (mode & GLM_MATERIAL && model->mtllibname) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1574 fprintf(file, "\nmtllib %s\n\n", model->mtllibname);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1575 glmWriteMTL(model, filename, model->mtllibname);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1576 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1577
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1578 /* spit out the vertices */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1579 fprintf(file, "\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1580 fprintf(file, "# %d vertices\n", model->numvertices);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1581 for (i = 1; i <= model->numvertices; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1582 fprintf(file, "v %f %f %f\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1583 model->vertices[3 * i + 0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1584 model->vertices[3 * i + 1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1585 model->vertices[3 * i + 2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1586 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1587
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1588 /* spit out the smooth/flat normals */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1589 if (mode & GLM_SMOOTH) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1590 fprintf(file, "\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1591 fprintf(file, "# %d normals\n", model->numnormals);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1592 for (i = 1; i <= model->numnormals; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1593 fprintf(file, "vn %f %f %f\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1594 model->normals[3 * i + 0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1595 model->normals[3 * i + 1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1596 model->normals[3 * i + 2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1597 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1598 } else if (mode & GLM_FLAT) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1599 fprintf(file, "\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1600 fprintf(file, "# %d normals\n", model->numfacetnorms);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1601 for (i = 1; i <= model->numnormals; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1602 fprintf(file, "vn %f %f %f\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1603 model->facetnorms[3 * i + 0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1604 model->facetnorms[3 * i + 1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1605 model->facetnorms[3 * i + 2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1606 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1607 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1608
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1609 /* spit out the texture coordinates */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1610 if (mode & GLM_TEXTURE) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1611 fprintf(file, "\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1612 fprintf(file, "# %d texcoords\n", model->texcoords);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1613 for (i = 1; i <= model->numtexcoords; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1614 fprintf(file, "vt %f %f\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1615 model->texcoords[2 * i + 0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1616 model->texcoords[2 * i + 1]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1617 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1618 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1619
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1620 fprintf(file, "\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1621 fprintf(file, "# %d groups\n", model->numgroups);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1622 fprintf(file, "# %d faces (triangles)\n", model->numtriangles);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1623 fprintf(file, "\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1624
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1625 group = model->groups;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1626 while(group) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1627 fprintf(file, "g %s\n", group->name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1628 if (mode & GLM_MATERIAL)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1629 fprintf(file, "usemtl %s\n", model->materials[group->material].name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1630 for (i = 0; i < group->numtriangles; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1631 if (mode & GLM_SMOOTH && mode & GLM_TEXTURE) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1632 fprintf(file, "f %d/%d/%d %d/%d/%d %d/%d/%d\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1633 T(group->triangles[i]).vindices[0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1634 T(group->triangles[i]).nindices[0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1635 T(group->triangles[i]).tindices[0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1636 T(group->triangles[i]).vindices[1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1637 T(group->triangles[i]).nindices[1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1638 T(group->triangles[i]).tindices[1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1639 T(group->triangles[i]).vindices[2],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1640 T(group->triangles[i]).nindices[2],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1641 T(group->triangles[i]).tindices[2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1642 } else if (mode & GLM_FLAT && mode & GLM_TEXTURE) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1643 fprintf(file, "f %d/%d %d/%d %d/%d\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1644 T(group->triangles[i]).vindices[0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1645 T(group->triangles[i]).findex,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1646 T(group->triangles[i]).vindices[1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1647 T(group->triangles[i]).findex,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1648 T(group->triangles[i]).vindices[2],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1649 T(group->triangles[i]).findex);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1650 } else if (mode & GLM_TEXTURE) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1651 fprintf(file, "f %d/%d %d/%d %d/%d\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1652 T(group->triangles[i]).vindices[0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1653 T(group->triangles[i]).tindices[0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1654 T(group->triangles[i]).vindices[1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1655 T(group->triangles[i]).tindices[1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1656 T(group->triangles[i]).vindices[2],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1657 T(group->triangles[i]).tindices[2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1658 } else if (mode & GLM_SMOOTH) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1659 fprintf(file, "f %d//%d %d//%d %d//%d\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1660 T(group->triangles[i]).vindices[0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1661 T(group->triangles[i]).nindices[0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1662 T(group->triangles[i]).vindices[1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1663 T(group->triangles[i]).nindices[1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1664 T(group->triangles[i]).vindices[2],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1665 T(group->triangles[i]).nindices[2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1666 } else if (mode & GLM_FLAT) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1667 fprintf(file, "f %d//%d %d//%d %d//%d\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1668 T(group->triangles[i]).vindices[0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1669 T(group->triangles[i]).findex,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1670 T(group->triangles[i]).vindices[1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1671 T(group->triangles[i]).findex,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1672 T(group->triangles[i]).vindices[2],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1673 T(group->triangles[i]).findex);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1674 } else {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1675 fprintf(file, "f %d %d %d\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1676 T(group->triangles[i]).vindices[0],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1677 T(group->triangles[i]).vindices[1],
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1678 T(group->triangles[i]).vindices[2]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1679 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1680 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1681 fprintf(file, "\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1682 group = group->next;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1683 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1684
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1685 fclose(file);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1686 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1687
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1688 /* glmDraw: Renders the model to the current OpenGL context using the
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1689 * mode specified.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1690 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1691 * model - initialized GLMmodel structure
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1692 * mode - a bitwise OR of values describing what is to be rendered.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1693 * GLM_NONE - render with only vertices
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1694 * GLM_FLAT - render with facet normals
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1695 * GLM_SMOOTH - render with vertex normals
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1696 * GLM_TEXTURE - render with texture coords
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1697 * GLM_COLOR - render with colors (color material)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1698 * GLM_MATERIAL - render with materials
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1699 * GLM_COLOR and GLM_MATERIAL should not both be specified.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1700 * GLM_FLAT and GLM_SMOOTH should not both be specified.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1701 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1702 GLvoid
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1703 glmDraw(GLMmodel* model, GLuint mode)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1704 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1705 static GLuint i;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1706 static GLMgroup* group;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1707 static GLMtriangle* triangle;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1708 static GLMmaterial* material;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1709
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1710 assert(model);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1711 assert(model->vertices);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1712
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1713 /* do a bit of warning */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1714 if (mode & GLM_FLAT && !model->facetnorms) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1715 printf("glmDraw() warning: flat render mode requested "
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1716 "with no facet normals defined.\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1717 mode &= ~GLM_FLAT;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1718 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1719 if (mode & GLM_SMOOTH && !model->normals) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1720 printf("glmDraw() warning: smooth render mode requested "
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1721 "with no normals defined.\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1722 mode &= ~GLM_SMOOTH;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1723 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1724 if (mode & GLM_TEXTURE && !model->texcoords) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1725 printf("glmDraw() warning: texture render mode requested "
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1726 "with no texture coordinates defined.\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1727 mode &= ~GLM_TEXTURE;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1728 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1729 if (mode & GLM_FLAT && mode & GLM_SMOOTH) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1730 printf("glmDraw() warning: flat render mode requested "
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1731 "and smooth render mode requested (using smooth).\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1732 mode &= ~GLM_FLAT;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1733 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1734 if (mode & GLM_COLOR && !model->materials) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1735 printf("glmDraw() warning: color render mode requested "
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1736 "with no materials defined.\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1737 mode &= ~GLM_COLOR;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1738 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1739 if (mode & GLM_MATERIAL && !model->materials) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1740 printf("glmDraw() warning: material render mode requested "
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1741 "with no materials defined.\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1742 mode &= ~GLM_MATERIAL;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1743 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1744 if (mode & GLM_COLOR && mode & GLM_MATERIAL) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1745 printf("glmDraw() warning: color and material render mode requested "
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1746 "using only material mode.\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1747 mode &= ~GLM_COLOR;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1748 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1749 if (mode & GLM_COLOR)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1750 glEnable(GL_COLOR_MATERIAL);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1751 else if (mode & GLM_MATERIAL)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1752 glDisable(GL_COLOR_MATERIAL);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1753
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1754 /* perhaps this loop should be unrolled into material, color, flat,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1755 smooth, etc. loops? since most cpu's have good branch prediction
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1756 schemes (and these branches will always go one way), probably
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1757 wouldn't gain too much? */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1758
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1759 group = model->groups;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1760 while (group) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1761 if (mode & GLM_MATERIAL) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1762 material = &model->materials[group->material];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1763 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material->ambient);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1764 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material->diffuse);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1765 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material->specular);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1766 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, material->shininess);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1767 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1768
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1769 if (mode & GLM_COLOR) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1770 glColor3fv(material->diffuse);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1771 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1772
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1773 glBegin(GL_TRIANGLES);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1774 for (i = 0; i < group->numtriangles; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1775 triangle = &T(group->triangles[i]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1776
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1777 if (mode & GLM_FLAT)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1778 glNormal3fv(&model->facetnorms[3 * triangle->findex]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1779
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1780 if (mode & GLM_SMOOTH)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1781 glNormal3fv(&model->normals[3 * triangle->nindices[0]]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1782 if (mode & GLM_TEXTURE)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1783 glTexCoord2fv(&model->texcoords[2 * triangle->tindices[0]]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1784 glVertex3fv(&model->vertices[3 * triangle->vindices[0]]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1785
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1786 if (mode & GLM_SMOOTH)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1787 glNormal3fv(&model->normals[3 * triangle->nindices[1]]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1788 if (mode & GLM_TEXTURE)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1789 glTexCoord2fv(&model->texcoords[2 * triangle->tindices[1]]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1790 glVertex3fv(&model->vertices[3 * triangle->vindices[1]]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1791
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1792 if (mode & GLM_SMOOTH)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1793 glNormal3fv(&model->normals[3 * triangle->nindices[2]]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1794 if (mode & GLM_TEXTURE)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1795 glTexCoord2fv(&model->texcoords[2 * triangle->tindices[2]]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1796 glVertex3fv(&model->vertices[3 * triangle->vindices[2]]);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1797
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1798 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1799 glEnd();
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1800
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1801 group = group->next;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1802 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1803 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1804
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1805 /* glmList: Generates and modelurns a display list for the model using
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1806 * the mode specified.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1807 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1808 * model - initialized GLMmodel structure
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1809 * mode - a bitwise OR of values describing what is to be rendered.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1810 * GLM_NONE - render with only vertices
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1811 * GLM_FLAT - render with facet normals
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1812 * GLM_SMOOTH - render with vertex normals
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1813 * GLM_TEXTURE - render with texture coords
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1814 * GLM_COLOR - render with colors (color material)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1815 * GLM_MATERIAL - render with materials
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1816 * GLM_COLOR and GLM_MATERIAL should not both be specified.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1817 * GLM_FLAT and GLM_SMOOTH should not both be specified. */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1818 GLuint
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1819 glmList(GLMmodel* model, GLuint mode)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1820 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1821 GLuint list;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1822
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1823 list = glGenLists(1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1824 glNewList(list, GL_COMPILE);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1825 glmDraw(model, mode);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1826 glEndList();
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1827
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1828 return list;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1829 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1830
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1831 /* glmWeld: eliminate (weld) vectors that are within an epsilon of
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1832 * each other.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1833 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1834 * model - initialized GLMmodel structure
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1835 * epsilon - maximum difference between vertices
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1836 * ( 0.00001 is a good start for a unitized model)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1837 *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1838 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1839 GLvoid
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1840 glmWeld(GLMmodel* model, GLfloat epsilon)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1841 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1842 GLfloat* vectors;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1843 GLfloat* copies;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1844 GLuint numvectors;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1845 GLuint i;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1846
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1847 /* vertices */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1848 numvectors = model->numvertices;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1849 vectors = model->vertices;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1850 copies = glmWeldVectors(vectors, &numvectors, epsilon);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1851
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1852 #if 0
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1853 printf("glmWeld(): %d redundant vertices.\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1854 model->numvertices - numvectors - 1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1855 #endif
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1856
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1857 for (i = 0; i < model->numtriangles; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1858 T(i).vindices[0] = (GLuint)vectors[3 * T(i).vindices[0] + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1859 T(i).vindices[1] = (GLuint)vectors[3 * T(i).vindices[1] + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1860 T(i).vindices[2] = (GLuint)vectors[3 * T(i).vindices[2] + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1861 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1862
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1863 /* free space for old vertices */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1864 free(vectors);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1865
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1866 /* allocate space for the new vertices */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1867 model->numvertices = numvectors;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1868 model->vertices = (GLfloat*)malloc(sizeof(GLfloat) *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1869 3 * (model->numvertices + 1));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1870
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1871 /* copy the optimized vertices into the actual vertex list */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1872 for (i = 1; i <= model->numvertices; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1873 model->vertices[3 * i + 0] = copies[3 * i + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1874 model->vertices[3 * i + 1] = copies[3 * i + 1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1875 model->vertices[3 * i + 2] = copies[3 * i + 2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1876 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1877
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1878 free(copies);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1879 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1880
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1881
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1882 #if 0
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1883 /* normals */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1884 if (model->numnormals) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1885 numvectors = model->numnormals;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1886 vectors = model->normals;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1887 copies = glmOptimizeVectors(vectors, &numvectors);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1888
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1889 printf("glmOptimize(): %d redundant normals.\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1890 model->numnormals - numvectors);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1891
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1892 for (i = 0; i < model->numtriangles; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1893 T(i).nindices[0] = (GLuint)vectors[3 * T(i).nindices[0] + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1894 T(i).nindices[1] = (GLuint)vectors[3 * T(i).nindices[1] + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1895 T(i).nindices[2] = (GLuint)vectors[3 * T(i).nindices[2] + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1896 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1897
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1898 /* free space for old normals */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1899 free(vectors);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1900
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1901 /* allocate space for the new normals */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1902 model->numnormals = numvectors;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1903 model->normals = (GLfloat*)malloc(sizeof(GLfloat) *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1904 3 * (model->numnormals + 1));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1905
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1906 /* copy the optimized vertices into the actual vertex list */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1907 for (i = 1; i <= model->numnormals; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1908 model->normals[3 * i + 0] = copies[3 * i + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1909 model->normals[3 * i + 1] = copies[3 * i + 1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1910 model->normals[3 * i + 2] = copies[3 * i + 2];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1911 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1912
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1913 free(copies);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1914 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1915
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1916 /* texcoords */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1917 if (model->numtexcoords) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1918 numvectors = model->numtexcoords;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1919 vectors = model->texcoords;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1920 copies = glmOptimizeVectors(vectors, &numvectors);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1921
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1922 printf("glmOptimize(): %d redundant texcoords.\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1923 model->numtexcoords - numvectors);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1924
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1925 for (i = 0; i < model->numtriangles; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1926 for (j = 0; j < 3; j++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1927 T(i).tindices[j] = (GLuint)vectors[3 * T(i).tindices[j] + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1928 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1929 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1930
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1931 /* free space for old texcoords */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1932 free(vectors);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1933
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1934 /* allocate space for the new texcoords */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1935 model->numtexcoords = numvectors;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1936 model->texcoords = (GLfloat*)malloc(sizeof(GLfloat) *
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1937 2 * (model->numtexcoords + 1));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1938
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1939 /* copy the optimized vertices into the actual vertex list */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1940 for (i = 1; i <= model->numtexcoords; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1941 model->texcoords[2 * i + 0] = copies[2 * i + 0];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1942 model->texcoords[2 * i + 1] = copies[2 * i + 1];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1943 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1944
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1945 free(copies);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1946 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1947
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1948 #endif
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1949
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1950 #if 0
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1951 /* look for unused vertices */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1952 /* look for unused normals */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1953 /* look for unused texcoords */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1954 for (i = 1; i <= model->numvertices; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1955 for (j = 0; j < model->numtriangles; i++) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1956 if (T(j).vindices[0] == i ||
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1957 T(j).vindices[1] == i ||
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1958 T(j).vindices[1] == i)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1959 break;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1960 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1961 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1962 #endif