How to store renderer vertex / index data in scene graph objects?


I have a SceneNode class which contains a Mesh instance. The Mesh class stores client side information such as vertex and index arrays (before they’re uploaded to the GPU). I also have an abstracted Renderer class, for GL and D3D, which render the SceneNodes. However, I’m not sure where I should store the API specific variables, e.g. GLuint via glGenBuffers for GL, and an ID3D11Buffer for D3D.

The few options I’ve considered are:

  1. Create a derived Mesh class for each API, e.g. GLMesh / D3DMesh
  2. Create a derived MeshData class for each API, which is stored in the main Mesh class
  3. Store a map of Mesh to API variables in each renderer, e.g. perform a lookup of Mesh to GLuint / ID3D11Buffer for each object that is rendered (variables would have to be generated after the scene had been updated, but before rendering).
  4. Separate the logic of rendering from scenes, by visiting the SceneGraph after update, and generating a RenderGraph of all renderable nodes in the scene.

What’s the recommended way of doing this?

SFML RectangleShape won't render


I have a sf::RectangleShape in my code which is defined like this :

sf::RectangleShape shape_1;

And it’s values are set like this :

shape_1.setFillColor(sf::Color::White);
shape_1.setPosition(10, 10);

But when I render it like this :

    window.draw(shape_1);

It wont display in my window.


My rendering part of the code looks like this :

    window.clear();

    window.draw(*box.getSfShape());
    window.draw(*box_static.getSfShape());
    window.draw(shape_1);

    window.display();

Not sure what I’m doing wrong. Have been going on for days thinking it was a position problem with a body but since this won’t render either it can’t be. :P

HLSL Shadowmap shader for large scale environments


I’m working on a racing game in Monogame. The game runs fine so far using Monogame’s BasicEffect (link to video).

I wanted to add shadows so I implemented a shadowmap using this as a base.
and a shadowmap of 4096×4096 I get nice shadows:

Shadows with a shadowmap shader

My game actually has a draw distance of 500 units instead of the 20 units used in my test code. This greatly reduces the accuracy of the shadowmap, so much it becomes unusable (some blobs appear under some of the cars). For example the same code with a projection distance of 200 units:

shadows with a drawdistance of 200 units

Now to my question:
How can I improve the quality of the shadows with bigger draw distances?

I tried using different drawdistances for the shadowmap and for the actual drawing (hoping shadows would not matter in the far distance) but it produced more artefacts than decent shadows.

I’m considering scaling my entire game down- but it would mean scaling the ingame logic as well so I’m hoping to find an answer here first.

Edit: this is the (currently not working) shader I’m working on based on János Turánszki suggestions

float4x4 World;
float4x4 View;
float4x4 Projection;
float4x4 LightViewProjClose;
float4x4 LightViewProjMedium;
float4x4 LightViewProjFar;


float3 LightDirection1 = float3(-1.5, 0.45, 0);
float4 LightColor1 = float4(1, 1, 1, 1);
float3 LightDirection2 =float3(1,1,0.2);
float4 LightColor2 = float4(0.2, 0.2, 0.25, 1);


float4 AmbientColor = float4(0.15, 0.15, 0.15, 0.15);
float DepthBias = 0.001f;

texture ShadowMap;
sampler ShadowMapSampler = sampler_state
{
    Texture = <ShadowMap>;
};

struct VertexShaderInput
{
    float4 Position : SV_POSITION;
    float4 Normal : NORMAL0;
    float4 Color : COLOR0;
};

struct ShadowVertexShaderInput
{
    float4 Position : SV_POSITION;
    float4 Normal : NORMAL0;
    float4 Color : COLOR0;
};

struct VertexShaderOutput
{
    float4 Position : SV_POSITION;
    float4 Normal   : TEXCOORD0;
    float4 Color    : COLOR0;
    float4 WorldPos : TEXCOORD2;
};

struct CreateShadowMap_VSOut
{
    float4 Position : POSITION;
    float4 Color : COLOR0;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    float4x4 WorldViewProj = mul(mul(World, View), Projection);

    // Transform the models verticies and normal
    output.Position = mul(input.Position, WorldViewProj);
    output.Normal =  input.Normal;
    output.Color = input.Color;

