Ejemplo n.º 1
0
void BloomEffect::filterBright(const sf::RenderTexture& input, sf::RenderTexture& output)
{
	sf::Shader& brightness = mShaders.get(Shaders::BrightnessPass);
	brightness.setParameter("source", input.getTexture());
	applyShader(brightness, output);
	output.display();
}
Ejemplo n.º 2
0
	void draw(sf::RenderTarget &tg)
	{
		if (first)
		{
			rt.clear();
			rt.draw(sprite);
			rt.draw(logo);
			if (logo.getPosition().y == 0.0)rt.draw(press);

			if (check && timer.getElapsedTime().asSeconds() < 1)
			{
				if ((int)(rand() % 10) == 3) rt.clear(sf::Color::White);
			}
		}
		else
		{
			rt.clear(sf::Color::White);
			rt.draw(system);
			rt.draw(back2);
			rt.draw(pointer);
		}
		rt.display();
		sm.draw(rt, tg);
		if (first) tg.draw(v);
	};
Ejemplo n.º 3
0
	void run()
	{
		while (m_window.isOpen())
		{
			sf::Event event;
			while (m_window.pollEvent(event))
			{
				if (event.type == sf::Event::Closed)
					m_window.close();
			}

			// Update the position of the 'flashlight' to the current mouse position
			m_pos = static_cast<sf::Vector2f>(sf::Mouse::getPosition(m_window));
			m_flashlight.setPosition(m_pos);

			// Stance-out the 'flashlight' circle
			m_layer.clear();
			m_layer.draw(m_flashlight, sf::BlendMultiply);
			m_layer.display();

			m_window.clear(sf::Color::Blue);

			// Draw the layer sprite on top of the 'scene'
			m_window.draw(m_rect);
			m_window.draw(m_sprite);

			m_window.display();
		}
	}
Ejemplo n.º 4
0
void BloomEffect::blur(const sf::RenderTexture& input, sf::RenderTexture& output, sf::Vector2f offsetFactor)
{
	sf::Shader& gaussianBlur = mShaders.get(Shaders::GaussianBlurPass);
	gaussianBlur.setParameter("source", input.getTexture());
	gaussianBlur.setParameter("offsetFactor", offsetFactor);
	applyShader(gaussianBlur, output);
	output.display();
}
Ejemplo n.º 5
0
void PostBloom::filterBright(const sf::RenderTexture& src, sf::RenderTexture& dst)
{
    auto& shader = m_shaderResource.get(Shader::BrightnessExtract);

    shader.setUniform("u_sourceTexture", src.getTexture());
    applyShader(shader, dst);
    dst.display();
}
Ejemplo n.º 6
0
void BloomEffect::downsample(const sf::RenderTexture& input, sf::RenderTexture& output)
{
	sf::Shader& downSampler = mShaders.get(Shaders::DownSamplePass);
	downSampler.setParameter("source", input.getTexture());
	downSampler.setParameter("sourceSize", sf::Vector2f(input.getSize()));
	applyShader(downSampler, output);
	output.display();
}
Ejemplo n.º 7
0
void PostBloom::blur(const sf::RenderTexture& src, sf::RenderTexture& dst, const sf::Vector2f& offset)
{
    auto& shader = m_shaderResource.get(Shader::GaussianBlur);

    shader.setUniform("u_sourceTexture", src.getTexture());
    shader.setUniform("u_offset", offset);

    applyShader(shader, dst);
    dst.display();
}
Ejemplo n.º 8
0
void PostBloom::downSample(const sf::RenderTexture& src, sf::RenderTexture& dst)
{
    auto& shader = m_shaderResource.get(Shader::DownSample);

    shader.setUniform("u_sourceTexture", src.getTexture());
    shader.setUniform("u_sourceSize", sf::Vector2f(src.getSize()));

    applyShader(shader, dst);
    dst.display();
}
Ejemplo n.º 9
0
		void        draw      (sf::RenderTarget & target, sf::RenderStates states = sf::RenderStates::Default) const
		{
			lightBuffer.clear();
			texture.display();
			states.texture = &texture.getTexture();
			for (const auto & light : lights)
			{
				light.draw(lightBuffer, getView(), states);
			}
			sf::Vector2f dimensions = sf::Vector2f(texture.getSize());
			sf::VertexArray vertices(sf::Quads, 4);
			vertices[0] = sf::Vertex(sf::Vector2f(0.f, 0.f), sf::Color(255, 255, 255, minAlpha), sf::Vector2f(0.f, 0.f));
			vertices[1] = sf::Vertex(sf::Vector2f(dimensions.x, 0.f), sf::Color(255, 255, 255, minAlpha), sf::Vector2f(dimensions.x, 0.f));
			vertices[2] = sf::Vertex(dimensions, sf::Color(255, 255, 255, minAlpha), dimensions);
			vertices[3] = sf::Vertex(sf::Vector2f(0.f, dimensions.y), sf::Color(255, 255, 255, minAlpha), sf::Vector2f(0.f, dimensions.y));
			lightBuffer.draw(vertices, states);
			lightBuffer.display();
			target.draw(sf::Sprite(lightBuffer.getTexture()));
		}
