Пример #1
0
//build spline with a frustum and a fileName to spline point data
SplineTraveler::SplineTraveler(Frustum *frus, string fileName)
{

	rotation = Vector3f(0,0,0); 
	nextPoint = 1;
        curPoint = 0;
	curU = 0;
	timer = 0;
	timeSinceKnot = 0;	
	elapseRate = 1;
	initSpline(fileName);
	frustum = frus;
	areGhostPoints = false;
}
/* openGL init */
void myInit() {

	t = clock();

	/* Enable a single OpenGL light. */
	initLighting();
	initMaterials();
	glEnable(GL_LIGHT0);
	glEnable(GL_LIGHTING);

	initGroundTexture();
	initSkyTexture();
	initRailTexture();
	initSpline();
	calculateNormalsAndBinormals();

	/* create the display list */
	theSpline = glGenLists(1);
	glNewList(theSpline, GL_COMPILE);

	/* create cross sections */
	glBegin(GL_LINES);

	for (int i = 0; i < normals.numControlPoints; i++) {
		glVertex3f(rollercoasterSpline.points[i].x, rollercoasterSpline.points[i].y, rollercoasterSpline.points[i].z);
		glVertex3f(rollercoasterSpline.points[i].x + normals.points[i].x / 2,
			rollercoasterSpline.points[i].y + normals.points[i].y / 2,
			rollercoasterSpline.points[i].z + normals.points[i].z / 2);
	}

	glEnd();

	/* create support beams */
	glBegin(GL_LINES);

	for (int i = 0; i < normals.numControlPoints; i++) {
		if (i % 20 == 0) {
			glVertex3f(rollercoasterSpline.points[i].x, rollercoasterSpline.points[i].y, rollercoasterSpline.points[i].z);
			glVertex3f(rollercoasterSpline.points[i].x, rollercoasterSpline.points[i].y, 0);

			glVertex3f(rollercoasterSpline.points[i].x + normals.points[i].x / 2,
				rollercoasterSpline.points[i].y + normals.points[i].y / 2,
				rollercoasterSpline.points[i].z + normals.points[i].z / 2);
			glVertex3f(rollercoasterSpline.points[i].x + normals.points[i].x / 2,
				rollercoasterSpline.points[i].y + normals.points[i].y / 2,
				0);
		}
	}

	glEnd();

	// modulate texture with lighting
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	glBindTexture(GL_TEXTURE_2D, railTexture);
	// turn on texture mapping
	glEnable(GL_TEXTURE_2D);


	/* compute the spline */
	glBegin(GL_QUADS);

	double alpha = 0.05;
	for (int i = 0; i < rollercoasterSpline.numControlPoints; i++) {
		double pointX = rollercoasterSpline.points[i].x;
		double pointY = rollercoasterSpline.points[i].y;
		double pointZ = rollercoasterSpline.points[i].z;
		double normX = normals.points[i].x;
		double normY = normals.points[i].y;
		double normZ = normals.points[i].z;
		double binX = binormals.points[i].x;
		double binY = binormals.points[i].y;
		double binZ = binormals.points[i].z;

		double pointX2 = rollercoasterSpline.points[i + 1].x;
		double pointY2 = rollercoasterSpline.points[i + 1].y;
		double pointZ2 = rollercoasterSpline.points[i + 1].z;
		double normX2 = normals.points[i + 1].x;
		double normY2 = normals.points[i + 1].y;
		double normZ2 = normals.points[i + 1].z;
		double binX2 = binormals.points[i + 1].x;
		double binY2 = binormals.points[i + 1].y;
		double binZ2 = binormals.points[i + 1].z;

		double v0X = pointX - alpha*(normX - binX);
		double v0Y = pointY - alpha*(normY - binY);
		double v0Z = pointZ - alpha*(normZ - binZ);

		double v1X = pointX + alpha*(normX + binX);
		double v1Y = pointY + alpha*(normY + binY);
		double v1Z = pointZ + alpha*(normZ + binZ);

		double v2X = pointX + alpha*(normX - binX);
		double v2Y = pointY + alpha*(normY - binY);
		double v2Z = pointZ + alpha*(normZ - binZ);

		double v3X = pointX - alpha*(normX + binX);
		double v3Y = pointY - alpha*(normY + binY);
		double v3Z = pointZ - alpha*(normZ + binZ);

		double v4X = pointX2 - alpha*(normX2 - binX2);
		double v4Y = pointY2 - alpha*(normY2 - binY2);
		double v4Z = pointZ2 - alpha*(normZ2 - binZ2);

		double v5X = pointX2 + alpha*(normX2 + binX2);
		double v5Y = pointY2 + alpha*(normY2 + binY2);
		double v5Z = pointZ2 + alpha*(normZ2 + binZ2);

		double v6X = pointX2 + alpha*(normX2 - binX2);
		double v6Y = pointY2 + alpha*(normY2 - binY2);
		double v6Z = pointZ2 + alpha*(normZ2 - binZ2);

		double v7X = pointX2 - alpha*(normX2 + binX2);
		double v7Y = pointY2 - alpha*(normY2 + binY2);
		double v7Z = pointZ2 - alpha*(normZ2 + binZ2);

		// // // RAIL TWO // // //

		double v0X2 = pointX - alpha*(-8 * normX - binX);
		double v0Y2 = pointY - alpha*(-8 * normY - binY);
		double v0Z2 = pointZ - alpha*(-8 * normZ - binZ);

		double v1X2 = pointX + alpha*(10 * normX + binX);
		double v1Y2 = pointY + alpha*(10 * normY + binY);
		double v1Z2 = pointZ + alpha*(10 * normZ + binZ);

		double v2X2 = pointX + alpha*(10 * normX - binX);
		double v2Y2 = pointY + alpha*(10 * normY - binY);
		double v2Z2 = pointZ + alpha*(10 * normZ - binZ);

		double v3X2 = pointX - alpha*(-8 * normX + binX);
		double v3Y2 = pointY - alpha*(-8 * normY + binY);
		double v3Z2 = pointZ - alpha*(-8 * normZ + binZ);

		double v4X2 = pointX2 - alpha*(-8 * normX2 - binX2);
		double v4Y2 = pointY2 - alpha*(-8 * normY2 - binY2);
		double v4Z2 = pointZ2 - alpha*(-8 * normZ2 - binZ2);

		double v5X2 = pointX2 + alpha*(10 * normX2 + binX2);
		double v5Y2 = pointY2 + alpha*(10 * normY2 + binY2);
		double v5Z2 = pointZ2 + alpha*(10 * normZ2 + binZ2);

		double v6X2 = pointX2 + alpha*(10 * normX2 - binX2);
		double v6Y2 = pointY2 + alpha*(10 * normY2 - binY2);
		double v6Z2 = pointZ2 + alpha*(10 * normZ2 - binZ2);

		double v7X2 = pointX2 - alpha*(-8 * normX2 + binX2);
		double v7Y2 = pointY2 - alpha*(-8 * normY2 + binY2);
		double v7Z2 = pointZ2 - alpha*(-8 * normZ2 + binZ2);

		// // // // // // // // //


		glNormal3f(-normals.points[i].x, -normals.points[i].y, -normals.points[i].z);
		// right side face
		glTexCoord2f(1.0, 0.0);
		glVertex3f(v0X, v0Y, v0Z);
		glTexCoord2f(0.0, 0.0);
		glVertex3f(v1X, v1Y, v1Z);
		glTexCoord2f(0.0, 1.0);
		glVertex3f(v5X, v5Y, v5Z);
		glTexCoord2f(1.0, 1.0);
		glVertex3f(v4X, v4Y, v4Z);

		glNormal3f(-binormals.points[i].x, -binormals.points[i].y, -binormals.points[i].z);
		// top face
		glTexCoord2f(1.0, 0.0);
		glVertex3f(v1X, v1Y, v1Z);
		glTexCoord2f(0.0, 0.0);
		glVertex3f(v2X, v2Y, v2Z);
		glTexCoord2f(0.0, 1.0);
		glVertex3f(v6X, v6Y, v6Z);
		glTexCoord2f(1.0, 1.0);
		glVertex3f(v5X, v5Y, v5Z);

		glNormal3f(normals.points[i].x, normals.points[i].y, normals.points[i].z);
		// left side face
		glTexCoord2f(1.0, 0.0);
		glVertex3f(v2X, v2Y, v2Z);
		glTexCoord2f(0.0, 0.0);
		glVertex3f(v3X, v3Y, v3Z);
		glTexCoord2f(0.0, 1.0);
		glVertex3f(v7X, v7Y, v7Z);
		glTexCoord2f(1.0, 1.0);
		glVertex3f(v6X, v6Y, v6Z);

		glNormal3f(binormals.points[i].x, binormals.points[i].y, binormals.points[i].z);
		// bottom face
		glTexCoord2f(1.0, 0.0);
		glVertex3f(v0X, v0Y, v0Z);
		glTexCoord2f(0.0, 0.0);
		glVertex3f(v3X, v3Y, v3Z);
		glTexCoord2f(0.0, 1.0);
		glVertex3f(v7X, v7Y, v7Z);
		glTexCoord2f(1.0, 1.0);
		glVertex3f(v4X, v4Y, v4Z);



		// // // RAIL TWO // // //
		// // // // // // // // //

		glNormal3f(-normals.points[i].x, -normals.points[i].y, -normals.points[i].z);
		// right side face
		glTexCoord2f(1.0, 0.0);
		glVertex3f(v0X2, v0Y2, v0Z2);
		glTexCoord2f(0.0, 0.0);
		glVertex3f(v1X2, v1Y2, v1Z2);
		glTexCoord2f(0.0, 1.0);
		glVertex3f(v5X2, v5Y2, v5Z2);
		glTexCoord2f(1.0, 1.0);
		glVertex3f(v4X2, v4Y2, v4Z2);

		glNormal3f(-binormals.points[i].x, -binormals.points[i].y, -binormals.points[i].z);
		// top face
		glTexCoord2f(1.0, 0.0);
		glVertex3f(v1X2, v1Y2, v1Z2);
		glTexCoord2f(0.0, 0.0);
		glVertex3f(v2X2, v2Y2, v2Z2);
		glTexCoord2f(0.0, 1.0);
		glVertex3f(v6X2, v6Y2, v6Z2);
		glTexCoord2f(1.0, 1.0);
		glVertex3f(v5X2, v5Y2, v5Z2);

		glNormal3f(normals.points[i].x, normals.points[i].y, normals.points[i].z);
		// left side face
		glTexCoord2f(1.0, 0.0);
		glVertex3f(v2X2, v2Y2, v2Z2);
		glTexCoord2f(0.0, 0.0);
		glVertex3f(v3X2, v3Y2, v3Z2);
		glTexCoord2f(0.0, 1.0);
		glVertex3f(v7X2, v7Y2, v7Z2);
		glTexCoord2f(1.0, 1.0);
		glVertex3f(v6X2, v6Y2, v6Z2);

		glNormal3f(binormals.points[i].x, binormals.points[i].y, binormals.points[i].z);
		// bottom face
		glTexCoord2f(1.0, 0.0);
		glVertex3f(v0X2, v0Y2, v0Z2);
		glTexCoord2f(0.0, 0.0);
		glVertex3f(v3X2, v3Y2, v3Z2);
		glTexCoord2f(0.0, 1.0);
		glVertex3f(v7X2, v7Y2, v7Z2);
		glTexCoord2f(1.0, 1.0);
		glVertex3f(v4X2, v4Y2, v4Z2);

	}
	glDisable(GL_TEXTURE_2D);
	glEnd();
	glEndList();


	/* setup gl view here */
	glClearColor(0.0, 0.0, 0.0, 0.0);
}