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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - fallahn

Pages: 1 ... 4 5 [6] 7 8 ... 33
76
General / Re: How to draw a cubemap on SFML with OpenGL
« on: November 29, 2020, 12:20:03 pm »
glew is also an option (it's an OpenGL function loader) but is perhaps a bit out of date. A quick search reveals a few options:

https://github.com/google/galogen
https://glad.dav1d.de/
https://github.com/skaslev/gl3w

and of course glew. There's a wiki page on the kronos site dedicated to opengl loaders: https://www.khronos.org/opengl/wiki/OpenGL_Loading_Library

Choose one whichever one best suits your project  :)

77
General / Re: How to draw a cubemap on SFML with OpenGL
« on: November 28, 2020, 10:37:16 am »
SFML only provides the most basic OpenGL functions via OpenGL.hpp. You can load the others yourself with a loader such as GLad : https://glad.dav1d.de/

Note that if you're using pure OpenGL you can target any version your hardware supports - however if you want to mix your OpenGL with the SFML graphics module you require a compatible version (this is what SFML uses: https://github.com/SFML/SFML/blob/master/extlibs/headers/glad/include/glad/gl.h)

From there you should be able to load your cubemap using standard OpenGL calls, as per any tutorial  ;D

78
I've tested this, and it works for me. I'm using Visual Studio 2019 with the latest revision of SFML. Can you share which platform you're using (OS/compiler etc) so other people can try and reproduce the problem? Also, what's your keyboard layout?

79
Pull Requests & Testing / Audio effects: Reverb, chrous, delay etc
« on: November 10, 2020, 08:03:11 pm »
https://github.com/SFML/SFML/pull/1708

Implements OpenAL sound effects for reverb, chorus and delay. Can be easily extended to more types supported by OpenAL.
This was discussed some time (10 years!) ago here: https://en.sfml-dev.org/forums/index.php?topic=2245.0 the conclusion being that available OpenAL implementations were not capable. Thankfully things have changed somewhat in the last decade. I've tested this on platforms using openal-soft (windows and ubuntu) and on macOS using Apple's OpenAL library. I was pleasantly surprised to find that the effects are supported by the Apple library, although they are audibly rendered differently - particularly the delay which sounds much softer. However it seems openal-soft is supported on macOS and building SFML against that makes the effects sound consistent across tested platforms. I'm unable to test on mobile platforms.

I'm putting this here to gauge interest - if there's enough feedback I'll add high/low/band pass filters.

80
General / Re: Is fading a single tile of a tilemap in and out efficient?
« on: November 09, 2020, 02:14:30 pm »
There's no need to recreate the entire array. Assuming you have 4 vertices per tile you'll have something like

std::vector<sf::vertex> verts(800);

To update the transparency of a single tile do

verts[8].color = sf::Color::Transparent;
verts[9].color = sf::Color::Transparent;
verts[10].color = sf::Color::Transparent;
verts[11].color = sf::Color::Transparent;
 

You can easily find out your starting index by looking at the grid index of the tile you want to modify and multiplying it by 4. It can even be simplified to

for(auto i = gridIndex; i < gridIndex + 4; ++i)
{
    verts[i].color = newColour;
}
 

81
Graphics / Re: Little help with 'getPixelPtr()'
« on: October 23, 2020, 06:21:54 pm »
You're welcome, I hope it's helpful. I haven't studied ray tracing techniques before, it was interesting to learn about  ;D

82
General / Re: Turning values into pixels for perlin noise
« on: October 23, 2020, 06:20:27 pm »
int pixel_w = (int)(noise[y * SCREEN_HEIGHT + x] * 12.0f);

probably ought to be

int pixel_w = (int)(noise[y * SCREEN_WIDTH + x] * 12.0f);

Also it's more memory access friendly if you loop like this:
for (int y{ 0 }; y < SCREEN_HEIGHT; ++y)
{
    for (int x{ 0 }; x < SCREEN_WIDTH; ++x)
    {

    }
}
 

as you're not skipping SCREEN_WIDTH values at a time, you'll be accessing your array sequentially. If you print pixel_w to the console you'll see what I mean  ;D

83
Graphics / Re: Little help with 'getPixelPtr()'
« on: October 19, 2020, 03:21:30 pm »
Hi,

