예제 #1
0
Water::Water(Loader loader)
{
	float positions[] = { -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, -1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f, 1.0f };
	int indices[] = { 0, 1, 3, 3, 1, 2 };

	// create a ground-plane that is 2x2 meters and scale it so it becomes 100x100 meters. 
	waterplane = loader.createModel(positions, 12, indices, 6);

	Mat4 scaleMatrix;
	scaleMatrix.loadScale(150, 1, 150);
	waterplane.setModelMatrix(scaleMatrix);

	dudvTexture = loader.loadBMPtexture("water_dudv.bmp");
	normalTexture = loader.loadBMPtexture("water_nomal.bmp");
	shader.createShader("waterVertexShader.glsl", "waterFragmentShader.glsl");
	
	reflectionFrameBuffer = FrameBufferUtilities::createBuffer();
	reflectionTexture = FrameBufferUtilities::createTextureAttachment(REFLECTION_WIDTH, REFLECTION_HEIGHT);
	reflectionDepthBuffer = FrameBufferUtilities::createDepthBufferAttachment(REFLECTION_WIDTH, REFLECTION_HEIGHT);
	FrameBufferUtilities::unbindCurrentBuffer();

	refractionFrameBuffer = FrameBufferUtilities::createBuffer();
	refractionTexture = FrameBufferUtilities::createTextureAttachment(REFRACTION_WIDTH, REFRACTION_HEIGHT);
	refractionDepthTexture = FrameBufferUtilities::createDepthTextureAttachment(REFRACTION_WIDTH, REFRACTION_HEIGHT);
	FrameBufferUtilities::unbindCurrentBuffer();

	double waterheight = 0; 
	reflection_clip_plane = Vec4{ 0, 1, 0, -waterheight + 0.05 }; 
	refraction_clip_plane = Vec4{ 0, -1, 0, -waterheight };
}
void TerrainChunk::loadLODlevel(Loader loader, int level)
{
	const int numberOfVertices[] = { NUMBER_OF_VERTICES , NUMBER_OF_VERTICES / 2 + 1, NUMBER_OF_VERTICES / 4 + 1 };

	int indices[(NUMBER_OF_VERTICES - 1) * (NUMBER_OF_VERTICES - 1) * 6];
	float positions[NUMBER_OF_VERTICES * NUMBER_OF_VERTICES * 3];
	float textureCoords[NUMBER_OF_VERTICES * NUMBER_OF_VERTICES * 2];
	float normals[NUMBER_OF_VERTICES * NUMBER_OF_VERTICES * 3];
	int indicesIndex = 0, positionIndex = 0, textureCoordIndex = 0, normalIndex = 0;

	for (int x = 0; x < numberOfVertices[level]; x++)
	{
		for (int z = 0; z < numberOfVertices[level]; z++)
		{
			float localVertexPosX = (float)(x * SIZE) / (float)(numberOfVertices[level] - 1);
			float localVertexPosZ = (float)(z * SIZE) / (float)(numberOfVertices[level] - 1);
			double height = TerrainHeightGenerator::getHeight(pos.x + localVertexPosX, pos.z + localVertexPosZ);

			if (x == 0 || z == 0 || x == numberOfVertices[level] - 1 || z == numberOfVertices[level] - 1)
			{
				if (level == 1)
					height -= 0.15;
				else if (level == 2)
					height -= 0.30;
			}

			positions[positionIndex++] = pos.x + localVertexPosX;
			positions[positionIndex++] = (float)height;
			positions[positionIndex++] = pos.z + localVertexPosZ;

			float texCoordU = (float)(x) / (float)(numberOfVertices[level] - 1);
			float texCoordV = (float)(z) / (float)(numberOfVertices[level] - 1);
			textureCoords[textureCoordIndex++] = texCoordU;
			textureCoords[textureCoordIndex++] = texCoordV;

			Vec3 normal = TerrainHeightGenerator::getNormal(pos.x + localVertexPosX, pos.z + localVertexPosZ);
			normals[normalIndex++] = normal.x;
			normals[normalIndex++] = normal.y;
			normals[normalIndex++] = normal.z;
		}
	}

	for (int x = 0; x < numberOfVertices[level] - 1; x++)
	{
		for (int z = 0; z < numberOfVertices[level] - 1; z++)
		{
			int topLeft = (z * numberOfVertices[level]) + x;
			int topRight = topLeft + 1;
			int bottomLeft = ((z + 1) * numberOfVertices[level]) + x;
			int bottomRight = bottomLeft + 1;
			indices[indicesIndex++] = topLeft;
			indices[indicesIndex++] = bottomLeft;
			indices[indicesIndex++] = topRight;
			indices[indicesIndex++] = topRight;
			indices[indicesIndex++] = bottomLeft;
			indices[indicesIndex++] = bottomRight;
		}
	}
	models[level] = loader.createModel(positions, positionIndex, textureCoords, textureCoordIndex, normals, normalIndex, indices, indicesIndex);
}