Example #1
0
void TerrainDemo::clientMoveAndDisplay(void)
{
	// elapsed time
	float us = getDeltaTimeMicroseconds();
	float seconds = 1.0e-6 * us;

	// we'll carefully iterate through each time step so we can update
	//   the dynamic model if necessary
	long nStepsPerIteration = 1;
	while (seconds > 1.0e-6) {
		float dt = nStepsPerIteration * s_engineTimeStep;
		if (dt > seconds) {
			dt = seconds;
		}
		seconds -= dt;
	//	std::cerr << "  Stepping through " << dt << " seconds\n";

		// if dynamic and radial, go ahead and update the field
		if (m_rawHeightfieldData && m_isDynamic && eRadial == m_model) {
			m_phase += s_deltaPhase * dt;
			if (m_phase > 2.0 * SIMD_PI) {
				m_phase -= 2.0 * SIMD_PI;
			}
			int bpe = getByteSize(m_type);
			btAssert(bpe > 0 && "Bad bytes per element");
			setRadial(m_rawHeightfieldData, bpe, m_type, m_phase);
		}

		if (m_dynamicsWorld) {
			m_dynamicsWorld->stepSimulation(dt,
			    nStepsPerIteration + 1, s_engineTimeStep);
		}
	}

	// okay, render
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	renderme();
	glFlush();
	glutSwapBuffers();
}
static byte_t * getRawHeightfieldData(eTerrainModel model, PHY_ScalarType type, btScalar& minHeight, btScalar& maxHeight){
//	std::cerr << "\nRegenerating terrain\n";
//	std::cerr << "  model = " << model << "\n";
//	std::cerr << "  type = " << type << "\n";

	long nElements = ((long) s_gridSize) * s_gridSize;
//	std::cerr << "  nElements = " << nElements << "\n";

	int bytesPerElement = sizeof(float);
//	std::cerr << "  bytesPerElement = " << bytesPerElement << "\n";
	btAssert(bytesPerElement > 0 && "bad bytes per element");

	long nBytes = nElements * bytesPerElement;
//	std::cerr << "  nBytes = " << nBytes << "\n";
	byte_t * raw = new byte_t[nBytes];
	btAssert(raw && "out of memory");

	// reseed randomization every 30 seconds
//	srand(time(NULL) / 30);

	// populate based on model
	setRadial(raw, bytesPerElement, type);

	if (0) {
		// inside if(0) so it keeps compiling but isn't
		// 	exercised and doesn't cause warnings
//		std::cerr << "final grid:\n";
		dumpGrid(raw, bytesPerElement, type, s_gridSize - 1);
	}

	// find min/max
	for (int i = 0; i < s_gridSize; ++i) {
		for (int j = 0; j < s_gridSize; ++j) {
			float z = getGridHeight(raw, i, j, type);
//			std::cerr << "i=" << i << ", j=" << j << ": z=" << z << "\n";

			// update min/max
			if (!i && !j) {
				minHeight = z;
				maxHeight = z;
			} else {
				if (z < minHeight) {
					minHeight = z;
				}
				if (z > maxHeight) {
					maxHeight = z;
				}
			}
		}
	}

	if (maxHeight < -minHeight) {
		maxHeight = -minHeight;
	}
	if (minHeight > -maxHeight) {
		minHeight = -maxHeight;
	}

//	std::cerr << "  minHeight = " << minHeight << "\n";
//	std::cerr << "  maxHeight = " << maxHeight << "\n";

	return raw;
}