-
Notifications
You must be signed in to change notification settings - Fork 25
/
object.cpp
148 lines (136 loc) · 4.02 KB
/
object.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
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
/* Object
*
* Copyright (C) 2003-2004, Alexander Zaprjagaev <frustum@frustum.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "engine.h"
#include "frustum.h"
#include "bsp.h"
#include "position.h"
#include "shader.h"
#include "material.h"
#include "rigidbody.h"
#include "object.h"
Object::Object(int type) : type(type), rigidbody(NULL), is_identity(1),
num_opacities(0), opacities(NULL), num_transparents(0), transparents(NULL),
shadows(1), time(0), frame(0) {
}
Object::~Object() {
for(int i = 0; i < pos.num_sectors; i++) Bsp::sectors[pos.sectors[i]].removeObject(this);
if(rigidbody) delete rigidbody;
}
/*
*/
void Object::update(float ifps) {
time += ifps;
if(rigidbody) {
if(Engine::physic_toggle) rigidbody->simulate();
} else {
pos.update(time,this);
}
}
/*
*/
void Object::updatePos(const vec3 &p) {
for(int i = 0; i < pos.num_sectors; i++) Bsp::sectors[pos.sectors[i]].removeObject(this);
pos.radius = getRadius();
pos = p;
for(int i = 0; i < pos.num_sectors; i++) Bsp::sectors[pos.sectors[i]].addObject(this);
}
/*
*/
int Object::bindMaterial(const char *name,Material *material) {
int bind = 0;
for(int i = 0; i < getNumSurfaces(); i++) {
if(Engine::match(name,getSurfaceName(i))) {
int j;
materials[i] = material;
if(materials[i]->blend) {
for(j = 0; j < num_opacities; j++) if(opacities[j] == i) break;
if(j != num_opacities) {
for(; j < num_opacities - 1; j++) opacities[j] = opacities[j + 1];
num_opacities--;
}
for(j = 0; j < num_transparents; j++) if(transparents[j] == i) break;
if(j == num_transparents) transparents[num_transparents++] = i;
else transparents[j] = i;
} else {
for(j = 0; j < num_transparents; j++) if(transparents[j] == i) break;
if(j != num_transparents) {
for(; j < num_transparents - 1; j++) transparents[j] = transparents[j + 1];
num_transparents--;
}
for(j = 0; j < num_opacities; j++) if(opacities[j] == i) break;
if(j == num_opacities) opacities[num_opacities++] = i;
else opacities[j] = i;
}
bind = 1;
}
}
return bind;
}
/*
*/
void Object::setRigidBody(RigidBody *rigidbody) {
is_identity = 0;
this->rigidbody = rigidbody;
}
/*
*/
void Object::setShadows(int shadows) {
this->shadows = shadows;
}
/*
*/
void Object::set(const vec3 &p) {
is_identity = 0;
transform.translate(p);
itransform = transform.inverse();
updatePos(p);
if(rigidbody) rigidbody->set(p);
}
void Object::set(const mat4 &m) {
is_identity = 0;
transform = m;
itransform = transform.inverse();
updatePos(m * vec3(0,0,0));
if(rigidbody) rigidbody->set(m);
}
/*
*/
void Object::enable() {
if(is_identity) return;
old_modelview = Engine::modelview;
old_imodelview = Engine::imodelview;
old_transform = Engine::transform;
old_itransform = Engine::itransform;
Engine::modelview = Engine::modelview * transform;
Engine::imodelview = itransform * Engine::imodelview;
Engine::transform = Engine::transform * transform;
Engine::itransform = itransform * Engine::itransform;
glLoadMatrixf(Engine::modelview);
// new transformation
if(Shader::old_shader) Shader::old_shader->bind();
}
void Object::disable() {
if(is_identity) return;
Engine::modelview = old_modelview;
Engine::imodelview = old_imodelview;
Engine::transform = old_transform;
Engine::itransform = old_itransform;
glLoadMatrixf(Engine::modelview);
if(Shader::old_shader) Shader::old_shader->bind();
}