    // Save the vertices postion in world space
    output.WorldPos = mul(input.Position, World);

    return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
    // Color of the model
    float4 diffuseColor = input.Color;

    // Intensity based on the direction of the light
    float diffuseIntensity1 = clamp(dot(input.Normal.xyz, LightDirection1.xyz),0,1);
    float diffuseIntensity2 = clamp(dot(input.Normal.xyz, LightDirection2.xyz), 0, 1);

    // Final diffuse color with ambient color added
    float4 diffuse = saturate((diffuseIntensity1 * LightColor1 + diffuseIntensity2 * LightColor2) * diffuseColor);


    // Find the position of this pixel in light space in the close projection
    float4 lightingPosition = mul(input.WorldPos, LightViewProjClose);

    if (((lightingPosition.x > -0.5) && (lightingPosition.x<0.5)) && ((lightingPosition.y>-0.5) && (lightingPosition.y < 0.5)))
    {
        // Find the position in the shadow map for this pixel
        float2 ShadowTexCoord = 0.5 * lightingPosition.xy /
            lightingPosition.w + float2(0.5, 0.5);
        ShadowTexCoord.y = 1.0f - ShadowTexCoord.y;

        // Get the current depth stored in the shadow map (red component for close)
        float shadowdepth = tex2D(ShadowMapSampler, ShadowTexCoord).r;

        // Calculate the current pixel depth
        // The bias is used to prevent folating point errors that occur when
        // the pixel of the occluder is being drawn
        float ourdepth = (lightingPosition.z / lightingPosition.w) - DepthBias;

        // Check to see if this pixel is in front or behind the value in the shadow map
        if (shadowdepth < ourdepth)
        {
            // Shadow the pixel by lowering the intensity
            diffuse *= float4(0.5, 0.5, 0.5, 0);

        };

    }

    lightingPosition = mul(input.WorldPos, LightViewProjMedium);

    if (((lightingPosition.x > -0.5) && (lightingPosition.x<0.5)) && ((lightingPosition.y>-0.5) && (lightingPosition.y < 0.5)))
    {
        // Find the position in the shadow map for this pixel
        float2 ShadowTexCoord = 0.5 * lightingPosition.xy /
            lightingPosition.w + float2(0.5, 0.5);
        ShadowTexCoord.y = 1.0f - ShadowTexCoord.y;

        // Get the current depth stored in the shadow map (green component for medium)
        float shadowdepth = tex2D(ShadowMapSampler, ShadowTexCoord).g;

        // Calculate the current pixel depth
        // The bias is used to prevent folating point errors that occur when
        // the pixel of the occluder is being drawn
        float ourdepth = (lightingPosition.z / lightingPosition.w) - DepthBias;

        // Check to see if this pixel is in front or behind the value in the shadow map
        if (shadowdepth < ourdepth)
        {
            // Shadow the pixel by lowering the intensity
            diffuse *= float4(0.5, 0.5, 0.5, 0);
        };
    }

    //do the same trick using the 'far' matrix.
    lightingPosition = mul(input.WorldPos, LightViewProjFar);
    if (((lightingPosition.x > -0.5) && (lightingPosition.x<0.5)) && ((lightingPosition.y>-0.5) && (lightingPosition.y < 0.5)))
    {
        // Find the position in the shadow map for this pixel
        float2 ShadowTexCoord = 0.5 * lightingPosition.xy /
            lightingPosition.w + float2(0.5, 0.5);
        ShadowTexCoord.y = 1.0f - ShadowTexCoord.y;

        // Get the current depth stored in the shadow map (blue component for far)
        float shadowdepth = tex2D(ShadowMapSampler, ShadowTexCoord).b;

        // Calculate the current pixel depth
        // The bias is used to prevent folating point errors that occur when
        // the pixel of the occluder is being drawn
        float ourdepth = (lightingPosition.z / lightingPosition.w) - DepthBias;

        // Check to see if this pixel is in front or behind the value in the shadow map
        if (shadowdepth < ourdepth)
        {
            // Shadow the pixel by lowering the intensity
            diffuse *= float4(0.5, 0.5, 0.5, 0);
        };
    }
    return diffuse;
}

