-
Notifications
You must be signed in to change notification settings - Fork 0
/
World.cpp
101 lines (87 loc) · 2.67 KB
/
World.cpp
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
#include "StdAfx.h"
#include "World.h"
World::World() {
}
World::~World(void) {
if ( _array.buffer != 0 ) {
delete[] _array.buffer;
}
}
void World::remove(MID id) {
_array.remove(id);
}
MID World::create(const Vector3f& pos,MeshData* meshData,int shader) {
if ( _array.total == 0 ) {
allocate(512);
}
if ( _array.num + 1 >= _array.total ) {
allocate(_array.total * 2);
}
MID id = _array.create(pos,meshData,shader);
D3DXMATRIX t;
_array.translate(id,pos);
return id;
}
void World::allocate(int size) {
if ( size > _array.total ) {
MeshArray sad;
int sz = size * (sizeof(MeshArrayIndex) + sizeof(Vector3f) + sizeof(Vector3f) + sizeof(Vector3f) + sizeof(MID) + sizeof(D3DXMATRIX) + sizeof(MeshData*) + sizeof(int));
sad.buffer = new char[sz];
sad.total = size;
sad.num = 0;
sad.indices = (MeshArrayIndex*)(sad.buffer);
sad.ids = (MID*)(sad.indices + size);
sad.positions = (Vector3f*)(sad.ids + size);
sad.scales = (Vector3f*)(sad.positions + size);
sad.rotations = (Vector3f*)(sad.scales + size);
sad.worlds = (D3DXMATRIX*)(sad.rotations + size);
sad.data = (MeshData**)(sad.worlds + size);
sad.shaders = (int*)(sad.data + size);
if ( _array.buffer != 0 ) {
memcpy(sad.indices, _array.indices, _array.num * sizeof(MeshArrayIndex));
memcpy(sad.ids, _array.ids, _array.num * sizeof(MID));
memcpy(sad.positions, _array.positions, _array.num * sizeof(Vector3f));
memcpy(sad.scales, _array.scales, _array.num * sizeof(Vector3f));
memcpy(sad.rotations, _array.rotations, _array.num * sizeof(Vector3f));
memcpy(sad.worlds, _array.worlds, _array.num * sizeof(D3DXMATRIX));
memcpy(sad.data, _array.data, _array.num * sizeof(MeshData*));
memcpy(sad.shaders, _array.shaders, _array.num * sizeof(int*));
sad.free_dequeue = _array.free_dequeue;
sad.free_enqueue = _array.free_enqueue;
sad.num = _array.num;
delete[] _array.buffer;
}
else {
sad.clear();
}
_array = sad;
}
}
void World::tick(float dt) {
D3DXMATRIX t;
D3DXMATRIX s;
D3DXMATRIX rx;
D3DXMATRIX ry;
D3DXMATRIX rz;
for ( int j = 0; j < _array.num; ++j ) {
const Vector3f& v = _array.positions[j];
D3DXMatrixTranslation(&t, v.x,v.y,v.z);
const Vector3f& sz = _array.scales[j];
D3DXMatrixScaling(&s,sz.x,sz.y,sz.z);
D3DXMatrixRotationX(&rx,_array.rotations[j].x);
D3DXMatrixRotationY(&ry,_array.rotations[j].y);
D3DXMatrixRotationZ(&rz,_array.rotations[j].z);
_array.worlds[j] = s * rz * ry * rx * t;
}
}
void World::render() {
}
void World::translate(MID id,const Vector3f& p) {
_array.translate(id,p);
}
void World::scale(MID id,const Vector3f& s) {
_array.scale(id,s);
}
void World::rotate(MID id,const Vector3f& r) {
_array.rotate(id,r);
}