Ejemplo n.º 10
0
void GuiElement::drawRenderTexture(sf::RenderTexture& texture, sf::RenderTarget& window, sf::Color color, const sf::RenderStates& states)
{
    texture.display();
    
    sf::Sprite sprite(texture.getTexture());
    sprite.setTextureRect(sf::IntRect(0, 0, texture.getSize().x * texture.getView().getViewport().width, texture.getSize().y * texture.getView().getViewport().height));
    sprite.setColor(color);
    sprite.setPosition(rect.left, rect.top);
    sprite.setScale(rect.width / float(texture.getSize().x * texture.getView().getViewport().width), rect.height / float(texture.getSize().y * texture.getView().getViewport().height));
    window.draw(sprite, states);
}
Ejemplo n.º 11
0
bool AppState::render(sf::RenderTexture& texture, int frame)
{
  texture.clear();

  if (m_preDesktopRenderFunctor) m_preDesktopRenderFunctor(texture, frame);
  the_desktop.render(texture, frame);
  if (m_postDesktopRenderFunctor) m_postDesktopRenderFunctor(texture, frame);

  texture.display();
  return true;
}
Ejemplo n.º 12
0
//writes frames to file 
//directory must already exist
void record(sf::RenderTexture& in, int frameskip, std::string directory) {

	static int frame = 0;

	if(frameskip > 0 && frame % frameskip == 0) {
		in.display();
		sf::Texture outTx = in.getTexture();
		sf::Image outImg = outTx.copyToImage();
		std::stringstream ugh;
		ugh << directory << "/" << frame << ".png";
		outImg.saveToFile(ugh.str());
	}
	frame++;
}
Ejemplo n.º 13
0
void Layer::MakeTexture(sf::RenderTexture& textureImage) const {
	if (WidthPixels == textureImage.getSize().x && HeightPixels == textureImage.getSize().y) { //layer is big enough to support creating a textured background from... if not, the texture will be left fully black
		VertexVector vertices[NUMBEROFTILESETTEXTURES]; //don't reuse Layer::Vertices, in case this layer needs to get drawn soon

		for (int x = 0; x < Width; ++x)
			for (int y = 0; y < Height; ++y)
				DrawTileToVertexArrays(vertices, x,y);

		for (int i = 0; i < NUMBEROFTILESETTEXTURES; ++i)
			if (!vertices[i].empty())
				textureImage.draw(vertices[i].data(), vertices[i].size(), OpenGLPrimitive, LevelPtr->TilesetPtr->TileImages[i]);
	}
	textureImage.display();
}
Ejemplo n.º 14
0
int main(){
	Node* head = new Node(SIZE / 2,SIZE/2,UP);
	head->next = new Node(SIZE/2,SIZE/2-1,UP);
	head->next->next = new Node(SIZE/2,SIZE/2 - 2,UP);
	head->next->next->next = new Node(SIZE/2,SIZE/2-3,UP);
	head->next->next->next->next = new Node(SIZE/2,SIZE/2-4,UP);
	head->next->next->next->next->next = nullptr;
	float speed = 150;
	sf::SoundBuffer soundBuffer;
	soundBuffer.loadFromFile("boop.wav");
	sf::Sound sound;
	sound.setBuffer(soundBuffer);
	gameOver = false;
	  srand (time(NULL));

	//sq[32][32] = UP;
	//sq[32][31] = UP;
	//sq[32][30] = UP;
	//sq[32][29] = UP;
	//sq[32][28] = UP;
	for(int x = 0; x <SIZE; x++){
		for( int y = 0; y < SIZE; y++){
			blacklist[x][y] = false;
		}
	}
	

	sf::RenderWindow window(sf::VideoMode(640, 640, 32), "Snake", sf::Style::Titlebar | sf::Style::Close);
	sf::Clock clock;
	clock.restart();
	sf::Event windowEvent;
	renderTexture.create(640,640);
	renderTexture.display();
	sf::Texture gameOverScreen;
	gameOverScreen.loadFromFile("gameover.png", sf::IntRect(320,192,640,640));
	sf::Sprite sprite;
	sprite.setTexture(gameOverScreen);
	bool upPress, downPress, leftPress, rightPress = false;
	int addCount = 0;
	int soundCount = 0;

	while(window.isOpen()){
		bool anyEvent = false ;
		while (window.pollEvent(windowEvent))
		{//Event Handling
			anyEvent = true;
			switch (windowEvent.type)
			{
				//running = false;
			case sf::Event::KeyPressed:
				if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
					//std::cout << "left" << leftPress <<"\n";
					if(head->dir != RIGHT && !leftPress){
						head->dir = LEFT;
						leftPress = true;
					}
				}else{
					leftPress = false;
				}

				if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
					//std::cout << "right\n";
					if(head->dir != LEFT && !rightPress){
						head->dir = RIGHT;
						rightPress = true;
					}
				}else{
					//std::cout << "rightcall";
					rightPress = false;
				}

				if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){//inverted
					//std::cout << "down\n";
					if(head->dir != DOWN && !upPress){
						head->dir = UP;
						upPress = true;
					}
				}else{
					upPress = false;
				}


				if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){//inverted
					//std::cout << "up\n";
					if(head->dir != UP && !downPress){
						head->dir = DOWN;
						downPress = true;
					}
				}else{
					downPress = false;
				}

				break;

			case sf::Event::Closed:
				//delete head;
				window.close();
				break;

			}
		}//\EventHandling
		if(!anyEvent){
			leftPress = false;
			rightPress = false;
			upPress = false;
			downPress = false;
		}
		anyEvent = false;


		//for(int x = 0; x < 64; ++x){
		//	for(int y = 0; y < 64; ++y){
		sf::Time t = clock.getElapsedTime();
		if(t.asMilliseconds() > speed){
			updatePos(*head);
			updateDir(*head);
			soundCount++;
			if(soundCount > 5){
				sound.play();
				soundCount = 0;
			}
			clock.restart();
			if(!gameOver)
				gameOver = !checkBounds(*head);
			checkIntersect(*head);//gen blacklist
			if(addCount > 0){
				--addCount;
				Node* end = getEnd(*head);
				switch(end->dir){
				case UP:
					end->next = new Node(end->x, end->y - 1, end->dir);//backwards
					break;
				case DOWN:
					end->next = new Node(end->x, end->y + 1, end->dir);//backwards
					break;
				case LEFT:
					end->next = new Node(end->x + 1, end->y, end->dir);//backwards
					break;
				case RIGHT:
					end->next = new Node(end->x - 1, end->y, end->dir);//backwards
					break;}
				end->next->next = nullptr;
			}

			if(newFood){
				addCount = 4;
				if(speed >100){
				speed *= .95;
				//sound.setPitch(sound.getPitch()*1.1);
				}
				foodX = rand() % SIZE;
				foodY = rand() % SIZE;
				while(blacklist[foodX][foodY] == true){
					foodX = rand() % SIZE;
					foodY = rand() % SIZE;
				}

				newFood = false;
			}
		}
		if(gameOver){
			window.draw(sprite);
			window.display();
			gameOver = false;
			sf::Time t = sf::seconds(.5);
			for(int i = 0; i < 6; ++i)
			sf::sleep(t);
			delete head->next;
			head->x = SIZE/2;
			head->y = SIZE/2;
			head->next = new Node(SIZE/2,SIZE/2-1,UP);
			head->next->next = new Node(SIZE/2,SIZE/2 - 2,UP);
			head->next->next->next = new Node(SIZE/2,SIZE/2-3,UP);
			head->next->next->next->next = new Node(SIZE/2,SIZE/2-4,UP);
			head->next->next->next->next->next = nullptr;
			float speed = 150;
			foodX = SIZE*.75;
			foodY = SIZE*.75;
			newFood = false;
		}
		if(!gameOver){
			displaySnake(*head);
			
			sf::RectangleShape draw1 = sf::RectangleShape(sf::Vector2f(640/SIZE,640/SIZE));
			draw1.setPosition(sf::Vector2f(foodX * (640/SIZE), foodY * (640/SIZE)));
			draw1.setFillColor(sf::Color::White);
			renderTexture.draw(draw1);
		}
		sf::Texture texture = renderTexture.getTexture();
		// draw it to the window
		sf::Sprite sprite(texture);
		window.draw(sprite);
		window.display();
		renderTexture.clear();
		for(int x = 0; x < SIZE; x++){
			for( int y = 0; y < SIZE; y++){
				blacklist[x][y] = false;
			}
		}
	}
}
Ejemplo n.º 15
0
void LightDirectionEmission::render(const sf::View &view, sf::RenderTexture &lightTempTexture, sf::RenderTexture &antumbraTempTexture, const std::vector<QuadtreeOccupant*> &shapes, sf::Shader &unshadowShader, float shadowExtension) {
	lightTempTexture.setView(view);

	LightSystem::clear(lightTempTexture, sf::Color::White);

	// Mask off light shape (over-masking - mask too much, reveal penumbra/antumbra afterwards)
	for (int i = 0; i < shapes.size(); i++) {
		LightShape* pLightShape = static_cast<LightShape*>(shapes[i]);

		// Get boundaries
		std::vector<LightSystem::Penumbra> penumbras;
		std::vector<int> innerBoundaryIndices;
		std::vector<int> outerBoundaryIndices;
		std::vector<sf::Vector2f> innerBoundaryVectors;
		std::vector<sf::Vector2f> outerBoundaryVectors;

		LightSystem::getPenumbrasDirection(penumbras, innerBoundaryIndices, innerBoundaryVectors, outerBoundaryIndices, outerBoundaryVectors, pLightShape->_shape, _castDirection, _sourceRadius, _sourceDistance);

		if (innerBoundaryIndices.size() != 2 || outerBoundaryIndices.size() != 2)
			continue;

		LightSystem::clear(antumbraTempTexture, sf::Color::White);

		antumbraTempTexture.setView(view);

		sf::ConvexShape maskShape;

		float maxDist = 0.0f;

		for (int j = 0; j < pLightShape->_shape.getPointCount(); j++)
			maxDist = std::max(maxDist, vectorMagnitude(view.getCenter() - pLightShape->_shape.getTransform().transformPoint(pLightShape->_shape.getPoint(j))));

		float totalShadowExtension = shadowExtension + maxDist;

		maskShape.setPointCount(4);

		maskShape.setPoint(0, pLightShape->_shape.getTransform().transformPoint(pLightShape->_shape.getPoint(innerBoundaryIndices[0])));
		maskShape.setPoint(1, pLightShape->_shape.getTransform().transformPoint(pLightShape->_shape.getPoint(innerBoundaryIndices[1])));
		maskShape.setPoint(2, pLightShape->_shape.getTransform().transformPoint(pLightShape->_shape.getPoint(innerBoundaryIndices[1])) + vectorNormalize(innerBoundaryVectors[1]) * totalShadowExtension);
		maskShape.setPoint(3, pLightShape->_shape.getTransform().transformPoint(pLightShape->_shape.getPoint(innerBoundaryIndices[0])) + vectorNormalize(innerBoundaryVectors[0]) * totalShadowExtension);

		maskShape.setFillColor(sf::Color::Black);

		antumbraTempTexture.draw(maskShape);

		sf::VertexArray vertexArray;

		vertexArray.setPrimitiveType(sf::PrimitiveType::Triangles);

		vertexArray.resize(3);

		{
			sf::RenderStates states;
			states.blendMode = sf::BlendAdd;
			states.shader = &unshadowShader;

			// Unmask with penumbras
			for (int j = 0; j < penumbras.size(); j++) {
				unshadowShader.setParameter("lightBrightness", penumbras[j]._lightBrightness);
				unshadowShader.setParameter("darkBrightness", penumbras[j]._darkBrightness);

				vertexArray[0].position = penumbras[j]._source;
				vertexArray[1].position = penumbras[j]._source + vectorNormalize(penumbras[j]._lightEdge) * totalShadowExtension;
				vertexArray[2].position = penumbras[j]._source + vectorNormalize(penumbras[j]._darkEdge) * totalShadowExtension;

				vertexArray[0].texCoords = sf::Vector2f(0.0f, 1.0f);
				vertexArray[1].texCoords = sf::Vector2f(1.0f, 0.0f);
				vertexArray[2].texCoords = sf::Vector2f(0.0f, 0.0f);

				antumbraTempTexture.draw(vertexArray, states);
			}
		}

		antumbraTempTexture.display();

		// Multiply back to lightTempTexture
		sf::RenderStates antumbraRenderStates;
		antumbraRenderStates.blendMode = sf::BlendMultiply;

		sf::Sprite s;

		s.setTexture(antumbraTempTexture.getTexture());

		lightTempTexture.setView(lightTempTexture.getDefaultView());

		lightTempTexture.draw(s, antumbraRenderStates);

		lightTempTexture.setView(view);
	}

	for (int i = 0; i < shapes.size(); i++) {
		LightShape* pLightShape = static_cast<LightShape*>(shapes[i]);

		if (pLightShape->_renderLightOverShape) {
			pLightShape->_shape.setFillColor(sf::Color::White);

			lightTempTexture.draw(pLightShape->_shape);
		}
	}

	// Multiplicatively blend the light over the shadows
	sf::RenderStates lightRenderStates;
	lightRenderStates.blendMode = sf::BlendMultiply;

	lightTempTexture.setView(lightTempTexture.getDefaultView());

	lightTempTexture.draw(_emissionSprite, lightRenderStates);

	lightTempTexture.display();
}
Ejemplo n.º 16
0
void AppStateSplashScreen::render_splash(sf::RenderTexture& texture, int frame)
{
  texture.draw(m_splash_sprite);
  texture.display();
}
Ejemplo n.º 17
0
void LightPointEmission::render(const sf::View &view, sf::RenderTexture &lightTempTexture, sf::RenderTexture &emissionTempTexture, sf::RenderTexture &antumbraTempTexture, const std::vector<QuadtreeOccupant*> &shapes, sf::Shader &unshadowShader, sf::Shader &lightOverShapeShader) {
	LightSystem::clear(emissionTempTexture, sf::Color::Black);

	emissionTempTexture.setView(view);

	emissionTempTexture.draw(_emissionSprite);

	emissionTempTexture.display();
	
	LightSystem::clear(lightTempTexture, sf::Color::Black);

	lightTempTexture.setView(view);

	lightTempTexture.draw(_emissionSprite);

	sf::Transform t;
	t.translate(_emissionSprite.getPosition());
	t.rotate(_emissionSprite.getRotation());
	t.scale(_emissionSprite.getScale());

	sf::Vector2f castCenter = t.transformPoint(_localCastCenter);

	float shadowExtension = _shadowOverExtendMultiplier * (getAABB().width + getAABB().height);

	struct OuterEdges {
		std::vector<int> _outerBoundaryIndices;
		std::vector<sf::Vector2f> _outerBoundaryVectors;
	};

	std::vector<OuterEdges> outerEdges(shapes.size());

	// Mask off light shape (over-masking - mask too much, reveal penumbra/antumbra afterwards)
	for (unsigned i = 0; i < shapes.size(); i++) {
		LightShape* pLightShape = static_cast<LightShape*>(shapes[i]);

		// Get boundaries
		std::vector<int> innerBoundaryIndices;
		std::vector<sf::Vector2f> innerBoundaryVectors;
		std::vector<LightSystem::Penumbra> penumbras;

		LightSystem::getPenumbrasPoint(penumbras, innerBoundaryIndices, innerBoundaryVectors, outerEdges[i]._outerBoundaryIndices, outerEdges[i]._outerBoundaryVectors, pLightShape->_shape, castCenter, _sourceRadius);

		if (innerBoundaryIndices.size() != 2 || outerEdges[i]._outerBoundaryIndices.size() != 2)
			continue;

		// Render shape
		if (!pLightShape->_renderLightOverShape) {
			pLightShape->_shape.setFillColor(sf::Color::Black);

			lightTempTexture.draw(pLightShape->_shape);
		}

		sf::RenderStates maskRenderStates;
		maskRenderStates.blendMode = sf::BlendNone;

		sf::Vector2f as = pLightShape->_shape.getTransform().transformPoint(pLightShape->_shape.getPoint(outerEdges[i]._outerBoundaryIndices[0]));
		sf::Vector2f bs = pLightShape->_shape.getTransform().transformPoint(pLightShape->_shape.getPoint(outerEdges[i]._outerBoundaryIndices[1]));
		sf::Vector2f ad = outerEdges[i]._outerBoundaryVectors[0];
		sf::Vector2f bd = outerEdges[i]._outerBoundaryVectors[1];

		sf::Vector2f intersectionOuter;

		// Handle antumbras as a seperate case
		if (rayIntersect(as, ad, bs, bd, intersectionOuter)) {
			sf::Vector2f asi = pLightShape->_shape.getTransform().transformPoint(pLightShape->_shape.getPoint(innerBoundaryIndices[0]));
			sf::Vector2f bsi = pLightShape->_shape.getTransform().transformPoint(pLightShape->_shape.getPoint(innerBoundaryIndices[1]));
			sf::Vector2f adi = innerBoundaryVectors[0];
			sf::Vector2f bdi = innerBoundaryVectors[1];

			LightSystem::clear(antumbraTempTexture, sf::Color::White);

			antumbraTempTexture.setView(view);

			sf::Vector2f intersectionInner;

			if (rayIntersect(asi, adi, bsi, bdi, intersectionInner)) {
				sf::ConvexShape maskShape;

				maskShape.setPointCount(3);

				maskShape.setPoint(0, asi);
				maskShape.setPoint(1, bsi);
				maskShape.setPoint(2, intersectionInner);

				maskShape.setFillColor(sf::Color::Black);

				antumbraTempTexture.draw(maskShape);	
			}
			else {
				sf::ConvexShape maskShape;

				maskShape.setPointCount(4);

				maskShape.setPoint(0, asi);
				maskShape.setPoint(1, bsi);
				maskShape.setPoint(2, bsi + vectorNormalize(bdi) * shadowExtension);
				maskShape.setPoint(3, asi + vectorNormalize(adi) * shadowExtension);

				maskShape.setFillColor(sf::Color::Black);

				antumbraTempTexture.draw(maskShape);
			}

			// Add light back for antumbra/penumbras
			sf::VertexArray vertexArray;

			vertexArray.setPrimitiveType(sf::PrimitiveType::Triangles);

			vertexArray.resize(3);

			sf::RenderStates penumbraRenderStates;
			penumbraRenderStates.blendMode = sf::BlendAdd;
			penumbraRenderStates.shader = &unshadowShader;

			// Unmask with penumbras
			for (unsigned j = 0; j < penumbras.size(); j++) {
				unshadowShader.setParameter("lightBrightness", penumbras[j]._lightBrightness);
				unshadowShader.setParameter("darkBrightness", penumbras[j]._darkBrightness);

				vertexArray[0].position = penumbras[j]._source;
				vertexArray[1].position = penumbras[j]._source + vectorNormalize(penumbras[j]._lightEdge) * shadowExtension;
				vertexArray[2].position = penumbras[j]._source + vectorNormalize(penumbras[j]._darkEdge) * shadowExtension;

				vertexArray[0].texCoords = sf::Vector2f(0.0f, 1.0f);
				vertexArray[1].texCoords = sf::Vector2f(1.0f, 0.0f);
				vertexArray[2].texCoords = sf::Vector2f(0.0f, 0.0f);

				antumbraTempTexture.draw(vertexArray, penumbraRenderStates);
			}

			antumbraTempTexture.display();

			// Multiply back to lightTempTexture
			sf::RenderStates antumbraRenderStates;
			antumbraRenderStates.blendMode = sf::BlendMultiply;

			sf::Sprite s;

			s.setTexture(antumbraTempTexture.getTexture());

			lightTempTexture.setView(lightTempTexture.getDefaultView());

			lightTempTexture.draw(s, antumbraRenderStates);

			lightTempTexture.setView(view);
		}
		else {
			sf::ConvexShape maskShape;

			maskShape.setPointCount(4);

			maskShape.setPoint(0, as);
			maskShape.setPoint(1, bs);
			maskShape.setPoint(2, bs + vectorNormalize(bd) * shadowExtension);
			maskShape.setPoint(3, as + vectorNormalize(ad) * shadowExtension);

			maskShape.setFillColor(sf::Color::Black);

			lightTempTexture.draw(maskShape);

			sf::VertexArray vertexArray;

			vertexArray.setPrimitiveType(sf::PrimitiveType::Triangles);

			vertexArray.resize(3);

			sf::RenderStates penumbraRenderStates;
			penumbraRenderStates.blendMode = sf::BlendMultiply;
			penumbraRenderStates.shader = &unshadowShader;

			// Unmask with penumbras
			for (unsigned j = 0; j < penumbras.size(); j++) {
				unshadowShader.setParameter("lightBrightness", penumbras[j]._lightBrightness);
				unshadowShader.setParameter("darkBrightness", penumbras[j]._darkBrightness);

				vertexArray[0].position = penumbras[j]._source;
				vertexArray[1].position = penumbras[j]._source + vectorNormalize(penumbras[j]._lightEdge) * shadowExtension;
				vertexArray[2].position = penumbras[j]._source + vectorNormalize(penumbras[j]._darkEdge) * shadowExtension;

				vertexArray[0].texCoords = sf::Vector2f(0.0f, 1.0f);
				vertexArray[1].texCoords = sf::Vector2f(1.0f, 0.0f);
				vertexArray[2].texCoords = sf::Vector2f(0.0f, 0.0f);

				lightTempTexture.draw(vertexArray, penumbraRenderStates);
			}
		}
	}

	for (unsigned i = 0; i < shapes.size(); i++) {
		LightShape* pLightShape = static_cast<LightShape*>(shapes[i]);

		if (pLightShape->_renderLightOverShape) {
			pLightShape->_shape.setFillColor(sf::Color::White);

			lightTempTexture.draw(pLightShape->_shape, &lightOverShapeShader);
		}
		else {
			pLightShape->_shape.setFillColor(sf::Color::Black);

			lightTempTexture.draw(pLightShape->_shape);
		}
	}

	lightTempTexture.display();
}
Ejemplo n.º 18
0
  void onRender(sf::RenderWindow &window, double time, double deltaTime)
  {
    double thermalViewYawRange = thermalViewPitchRange/windowHeight*windowWidth;
    double vxmin = 0.0;//viewYaw-thermalViewYawRange/2.0;
    double vxmax = 360.0;//viewYaw+thermalViewYawRange/2.0;
    double vymin = 0;//viewPitch-thermalViewPitchRange/2.0;
    double vymax = thermalObservedPitchRange;//viewPitch+thermalViewPitchRange/2.0;

    double cursorTemp = 0.0;
    double cursorOverlap = 0.0;

    window.clear(sf::Color::Black);

    sf::RectangleShape pixel;
    //pixel.setSize(sf::Vector2f(thermalViewPixelSize, thermalViewPixelSize));

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::P)) {
      viewImage.clear();
      for(int px = 0; px < windowWidth; px += thermalViewPixelSize) {
        for(int py = 0; py < windowHeight; py += thermalViewPixelSize) {
          updatedPixels.push_back(sf::Vector2<int>(px, py));
        }
      }
    } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::C)) {
      /*std::vector<RectTree<SensorReading>::RectResult> resList;
      readingsTree.retrieveAllRects(resList);
      readingsTree.removeResults(resList);*/
      viewImage.clear();
      readingsTree.reset(0.0, 0.0, 360.0, thermalObservedPitchRange, 4);
    }

    //std::deque< sf::Vector2<int> >::iterator iter = updatedPixels.begin();
    auto iter = updatedPixels.begin();
    for(; iter != updatedPixels.end(); iter++) {
      int px = iter->x, py = iter->y;

      double vx = vxmin+(vxmax-vxmin)*((double)px/(double)windowWidth );
      double vy = vymin+(vymax-vymin)*((double)py/(double)windowHeight);
      double vw = thermalPixelSizeYaw;
      double vh = thermalPixelSizePitch;

      std::vector<RectTree<SensorReading>::RectResult> resList;
      readingsTree.findRectsAtPoint(vx, vy, resList);

      std::vector<RectTree<SensorReading>::RectResult> delList;

      pixel.setPosition(sf::Vector2f(px, py));
      pixel.setSize(sf::Vector2f(
        thermalViewPixelSize,
        thermalViewPixelSize //(double)thermalViewPixelSize/(double)windowWidth*(double)windowHeight
      ));

      double temp = 0.0; int count = 0; double sum = 0;
      RectTree<SensorReading>::RectResult *oldestRes = NULL; double oldestTime = 0.0;
      std::vector<RectTree<SensorReading>::RectResult>::iterator iter = resList.begin();
      for(; iter != resList.end(); iter++) {
        RectTree<SensorReading>::RectResult &res = *iter;
        double age = time-res.rect->value.time;
        /*if(res.rect->value.time < oldestTime || oldestRes == NULL) {
          oldestTime = res.rect->value.time;
          oldestRes = &res;
        }*/
        double ageScalar = 1.0/(1.0+age);
        temp += res.rect->value.temp*ageScalar;
        sum += ageScalar;
        count++;
      }
      if(count > 40) {
        delList.insert(delList.end(), resList.begin(), resList.begin()+10);
      }
      temp /= sum;
      if(count > 0) {
        pixel.setFillColor(getThermalPixelColor(temp));
      } else {
        pixel.setFillColor(sf::Color::Black);
      }
      viewImage.draw(pixel);
      if(
            cursorX > px && cursorX < px+thermalViewPixelSize
        &&  cursorY > py && cursorY < py+thermalViewPixelSize
      ) {
        cursorTemp = temp;
      }

      readingsTree.removeResults(delList);
    }
    int updatedPixelCount = updatedPixels.size();
    updatedPixels.clear();

    viewImage.display();
    sf::Sprite sprite(viewImage.getTexture());
    window.draw(sprite);


    if(0) {
      std::vector<RectTree<SensorReading>::RectResult> resList;
      readingsTree.retrieveAllRects(resList);

      std::vector<RectTree<SensorReading>::RectResult>::iterator iter = resList.begin();
      for(; iter != resList.end(); iter++) {
        RectTree<SensorReading>::RectResult &res = *iter;
        double px = res.rect->x/vxmax*windowWidth;
        double py = res.rect->y/vymax*windowHeight;
        double pw = res.rect->w/vxmax*windowWidth;
        double ph = res.rect->h/vymax*windowHeight;
        pixel.setFillColor(sf::Color(255*(double)res.rect->value.temp/30.0, 50, 50, 100));
        pixel.setPosition(sf::Vector2f(px, py));
        pixel.setSize(sf::Vector2f(pw, ph));
        sf::RenderStates rs;
        rs.blendMode = sf::BlendAlpha;
        window.draw(pixel);
      }
    }
    if(1) {
      pixel.setFillColor(sf::Color(255, 255, 255));
      std::vector<RectTree<SensorReading>::RectResult> resList;
      double cvx = (double)cursorX/(double)windowWidth*vxmax;
      double cvy = (double)cursorY/(double)windowHeight*vymax;
      readingsTree.findRectsAtPoint(cvx, cvy, resList);

      updatedPixels.push_back(sf::Vector2<int>(
        floor(cursorX/thermalViewPixelSize)*thermalViewPixelSize,
        floor(cursorY/thermalViewPixelSize)*thermalViewPixelSize
      ));
      
      std::vector<RectTree<SensorReading>::RectResult>::iterator iter = resList.begin();
      for(; iter != resList.end(); iter++) {
        RectTree<SensorReading>::RectResult &res = *iter;
        double px = res.rect->x/vxmax*windowWidth;
        double py = res.rect->y/vymax*windowHeight;
        double pw = res.rect->w/vxmax*windowWidth;
        double ph = res.rect->h/vymax*windowHeight;
        pixel.setPosition(sf::Vector2f(px, py));
        pixel.setSize(sf::Vector2f(pw, ph));
        window.draw(pixel);
        cursorOverlap++;
      }

      // RectTree<SensorReading>::Node &node = readingsTree.getNodeAtPoint(cvx, cvy);
      // pixel.setPosition(sf::Vector2f(node.x/vxmax*windowWidth, node.y/vymax*windowHeight));
      // pixel.setSize(sf::Vector2f(node.w/vxmax*windowWidth, node.h/vymax*windowHeight));
      // pixel.setFillColor(sf::Color::Green);
      // window.draw(pixel);
    }

    sf::Text text;
    text.setFont(font);
    std::stringstream ss;
    ss << viewYaw << ", " << viewPitch << "; "
       << drAlpha << ", " << drGamma
       << " -- " << cursorTemp << ", " << cursorOverlap
       << " -- " << 1/deltaTime << "FPS, " << updatedPixelCount
    ;
    text.setString(ss.str());
    text.setCharacterSize(24);
    text.setColor(sf::Color::White);

    window.draw(text);
    window.display();
  }