il_vec3 il_vec3_normal(const il_vec3 a, il_vec3 vec) { if (!vec) { vec = il_vec3_new(); } float len = il_vec3_len(a); vec[0] = a[0]/len; vec[1] = a[1]/len; vec[2] = a[2]/len; return vec; }
il_vec3 il_vec4_to_vec3(const il_vec4 a, il_vec3 vec) { if (!vec) { vec = il_vec3_new(); } vec[0] = a[0] / a[3]; vec[1] = a[1] / a[3]; vec[2] = a[2] / a[3]; vec[3] = 1.0; return vec; }
il_vec3 il_vec3_cross(const il_vec3 a, const il_vec3 b, il_vec3 vec) { if (!vec) { vec = il_vec3_new(); } float n[3]; n[0] = a[1] * b[2] - b[1] * a[2]; n[1] = a[2] * b[0] - b[2] * a[0]; n[2] = a[0] * b[1] - b[0] * a[1]; vec[0] = n[0]; vec[1] = n[1]; vec[2] = n[2]; return vec; }
il_vec3 il_vec3_rotate(const il_vec3 a, const il_quat q, il_vec3 vec) { if (!vec) { vec = il_vec3_new(); } /* From glm/gtc/quaternion.inl typename detail::tquat<T>::value_type Two(2); detail::tvec3<T> uv, uuv; detail::tvec3<T> QuatVector(q.x, q.y, q.z); uv = glm::cross(QuatVector, v); uuv = glm::cross(QuatVector, uv); uv *= (Two * q.w); uuv *= Two; return v + uv + uuv; */ il_vec3 uv, uuv, qv; qv = il_vec3_set(NULL, q[0], q[1], q[2]); uv = il_vec3_cross(qv, a, NULL); uuv = il_vec3_cross(qv, uv, NULL); uv[0] *= 2*q[3]; uv[1] *= 2*q[3]; uv[2] *= 2*q[3]; uuv[0] *= 2; uuv[1] *= 2; uuv[2] *= 2; vec = il_vec3_add(a, uv, vec); vec = il_vec3_add(vec, uuv, vec); il_vec3_free(uv); il_vec3_free(uuv); il_vec3_free(qv); return vec; }
int main(int argc, char **argv) { demoLoad(argc, argv); Window window = createWindow("Teapots"); Graphics graphics(window); Graphics::Flags flags; if (!graphics.init(flags)) { return 1; } il_pos pos = il_pos_new(&graphics.space); il_pos_setPosition(&pos, il_vec3_new(0, -4, 0)); Teapot teapot; Teapots drawable(pos.id, teapot); graphics.drawables.push_back(&drawable); char *error; if (!teapot.build(graphics.rm, &error)) { il_error("Teapot: %s", error); free(error); return 1; } il_pos_setPosition(&graphics.space.camera, il_vec3_new(0, 0, 20)); il_pos lightp = il_pos_new(&graphics.space); il_pos_setPosition(&lightp, il_vec3_new(20, 3, 20)); ilG_light lightl; lightl.color = il_vec3_new(.8f*2, .7f*2, .2f*2); lightl.radius = 50; State state; unsigned lightp_id = lightp.id; state.sunlight_locs = &lightp_id; state.sunlight_lights = &lightl; state.sunlight_count = 1; typedef std::chrono::steady_clock clock; typedef std::chrono::duration<double> duration; clock::time_point start = clock::now(); while (1) { SDL_Event ev; while (SDL_PollEvent(&ev)) { switch (ev.type) { case SDL_QUIT: il_log("Stopping"); window.close(); return 0; } } duration delta = clock::now() - start; const double secs = 5.0; const int scale = 20; il_vec3 v; v.x = float(sin(delta.count() * M_PI * 2.0 / secs) * scale); v.y = 0.f; v.z = float(cos(delta.count() * M_PI * 2.0 / secs) * scale); il_quat q = il_quat_fromAxisAngle(0, 1, 0, float(delta.count() * M_PI * 2.0 / secs)); il_pos_setPosition(&graphics.space.camera, v); il_pos_setRotation(&graphics.space.camera, q); graphics.draw(state); } }