void drawCompassTriangle(float scale) { int i; struct point p, aux; float size = 3 / scale; vecMinus(compass[S], compass[P], &aux); glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); glPushMatrix(); glBegin(GL_TRIANGLES); for (i = 0; i < COMPASS_POINT_N; i++) { if (i == S) continue; vecMinus(compass[i], compass[P], &p); vecTimes(&p, size); if( i == A || i == D) /* makes the triangle less flattened */ vecPlus(p, aux, &p); vecPlus(compass[P], p, &p); glVertex2f(p.x * scale, p.y * scale); } glEnd(); glPopMatrix(); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); }
bool intersectRaySphere(Ray *ray, Sphere *sphere, float *t) { bool ret = FALSE; /* Compute A, B and C coefficients */ //printf("Ray Pos: %d\n", ray->position->x); //printf("Sphere Pos: %d\n", sphere->position->x); float a = vecDot(&ray->direction, &ray->direction); Vector dist = vecMinus(&ray->position, &sphere->position); float b = 2 * vecDot(&dist, &ray->direction); float c = vecDot(&dist, &dist) - (sphere->radius * sphere->radius); /* Find discriminant */ float disc = b * b - 4 * a * c; /* If discriminant is negative, there are no real roots. * Return false as the ray misses the sphere. */ if(disc < 0) return FALSE; /* Compute q */ float discSqrt = sqrtf(disc); float q; if(b < 0) q = (-b - discSqrt)/2.0; else q = (-b + discSqrt)/2.0; /* The roots */ float t0 = q / a; float t1 = c / q; if(t0 > t1) t0 = t1; /* Verify t0 larger than 0 and less than the original t */ if((t0 > 0.001f) && (t0 < *t)){ *t = t0; ret = TRUE; } return ret; }