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 - Nekroze

Pages: 1 [2]
16
Graphics / Re: VertexArray
« on: November 17, 2012, 08:59:55 am »
Cool that answered a lot of my questions.

As far as reaching the max of a float while it isn't likely to happen it would be possible in my game and i would rather not put limits that i don't have to. with a 32 bit float and 64x64 tile chunks with each tile 32x32 that would impose a limit of 4882x4882 chunks where the current system i have will allow as many chunks as the max an int can store on the host system, needless to say a lot more then 4882x4882.

By perspective i mean some way to make it so you are not looking straight down on perfectly square tiles, something so that tiles in the distance are smaller or something to give the illusion that the top of the screen is farther away.

17
Graphics / Re: VertexArray
« on: November 16, 2012, 11:03:52 pm »
Quote
You can (and should) create the chunk's vertices directly at their final position. For runtime you can them use sf::View to scroll the map.

By this i assume you would say that if i was using a vertex array, my current system of storing each chunks screen position and the character moves by moving all chunks to the opposite direction to what the player wants to move, would be incorrect?

See i did this so that i didn't have to move a view across a pseudo-infinite map because i figured that eventually the coordinates would be larger then the floats and int's that store the positions of things. So this way the only coordinates that ever need to be used are all pixel positions in the screen plus and minus about 500-1000 max.

First question then, am i right that placing down the vertex arrays in absolute positions and using the sfView to move the character around will, eventually, hit undefined behavior with coordinates beyond a float's range for example.

Secondly should i attempt to use my current method then but with vertex arrays even though you say they should be placed in their final positions.

Last up, what would be the performed way of doing perspective on tiles then?

18
Graphics / Re: Shader Lighting? Newbie here.
« on: November 13, 2012, 05:39:14 am »
unfortunantly i had to take a couple days break on this, will be back on it in a little i tried to do more like what you posted Cire but it still doesnt work. It fails after i set the shader parameters the whole thing just crashes. I cant even get it back to the way it was before where it was just doing nothing at all visually but the app still ran, it just crashes now and im not sure what i did differently to cause it or am doing wrong.

Here is my entire light class and the shader atm, sorry its in D but its very close to C/C++ and easy to understand from a C/C++ only understanding and uses the CSFML bindings. The code is not the cleanest, please forgive that i have just been mashing at it to try and get it to work:

static float Minute = 1.0 / 60;
enum int SampleScale = 2;
static string LightingFragment = r"uniform sampler2D texture; uniform vec3 light; uniform vec4 lcolor;
void main() {
   float dist = distance(gl_FragCoord.xy, light.xy);
   if (dist < light.z) {
      dist = light.z;
   }
   vec2 pos = gl_TexCoord[0].xy;
   gl_FragColor = mix(texture2D(texture, gl_FragCoord.xy), lcolor, dist / light.z * 1.0);
}"
;

class DDLightOverhaul {
   sfRenderWindow* Screen;
   sfSprite* Sprite;
   sfRenderTexture* Render;
   sfTexture* Texture;
   sfShader* Shader;
   sfRenderStates State;
   sfVector2u Size;
   static sfVector2f Scale = {SampleScale, SampleScale};
   sfIntRect PixelRect;
   
   sfColor MidDay, MidNight, Current;
   float Hour, Gradient;
   
   this(sfRenderWindow* screen, float hour, ubyte[4] midday, ubyte[4] midnight) {
      Screen = screen;
      Sprite = sfSprite_create();
      Size = Screen.sfRenderWindow_getSize();
      PixelRect.width = Size.x;
      PixelRect.height = Size.y;
      Size.x /= SampleScale;
      Size.y /= SampleScale;
      Sprite.sfSprite_setScale(Scale);
      Render = sfRenderTexture_create(Size.x, Size.y, 0);
      Shader = sfShader_createFromMemory(null, LightingFragment.ptr);
      State.shader = Shader;
      State.blendMode = sfBlendAdd;
      Sprite.sfSprite_setTexture(Render.sfRenderTexture_getTexture(), 1);
     
      MidDay.setColor(midday);
      MidNight.setColor(midnight);
      Hour = hour;
      this.modulateColor();
   }
   