So I've had a look at this and here are a few comments:

Firstly the fact that you use sf::Image to load the textures is a good thing. It means that the images are automatically in RGBA format and no conversion is being done on them. In fact when I looked to see how often the conversion function was being called there were only two places: here and here. The fact that the colour values are called explicitly on them makes it easy to remove the conversion - just change 0xff777777 to 0x777777ff! Hopefully my previous explanations should make this obvious as to why this is.

Elsewhere I noticed this function which doesn't appear to be used anywhere, but should you decide to use it will have the bytes of the colour value in the wrong order. However this should be trivial to correct so I shall leave this as an exercise for you ;)

As for performance the only potential bottleneck I noticed was here. If you change colorBufferTexture->loadFromImage() to colorBufferTexture->update() you're likely to get an improvement as loadFromImage() probably recreates the texture each time, instead of updating the existing one.

I also noticed this line which doesn't actually do anything to affect the output and can be removed completely, as the output of generate3DProjection() overwrites everything done by it. static_cast<sf::Uint8>(0xFF000000) is also somewhat redundant - you merely end up truncating a 32bit value to 00.

Overall, having run the program through visual studio's profiler (Debug->performance profiler), the hottest function appears to be generate3DProjection() - which is fair enough as that's where the pixel by pixel copies are happening. My benchmarks in release mode showed ~150fps or average of 6ms per frame. This seems reasonable considering most games target 60 or even 30fps, but without anything to compare it with, for example the performance of the original SDL version, I couldn't say if this is a really good benchmark or not.

84
Audio / Re: Analyzing "loudness" of an audio input.
« on: October 13, 2020, 02:14:42 pm »
This is possible with a class which inherits sf::SoundRecorder and implements onProcessSamples():
https://www.sfml-dev.org/tutorials/2.5/audio-recording.php#custom-recording

I did something similar a few years back which you might be able to adapt: https://github.com/fallahn/scratchvaders/blob/master/src/AudioIn.cpp#L58

85
I can't say for sure what causes the crash, but you should probably not be processing your drawables between window.clear() and wndow.display(). The array is also VERY large to be calling draw() on for each square - this is usually a source for a bottleneck and keeping the number of times you call draw() to a minimum is preferable. To do this you could look at using a vertex array https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php

With this you can first process all your geometry outside of the clear/draw/display loop into maybe one or two vertex arrays, and then just draw those.

86
Graphics / Re: Little help with 'getPixelPtr()'
« on: October 13, 2020, 01:56:14 pm »
sf::Image is fundamentally a wrapper around an array of bytes representing the pixels, with a few utility functions to make setting pixel values a bit simpler. You could do the same thing yourself with a std::vector<sf::Uint8> and a function for setting the pixel values at a given index. To see how sf::Image does this you can look at the source: https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/Image.cpp

Image file loading is a bit more involved - image files are rarely stored as raw colour data, usually opting for some kind of compression. What this compression is and how it's implemented varies wildly. Fortunately sf::Image hides that away for us, using a library called stb_image under the hood. The source for that library is publicly available and a quick glance at it will tell you most image loading is non-trivial to say the least. Hooray for sf::Image looking after that for us! https://github.com/nothings/stb/blob/master/stb_image.h

The reason the pixel array is W x H x 4 is because W and H are measured in *pixels* - not bytes - where each pixel has a value of 4 bytes - R,G,B and A. When dealing with the pixelPtr() function you'll always need to bear this in mind.

To your problem of performance: this is almost certainly down to the conversion process (as I mentioned in the previous topic). Ideally you want to eliminate this conversion completely. There are going to be two ways of doing this - make your destination ARGB compatible or make your source generate RGBA data. While it is technically possible to alter the format of sf::Texture it's hacky and convoluted, so in this case let's consider it not possible. This leaves us with modifying the source to generate RGBA data.

What I can say here is that SDL will support RGBA pixels, but I couldn't say much else without seeing the example code you're working from. I am pretty sure, however, that as you're converting code anyway it's totally possible to make it output RGBA pixels for your render target, and this is likely the way to go. Perhaps you could share the source you're working from?

