Skip to content

Instantly share code, notes, and snippets.

@CapSel
Created May 3, 2018 17:15
Show Gist options
  • Save CapSel/9388b63396ddd14a2f44d20bdecbfc6b to your computer and use it in GitHub Desktop.
Save CapSel/9388b63396ddd14a2f44d20bdecbfc6b to your computer and use it in GitHub Desktop.
#include <math.h>
#include <assert.h>
#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_native_dialog.h>
int main() {
al_init();
al_install_keyboard();
al_init_native_dialog_addon();
al_init_primitives_addon();
al_set_new_display_flags(ALLEGRO_WINDOWED | ALLEGRO_OPENGL);
// al_set_new_display_option(ALLEGRO_REQUIRE, ALLEGRO_COMPATIBLE_DISPLAY, 1);
al_set_new_display_option(ALLEGRO_SUGGEST, ALLEGRO_DEPTH_SIZE, 16);
//al_set_new_display_option(ALLEGRO_REQUIRE, ALLEGRO_SAMPLE_BUFFERS, 1);
//al_set_new_display_option(ALLEGRO_REQUIRE, ALLEGRO_SAMPLES, 8);
ALLEGRO_DISPLAY* display = al_create_display(1024, 768);
assert(display);
al_set_target_bitmap(al_get_backbuffer(display));
al_clear_depth_buffer(1);
al_clear_to_color(al_map_rgb(0,0,0));
ALLEGRO_TRANSFORM camera;
al_build_camera_transform(&camera, 0, 128, -512, 0, 128, -511, 0, 1, 0);
ALLEGRO_TRANSFORM default_regular;
ALLEGRO_TRANSFORM default_projection;
al_copy_transform(&default_regular, al_get_current_transform());
al_copy_transform(&default_projection, al_get_current_projection_transform());
ALLEGRO_TRANSFORM projection;
float proportions = (float)1024 / (float)768;
float angle = 90;
float f = tanf((angle * ALLEGRO_PI / 180) / 2);
al_identity_transform(&projection);
al_perspective_transform(&projection,
-1 * proportions * f, 1 * f, 1,
1 * proportions * f, -1 * f, 10000);
al_set_render_state(ALLEGRO_DEPTH_TEST, 1);
al_use_projection_transform(&projection);
ALLEGRO_TRANSFORM rectangle_transform;
al_identity_transform(&rectangle_transform);
al_translate_transform_3d(&rectangle_transform, 0, 0, 0);
al_rotate_transform_3d(&rectangle_transform, 1, 0, 0, (float) ALLEGRO_PI / 2);
ALLEGRO_TRANSFORM camera_rectangle_transform;
al_copy_transform(&camera_rectangle_transform, &rectangle_transform);
al_compose_transform(&camera_rectangle_transform, &camera);
al_use_transform(&camera_rectangle_transform);
ALLEGRO_COLOR grey = al_map_rgba(100, 100, 100, 100);
al_draw_filled_rectangle(-128, -128, 128, 128, grey); // around (0,0,0)
al_use_transform(&default_regular);
al_use_projection_transform(&default_projection);
float x = 0;
float y = 0;
float z = 0;
al_transform_coordinates_3d(&camera_rectangle_transform, &x, &y, &z);
printf("vector 3d: x:%f y:%f z:%f\n", x, y, z);
al_transform_coordinates_3d_projective(&projection, &x, &y, &z);
printf("vector 3d: x:%f y:%f z:%f\n", x, y, z);
ALLEGRO_COLOR red = al_map_rgb_f(1,0,0);
al_draw_filled_rectangle(x - 4, y - 4, x + 4, y + 4, red);
x += 512;
y += 384;
printf("vector 3d: x:%f y:%f z:%f\n", x, y, z);
ALLEGRO_COLOR green = al_map_rgb_f(0,1,0);
al_draw_filled_rectangle(x - 4, y - 4, x + 4, y + 4, green);
al_flip_display();
while (true) sleep(1000);
al_destroy_display(display);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment