void Asteroid::initGL() { float texScale = 2.0; Point4D* vertices4 = new Point4D[nvertices+2]; vertices4[0] = Point4D (0 , 0, texCenterX , texCenterY); for (int i =0; i< nvertices; i++) vertices4[i+1] = Point4D(vertices[i].x, vertices[i].y, texCenterX + vertices[i].x * texScale , texCenterY + vertices[i].y * texScale); vertices4[nvertices+1] = vertices4[1]; glBindBuffer(GL_ARRAY_BUFFER, vboIds[0]); glBufferData(GL_ARRAY_BUFFER, (nvertices+2) * sizeof(Point4D), vertices4, GL_STATIC_DRAW); delete[] vertices4; if (_bonus) _bonus->initGL(); }
Explosion::Explosion (View* _view, float __radius) : FlyingObject(_view, 1, 0), _radius(__radius) { x = _view->getShip()->X(); y = _view->getShip()->Y() + view->getShip()->height(); vx = 0; vy = 0; nvertices = NP; vertices = new Point[nvertices]; for (int i=0; i< nvertices; i++) { float fi = M_PI* 2* i/ (nvertices-1); vertices[i] = Point(_radius* sin(fi), _radius* cos(fi)); } _color = Point4D(0.5,0,0,1); _endTime = currTime() + 1500; initGL(); }
size_t Matrix4x4::TransformAndClipRect(const Rect& aRect, const Rect& aClip, Point* aVerts) const { // Initialize a double-buffered array of points in homogenous space with // the input rectangle, aRect. Point4D points[2][kTransformAndClipRectMaxVerts]; Point4D* dstPoint = points[0]; *dstPoint++ = *this * Point4D(aRect.x, aRect.y, 0, 1); *dstPoint++ = *this * Point4D(aRect.XMost(), aRect.y, 0, 1); *dstPoint++ = *this * Point4D(aRect.XMost(), aRect.YMost(), 0, 1); *dstPoint++ = *this * Point4D(aRect.x, aRect.YMost(), 0, 1); // View frustum clipping planes are described as normals originating from // the 0,0,0,0 origin. Point4D planeNormals[4]; planeNormals[0] = Point4D(1.0, 0.0, 0.0, -aClip.x); planeNormals[1] = Point4D(-1.0, 0.0, 0.0, aClip.XMost()); planeNormals[2] = Point4D(0.0, 1.0, 0.0, -aClip.y); planeNormals[3] = Point4D(0.0, -1.0, 0.0, aClip.YMost()); // Iterate through each clipping plane and clip the polygon. // In each pass, we double buffer, alternating between points[0] and // points[1]. for (int plane=0; plane < 4; plane++) { planeNormals[plane].Normalize(); Point4D* srcPoint = points[plane & 1]; Point4D* srcPointEnd = dstPoint; dstPoint = points[~plane & 1]; Point4D* prevPoint = srcPointEnd - 1; float prevDot = planeNormals[plane].DotProduct(*prevPoint); while (srcPoint < srcPointEnd) { float nextDot = planeNormals[plane].DotProduct(*srcPoint); if ((nextDot >= 0.0) != (prevDot >= 0.0)) { // An intersection with the clipping plane has been detected. // Interpolate to find the intersecting point and emit it. float t = -prevDot / (nextDot - prevDot); *dstPoint++ = *srcPoint * t + *prevPoint * (1.0 - t); } if (nextDot >= 0.0) { // Emit any source points that are on the positive side of the // clipping plane. *dstPoint++ = *srcPoint; } prevPoint = srcPoint++; prevDot = nextDot; } } size_t dstPointCount = 0; size_t srcPointCount = dstPoint - points[0]; for (Point4D* srcPoint = points[0]; srcPoint < points[0] + srcPointCount; srcPoint++) { Point p; if (srcPoint->w == 0.0) { // If a point lies on the intersection of the clipping planes at // (0,0,0,0), we must avoid a division by zero w component. p = Point(0.0, 0.0); } else { p = srcPoint->As2DPoint(); } // Emit only unique points if (dstPointCount == 0 || p != aVerts[dstPointCount - 1]) { aVerts[dstPointCount++] = p; } } return dstPointCount; }
Point4D Layer::getPositionInOrbit(double rotationAngle, double raio, double orbitAngle) { double x = (raio * cos(toRadians(rotationAngle)) * cos(toRadians(orbitAngle))); double y = (raio * sin(toRadians(rotationAngle)) * cos(toRadians(orbitAngle))); double z = (raio * sin(toRadians(rotationAngle))); return Point4D(x,y,z); }