87
IIRC this is a quirk in visual studio - you need to add at least one cpp file to your project (like main.cpp) and the properties will then appear.

88
Setting up a pixel buffer with SFML is quite easy using the built in classes. Instead of an array  you can use sf::Image which has functions for setting the colours for individual pixels. Once you finish updating the pixels for a frame, copy it to a texture using sf::Texture::update(). You can draw this texture as you normally would using sf::Sprite

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");

    //this is your pixel buffer
    sf::Image img;
    img.create(50,50);

    sf::Texture tex;
    tex.create(50, 50);

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

        //do ALL pixel buffer operations
        for(auto y = 10; y < 20; ++y)
        {
            for(auto x = 10; x < 20; ++x)
            {
                img.setPixel(x, y, sf::Color::Green);
            }
        }

        //update the texture from the pixel buffer
        tex.update(img);

        window.clear();
        window.draw(sf::Sprite(tex));
        window.display();
    }

    return 0;
}

You can of course use an array instead of an Image and use that to update the texture. In this case a std::vector<sf::Uint8> would do - no need to 'new' anything, the vector takes care of memory management.

The problem here is, as you point out, the pixel format. The reason the SDL code uses Uint32 is that it represents the 4 colour channels packed into a single value. SFML uses a byte for each channel which is why just a simple cast will not work. This also affects how you calculate the size of the buffer. WIDTH x HEIGHT is fine for Uint32, but when creating a buffer of Uint8 values you will need WIDTH x HEIGHT x 4.

 sf::Color will take a uint32 value and convert it to bytes automatically, but as you also point out SFML expects RGBA not ARGB values. You could potentially convert these but they will be very slow operations if done on EVERY pixel, especially at higher resolutions.

sf::Uint8 r = (pixel & 0x00ff0000) >> 16;
sf::Uint8 g = (pixel & 0x0000ff00) >> 8
sf::Uint8 b = (pixel & 0x000000ff);
sf::Uint8 a = (pixel & 0xff000000) >> 24;
 

or to an RGBA uint32:

sf::Uint32 rgbaPixel = ((pixel & 0xff000000) >> 24) | ((pixel & 0x00ffffff) << 8);
 

Another tack would be to use OpenGL directly to modify the sf::Texture to accept ARGB - this would save performing any pixel conversions but would considerably complicate the setup of the pixel buffer. It would also prevent using an sf::Image.

Utlimately (as you're converting the code anyway) the best bet is to find where the pixel values are actually generated and modify the code to create RGBA output, either as packed uint32 or individual uint8 channels.

89
Graphics / Re: I can't get the math right.
« on: October 03, 2020, 12:42:54 pm »
(Assuming I understand correctly:) If you want the point to be fixed at 90 degrees to the direction of the player you can actually take the rotation out of this and just use vectors.

First find the direction from the scope to the player

auto direction = scope.getPosition() - player.getPosition();

Then normalise the direction (so that it has a length of 1)

direction = normalise(direction);

To rotate a vector 90 degrees you can swap its components then negate one of them. Which one you negate affects whether the vector is rotated +/- 90 degrees.

direction = { -direction.y, direction.x };

or

direction = { direction.y, -direction.x };

Multiply the direction by the radius of the scope to bring it out to the edge:

direction *= radius;

Then finally add it back to the position of the circle.

auto finalPoint = scope.getPosition() + direction;


In general though (and cases where you want angles other than 90 degrees) this sounds like a good case for a Scene Graph, where the square is parented to the circle, which is then parented to the player. There are SFML specific tutorials on how to create a scene graph in the tutorial section and, once again (I swear I'm not sponsored ;)), in the SFML Game Development book:

https://www.sfml-dev.org/tutorials/2.5/graphics-transform.php#object-hierarchies-scene-graph
https://github.com/SFML/SFML-Game-Development-Book/blob/master/03_World/Include/Book/SceneNode.hpp

90
Graphics / Re: Setting the origin of sf::ConvexShapes.
« on: September 27, 2020, 04:39:05 pm »
Convex shapes *do* have local origins. When you call setPoint() the position is relative to that origin. If you want to move one of your shapes just use setPosition() as the convex shape class inherits sf::Transformable as other drawables do.

Pages: 1 ... 4 5 [6] 7 8 ... 33