Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 27124fa29933072f1a6d711eb3586c919c8654a4 (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

/*
   model/vertexarray.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include "math/mathlib.h"
#include "model/vertexarray.h"
#include "sys/sys.h"

namespace model {

VertexArray *VertexArray::vertex_instance = 0 ;

VertexArray::VertexArray(size_t size)
{
	vertex_instance = this;
	vertex_size = size * 1024*1024;			// megabytes
	vertex_size = vertex_size / sizeof(float);	// sizeof float
	vertex_size = vertex_size / 4;			// 4 arrays
	
	vertex_vertex = (float *) malloc(vertex_size * sizeof(float));
	vertex_color = (float *) malloc(vertex_size * sizeof(float));
	vertex_normal = (float *) malloc(vertex_size * sizeof(float));
	vertex_texture = (float *) malloc(vertex_size * sizeof(float));

	con_print << "^BInitializing vertex array..." << std::endl;
	con_print << "  " << size << " Mb allocated" << std::endl;

	clear();
}

VertexArray::~VertexArray()
{
	free(vertex_vertex);
	free(vertex_normal);
	free(vertex_color);
	free(vertex_texture);
	
	vertex_instance = 0 ;
}

void VertexArray::clear()
{
	vertex_index = 0;
	vertex_overflow = false;

	memset(vertex_vertex, 0, sizeof(vertex_vertex));
	memset(vertex_color, 0, sizeof(vertex_color));
	memset(vertex_normal, 0, sizeof(vertex_normal));
	memset(vertex_texture, 0, sizeof(vertex_texture));

	add_sphere();
}

void VertexArray::add_sphere()
{
	// load sphere vertices into the VertexArray

	// build sin/cos table
	float  *sintable;
	float  *costable;

	sintable = new float[SPHERESEGMENTS];
	costable = new float[SPHERESEGMENTS];
	float d = 2 * M_PI / (SPHERESEGMENTS-1);

	for (int i=0; i < SPHERESEGMENTS; i++) {
		sintable[i] = sin( d * (float) i );
		costable[i] = cos ( d * (float) i );
	}

	// draw body
	math::Color white(1.0f, 1.0f, 1.0f);
	math::Vector3f v;
	math::Vector3f n;
	math::Vector3f tex;
	int count;

	// add sphere
	for (int j=0; j < (SPHERESEGMENTS-1) / 2; j++) {

		float r = sintable[j];
		float r1 = sintable[j+1];

		for (int i = 0; i < SPHERESEGMENTS; i++) {
			v = math::Vector3f(r*costable[i], r*sintable[i], costable[j]);
			n = v;
			n.normalize();
			tex.assign((float)i/(float)(SPHERESEGMENTS-1), -costable[j]/2 + 0.5f , 0);
			add_vertex(v, n, white, tex);
			
			v = math::Vector3f(r1*costable[i], r1*sintable[i], costable[j+1]);
			n = v;
			n.normalize();			
			tex.assign((float)i/(float)(SPHERESEGMENTS-1), -costable[j+1]/2 + 0.5f, 0);
			add_vertex(v, n, white, tex);
			count +=2;
		}
		
	}

	// add inside-out sphere
	for (int j=0; j < (SPHERESEGMENTS-1) / 2; j++) {

		float r = sintable[j];
		float r1 = sintable[j+1];

		
		for (int i = SPHERESEGMENTS -1 ; i >= 0; i--) {
			v = math::Vector3f(r*costable[i], r*sintable[i], costable[j]);
			n = v;
			n.normalize();
			tex.assign(1-(float)i/(float)(SPHERESEGMENTS-1), -costable[j]/2 + 0.5f , 0);
			add_vertex(v, n, white, tex);
			
			v = math::Vector3f(r1*costable[i], r1*sintable[i], costable[j+1]);
			n = v;
			n.normalize();			
			tex.assign(1-(float)i/(float)(SPHERESEGMENTS-1), -costable[j+1]/2 + 0.5f, 0);
			add_vertex(v, n, white, tex);
			count +=2;
		}
		
	}

	delete[] sintable;
	delete[] costable;
}

size_t VertexArray::add_vertex(math::Vector3f const &v, math::Vector3f const &n, math::Color const &color) {
	if (vertex_index + 3 >= vertex_size) {
		con_warn << "VertexArray overflow!" << std::endl;
		vertex_overflow = true;
		return 0;
	}

	for (int i = 0; i < 3; i ++) {
		vertex_vertex[vertex_index+i] = v[i];
		vertex_normal[vertex_index+i] = n[i];
	}

	vertex_color[vertex_index] = color.r;
	vertex_color[vertex_index+1] = color.g;
	vertex_color[vertex_index+2] = color.b;

	vertex_index += 3;

	return 1;
}

size_t VertexArray::add_vertex(math::Vector3f const &v, math::Vector3f const &n, math::Color const &color, math::Vector3f const &tex) {
	if (vertex_index + 3 >= vertex_size) {
		con_warn << "VertexArray overflow!" << std::endl;
		vertex_overflow = true;
		return 0;
	}

	for (int i = 0; i < 3; i ++) {
		vertex_vertex[vertex_index+i] = v[i];
		vertex_normal[vertex_index+i] = n[i];
		vertex_texture[vertex_index+i] = tex[i];
	}

	vertex_color[vertex_index] = color.r;
	vertex_color[vertex_index+1] = color.g;
	vertex_color[vertex_index+2] = color.b;

	vertex_index += 3;
	return 1;
}

}