void Renderer::setupProjection(ALLEGRO_TRANSFORM *m) { int dw = al_get_display_width(dpy_); int dh = al_get_display_height(dpy_); // al_perspective_transform(m, -180 * dw / dh, -180, 1, // 180 * dw / dh, 180, 1000); // al_perspective_transform(m, -500, -500, 1, // 500, 500, 10000); double zNear = 0.5, zFar = 1000.0, fov = 90.0, aspect = dw / dh; double left, right; double bottom, top; top = tan (fov*ALLEGRO_PI/360.0)*zNear; bottom = -top; left = aspect*bottom; right = aspect*top; //NBT_Debug("rect: %f %f %f %f", left, top, right, bottom); al_perspective_transform(m, left, top, zNear, right, bottom, zFar); al_set_projection_transform(dpy_, m); }
/** Updates the camera. */ Camera * camera_update(Camera * self, double dt) { ALLEGRO_DISPLAY *display = al_get_current_display(); double dw = al_get_display_width(display); double dh = al_get_display_height(display); double f = tan(self->field_of_view / 2.0); /* Set up orthograĥic transform for UI, etc. */ al_identity_transform(&self->orthographic_transform); al_orthographic_transform(&self->orthographic_transform, 0, 0, -1, dw, dh, 1000); /* Perspective transform for the main screen 3D view. */ al_identity_transform(&self->perspective_transform); /* Back up camera a bit. */ /* Set up a nice 3D view. */ al_translate_transform_3d(&self->perspective_transform, 0, 0, -1); al_perspective_transform(&self->perspective_transform, -1 * dw / dh * f, f, 1, f * dw / dh, -f, 1000); /* al_perspective_transform(&projection, -1 * dw / dh * f, f, 1, f * dw / dh, -f, 1000); */ /* Set up the camera's position and view transform. */ /* al_build_camera_transform(&self->camera_transform, self->position.x, self->position.y, self->position.z, self->look.x , self->look.y , self->look.z , self->up.x , self->up.y , self->up.z ); */ al_identity_transform(&self->camera_transform); al_translate_transform_3d(&self->camera_transform, self->position.x, self->position.y, self->position.z); al_rotate_transform_3d(&self->camera_transform, 0, -1, 0, self->alpha); al_rotate_transform_3d(&self->camera_transform, -1, 0, 0, self->theta); /* Finally move at the set speed. */ self->position = vec3d_add(self->position, vec3d_mul(self->speed, dt)); return self; }
void setup_projection() { // setup the render settings al_set_render_state(ALLEGRO_DEPTH_TEST, 1); al_set_render_state(ALLEGRO_WRITE_MASK, ALLEGRO_MASK_DEPTH | ALLEGRO_MASK_RGBA); al_clear_depth_buffer(1); ALLEGRO_TRANSFORM t; camera.reverse_position_transform(&t); float aspect_ratio = (float)al_get_bitmap_height(backbuffer_sub_bitmap) / al_get_bitmap_width(backbuffer_sub_bitmap); al_perspective_transform(&t, -1, aspect_ratio, 1, 1, -aspect_ratio, 1000); //al_set_target_bitmap(backbuffer_sub_bitmap); << I don't think this is necessairy, it also occours just prior to this function al_use_projection_transform(&t); }
void set_projection(ALLEGRO_BITMAP *bitmap, ALLEGRO_TRANSFORM *t) { float aspect_ratio = (float)al_get_bitmap_height(bitmap) / al_get_bitmap_width(bitmap); al_perspective_transform(t, -1, aspect_ratio, 1, 1, -aspect_ratio, 100); al_use_projection_transform(t); }
int main(void) { ALLEGRO_VERTEX v[8]; ALLEGRO_DISPLAY * display; ALLEGRO_TRANSFORM perst; ALLEGRO_TRANSFORM camt; ALLEGRO_BITMAP * texture, * texture2; ALLEGRO_EVENT_QUEUE * queue; ALLEGRO_EVENT event; ALLEGRO_FONT * font; int busy = 1; float cx = 0; float cy = -2; // 128; float cz = 0; int face = 0; int hori = 0; float angle = 0; float theta = 0; float near = 2; float far = 8192; float zoom = 1; float scale = 1.0; float us = 20; float ratio = 480.0 / 640.0; al_init(); al_init_image_addon(); al_init_font_addon(); al_install_keyboard(); font = al_create_builtin_font(); queue = al_create_event_queue(); al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 16, ALLEGRO_SUGGEST); display = al_create_display(640, 480); al_register_event_source(queue, al_get_keyboard_event_source()); texture = al_load_bitmap("tile_1.png"); texture2 = al_load_bitmap("tile_2.png"); /* Allegro coordinates: +Y is down, +X is to the right, * and +Z comes "out of" the screen. * */ while (busy) { init_vertex(v+0, 0, 0, 0, 0, 0, 1, 0, 0, 1); init_vertex(v+1, 0, 0, us, 0, 256, 0, 1, 0, 1); init_vertex(v+2, us, 0, us, 256, 256, 0, 0, 1, 1); init_vertex(v+3, us, 0, 0, 256, 0, 1, 1, 0, 1); init_vertex(v+4, us, -us, 0, 256, 256, 1, 0, 1, 1); init_vertex(v+5, 0, -us, 0, 256, 0, 0, 1, 1, 1); init_vertex(v+6, 0, -us, us, 256, 256, 0, 0, 0, 1); init_vertex(v+7, 0, 0, us, 0, 256, 1, 1, 1, 1); al_identity_transform(&camt); al_scale_transform_3d(&camt, scale, scale, scale); al_translate_transform_3d(&camt, cx, cy, cz); angle = face * 0.125 * ALLEGRO_PI; al_rotate_transform_3d(&camt, 0, -1, 0, angle); theta = hori * 0.125 * ALLEGRO_PI; al_rotate_transform_3d(&camt, -1, 0, 0, theta); al_use_transform(&camt); // al_set_projection_transform(display, &perst); al_clear_to_color(al_map_rgb_f(0.75, 0.75, 0.95)); al_set_render_state(ALLEGRO_DEPTH_TEST, 1); al_clear_depth_buffer(far); al_identity_transform(&perst); al_perspective_transform(&perst, -1, ratio, near, 1, -ratio, far); al_use_projection_transform(&perst); al_draw_filled_rectangle(0, 0, 4, 5, al_map_rgb_f(0, 0.25, 0.25)); al_draw_prim(v, NULL, texture, 0, 8, ALLEGRO_PRIM_TRIANGLE_FAN); draw_textured_colored_rectangle_3d(0 , -us, 0, us, us, 0, 1.0, 1.0, texture2, al_map_rgb_f(1,1,1)); draw_textured_colored_rectangle_3d(0 , -us, 0, 0, us, us, 1.0, 1.0, texture2, al_map_rgb_f(1,1,1)); draw_textured_colored_rectangle_3d(0 , 0, 0, us, 0, us, 1.0, 1.0, texture2, al_map_rgb_f(1,1,1)); al_identity_transform(&perst); al_orthographic_transform(&perst, 0, 0, -1.0, 640, 480, 1.0); al_identity_transform(&camt); al_use_projection_transform(&perst); al_use_transform(&camt); al_draw_filled_rectangle(111, 22, 133, 44, al_map_rgb_f(0.25, 0.25, 0)); al_draw_multiline_textf(font, al_map_rgb_f(1,1,1), 10, 10, 620, 0, 0, "Coords: (%f %f %f)\nAngle: (%f %f)\nView: [%f %f %f %f]\nScale: %f", cx, cy, cz, angle, theta, near, far, zoom, scale); al_flip_display(); al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_KEY_DOWN) { switch (event.keyboard.keycode) { case ALLEGRO_KEY_RIGHT: cx += 8; break; case ALLEGRO_KEY_LEFT: cx -= 8; break; case ALLEGRO_KEY_UP: cy += 8; break; case ALLEGRO_KEY_DOWN: cy -= 8; break; case ALLEGRO_KEY_HOME: cz += 8; break; case ALLEGRO_KEY_END: cz -= 8; break; case ALLEGRO_KEY_R: face++; break; case ALLEGRO_KEY_L: face--; break; case ALLEGRO_KEY_H: hori++; break; case ALLEGRO_KEY_G: hori--; break; case ALLEGRO_KEY_N: near *= 2.0; break; case ALLEGRO_KEY_M: near /= 2.0; break; case ALLEGRO_KEY_F: far += 64; break; case ALLEGRO_KEY_V: far -= 64; break; case ALLEGRO_KEY_Z: zoom *= 2.0f; break; case ALLEGRO_KEY_S: zoom /= 2.0f; break; case ALLEGRO_KEY_A: scale *= 2.0f; break; case ALLEGRO_KEY_Q: scale /= 2.0f; break; case ALLEGRO_KEY_ESCAPE: busy = 0 ; break; default: break; } } } al_destroy_bitmap(texture); al_destroy_bitmap(texture2); return 0; }