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

Author Topic: Wanting suggestions for improving "lighting" performance  (Read 3452 times)

0 Members and 2 Guests are viewing this topic.

Proply

  • Newbie
  • *
  • Posts: 7
    • View Profile
Wanting suggestions for improving "lighting" performance
« on: April 26, 2013, 06:38:01 am »
Hi All, I am building a game for fun/learning using SFML that basically simulates a planet. The surface of the planet is represented through a layer of square terrain sprites, with sprites of other objects such as trees or clouds drawn on layers above this base. When fully zoomed out there can be anywhere up to 36K 8x8 px terrain sprites drawn on my 1920x1200 screen at a time, with sprites on subsequent layers further increasing this count. Obviously this results in low FPS, and I am looking at ways to improve this (specifically I need to investigate the suitability of vertexArrays and/or tileMaps which I currently know nothing about, or as a last resort just limit zoom!), but I have a specific question regarding my implementation of lighting for a day/night cycle. Each tile on my world map has an insolation value (amount of light from sun hitting this location), and to illustrate this value the final graphical layer is an array of black tiles the same size as the terrain tiles underneath, with an opacity value applied to each tile that is the inverse of the insolation value - so in light areas most or all of the tiles underneath the black tile layer are visible, but in dark regions very little makes it through the opacity filter. Obviously as this is the equivalent of drawing the entire terrain layer again it reduces performance significantly.

My question is if this is the lowest-cost way of achieving this effect, or if there is some way of shading each terrain tile/tree/cloud/etc sprite as they are drawn to achieve the same effect that would be faster, or perhaps there is some other method that I am not aware of that would be helpful. Feel free to offer advice on improving performance of displaying my terrain tile array/trees/clouds etc if you wish also, but I have yet to research this thoroughly myself.

-Thank you for your attention.

[Edit]: spelling

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Wanting suggestions for improving "lighting" performance
« Reply #1 on: April 26, 2013, 07:54:37 am »
Vertex arrays will definitely improve your performances a lot.

For the lighting effect, you'd better change the color of the sprites/vertices instead of drawing a whole layer of black sprites. To do so you have two options (given that you've already switched to a vertex array):
- edit the color member of each sf::Vertex in your vertex array
- use a fragment shader; you could store the insolation value of each vertex in its color component, then extract/use it in the fragment shader according to some global parameters (sun intensity, ...), and compute the final color to apply to the vertex
Laurent Gomila - SFML developer

Proply

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Wanting suggestions for improving "lighting" performance
« Reply #2 on: April 27, 2013, 12:58:17 pm »
Thanks for the reply, will update once I have implemented your suggestions (or tried to at least!).

Proply

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Wanting suggestions for improving "lighting" performance
« Reply #3 on: April 28, 2013, 07:55:55 am »
Is it only possible to apply one texture to a vertex array? My terrain tile map consists of many different types of terrain and therefore many types of textures need displaying on the screen.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Wanting suggestions for improving "lighting" performance
« Reply #4 on: April 28, 2013, 09:47:07 am »
Yes, you can only use one texture per vertex array. Tiles are usually grouped into a single texture (the tileset), so this is rarely a problem.
Laurent Gomila - SFML developer

Proply

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Wanting suggestions for improving "lighting" performance
« Reply #5 on: April 28, 2013, 11:44:17 am »
Of course, thankyou! I knew I must have been missing something obvious.

Proply

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Wanting suggestions for improving "lighting" performance
« Reply #6 on: April 30, 2013, 01:56:06 pm »
Ok I have implemented vertex arrays for displaying terrain tiles, but am now running into a problem re-implementing the zoom functionality. Previously all terrain tiles were sprites, so i simply used setScale() to scale the tiles to any square from 8x8 px to 64x64 px, resulting in a nice smooth zoom effect. The problem with zooming using vertex arrays is that sf::Texture is not scalable. What is the best alternative to including a tileset for every desired zoom level? (which would be fast but uses quite a lot of video memory).

-Thanks, Proply.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Wanting suggestions for improving "lighting" performance
« Reply #7 on: April 30, 2013, 02:07:56 pm »
So you want to zoom (scale) your entire vertex array? Look at the "Transforming vertex arrays" section of the vertex array tutorial.

You can also use a view, if you want to scale things globally.
Laurent Gomila - SFML developer

 

anything