Added lookat function. Fixed various coordinate missmatches and issues. Fixed camera movement. Added some input functions. I need some way to log fps and find those hitches

This commit is contained in:
MitchellHansen
2016-12-30 21:02:04 -08:00
parent 58867415c7
commit bb9fab6305
8 changed files with 189 additions and 105 deletions

View File

@@ -33,22 +33,22 @@ int Camera::add_relative_impulse(DIRECTION impulse_direction, float speed) {
switch (impulse_direction) {
case DIRECTION::UP:
dir = sf::Vector2f(direction.y, direction.x + PI_F);
break;
case DIRECTION::DOWN:
case DIRECTION::FORWARD:
dir = sf::Vector2f(direction.y, direction.x);
break;
case DIRECTION::REARWARD:
dir = sf::Vector2f(direction.y, direction.x + PI_F);
break;
case DIRECTION::LEFT:
dir = sf::Vector2f(direction.y + PI_F + PI_F / 2, PI_F / 2);
break;
case DIRECTION::RIGHT:
dir = sf::Vector2f(direction.y + PI_F / 2, PI_F / 2);
break;
case DIRECTION::FORWARD:
case DIRECTION::UP:
dir = sf::Vector2f(direction.y, direction.x + PI_F / 2);
break;
case DIRECTION::REARWARD:
case DIRECTION::DOWN:
dir = sf::Vector2f(direction.y + PI_F, (direction.x * -1) + PI_F / 2 );
break;
@@ -65,10 +65,16 @@ int Camera::slew_camera(sf::Vector2f input) {
return 1;
}
void Camera::set_camera(sf::Vector2f input) {
direction = input;
}
void Camera::set_camera(sf::Vector3f input) {
direction = CartToNormalizedSphere(input);
}
int Camera::update(double delta_time) {
// so vector multiplication broke?
// have to do it component wise
double multiplier = 40;
position.x += static_cast<float>(movement.x * delta_time * multiplier);
@@ -80,6 +86,22 @@ int Camera::update(double delta_time) {
return 1;
}
void Camera::look_at_center() {
//std::cout << "X:" << position.x << std::endl;
//std::cout << "Y:" << position.y << std::endl;
//std::cout << "Z:" << position.z << std::endl;
//std::cout << "dx:" << direction.x << std::endl;
//std::cout << "dy:" << direction.y << std::endl;
direction = CartToNormalizedSphere(sf::Vector3f(75, 75, 75) - position);
//std::cout << "dx:" << direction.x << std::endl;
//std::cout << "dy:" << direction.y << std::endl;
}
sf::Vector2f* Camera::get_direction_pointer() {
return &direction;
}

View File

@@ -174,69 +174,40 @@ void Map::generate_octree() {
generate_children(sf::Vector3i(0, 0, 0), OCT_DIM/2);
DumpLog(&ss, "raw_output.txt");
std::stringstream sss;
for (int i = 0; i < (int)pow(2, 15); i++) {
PrettyPrintUINT64(a.dat[i], &sss);
sss << "\n";
}
DumpLog(&sss, "raw_data.txt");
a.print_block(0);
// levels defines how many levels to traverse before we hit raw data
// Will be the map width I presume. Will still need to handle how to swap in and out data.
// Possible have some upper static nodes that will stay full regardless of contents?
int levels = static_cast<int>(log2(64));
std::list<int> parent_stack;
int byte_pos = 0;
unsigned int parent = 0;
for (int i = 0; i < 16; i++) {
parent ^= 1 << i;
}
unsigned int leafmask = 255;
unsigned int validmask = leafmask << 8;
parent &= validmask;
parent &= leafmask;
std::cout << BitCount(parent & leafmask);
unsigned int children[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
}
void Map::load_unload(sf::Vector3i world_position) {
sf::Vector3i chunk_pos(world_to_chunk(world_position));
//Don't forget the middle chunk
if (chunk_map.find(chunk_pos) == chunk_map.end()) {
chunk_map[chunk_pos] = Chunk(5);
}
//sf::Vector3i chunk_pos(world_to_chunk(world_position));
//
////Don't forget the middle chunk
//if (chunk_map.find(chunk_pos) == chunk_map.end()) {
// chunk_map[chunk_pos] = Chunk(5);
//}
for (int x = chunk_pos.x - chunk_radius / 2; x < chunk_pos.x + chunk_radius / 2; x++) {
for (int y = chunk_pos.y - chunk_radius / 2; y < chunk_pos.y + chunk_radius / 2; y++) {
for (int z = chunk_pos.z - chunk_radius / 2; z < chunk_pos.z + chunk_radius / 2; z++) {
//for (int x = chunk_pos.x - chunk_radius / 2; x < chunk_pos.x + chunk_radius / 2; x++) {
// for (int y = chunk_pos.y - chunk_radius / 2; y < chunk_pos.y + chunk_radius / 2; y++) {
// for (int z = chunk_pos.z - chunk_radius / 2; z < chunk_pos.z + chunk_radius / 2; z++) {
if (chunk_map.find(sf::Vector3i(x, y, z)) == chunk_map.end()) {
chunk_map.emplace(sf::Vector3i(x, y, z), Chunk(rand() % 6));
//chunk_map[sf::Vector3i(x, y, z)] = Chunk(rand() % 6);
}
}
}
}
// if (chunk_map.find(sf::Vector3i(x, y, z)) == chunk_map.end()) {
// chunk_map.emplace(sf::Vector3i(x, y, z), Chunk(rand() % 6));
// //chunk_map[sf::Vector3i(x, y, z)] = Chunk(rand() % 6);
// }
// }
// }
//}
}
void Map::load_single(sf::Vector3i world_position) {
sf::Vector3i chunk_pos(world_to_chunk(world_position));
//sf::Vector3i chunk_pos(world_to_chunk(world_position));
//Don't forget the middle chunk
if (chunk_map.find(chunk_pos) == chunk_map.end()) {
chunk_map[chunk_pos] = Chunk(0);
}
////Don't forget the middle chunk
//if (chunk_map.find(chunk_pos) == chunk_map.end()) {
// chunk_map[chunk_pos] = Chunk(0);
//}
}
sf::Vector3i Map::getDimensions() {
@@ -245,16 +216,16 @@ sf::Vector3i Map::getDimensions() {
void Map::setVoxel(sf::Vector3i world_position, int val) {
load_single(world_position);
sf::Vector3i chunk_pos(world_to_chunk(world_position));
sf::Vector3i in_chunk_pos(
world_position.x % CHUNK_DIM,
world_position.y % CHUNK_DIM,
world_position.z % CHUNK_DIM
);
//load_single(world_position);
//sf::Vector3i chunk_pos(world_to_chunk(world_position));
//sf::Vector3i in_chunk_pos(
// world_position.x % CHUNK_DIM,
// world_position.y % CHUNK_DIM,
// world_position.z % CHUNK_DIM
//);
chunk_map.at(chunk_pos).voxel_data[in_chunk_pos.x + CHUNK_DIM * (in_chunk_pos.y + CHUNK_DIM * in_chunk_pos.z)]
= val;
//chunk_map.at(chunk_pos).voxel_data[in_chunk_pos.x + CHUNK_DIM * (in_chunk_pos.y + CHUNK_DIM * in_chunk_pos.z)]
// = val;
}
@@ -262,16 +233,3 @@ char Map::getVoxel(sf::Vector3i pos){
return voxel_data[pos.x + OCT_DIM * (pos.y + OCT_DIM * pos.z)];
}
void Chunk::set(int type) {
for (int i = 0; i < CHUNK_DIM * CHUNK_DIM * CHUNK_DIM; i++) {
voxel_data[i] = 0;
}
for (int x = 0; x < CHUNK_DIM; x+=2) {
for (int y = 0; y < CHUNK_DIM; y+=2) {
//list[x + dim.x * (y + dim.z * z)]
voxel_data[x + CHUNK_DIM * (y + CHUNK_DIM * 1)] = type;
}
}
}

View File

@@ -26,6 +26,8 @@ void Old_Map::generate_terrain() {
voxel_data[i] = 0;
}
set_voxel(sf::Vector3i(75, 75, 75), 5);
for (int i = 0; i < dimensions.x * dimensions.y; i++) {
height_map[i] = 0;
}

View File

@@ -65,8 +65,8 @@ sf::Texture window_texture;
int main() {
Map _map(sf::Vector3i(0, 0, 0));
_map.generate_octree();
//Map _map(sf::Vector3i(0, 0, 0));
//_map.generate_octree();
glewInit();
@@ -101,8 +101,8 @@ int main() {
rc->assign_map(map);
Camera *camera = new Camera(
sf::Vector3f(10, 11, 12),
sf::Vector2f(0.1f, 1.00f)
sf::Vector3f(50, 50, 50),
sf::Vector2f(0.0f, 1.5707f)
);
rc->assign_camera(camera);
@@ -126,6 +126,9 @@ int main() {
sf::Vector2f *dp = camera->get_direction_pointer();
debug_text cam_text_x(1, 30, &dp->x, "incli: ");
debug_text cam_text_y(2, 30, &dp->y, "asmth: ");
debug_text cam_text_pos_x(3, 30, &camera->get_position_pointer()->x, "x: ");
debug_text cam_text_pos_y(4, 30, &camera->get_position_pointer()->y, "y: ");
debug_text cam_text_pos_z(5, 30, &camera->get_position_pointer()->z, "z: ");
// ===========================
@@ -154,13 +157,19 @@ int main() {
window.close();
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Space) {
if (event.key.code == sf::Keyboard::M) {
if (mouse_enabled)
mouse_enabled = false;
else
mouse_enabled = true;
} if (event.key.code == sf::Keyboard::R) {
reset = true;
} if (event.key.code == sf::Keyboard::X) {
std::vector<float> tvf = sfml_get_float_input(&window);
if (tvf.size() == 3){
sf::Vector3f tv3(tvf.at(0), tvf.at(1), tvf.at(2));
camera->set_position(tv3);
}
}
}
}
@@ -170,6 +179,9 @@ int main() {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)) {
speed = 0.2f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::L)) {
camera->look_at_center();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) {
camera->add_relative_impulse(Camera::DIRECTION::DOWN, speed);
}
@@ -252,6 +264,10 @@ int main() {
cam_text_x.draw(&window);
cam_text_y.draw(&window);
cam_text_pos_x.draw(&window);
cam_text_pos_y.draw(&window);
cam_text_pos_z.draw(&window);
window.display();
}