MatP* pivot_world_transform(const struct Pivot* pivot, Mat world_transform) { Mat translation = {0}; mat_translate(NULL, pivot->position, translation); Mat rotation = {0}; quat_to_mat(pivot->orientation, rotation); mat_mul(rotation, translation, world_transform); return world_transform; }
int extGetNextRecord(double *in, int numIn, double *out, int numOut) { double inrtl_body[3][3]; /* Inertial to body transformation matrix */ double q_inrtl_body[4]; /* Inertial to body quaternian */ static int status = 0; static int a[3]; /* Extracted Euler angle sequence */ FILE *pipe; char i[10]; char j[10]; char k[10]; /* Call Tcl GUI to choose angle sequence */ if (status == 0) { if ((pipe = popen("/user3/test_acct/Tcl/choose_sequence.tcl", "r")) != NULL) { fscanf(pipe, "%s %s %s\n", i, j, k); } status = 1; pclose(pipe); a[0] = atoi(i); a[1] = atoi(j); a[2] = atoi(k); } /* Set initial matrix with input from SIM. Run matrix functions. */ q_inrtl_body[0] = in[0]; q_inrtl_body[1] = in[1]; q_inrtl_body[2] = in[2]; q_inrtl_body[3] = in[3]; quat_to_mat(inrtl_body, q_inrtl_body); /* Call external programs according to angle sequence */ if (a[0] == ROLL && a[1] == PITCH && a[2] == YAW) euler123(out, inrtl_body, 1, out, "", 0); else if (a[0] == ROLL && a[1] == YAW && a[2] == PITCH) euler132(out, inrtl_body, 1, out, "", 0); else if (a[0] == PITCH && a[1] == ROLL && a[2] == YAW) euler213(out, inrtl_body, 1, out, "", 0); else if (a[0] == PITCH && a[1] == YAW && a[2] == ROLL) euler231(out, inrtl_body, 1, out, "", 0); else if (a[0] == YAW && a[1] == ROLL && a[2] == PITCH) euler312(out, inrtl_body, 1, out, "", 0); else if (a[0] == YAW && a[1] == PITCH && a[2] == ROLL) euler321(out, inrtl_body, 1, out, "", 0); return (0); }
void inertia_triangle(double *idiag, double *quat, double mass, double *inertia) { double p[3][3],ptrans[3][3],itemp[3][3],tensor[3][3]; quat_to_mat(quat,p); quat_to_mat_trans(quat,ptrans); diag_times3(idiag,ptrans,itemp); times3(p,itemp,tensor); inertia[0] = tensor[0][0]; inertia[1] = tensor[1][1]; inertia[2] = tensor[2][2]; inertia[3] = tensor[1][2]; inertia[4] = tensor[0][2]; inertia[5] = tensor[0][1]; }
void disp(void) { mat4_t xform; quat_to_mat(xform, rot); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(pos.x, pos.y, pos.z); glMultMatrixf((float*)xform); draw_cube(); glutSwapBuffers(); }
void inertia_ellipsoid(double *radii, double *quat, double mass, double *inertia) { double p[3][3],ptrans[3][3],itemp[3][3],tensor[3][3]; double idiag[3]; quat_to_mat(quat,p); quat_to_mat_trans(quat,ptrans); idiag[0] = 0.2*mass * (radii[1]*radii[1] + radii[2]*radii[2]); idiag[1] = 0.2*mass * (radii[0]*radii[0] + radii[2]*radii[2]); idiag[2] = 0.2*mass * (radii[0]*radii[0] + radii[1]*radii[1]); diag_times3(idiag,ptrans,itemp); times3(p,itemp,tensor); inertia[0] = tensor[0][0]; inertia[1] = tensor[1][1]; inertia[2] = tensor[2][2]; inertia[3] = tensor[1][2]; inertia[4] = tensor[0][2]; inertia[5] = tensor[0][1]; }
int32_t main(int32_t argc, char *argv[]) { if( init_sdl2() ) { return 1; } SDL_Window* window; sdl2_window("test-shading", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, &window); SDL_GLContext* context; sdl2_glcontext(3, 2, window, (Color){0, 0, 0, 255}, &context); if( init_shader() ) { return 1; } if( init_vbo() ) { return 1; } if( init_canvas(1280,720) ) { return 1; } canvas_create("global_dynamic_canvas", 1280, 720, &global_dynamic_canvas); canvas_create("global_static_canvas", 1280, 720, &global_static_canvas); struct Vbo test_vbo = {0}; vbo_create(&test_vbo); vbo_add_buffer(&test_vbo, SHADER_ATTRIBUTE_VERTEX, 3, GL_FLOAT, GL_STATIC_DRAW); struct Ibo test_ibo = {0}; ibo_create(GL_TRIANGLES, GL_UNSIGNED_INT, GL_STATIC_DRAW, &test_ibo); struct VboMesh test_mesh1 = {0}; vbo_mesh_create(&test_vbo, &test_ibo, &test_mesh1); float triangle1[3*3] = {1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0}; vbo_mesh_append_attributes(&test_mesh1, SHADER_ATTRIBUTE_VERTEX, 3, GL_FLOAT, 3, &triangle1); vbo_mesh_print(&test_mesh1); struct VboMesh test_mesh2 = {0}; vbo_mesh_create(&test_vbo, &test_ibo, &test_mesh2); float triangle2[3*3] = {4.0, 4.0, 4.0, 5.0, 5.0, 5.0, 6.0, 6.0, 6.0}; vbo_mesh_append_attributes(&test_mesh2, SHADER_ATTRIBUTE_VERTEX, 3, GL_FLOAT, 3, &triangle2); vbo_mesh_print(&test_mesh2); struct Vbo vbo = {0}; vbo_create(&vbo); vbo_add_buffer(&vbo, SHADER_ATTRIBUTE_VERTEX, 3, GL_FLOAT, GL_STATIC_DRAW); vbo_add_buffer(&vbo, SHADER_ATTRIBUTE_VERTEX_NORMAL, 3, GL_FLOAT, GL_STATIC_DRAW); vbo_add_buffer(&vbo, SHADER_ATTRIBUTE_VERTEX_COLOR, 4, GL_UNSIGNED_BYTE, GL_STATIC_DRAW); vbo_add_buffer(&vbo, SHADER_ATTRIBUTE_SMOOTH_NORMAL, NORMAL_SIZE, GL_FLOAT, GL_STATIC_DRAW); struct Ibo ibo = {0}; ibo_create(GL_TRIANGLES, GL_UNSIGNED_INT, GL_STATIC_DRAW, &ibo); struct SolidTetrahedron tetrahedron = {0}; solid_tetrahedron_create(1.0, (Color){255, 0, 0, 255}, &tetrahedron); solid_optimize((struct Solid*)&tetrahedron); struct SolidTetrahedron tetrahedron2 = {0}; solid_tetrahedron_create(1.0, (Color){255, 0, 255, 255}, &tetrahedron2); solid_optimize((struct Solid*)&tetrahedron2); struct SolidBox cube = {0}; solid_cube_create(1.0, (Color){0, 255, 0, 255}, &cube); solid_optimize((struct Solid*)&cube); struct SolidBox cube2 = {0}; solid_cube_create(1.0, (Color){0, 255, 255, 255}, &cube2); solid_optimize((struct Solid*)&cube2); struct VboMesh tetrahedron_mesh; vbo_mesh_create_from_solid((struct Solid*)&tetrahedron, &vbo, &ibo, &tetrahedron_mesh); struct VboMesh cube_mesh = {0}; vbo_mesh_create_from_solid((struct Solid*)&cube, &vbo, &ibo, &cube_mesh); struct VboMesh tetrahedron_mesh2; vbo_mesh_create_from_solid((struct Solid*)&tetrahedron2, &vbo, &ibo, &tetrahedron_mesh2); struct VboMesh cube_mesh2 = {0}; vbo_mesh_create_from_solid((struct Solid*)&cube2, &vbo, &ibo, &cube_mesh2); vbo_mesh_print(&tetrahedron_mesh); vbo_mesh_print(&tetrahedron_mesh2); vbo_mesh_print(&cube_mesh); vbo_mesh_print(&cube_mesh2); struct Shader flat_shader = {0}; shader_create(&flat_shader); shader_attach(&flat_shader, GL_VERTEX_SHADER, "prefix.vert", 1, "flat_shading.vert"); shader_attach(&flat_shader, GL_FRAGMENT_SHADER, "prefix.frag", 1, "flat_shading.frag"); shader_make_program(&flat_shader, "flat_shader"); Vec4f light_direction = { 0.2, -0.5, -1.0 }; shader_set_uniform_3f(&flat_shader, flat_shader.program, SHADER_UNIFORM_LIGHT_DIRECTION, 3, GL_FLOAT, light_direction); Color ambiance = {50, 25, 150, 255 }; shader_set_uniform_4f(&flat_shader, flat_shader.program, SHADER_UNIFORM_AMBIENT_LIGHT, 4, GL_UNSIGNED_BYTE, ambiance); struct Arcball arcball = {0}; arcball_create(window, (Vec4f){0.0,8.0,8.0,1.0}, (Vec4f){0.0,0.0,0.0,1.0}, 0.1, 100.0, &arcball); Mat identity; mat_identity(identity); Quat grid_rotation = {0}; quat_from_vec_pair((Vec4f){0.0, 0.0, 1.0, 1.0}, (Vec4f){0.0, 1.0, 0.0, 1.0}, grid_rotation); Mat grid_transform = {0}; quat_to_mat(grid_rotation, grid_transform); draw_grid(&global_static_canvas, 0, grid_transform, (Color){127, 127, 127, 255}, 0.03f, 12.0f, 12.0f, 12); while (true) { SDL_Event event; while( SDL_PollEvent(&event) ) { switch (event.type) { case SDL_QUIT: goto done; case SDL_KEYDOWN: { SDL_KeyboardEvent* key_event = (SDL_KeyboardEvent*)&event; if(key_event->keysym.scancode == SDL_SCANCODE_ESCAPE) { goto done; } break; } } arcball_event(&arcball, event); } sdl2_gl_set_swap_interval(1); ogl_debug( glClearDepth(1.0f); glClearColor(.0f, .0f, .0f, 1.0f); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); ); Mat transform = {0}; mat_translate(identity, (float[4]){ -1.0, 1.0, -1.0, 1.0 }, transform);
int main(int argc, char *argv[]) { (void)argc; (void)argv; #ifdef __SSE__ printf("SSE "); #endif #ifdef __SSE2__ printf("SSE2 "); #endif #ifdef __SSE3__ printf("SSE3 "); #endif #ifdef __SSE4__ printf("SSE4 "); #endif #ifdef __SSE4_1__ printf("SSE4.1 "); #endif #ifdef __SSE4_2__ printf("SSE4.2 "); #endif #ifdef __AVX__ printf("AVX "); #endif #ifdef __FMA4__ printf("FMA4 "); #endif printf("\n"); printv(vec(1, 2, 3, 4)); printv(vzero()); printm(mzero()); printm(midentity()); vec4 a = { 1, 2, 3, 4 }, b = { 5, 6, 7, 8 }; printv(a); printv(b); printf("\nshuffles:\n"); printv(vshuffle(a, a, 0, 1, 2, 3)); printv(vshuffle(a, a, 3, 2, 1, 0)); printv(vshuffle(a, b, 0, 1, 0, 1)); printv(vshuffle(a, b, 2, 3, 2, 3)); printf("\ndot products:\n"); printv(vdot(a, b)); printv(vdot(b, a)); printv(vdot3(a, b)); printv(vdot3(b, a)); //vec4 blendmask = { 1, -1, 1, -1 }; //printv(vblend(x, y, blendmask)); vec4 x = { 1, 0, 0, 0 }, y = { 0, 1, 0, 0 }, z = { 0, 0, 1, 0 }, w = { 0, 0, 0, 1 }; printf("\ncross products:\n"); printv(vcross(x, y)); printv(vcross(y, x)); printv(vcross_scalar(x, y)); printv(vcross_scalar(y, x)); printf("\nquaternion products:\n"); printv(qprod(x, y)); printv(qprod(y, x)); printv(qprod_mad(x, y)); printv(qprod_mad(y, x)); printv(qprod_scalar(x, y)); printv(qprod_scalar(y, x)); printf("\nquaternion conjugates:\n"); printv(qconj(x)); printv(qconj(y)); printv(qconj(z)); printv(qconj(w)); printf("\nmat from quat:\n"); printm(quat_to_mat(w)); printm(quat_to_mat_mmul(w)); printm(quat_to_mat_scalar(w)); vec4 angles = { 0.0, 0.0, 0.0, 0.0 }; printf("\neuler to quaternion:\n"); printv(quat_euler(angles)); printv(quat_euler_scalar(angles)); printv(quat_euler_gems(angles)); printf("\neuler to matrix:\n"); printm(mat_euler(angles)); printm(mat_euler_scalar(angles)); printm(quat_to_mat(quat_euler(angles))); printf("\nperspective matrix:\n"); printm(mat_perspective_fovy(M_PI/4.0, 16.0/9.0, 0.1, 100.0)); printm(mat_perspective_fovy_inf_z(M_PI/4.0, 16.0/9.0, 0.1)); printm(mat_perspective_fovy_scalar(M_PI/4.0, 16.0/9.0, 0.1, 100.0)); printf("\northogonal matrix:\n"); printm(mat_ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)); printm(mat_ortho(-1.0, 2.0, -1.0, 2.0, -1.0, 2.0)); printf("\ntranslate matrix:\n"); printm(mtranslate(a)); printf("\nscale matrix:\n"); printm(mscale(a)); return 0; }
int32_t main(int32_t argc, char *argv[]) { printf("<<watchlist//>>\n"); if( init_sdl2() ) { return 1; } int32_t width = 1280; int32_t height = 720; SDL_Window* window; sdl2_window("cute3d: " __FILE__, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, &window); SDL_GLContext* context; sdl2_glcontext(3, 2, window, &context); if( init_shader() ) { return 1; } if( init_canvas(width, height) ) { return 1; } canvas_create("global_dynamic_canvas", &global_dynamic_canvas); struct Arcball arcball = {0}; arcball_create(width, height, (Vec4f){0.0,6.0,10.0,1.0}, (Vec4f){0.0,0.0,0.0,1.0}, 0.001f, 100.0, &arcball); struct GameTime time = {0}; gametime_create(1.0f / 60.0f, &time); Vec4f a = {0.0f, 0.0f, 1.0f}; Vec4f b = {1.0f, 0.0f, 1.0f}; draw_vec(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){25, 255, 25, 255}, 0.01f, a, (Vec3f){0.0f, 0.0f, 0.0f}, 1.0f, 1.0f); draw_vec(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){255, 25, 25, 255}, 0.01f, b, (Vec3f){0.0f, 0.0f, 0.0f}, 1.0f, 1.0f); Quat axis_angle_rot = {0}; quat_from_axis_angle((Vec4f)Y_AXIS, PI/4, axis_angle_rot); draw_quaternion(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){255, 255, 255, 255}, (Color){255, 0, 255, 255}, 0.01f, axis_angle_rot, 2.0f); vec_print("axis_angle_rot: ", axis_angle_rot); Vec4f axis_angle_result = {0}; vec_rotate(a, axis_angle_rot, axis_angle_result); draw_vec(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){255, 255, 25, 255}, 0.01f, axis_angle_result, (Vec3f){0.0f, 0.0f, 0.0f}, 1.0f, 2.0f); Vec4f axis = {0}; float angle = 0.0f; quat_to_axis_angle(axis_angle_rot, axis, &angle); Quat axis_angle_rot2 = {0}; quat_from_axis_angle(axis, angle, axis_angle_rot2); vec_print("axis_angle_rot2: ", axis_angle_rot2); Quat euler_angles_rot = {0}; quat_from_euler_angles(0.0f, PI/4, 0.0f, euler_angles_rot); vec_print("euler_angles_rot: ", euler_angles_rot); Vec4f euler_angles_result = {0}; vec_rotate(a, euler_angles_rot, euler_angles_result); draw_vec(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){25, 255, 255, 255}, 0.01f, euler_angles_result, (Vec3f){0.0f, 0.0f, 0.0f}, 1.0f, 3.0f); Quat vec_pair_rot = {0}; quat_from_vec_pair(a, b, vec_pair_rot); vec_print("vec_pair_rot: ", vec_pair_rot); Vec4f vec_pair_result = {0}; vec_rotate(a, vec_pair_rot, vec_pair_result); draw_vec(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){255, 25, 255, 255}, 0.01f, vec_pair_result, (Vec3f){0.0f, 0.0f, 0.0f}, 1.0f, 4.0f); Mat xaxis_control = {0}; xaxis_control[0] = 1; xaxis_control[4] = 0; xaxis_control[8] = 0; xaxis_control[12] = 0; xaxis_control[1] = 0; xaxis_control[5] = cosf(PI/4); xaxis_control[9] = -sinf(PI/4); xaxis_control[13] = 0; xaxis_control[2] = 0; xaxis_control[6] = sinf(PI/4); xaxis_control[10] = cosf(PI/4); xaxis_control[14] = 0; xaxis_control[3] = 0; xaxis_control[7] = 0; xaxis_control[11] = 0; xaxis_control[15] = 1; mat_print("xaxis_control: ", xaxis_control); Quat xaxis_rot = {0}; quat_from_axis_angle((Vec4f)X_AXIS, PI/4, xaxis_rot); Mat xaxis_mat = {0}; quat_to_mat(xaxis_rot, xaxis_mat); mat_print("xaxis_mat: ", xaxis_mat); Mat yaxis_control = {0}; yaxis_control[0] = cosf(PI/4); yaxis_control[4] = 0; yaxis_control[8] = sinf(PI/4); yaxis_control[12] = 0; yaxis_control[1] = 0; yaxis_control[5] = 1; yaxis_control[9] = 0; yaxis_control[13] = 0; yaxis_control[2] = -sinf(PI/4); yaxis_control[6] = 0; yaxis_control[10] = cosf(PI/4); yaxis_control[14] = 0; yaxis_control[3] = 0; yaxis_control[7] = 0; yaxis_control[11] = 0; yaxis_control[15] = 1; mat_print("yaxis_control: ", yaxis_control); Quat yaxis_rot = {0}; quat_from_axis_angle((Vec4f)Y_AXIS, PI/4, yaxis_rot); Mat yaxis_mat = {0}; quat_to_mat(yaxis_rot, yaxis_mat); mat_print("yaxis_mat: ", yaxis_mat); Mat zaxis_control = {0}; zaxis_control[0] = cosf(PI/4); zaxis_control[4] = -sinf(PI/4); zaxis_control[8] = 0; zaxis_control[12] = 0; zaxis_control[1] = sinf(PI/4); zaxis_control[5] = cosf(PI/4); zaxis_control[9] = 0; zaxis_control[13] = 0; zaxis_control[2] = 0; zaxis_control[6] = 0; zaxis_control[10] = 1; zaxis_control[14] = 0; zaxis_control[3] = 0; zaxis_control[7] = 0; zaxis_control[11] = 0; zaxis_control[15] = 1; mat_print("zaxis_control: ", zaxis_control); Quat zaxis_rot = {0}; quat_from_axis_angle((Vec4f)Z_AXIS, PI/4, zaxis_rot); Mat zaxis_mat = {0}; quat_to_mat(zaxis_rot, zaxis_mat); mat_print("zaxis_mat: ", zaxis_mat); draw_grid(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){120, 120, 120, 255}, 0.01f, 12.0f, 12.0f, 12); while(true) { SDL_Event event; while( sdl2_poll_event(&event) ) { if( sdl2_handle_quit(event) ) { goto done; } sdl2_handle_resize(event); arcball_handle_resize(&arcball, event); arcball_handle_mouse(&arcball, event); } sdl2_gl_set_swap_interval(0); ogl_debug( glClearDepth(1.0f); glClearColor(.0f, .0f, .0f, 1.0f); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); ); gametime_advance(&time, sdl2_time_delta()); gametime_integrate(&time); canvas_render_layers(&global_static_canvas, 0, MAX_CANVAS_LAYERS, &arcball.camera, (Mat)IDENTITY_MAT); sdl2_gl_swap_window(window); }
QuatP* qto_mat(const Quat q, Mat m) { quat_to_mat(q,m); return m; }