CreateShadowMap_VSOut ShadowVertexShaderFunction(VertexShaderInput input)
{
    CreateShadowMap_VSOut output;

    float4 position;
    float4 color;

    //check against close projection
    float4 lightingPosition = mul(mul(input.Position, World), LightViewProjClose);
    //by calculating the pixel position- find if it is part of the close projection matrix.

    if (lightingPosition.x > -0.5 && lightingPosition.x<0.5 && lightingPosition.y>-0.5 && lightingPosition.y < 0.5)
    {
        position = mul(input.Position, mul(World, LightViewProjClose));
        color = float4(position.z / position.w, 0,0,0); //place in R component
    }

    //check against medium projection
    lightingPosition = mul(mul(input.Position, World), LightViewProjMedium);

    if (lightingPosition.x > -0.5 && lightingPosition.x<0.5 && lightingPosition.y>-0.5 && lightingPosition.y < 0.5)
    {
        position = mul(input.Position, mul(World, LightViewProjMedium));
        color = float4(0,position.z / position.w, 0, 0); //place in G component
    }

    //check against far projection
    lightingPosition = mul(mul(input.Position, World), LightViewProjFar);

    if (lightingPosition.x > -0.5 && lightingPosition.x<0.5 && lightingPosition.y>-0.5 && lightingPosition.y < 0.5)
    {
        position = mul(input.Position, mul(World, LightViewProjFar));
        color = float4(0, 0, position.z / position.w, 0); //place in B component
    }

    output.Position = position;
    output.Color = color;
    return output;
}

float4 ShadowPixelShaderFunction(CreateShadowMap_VSOut input) : COLOR0
{
    return input.Color;
}


technique MyTechnique
{
    pass Pass1
    {
        VertexShader = compile vs_4_0_level_9_3 VertexShaderFunction();
        PixelShader = compile ps_4_0_level_9_3 PixelShaderFunction();
    }
}

technique ShadowTechnique
{
    pass Pass1
    {
        VertexShader = compile vs_4_0_level_9_3 ShadowVertexShaderFunction();
        PixelShader = compile ps_4_0_level_9_3 ShadowPixelShaderFunction();
    }
}

How do I increase the speed of an object after it spawns?


I’m trying to increase the speed of a game object based on the wave it spawns in. How would I go about doing this?

public class GameController:MonoBehaviour{
     public GameObject hazard;
     public Vector3 hazard_values;
     public int min_asteroids;
     public int max_asteroids;
     private int hazard_count;
     public float spawn_wait;
     public float start_wait;
     public float wave_wait;
     private int wave_count = 0;
     public Text score_text;
     private int score;
     public Text restart;
     public Text game_over_text;
     private bool game_over;
     private bool new_game;
     private float new_speed = 1f;

     void Start(){
         game_over = false;
         new_game = false;
         restart.text = "";
         game_over_text.text = "";
         score = 0;
         updateScore();
         StartCoroutine(asteroidWaves());
     }

     void Update(){
         if(new_game){
             if(Input.GetKeyDown(KeyCode.Return)){
                 Application.LoadLevel(Application.loadedLevel);
             }
         }
     }

     IEnumerator asteroidWaves(){
         yield return new WaitForSeconds(start_wait);
         while(true){
             wave_count += 1;
             AsteroidMover asteroid = GetComponent<AsteroidMover>();
             if(asteroid != null){
                 if(wave_count > 1){
                     min_asteroids *= wave_count;
                     max_asteroids += min_asteroids;
                     asteroid.increaseSpeed(new_speed);
                 }
             }
             if(asteroid == null){
                 Debug.Log ("Meh");
             }

             hazard_count = Random.Range(min_asteroids, max_asteroids);
             for(int i = 0; i < hazard_count; i++){
                 Vector3 hazard_position = new Vector3(Random.Range(-hazard_values.x, hazard_values.x), hazard_values.y, hazard_values.z);
                 Quaternion hazard_rotation = Quaternion.identity;
                 Instantiate(hazard, hazard_position, hazard_rotation);
                 yield return new WaitForSeconds(spawn_wait);
             }
             yield return new WaitForSeconds(wave_wait);
             if(game_over){
                 restart.text = "Press 'Enter' to start a new game.";
                 new_game = true;
                 break;
             }
         }
     }