   void draw(DDLight*[] lights = []) {
      Render.sfRenderTexture_clear(Current);
      Screen.sfRenderWindow_setActive(0);
      Render.sfRenderTexture_setActive(1);
      Shader.sfShader_setCurrentTextureParameter("texture");
      sfVector3f shaderlight;
      sfRectangleShape* rect = sfRectangleShape_create();
      sfVector2f size = {Size.x, Size.y};
      rect.sfRectangleShape_setSize(size);
     
      foreach (DDLight* light; lights) {
         shaderlight.x = light.Position.x / SampleScale;
         shaderlight.y = light.Position.y / SampleScale;
         shaderlight.z = light.Range / SampleScale;
         
         Shader.sfShader_setCurrentTextureParameter("texture");
         Shader.sfShader_setColorParameter("lcolor", light.Color);
         Shader.sfShader_setVector3Parameter("light", shaderlight);
         
         // change rect size here to cull pixels
         
         Render.sfRenderTexture_drawRectangleShape(rect, &State);
      }
      Render.sfRenderTexture_display();
      Render.sfRenderTexture_setActive(0);
      Screen.sfRenderWindow_setActive(1);
      Screen.sfRenderWindow_drawSprite(Sprite, &State);
   }

   void update() {
      Size = Screen.sfRenderWindow_getSize();
      PixelRect.width = Size.x;
      PixelRect.height = Size.y;
      Size.x /= SampleScale;
      Size.y /= SampleScale;
     
      Render.sfRenderTexture_destroy();
      Render = sfRenderTexture_create(Size.x, Size.y, 0);
      Sprite.sfSprite_setTexture(Render.sfRenderTexture_getTexture(), 1);
   }
   
   void modulateColor() {
      if (Hour > 5 && Hour < 7) {
         Gradient = ((Hour - 5) / 2.0) * 1.0;
         Current = interpolateColor(MidDay, MidNight, Gradient);
      }
      else if (Hour >= 7 && Hour <= 17) {
         Current = MidDay;
      }
      else if (Hour > 17 && Hour < 19) {
         Gradient = ((Hour - 17) / 2.0) * 1;
         Current = interpolateColor(MidNight, MidDay, Gradient);
      }
      else if (Hour >= 19 || Hour <= 5) {
         Current = MidNight;
      }
   }
   
   void addMinute(float minutes = 1) {
      Hour += Minute * minutes;
      if (Hour > 24) Hour = 0;
   }
}

I am sorry to go for that last ditch sad effort of "here is everything what the heck is wrong" but i just cant get these shaders right so i am sorry to have to ask for so much help but thanks to anyone who can assist me.

19
Graphics / Re: [Solved]Possible to reuse pixels?
« on: November 13, 2012, 05:31:46 am »
I don't know the normal way of figuring out of buffering and culling chunks so here is what i do at the moment:

Firstly i get the position of the current chunk that the center screen pixel is in, each chunk has its own global coordinate (they also store a screen coordinate because i move the world around the player not the player around the world (not sure if that's normal bu it has worked well so far)) then i use that to make a sfIntRect based on the chunk load range i am currently using (radius of chunks to be pre-loaded around the center current chunk. then i loop over the x and y of positions within that fsIntRect and if those coordinates in my hashmap of chunks are null or not already loaded then i add the coordinates to a list of chunks to be loaded in (one chunk is created from this lists coordinates per frame, loading all required chunks as they were found to be needed caused a slight stutter when it did so all at once) then i loop over each chunk currently in the hashmap of chunks and use its coordinate/key to use the sfIntRect contains function, if it is false then i add that to a list to be unloaded same as the loading list works pretty much.

Using this so far has been pretty good and fast for me but i don't know why if your tiles are 32x32 you would need 4 million of them, i suppose zooming out enough could warrant that, in which case you would just have to change the load range according to the zoom level... interesting thought.

Also i would like more details about changing the vertex array accordingly as well, as far as i can see doing the vertex array in chunks is a solid idea. and i don't see why it couldn't be used for pseudo infinite worlds.

20
Graphics / Re: [Solved]Possible to reuse pixels?
« on: November 10, 2012, 11:08:04 pm »
Would this be safe or even sane however for a pseudo-infinite world? I imagine, as efficient as it is, that after say hours of moving over large distances that it would *eventually* reach some diminishing returns or some limit because of the lack of culling. I suppose this could be mitigated by after some arbitrary amount of time, scrapping the entire vertex array and starting it again at the last position in the "players" current "chunk" which may be a little tricky.

Also sorry to inject a newbie question to a solved thread but, with a vertex array could that be used with all its benefits to draw tiles from a tileset texture. I see that vertices have texcoords but i see no way to define a texture to use for the vertex/array or even how to define the rectangle (i get the coords may be the x/y of a texture but how many pixels across or high?) of that texture to use or some such. Although i may well be misunderstanding them entirely as i came from a very limited sdl background and some things are just un-natural for me to think of just from reading a little bit of documentation.

The above question does pre-surpose my assumtion that a vertex array is essentially a large array of "windows" into a section of a texture that is grouped as one.

21
D / Re: problem when compiling derelict3 sfml2 wrong dll
« on: November 09, 2012, 02:09:00 pm »
Here mate, download my compiled binaries, asuming thats permitted: https://dl.dropbox.com/u/27223793/CSFML2.7z They work fine with derelict3.

I if there is a problem with me sharing these i will take them down immediately, just say so.

22
Graphics / Re: Shader Lighting? Newbie here.
« on: November 09, 2012, 02:35:26 am »
I have noticed that in my calculateLightsGPU function this line:
Screen.sfRenderWindow_drawSprite(Sprite, &States);
which is where i beleive the fragment shader is ran from yet outputs nothing at all to the screen.

Now i thought hang on i have earlier in the function already set the entire texture to be a single color which is the overlay i use for "night time" that has alpha same color i use for the cpu version of this function.

There should atleast be that color overlay over the screen regardless of if the shader has drawn the lights on the overlay at all. so i tried setting it to this:
Screen.sfRenderWindow_drawSprite(Sprite, null);

Now, while ofcause the shader doesnt run it is drawing the overlay to the screen without any of the lights on it as expected. so because of this is it possible that there is something wrong with my rendering states.

How am i meant to set them up to work with shaders? currently i have the shader loaded from file and then i do this:
sfRenderStates States;
States.shader = Shader;
States.blendMode = sfBlendAdd;
is it possible that this is where i went wrong? and if so how should it be done?

In an attempt to do this differently then i have gone back to the idea of using a RenderTexture, clearing it each frame to the ambient color value (mostly transparent dark blue for night time) and the texture for the sprite that i am applying the shader to will be updated to entirely transparent for each light, then the idea is for the shader to draw it, draw it to the RenderTexture until all lights are done then draw the RenderTexture to the screen.

Firstly to be sure i have set the texture of the state as such:
States.texture = RenTex.sfRenderTexture_getTexture();

Now what i have is the overlay color being correctly overlayed over the screen but still no "lights" here is the updated funciton:
    void calculateLightsGPU(DDLight*[] lights = []) {
                RenTex.sfRenderTexture_clear(Color);
                Sprite.sfSprite_setTexture(Texture, 1);

                sfVector3f shaderlight;

                foreach (DDLight* light; lights) {
                        Texture.sfTexture_updateFromImage(Image, 0, 0);

                        shaderlight.x = light.Position.x;
                        shaderlight.y = light.Position.y;
                        shaderlight.z = light.Range;

                        Shader.sfShader_setVector3Parameter("light", shaderlight);
                        Shader.sfShader_setColorParameter("lcolor", light.Color);
                        RenTex.sfRenderTexture_drawSprite(Sprite, &States);
                }

                RenTex.sfRenderTexture_display();
                Sprite.sfSprite_setTexture(States.texture, 0);
                Screen.sfRenderWindow_drawSprite(Sprite, null);
        }

Please guys i need help with this, the shader should be doing something but if i remove the entire foreach loop the output is exactly the same!

23
Graphics / Re: Shader Lighting? Newbie here.
« on: November 09, 2012, 01:44:53 am »
As i call in the calculateLightsGPU function:
Shader.sfShader_setColorParameter("lcolor", light.Color);

and assuming the documentation is correct yes it does automatically push the color down to a 0.0 to 1.0 range:
Quote
CSFML_GRAPHICS_API void sfShader_setColorParameter  ( sfShader *  shader, 
  const char *  name, 
  sfColor  color 
 )   

Change a color parameter of a shader.

name is the name of the variable to change in the shader. The corresponding parameter in the shader must be a 4x1 vector (vec4 GLSL type).

It is important to note that the components of the color are normalized before being passed to the shader. Therefore, they are converted from range [0 .. 255] to range [0 .. 1]. For example, a sf::Color(255, 125, 0, 255) will be transformed to a vec4(1.0, 0.5, 0.0, 1.0) in the shader.

I have never done shaders before so am i right in that i should have placed it in a sfRenderState and added that to the sprite draw of the lighting image overlay?

EDIT: using that reference site i have simplified the shader to the following but still nothing:
uniform vec3 light;
uniform vec4 lcolor;

void main() {
        gl_FragColor = mix(gl_Color, lcolor,
                distance(gl_FragCoord.xy, light.xy) / light.z * 1.0);
}

24
Graphics / Re: Shader Lighting? Newbie here.
« on: November 09, 2012, 01:26:47 am »
I am not entirely clear on what you have said but that i gather by the reference page you are suggesting rather then using the previous interpolateColor function i could do this at the end:

gl_FragColor = mix(gl_Color, lcolor, distance / light.z * 1.0);

in order to do that same thing but with a built in function yes?

Even still now i have made that change there is absolutely no graphical changes on the screen, it is as if i had not drawn the light map texture to the screen at all.

I have since attempted to draw the sprite of the light map with additive blending a RenderTexture rather then just to the screen and then draw that RenderTexture to the screen. As this made no change i removed it but i am trying.

25
Graphics / Re: Shader Lighting? Newbie here.
« on: November 08, 2012, 09:38:09 pm »
As is my nature even without much info i have given it a go as best i can.

Here is my current function to calculate lights on the gpu:
void calculateLightsGPU(DDLight*[] lights = []) {
                Texture.sfTexture_updateFromImage(Image, 0, 0);
                sfVector3f shaderlight;

                foreach (DDLight* light; lights) {
                        shaderlight.x = light.Position[0];
                        shaderlight.y = light.Position[1];
                        shaderlight.z = light.Range;

                        Shader.sfShader_setVector3Parameter("light", shaderlight);
                        Shader.sfShader_setColorParameter("lcolor", light.Color);
                        Screen.sfRenderWindow_drawSprite(Sprite, &States);
                }
        }

States is simply a sfRenderState with .shader set to the fragement shader below and .blendMode is set to sfBlendAdd as i beleive that is what i want from what i can gather.

note that image is just a static image that contains the base color of the lighting that changes with the day night cycle and updating the texture from this image is just my way of clearing it.

Here is my first attempt at a fragment shader, probably very wrong as i will detail more in a moment:
uniform vec3 light;
uniform vec4 lcolor;

vec4 interpolateColor(vec4 color1, vec4 color2, float p) {
        vec4 inter;
        inter.r = (color1.r * p + color2.r * (1.0 - p));
        inter.g = (color1.g * p + color2.g * (1.0 - p));
        inter.b = (color1.b * p + color2.b * (1.0 - p));
        inter.a = (color1.a * p + color2.a * (1.0 - p));
        return inter;
}

void main() {
        float distance = sqrt(pow(gl_FragCoord.x - light.x, 2) + pow(gl_FragCoord.y - light.y, 2));

        if (floor(light.x) == floor(gl_FragCoord.x) && floor(light.y) == floor(gl_FragCoord.y)) distance = 0.1;
        if (distance > light.z) distance = 0.0;

        gl_FragColor = interpolateColor(gl_Color, lcolor, distance / light.z * 1.0);
}

Now as far as i can see this should work at least in some visible way that i may be able to further fix however it does absolutely nothing, at least visually.

Can anyone help me with why this is and or spot any of my glaring errors i have undoubtedly made?

26
Graphics / Shader Lighting? Newbie here.
« on: November 08, 2012, 06:56:55 pm »
Firstly apologies for likely rather dim/broad questions here but i have never once developed anything with shaders and i am new to sfml2 (i am using CSFML).

I currently have a function that, once a frame, goes over every pixel in an sfImage that is half the size of the screen. There is a base color (depending on the time of day this changes) that is drawn to each pixel of the image unless one of the given light sources is within range, in which case it adds that light's color to the current pixel color with a gradient depending on the distance from the light source divided by the light sources range.

Once the image has been updated over each pixel i then update a texture from that image then draw it to the screen with a sprite using a scale of 2.

For reference this is the function i am using to give you a bit of an idea:
        void calculateLights(DDLight*[] lights = []) {
                sfColor color;
                float distance;
                int pos;

                for (int y = 0; y < Size.y; y++) {
                        for (int x = 0; x < Size.x; x++) {
                                color = Color;
                                foreach (DDLight* light; lights) {
                                        distance = light.getDistance(x * 2, y * 2);
                                        if (distance != 0.0) {
                                                        color = interpolateColor(color, light.Color,
                                                                                 (distance / light.Range * 1.0));
                                        }
                                }
                                sfImage_setPixel(Image, x, y, color);
                        }
                }
                sfTexture_updateFromImage(Texture, Image, 0, 0);
        }

Now this works fine visually and even on my high end computer the performance impact is negligible. However i do believe that with enough lights and with the later stages of my game running at once this performance will likely be a problem. Also along with the desire to work for the first time with shaders I would love to make this into a shader rather then run it entirely on the CPU.

So i have looked around the web for information on how to shaders in 2d for this kind of thing but i am having a problem finding anything useful to me for multiple reasons:

They require some knowledge of shaders already and explain little bar an outline of the theory of doing lighting.

They often have examples showing how it can be done but seems to depend heavily on using openGL directly but i am using ONLY sfml2 and no openGL calls at all.

Or the information plainly doesn't tell me anything about 2d.

So here i am asking can anyone give me any information on how to actually setup sfml2 to draw a texture to the screen that is generated by a shader. Also how would i begin to implement this kind of shader.

Thanks for the read, please forgive the green-ness of my questions.

27
D / Re: problem when compiling derelict3 sfml2 wrong dll
« on: November 08, 2012, 06:06:57 am »
I have recently gone through this proccess myself. The problem is with the latest csfml2 binaries.

What has to be done is you must first download the source snapshot for sfml2 c++ version on the download page near the top where it says SFML 2.0 snapshot.

Then you must compile that as static with cmake.

then get the latest CSFML2 sources here https://github.com/SFML/CSFML

then compile that with cmake aswell but NOT as static just normal dlls. then use those dll's and it will work.

It was a PAIN to get that all working fine but its worth it.

But also beware of a little problem with derelict3 bindings atm it has functions included that the current csfml2 dlls that you will build do not have. I have outlined them here https://github.com/aldacron/Derelict3/issues/62 the solution is rather simple unless ofcause you need those functions.

Hope this helps mate, stick with it.

28
Graphics / Re: Flipping RenderTexture
« on: November 06, 2012, 10:38:51 pm »
I will later try and trace through the app till the BSOD occurs to locate the exact problem first before i proceed further with the Intel problem. Source code may be a little more difficult as i am not very good with c/c++ so i actually use D with derelict3, tho i believe it is close enough to C that i can still ask if i cannot find my own solution.

I will, so long as you think it is a good idea leave this thread around rather then delete it because even through the answer is simple enough to shame me a little i believe at least someone else may benefit from it.

Also thanks for SFML it is a grand system that i have accomplished more in over the last few days then a much longer period i have spent on many other game engines or graphics libraries so thank you.

Awesome, that is also what i was hoping it would do when displaying!

29
Graphics / Re: Flipping RenderTexture
« on: November 06, 2012, 10:25:26 pm »
I compiled SFML+CSFML myself about 4 days ago so i believe i have the latest version.

About the display, you are right and i am sorry that does make it work, i had previously tried bind and unbind before drawing on the RenderTexture. I am working off of the generated CSFML docs 90% of the time so i am not aware the exact use of display... or even bind.

So apologies and thank you very much.

About the intel gpu issues other then using newer sources are there any other solutions, although i doubt it, would the new use of display perhaps stop these issues? or should i just refrain from testing on my main coding laptop.

EDIT: Unrelated question, if i enable vsync will that slow down my main render loop that updates and displays the screen. or will it draw superfluous frames to the screen that are never shown?

30
Graphics / Flipping RenderTexture
« on: November 06, 2012, 09:36:44 pm »
Hello, a few forewords, i am very new to SFML, i am using CSFML2(although my problem doesnt seem to be with the C bindings at all) and not using any openGL directly.

I am working on a little system that takes a bunch of sprites that are tiles for a map and draws them to the screen in chunks of 32 by 32 tiles at a time.

Previously i had been storing a position for each chunk in screen coordinates and each frame looping over each tile and re positioning the sprite for that tile to the chunkPosition + (tilesize * chunkTilePosition) then drawing each tile.

I know not the most efficient way of doing it but it works fine on my main computer but there is a bit of slowing on my little laptop so i thought of a better, obvious but new to me, way to do it.

My idea was to use a RenderTexture and instead of drawing the sprites onto the screen each frame, just draw the sprites to the RenderTexture once and then just draw the RenderTexture to the screen with a sprite at the chunk position and you have your entire chunk generated into a static image once and drawn easily.

Now this works on its own and i am getting the tiles onto the screen some of the chunks, not all, seem to be flipped and i cannot figure out why.

I am using the exact same loop to draw the sprites as i did with the first method just removed adding the chunks position to the tile position and drawing onto the RenderTexture rather then the screen.

I cant for the life of me figure out why the chunk would be flipped at all, can anyone help me out with this or even have some tips?

Thanks in advance to anyone kind enough to lend assistance.

EDIT: haha, this was in the hopes to increase performance atleast on the only computer i have that isnt way to fast to do performance testing... the version that uses RenderTexture causes it to BSOD upon launch of the app. I guess RenderTexture has a problem with the intelGPU then, is that documented?

EDIT:
quick demonstration of problem, first picture is what i want and how it works when i render to the screen, the second is rendering to RenderTexture. Although the next chunk down doesnt appear to be flipped but the one to its left does, and as far as i can see, neither in any predictable direction but i may be wrong there as the tiles are generated proceduraly off of the same seed, i don't know them that intimately nor are they so unique to me to be able to instantly tell if they are all flipped in one direction or something.


Pages: 1 [2]