summaryrefslogtreecommitdiff
path: root/test/sphere.cpp
blob: adcce4e951ff482ee6fe57121acca213dc5a5ae1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <cassert>
#include <cstdio>
#include "glu3.h"

class check_sphere : public GLUshapeConsumer {
public:
	check_sphere(const GLUshape &shape, double r) :
		pass(true), vert(0), prim(0), elts(0), r(r), mode(0)
	{
		primitive_count = shape.primitive_count();
		vertex_count = shape.vertex_count();
	}

	bool pass;
	unsigned vert;
	unsigned prim;
	unsigned elts;
	double r;
	GLenum mode;

	unsigned vertex_count;
	unsigned primitive_count;

	virtual void vertex(const GLUvec4 &position,
				 const GLUvec4 &normal,
				 const GLUvec4 &tangent,
				 const GLUvec4 &tex_coord)
	{
		printf("vert %u: {%.3f %.3f %.3f %.3f} ",
		       vert,
		       position.values[0],
		       position.values[1],
		       position.values[2],
		       position.values[3]);
		printf("{%.3f %.3f %.3f %.3f} ",
		       normal.values[0],
		       normal.values[1],
		       normal.values[2],
		       normal.values[3]);
		printf("{%.3f %.3f %.3f %.3f} ",
		       tangent.values[0],
		       tangent.values[1],
		       tangent.values[2],
		       tangent.values[3]);
		printf("{%.3f %.3f %.3f %.3f}\n",
		       tex_coord.values[0],
		       tex_coord.values[1],
		       tex_coord.values[2],
		       tex_coord.values[3]);

		if (fabs(gluLength(normal) - 1.0) > 0.00001) {
			printf("vert %u: bad normal |n| = %f\n",
			       vert,
			       gluLength(normal));
			pass = false;
		}

		if (fabs(gluLength(tangent) - 1.0) > 0.00001) {
			printf("vert %u: bad tangent |t| = %f\n",
			       vert,
			       gluLength(tangent));
			pass = false;
		}

		GLUvec4 p_dir(position);
		p_dir.values[3] = 0.0;

		if (fabs(gluLength(p_dir) - r) > 0.00001) {
			printf("vert %u: bad position |p| = %f vs. %f expected\n",
			       vert,
			       gluLength(p_dir),
			       r);
			pass = false;
		}

		if (position.values[3] != 1.0) {
			printf("vert %u: bad position W = %f\n",
			       vert,
			       position.values[3]);
			pass = false;
		}

		const GLUvec4 p_norm(gluNormalize(p_dir));
		if (fabs(gluDot3(p_norm, normal) - 1.0) > 0.00001) {
			printf("vert %u: bad position or normal direction\n",
			       vert);
			pass = false;
		}

		/* The tangent and the normal must always be orthogonal to
		 * each other, so the dot-product must be zero.
		 */
		if (fabs(gluDot3(tangent, normal)) > 0.000001) {
			printf("vert %u: bad tangent or normal direction\n",
			       vert);
			pass = false;
		}

		vert++;
	}

	virtual void begin_primitive(GLenum mode)
	{
		printf("prim %u: begin\n", prim);

		if (this->mode != 0) {
			printf("prim %u: bad primitive begin inside another "
			       "begin\n",
			       prim);
			pass = false;
		}

		if (prim == primitive_count) {
			printf("prim %u: too many primitives\n",
			       prim);
			pass = false;
		}

		switch (mode) {
		case GL_TRIANGLES:
		case GL_TRIANGLE_STRIP:
		case GL_TRIANGLE_FAN:
			break;

		default:
			printf("prim %u: invalid primitive mode 0x%04x\n",
			       prim, mode);
			pass = false;
			break;
		}

		this->mode = mode;
	}

	virtual void index(unsigned idx)
	{
		printf("prim %u, elt %u: %u\n", prim, elts, idx);

		if (idx >= vertex_count) {
			printf("prim %u, elt %u: bad elt %u > %u\n",
			       prim, elts, idx, vertex_count - 1);
			pass = false;
		}

		elts++;
	}

	virtual void end_primitive(void)
	{
		printf("prim %u: end\n", prim);

		if (this->mode == 0) {
			printf("prim %u: bad primitive end outside begin\n",
			       prim);
			pass = false;
		}

		mode = 0;
		prim++;
	}
};


int
main(int argc, char **argv)
{
	GLUsphere s(4.0, 5, 5);
	check_sphere c(s, 4.0);

	s.generate(& c);
	assert(c.pass);
	assert(c.vert == s.vertex_count());
	assert(c.prim == s.primitive_count());
	assert(c.elts == s.element_count());
	return 0;
}