Properly re-initializing entity on module reload, fixed mesh merging

master
Martin Felis 2017-02-22 14:50:41 +01:00
parent 28c45e05f6
commit fcd44dc3b9
3 changed files with 20 additions and 11 deletions

View File

@ -31,12 +31,25 @@ CharacterEntity::CharacterEntity() {
cout << "Creating render entity mesh ..." << endl;
// Mesh* base_mesh = Mesh::sCreateUVSphere(45, 45, 0.9);
// Transform sub_transform =
// Transform::fromTransRot(
// Vector3f (4.f, 3.3f, 0.12f),
// Quaternion::fromEulerYXZ (Vector3f(1.f, 2.f, 0.f))
// );
// Mesh* quad = Mesh::sCreateCuboid(1.05, 0.1, 2.05);
//
// base_mesh->Merge (*quad, sub_transform.toMatrix());
// base_mesh->Update();
// delete quad;
// Build the snowman
entity->mesh.addMesh(
- 1,
Transform::fromTrans(
Vector3f (0.0f, 0.9 * 0.5f, 0.0f)
),
// base_mesh
Mesh::sCreateUVSphere(45, 45, 0.9)
);

View File

@ -912,8 +912,8 @@ void Mesh::Update() {
void Mesh::Merge (const Mesh& other, const Matrix44f &transform) {
for (int i = 0; i < other.mVertices.size(); ++i) {
mVertices.push_back (transform * other.mVertices[i]);
mNormals.push_back (Matrix33f(transform.block<3,3>(0,0)) * other.mNormals[i]);
mVertices.push_back (transform.transpose() * other.mVertices[i]);
mNormals.push_back (Matrix33f(transform.block<3,3>(0,0)).transpose() * other.mNormals[i]);
if (other.mColors.size() == other.mVertices.size())
mColors.push_back(other.mColors[i]);

View File

@ -175,8 +175,6 @@ void update_character(module_state* state, float dt) {
static struct module_state *module_init() {
std::cout << "Module init called" << std::endl;
module_state *state = (module_state*) malloc(sizeof(*state));
state->character = new CharacterEntity;
state->character->position = Vector3f (0.f, 0.f, 0.f);
state->modules_window_selected_index = -1;
fps_camera = true;
@ -205,6 +203,10 @@ static void module_reload(struct module_state *state, void* read_serializer) {
std::cout << "Module reload called. State: " << state << std::endl;
cout << "Creating render entity ..." << endl;
state->character = new CharacterEntity;
state->character->position = Vector3f (0.f, 0.f, 0.f);
// load the state of the entity
if (read_serializer != nullptr) {
module_serialize(state, static_cast<ReadSerializer*>(read_serializer));
@ -218,13 +220,7 @@ static void module_unload(struct module_state *state, void* write_serializer) {
}
// clean up
cout << "destroying render entity " << state->character->entity << endl;
if (!gRenderer->destroyEntity (state->character->entity)) {
cerr << "Warning: could not destroy entity " << state->character->entity << endl;
} else {
cout << "Successfully destroyed entity " << state->character->entity << endl;
}
delete state->character;
state->character->entity = nullptr;
std::cout << "TestModule unloaded. State: " << state << std::endl;