     public void addScore(int new_score){
         score += new_score;
         updateScore();
     }

     void updateScore(){
         score_text.text = "SCORE: " +score;
     }

     public void gameOver(){
         game_over_text.text = "Game Over";
         game_over = true;
     }
 }

         public class AsteroidMover:MonoBehaviour{
             private Rigidbody asteroid;
             public float speed;

             void Start(){
                 asteroid = GetComponent<Rigidbody>();
                 asteroid.velocity = transform.forward * speed;
             }

             public void increaseSpeed(float new_speed){
                 speed += (new_speed + 0.5f);
                 Debug.Log (speed);
             }
         }

To better clarify what I’m trying to do, the feature I’m trying to make work is alot like the falling blocks of Tetris. On the first level, the blocks fall at a predetermined speed. When the player beats the level, the blocks fall at a faster speed. This is exactly what I am trying to accomplish. When my game starts, the hazards(asteroids) fall at a predetermined speed. When the player reaches the next wave, the asteroids need to fall at a faster speed.

Does all box2d bodies have collision detection?


What I want to know is if both b2-static and dynamic bodies have automatic collision detection.

And how do I tell box2D what it should do when it collides with a specific body?

GL.Scissor not working as expected


I have some code for rendering a button in an application and I want to clip the text if it’s wider than the button itself. Here a bit of code to describe what I’m doing:

GL.Enable(EnableCap.ScissorTest);
GL.Scissor(Frame.X, ScreenSize.Y - Frame.Y - Frame.Height, Frame.Width, Frame.Height);

RenderRect(Frame, Color.Red);
RenderText(Text, Color.White);

GL.Disable(EnableCap.ScissorTest);

I’m using OpenTK and QuickFont as the text rendering library. The main problem is, the rectangle gets clipped just like it should, but the text will render outside the scissor region if it’s too long. I wondered what QuickFont was actually doing during the text rendering, but I couldn’t find anything else than some GL.PushMatrix and GL.LoadIdentity calls that could interfere. Are these functions resetting the scissor test? Would a GL.PushMatrix call make it so that it would be possible to render outside the clipping rectangle?

Introduction movie running before game starts


There are many games out there that feature a movie or cut scene to feature either in-game features or to set the mood for the player. Some examples are the intro from Tony Hawk Pro Skater 4, Fifa Street 2, Modern Warfare 3, etc. How do I integrate a movie I made to Unity so it launches right at the start, ends playing and then shows the main menu? Is it available for unity personal edition or is it only for pro users?

Use an int as a reference to a specific class


So I’m trying to make an efficient game where there is a grid system and every grid has a number that is able to identify what kind of block is located there. This lets me just have an array of numbers rather than an array of block-objects.

But now here is my problem, when I want to call a method from one of block classes, I only have a number to reference it. And I don’t want to have a giant switch block to try and determine which block it is.

So in short, how can I get an specific int to reference a certain class without a massive if chain or switch block?

I believe Minecraft does something like this. All the blocks are saved in memory as a number, and then when the game wants to draw or call a method on one of the blocks, it uses the number at that location to reference to block to call the method. How does it do this?

Verifying game completion knowing players id


I work at a school and I am producing a simple game for language learners via RPG Maker.

All students have a unique student # that their processor has access to.

At the moment, I am imagining an end-game NPC providing the successful student with a special number that they will then screen capture and submit to their teacher. The number is based on a secret algorithm that uses their student #, so each “end-game number” is unique. The teacher can then verify that each submitted number is the correct end-game number for any given student.

Is there a better way for me to verify whether or not each student has successfully played through the game?

Removed parent category path – New URL's not showing up in Google search results


Last month we removed parent category path from the sub category URL’s. After that we changed the sitemap file with new URL’s and submit it to the webmaster tools for indexing.

For example example.com/display-cabinets/display-counters has been changed to example.com/display-counters

The problem is still the new URL’s are not getting indexed by Google. I have done everything like removing breadcrumbs and used “Fetch as Google” option for re-crawling, but nothing worked.

Can anyone please tell me a solution for this issue?

Question and Answer is proudly powered by WordPress.
Theme "The Fundamentals of Graphic Design" by Arjuna
Icons by FamFamFam