static void drawSpires(float radius) { int i; vec3 right, left; vec3 zUnit = { { 0, 0, 1} }; vec3 vectors[NUM_SPIRES] = { { { 1.00f, 0.20f, 0.00f } }, { { 0.80f, 0.25f, 0.00f } }, { { 0.90f, 0.50f, 0.00f } }, { { 0.70f, 0.50f, 0.00f } }, { { 0.52f, 0.45f, 0.00f } }, { { 0.65f, 0.75f, 0.00f } }, { { 0.42f, 0.68f, 0.00f } }, { { 0.40f, 1.02f, 0.00f } }, { { 0.20f, 0.90f, 0.00f } }, { { 0.08f, 0.65f, 0.00f } }, { { 0.00f, 1.00f, 0.00f } }, /* vertical spire */ { { -0.08f, 0.65f, 0.00f } }, { { -0.20f, 0.90f, 0.00f } }, { { -0.40f, 1.02f, 0.00f } }, { { -0.42f, 0.68f, 0.00f } }, { { -0.65f, 0.75f, 0.00f } }, { { -0.52f, 0.45f, 0.00f } }, { { -0.70f, 0.50f, 0.00f } }, { { -0.90f, 0.50f, 0.00f } }, { { -0.80f, 0.30f, 0.00f } }, { { -1.00f, 0.20f, 0.00f } } }; glColor3f(1, 1, 1); glVertex3f(0, 0, 0); glBlendFunc(GL_ONE, GL_ONE); glBegin(GL_TRIANGLES); for (i=0; i < NUM_SPIRES; i++) { vec3_Cross(&right, vectors + i, &zUnit); vec3_Normalize(&right, &right); vec3_Scale(&right, &right, SPIRE_WIDTH); vec3_Cross(&left, &zUnit, vectors + i); vec3_Normalize(&left, &left); vec3_Scale(&left, &left, SPIRE_WIDTH); glColor4f(1,1,1,0.0); glVertex3fv(right.v); glVertex3f(radius * vectors[i].v[0], radius * vectors[i].v[1], 0.0); glVertex3fv(left.v); } glEnd(); }
void State_Recalculate(State* state) { vec3_Scale(state->velocity, state->momentum, state->inverse_mass); vec3_Scale(state->angular_velocity, state->angular_momentum, state->inverse_inertia_tensor); quat_Norm(state->transform.quaternion, state->transform.quaternion); quat angular_velocity; angular_velocity[0] = state->angular_velocity[0]; angular_velocity[1] = state->angular_velocity[1]; angular_velocity[2] = state->angular_velocity[2]; angular_velocity[3] = 0.0f; quat_Mul(state->spin, angular_velocity, state->transform.quaternion); vec4_Scale(state->spin, state->spin, 0.5f); Transform_GetWorldMat(&state->transform); }
void nebu_Camera_Zoom(nebu_Camera *pCamera, float d) { vec3 v; vec3_Sub(&v, &pCamera->vLookAt, &pCamera->vEye); vec3_Normalize(&v, &v); vec3_Scale(&v, &v, d); vec3_Add(&pCamera->vEye, &pCamera->vEye, &v); }
void nebu_Camera_Slide(nebu_Camera *pCamera, float dx, float dy, float dz) { vec3 vLook, vUp, vRight, vMovement; vec3_Sub(&vLook, &pCamera->vLookAt, &pCamera->vEye); vec3_Cross(&vRight, &vLook, &pCamera->vUp); vec3_Cross(&vUp, &vRight, &vLook); vec3_Normalize(&vUp, &vUp); vec3_Normalize(&vRight, &vRight); vec3_Normalize(&vLook, &vLook); vec3_Scale(&vUp, &vUp, dy); vec3_Scale(&vRight, &vRight, dx); vec3_Scale(&vLook, &vLook, dz); vec3_Add(&vMovement, &vUp, &vRight); vec3_Add(&vMovement, &vMovement, &vLook); vec3_Add(&pCamera->vEye, &pCamera->vEye, &vMovement); vec3_Add(&pCamera->vLookAt, &pCamera->vLookAt, &vMovement); }
void nebu_Camera_Rotate(nebu_Camera *pCamera, int flags, float dx, float dy) { // adjust up vector, so that it is orthogonal to // view direction vec3 vDiff, vView, vRight, vUp; vec3 vdx, vdy; vec3 *pvCenter, *pvMoving; switch(flags & NEBU_CAMERA_ROTATE_MASK) { case NEBU_CAMERA_ROTATE_AROUND_EYE: pvCenter = &pCamera->vEye; pvMoving = &pCamera->vLookAt; break; case NEBU_CAMERA_ROTATE_AROUND_LOOKAT: pvCenter = &pCamera->vLookAt; pvMoving = &pCamera->vEye; break; default: assert(0); return; } vec3_Sub(&vDiff, pvCenter, pvMoving); vec3_Normalize(&vView, &vDiff); vec3_Cross(&vRight, &pCamera->vUp, &vView); vec3_Normalize(&vRight, &vRight); vec3_Cross(&vUp, &vView, &vRight); vec3_Normalize(&vUp, &vUp); // horizontal movement (dx): if(dx == 0) { vec3_Zero(&vdx); } else { // rotate moving around up vector through center point vec3 v = vDiff; float fAngle = dx * 2 * (float)M_PI / 360.0f; matrix matRotation; matrixRotationAxis(&matRotation, fAngle, &vUp); vec3_Transform(&v, &vDiff, &matRotation); vec3_Sub(&vdx, &v, &vDiff); } // vertical movement (dy): if(dy == 0) { vec3_Zero(&vdy); } else { // rotate eye point around up vector through lookAt point vec3 v = vDiff; float fAngle = dy * 2 * (float)M_PI / 360.0f; matrix matRotation; matrixRotationAxis(&matRotation, fAngle, &vRight); vec3_Transform(&v, &vDiff, &matRotation); vec3_Sub(&vdy, &v, &vDiff); matrixTranspose(&matRotation, &matRotation); if(!(flags & NEBU_CAMERA_FIXED_UP)) vec3_Transform(&pCamera->vUp, &pCamera->vUp, &matRotation); } { vec3 v; vec3_Add(&v, &vdx, &vdy); vec3_Add(&v, &v, pvMoving); vec3_Sub(&v, &v, pvCenter); vec3_Normalize(&v, &v); // printf("up dot view: %.4f\n", - vec3_Dot(&v, &pCamera->vUp)); vec3_Scale(&v, &v, vec3_Length(&vDiff)); vec3_Add(pvMoving, &v, pvCenter); } }
void nebu_Camera_RotateAroundTarget(nebu_Camera *pCamera, float dx, float dy) { // adjust up vector, so that it is orthogonal to // view direction vec3 vDiff, vView, vRight, vUp; vec3 vdx, vdy; vec3_Sub(&vDiff, &pCamera->vLookAt, &pCamera->vEye); vec3_Normalize(&vView, &vDiff); vec3_Cross(&vRight, &pCamera->vUp, &vView); vec3_Normalize(&vRight, &vRight); vec3_Cross(&vUp, &vView, &vRight); vec3_Normalize(&vUp, &vUp); // horizontal movement (dx): if(dx == 0) { vec3_Zero(&vdx); } else { // rotate eye point around up vector through lookAt point vec3 v = vDiff; float fAngle = dx * 2 * M_PI / 360.0; matrix matRotation; matrixRotationAxis(&matRotation, fAngle, &vUp); vec3_Transform(&v, &vDiff, &matRotation); vec3_Sub(&vdx, &v, &vDiff); } // vertical movement (dy): if(dy == 0) { vec3_Zero(&vdy); } else { // rotate eye point around up vector through lookAt point vec3 v = vDiff; float fAngle = dy * 2 * M_PI / 360.0; matrix matRotation; matrixRotationAxis(&matRotation, fAngle, &vRight); vec3_Transform(&v, &vDiff, &matRotation); vec3_Sub(&vdy, &v, &vDiff); matrixTranspose(&matRotation, &matRotation); vec3_Transform(&pCamera->vUp, &pCamera->vUp, &matRotation); } // add relative movements to camera position /* vec3_Add(&pCamera->vEye, &pCamera->vEye, &vdx); vec3_Add(&pCamera->vEye, &pCamera->vEye, &vdy); */ { vec3 v; vec3_Add(&v, &vdx, &vdy); vec3_Add(&v, &v, &pCamera->vEye); vec3_Sub(&v, &v, &pCamera->vLookAt); vec3_Normalize(&v, &v); // printf("up dot view: %.4f\n", - vec3_Dot(&v, &pCamera->vUp)); vec3_Scale(&v, &v, vec3_Length(&vDiff)); vec3_Add(&pCamera->vEye, &v, &pCamera->vLookAt); } }