Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Is it possible to manipulate each pixel of a texture?  (Read 6215 times)

0 Members and 1 Guest are viewing this topic.

antares

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Is it possible to manipulate each pixel of a texture?
« on: April 04, 2018, 09:00:29 am »
I am trying to create a texture pixel by pixel, which I later on will be passing to a fragment shader. Is there any way to achieve this?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Is it possible to manipulate each pixel of a texture?
« Reply #1 on: April 04, 2018, 09:07:26 am »
Why not use the fragment shader from the start?

You can update a texture from a pixel array or an sf::Image, you can't manipulate a texture directly, except by using a shader.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Martin Sand

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • Email
Re: Is it possible to manipulate each pixel of a texture?
« Reply #2 on: April 05, 2018, 10:20:22 pm »
I agree with eXpl0it3r. If you want to understand the technique to manipulate pixels in an image, here is a code example.
Please note that you need a background.png image

#include <iostream>
#include <SFML/Graphics.hpp>

sf::Color setPixelColor(sf::Texture& texture, int x, int y, sf::Color color){
  // Get texture image
  sf::Image image = texture.copyToImage();

  // Optional show case: read size of image (not used in this function)
  int width = image.getSize().x;
  int height = image.getSize().y;

  // Get color of specific position
  sf::Color imagecolor = image.getPixel(x, y);

  // Just an example: if alpha is above 128, make it green
  if (imagecolor.a > 128){
    imagecolor.g = 255;
  }

  // Set pixel to color
  image.setPixel(x, y, color);

  // Update texture
  texture.loadFromImage(image);
}

int main(){
  sf::RenderWindow screen(sf::VideoMode(900, 600), "window");

  sf::Sprite background;
  sf::Texture texture;

  if (!texture.loadFromFile("background.png")){
     std::cout << "Error loading texture" << std::endl;
  }
  background.setTexture(texture);

  while (screen.isOpen()){
    sf::Event event;
    while (screen.pollEvent(event)){
      if (sf::Event::Closed == event.type){
        screen.close();
        break;
      }
    }

    setPixelColor(texture, 10, 10, sf::Color::Green);

    screen.clear();
    screen.draw(background);
    screen.display();
  }
}