Пример #1
0
	bool initBuffer()
	{
		std::size_t tileAddress();
		
		glm::vec2 const WindowSize(this->getWindowSize());

		glm::uvec2 const TileCount((WindowSize / this->TileSize) + glm::mix(glm::vec2(0), glm::vec2(1), glm::greaterThan(glm::mod(glm::vec2(WindowSize), this->TileSize), glm::vec2(0))));
		std::vector<vertex> Vertices;
		Vertices.resize(TileCount.x * TileCount.y * 6 * this->TrianglePairPerTile);
		VertexCount = static_cast<GLsizei>(Vertices.size());

		for(glm::uint TileIndexY = 0; TileIndexY < TileCount.y; ++TileIndexY)
		for(glm::uint TileIndexX = 0; TileIndexX < TileCount.x; ++TileIndexX)
		{
			glm::uint TileIndex = (TileIndexX + TileIndexY * TileCount.x);

			glm::vec4 const RandColor = glm::linearRand(glm::vec4(0.0), glm::vec4(255.0));
			glm::u8vec4 Color(glm::clamp(RandColor, glm::vec4(0.0), glm::vec4(255.0)));

			for(std::size_t DrawIndex = 0; DrawIndex < this->TrianglePairPerTile; ++DrawIndex)
			{
				Vertices[TileIndex * (6 * this->TrianglePairPerTile) + DrawIndex * 6 + 0] = vertex(glm::vec3((TileIndexX + 0) * this->TileSize.x, (TileIndexY + 0) * this->TileSize.y, static_cast<float>(2 + DrawIndex)), Color);
				Vertices[TileIndex * (6 * this->TrianglePairPerTile) + DrawIndex * 6 + 1] = vertex(glm::vec3((TileIndexX + 1) * this->TileSize.x, (TileIndexY + 0) * this->TileSize.y, static_cast<float>(2 + DrawIndex)), Color);
				Vertices[TileIndex * (6 * this->TrianglePairPerTile) + DrawIndex * 6 + 2] = vertex(glm::vec3((TileIndexX + 1) * this->TileSize.x, (TileIndexY + 1) * this->TileSize.y, static_cast<float>(2 + DrawIndex)), Color);
				Vertices[TileIndex * (6 * this->TrianglePairPerTile) + DrawIndex * 6 + 3] = vertex(glm::vec3((TileIndexX + 1) * this->TileSize.x, (TileIndexY + 1) * this->TileSize.y, static_cast<float>(2 + DrawIndex)), Color);
				Vertices[TileIndex * (6 * this->TrianglePairPerTile) + DrawIndex * 6 + 4] = vertex(glm::vec3((TileIndexX + 0) * this->TileSize.x, (TileIndexY + 1) * this->TileSize.y, static_cast<float>(2 + DrawIndex)), Color);
				Vertices[TileIndex * (6 * this->TrianglePairPerTile) + DrawIndex * 6 + 5] = vertex(glm::vec3((TileIndexX + 0) * this->TileSize.x, (TileIndexY + 0) * this->TileSize.y, static_cast<float>(2 + DrawIndex)), Color);
			}
		}

		GLsizei VertexSize = static_cast<GLsizei>(Vertices.size() * sizeof(vertex));

		glm::mat4 Perspective = glm::ortho(0.0f, static_cast<float>(WindowSize.x), 0.0f, static_cast<float>(WindowSize.y));

		glGenBuffers(buffer::MAX, &BufferName[0]);
		glBindBuffer(GL_ARRAY_BUFFER, BufferName[buffer::VERTEX]);
		glBufferData(GL_ARRAY_BUFFER, VertexSize, &Vertices[0], GL_STATIC_DRAW);
		glBindBuffer(GL_ARRAY_BUFFER, 0);

		glBindBuffer(GL_UNIFORM_BUFFER, BufferName[buffer::TRANSFORM]);
		glBufferData(GL_UNIFORM_BUFFER, sizeof(Perspective), &Perspective[0][0], GL_STATIC_DRAW);
		glBindBuffer(GL_UNIFORM_BUFFER, 0);

		return true;
	}
	bool initBuffer()
	{
		std::size_t tileAddress();
		
		glm::vec2 const WindowSize(this->getWindowSize());

		glm::uvec2 const TileCount((WindowSize / this->TileSize) + glm::mix(glm::vec2(0), glm::vec2(1), glm::greaterThan(glm::mod(glm::vec2(WindowSize), this->TileSize), glm::vec2(0))));
		std::vector<vertex> Vertices;
		Vertices.resize(TileCount.x * TileCount.y * 6);
		VertexCount = static_cast<GLsizei>(Vertices.size());

		for(glm::uint TileIndexY = 0; TileIndexY < TileCount.y; ++TileIndexY)
		for(glm::uint TileIndexX = 0; TileIndexX < TileCount.x; ++TileIndexX)
		{
			glm::uint TileIndex = 0;

			switch(this->Layout)
			{
			case LAYOUT_LINEAR:
				TileIndex = (TileIndexX + TileIndexY * TileCount.x);
				break;
			case LAYOUT_MORTON:
				TileIndex = glm::bitfieldInterleave(TileIndexY, TileIndexX);
				break;
			case LAYOUT_RANDOM:
			{
				glm::uvec2 RandTileIndex(glm::clamp(glm::linearRand(glm::vec2(0.0f), glm::vec2(TileCount - 1u)), glm::vec2(0.0f), glm::vec2(TileCount - 1u)));
				TileIndex = (RandTileIndex.x + RandTileIndex.y * TileCount.x);
				break;
			}
			default: // Unknown layout
				assert(0);
			}

			//glm::vec4 const RGBA(glm::rgbColor(glm::vec3(float(TileIndex % 255) / 255.f * 360.f, 1.0f, 1.0f)), 1.0f);
			//glm::u8vec4 Color(RGBA * 255.f);

			glm::vec4 const RandColor = glm::linearRand(glm::vec4(0.0), glm::vec4(255.0));
			glm::u8vec4 Color(glm::clamp(RandColor, glm::vec4(0.0), glm::vec4(255.0)));

			Vertices[TileIndex * 6 + 0] = vertex(glm::vec3((TileIndexX + 0) * this->TileSize.x, (TileIndexY + 0) * this->TileSize.y, 0.0f), Color);
			Vertices[TileIndex * 6 + 1] = vertex(glm::vec3((TileIndexX + 1) * this->TileSize.x, (TileIndexY + 0) * this->TileSize.y, 0.0f), Color);
			Vertices[TileIndex * 6 + 2] = vertex(glm::vec3((TileIndexX + 1) * this->TileSize.x, (TileIndexY + 1) * this->TileSize.y, 0.0f), Color);
			Vertices[TileIndex * 6 + 3] = vertex(glm::vec3((TileIndexX + 1) * this->TileSize.x, (TileIndexY + 1) * this->TileSize.y, 0.0f), Color);
			Vertices[TileIndex * 6 + 4] = vertex(glm::vec3((TileIndexX + 0) * this->TileSize.x, (TileIndexY + 1) * this->TileSize.y, 0.0f), Color);
			Vertices[TileIndex * 6 + 5] = vertex(glm::vec3((TileIndexX + 0) * this->TileSize.x, (TileIndexY + 0) * this->TileSize.y, 0.0f), Color);
		}

		GLsizei VertexSize = static_cast<GLsizei>(Vertices.size() * sizeof(vertex));

		glm::mat4 Perspective = glm::ortho(0.0f, static_cast<float>(WindowSize.x), 0.0f, static_cast<float>(WindowSize.y));

		glGenBuffers(buffer::MAX, &BufferName[0]);
		glBindBuffer(GL_ARRAY_BUFFER, BufferName[buffer::VERTEX]);
		glBufferData(GL_ARRAY_BUFFER, VertexSize, &Vertices[0], GL_STATIC_DRAW);
		glBindBuffer(GL_ARRAY_BUFFER, 0);

		glBindBuffer(GL_UNIFORM_BUFFER, BufferName[buffer::TRANSFORM]);
		glBufferData(GL_UNIFORM_BUFFER, sizeof(Perspective), &Perspective[0][0], GL_STATIC_DRAW);
		glBindBuffer(GL_UNIFORM_BUFFER, 0);

		return true;
	}