samedi 27 juin 2015

Shadow mapping: my shadow is correct but cut

this is my first time on SO so I may miss a few things, tell me if that's the case. Also I'm from Belgium so my English may not be perfect.

I'm currently working on a shadow mapping implementation for my game engine, it seems to be rendering and casting the shadow map well but if the shadow is too close from the emitter, it seems to be cut, as shown in this screenshot: Screenshot
(the white lines are there to check if the shadow is at the right location, they are projection from the spot light origin towards the cube's vertices).

As you can see, the shadow is cut, as it should starts from the cube's edge on the floor.

I'm using a 256*256 depth16 shadow map rendered from the light point of view with a perspective matrix: NzMatrix4f::Perspective(lightComponent.GetOuterAngle()*2.f, 1.f, 2.f, lightComponent.GetRadius())

Which ultimatly gives us the following projection matrix:

1, 0, 0, 0,
0, 1, 0, 0,
0, 0, -1.02041, -1,
0, 0, -2.04082, 0

I found out that reducing the zFar value was a little bit improving the shadow: zNear = 0.1, zFar = 1000 zNear = 0.1, zFar = 500

I think the problem comes from the test, although I have no idea what I'm doing wrong.

Here's the shader code (when projecting the shadow): Vertex Shader:

vLightSpacePos = LightProjMatrix * LightViewMatrix * WorldMatrix * vec4(VertexPosition, 1.0);

Fragment Shader:

#if SHADOW_MAPPING
if (vLightSpacePos.w > 0.0)
{
    vec3 ProjectionCoords = vLightSpacePos.xyz / vLightSpacePos.w;

    vec2 UVCoords;
    UVCoords.x = 0.5 * ProjectionCoords.x + 0.5;
    UVCoords.y = 0.5 * ProjectionCoords.y + 0.5;

    float Depth = texture(ShadowMap, UVCoords).x;
    if (Depth < ProjectionCoords.z)
    {
        lightDiffuse *= 0.5;
        lightSpecular = vec3(0.0);
    }
}
#endif

Here's a video I made to show the bug, with a spotlight casting a shadow from a cube (both are not moving) and where I'm making the floor going down, the shadow seems to fix itself once the distance is great enough: https://youtu.be/sisbOOml_cg

Am I missing something?

Are libtorrent transfers by default encrypted?

Sorry if this sounds blunt, still I wish to ask this question as I am new to libtorrent. I am trying to write a file transfer mechanism using libtorrent but I run to this interesting error while doing a CMake Build :

CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.2/Modules/FindPackageHandleStandardArgs.cmake:138 (message):
  Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the
  system variable OPENSSL_ROOT_DIR (missing: OPENSSL_LIBRARIES
  OPENSSL_INCLUDE_DIR)
Call Stack (most recent call first):
  C:/Program Files (x86)/CMake/share/cmake-3.2/Modules/FindPackageHandleStandardArgs.cmake:374 (_FPHSA_FAILURE_MESSAGE)
  C:/Program Files (x86)/CMake/share/cmake-3.2/Modules/FindOpenSSL.cmake:334 (find_package_handle_standard_args)
  CMakeLists.txt:162 (FIND_PACKAGE)


The error is pretty much understood - I have a missing dependancy and I could install it. But what I am wondering is whether the torrent traffic using the libtorrent is by default encrypted. I have already implemented an encryption scheme in my program and I wouldn't want to encrypt already encrypted data!

Executing external exe in a child window (cpp,win32)

i have written a simple win32 program say abc.exe. I have added a button in it, clicking on which an external exe (say xyz.exe) should start. But the original program i.e. abc.exe should be inaccessible while xyz.exe is running.(same as in the case of message box.. the parent window is inactive unless message box is closed). How can do it ? It would be great if you could post an example code.

C++ Text-RPG Inventory system

I'm building text-rpg inventory system but I'm not really sure how to properly create equipable items. For example, I can equip item which is in player inventory but I cannot identify what kind of item that is(Sword, Shield, Gloves or something else..) because item should be equiped in proper place(Helmet on head, sword in hands and so on). Is there any way to do so?

#include <iostream>
#include <vector>
#include <Windows.h>
#include <string>

using namespace std;

void Red()
{
    SetConsoleTextAttribute
    (GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
} //Intensive red console text color.

void Green()
{
    SetConsoleTextAttribute
        (GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
} //Intensive green console text color.

void Normal()
{
    SetConsoleTextAttribute
        (GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);
} //Default console text color.

struct Item{
    string name; //Item name.
    int price; //Item price.
    int purpose; // 0 - Head, 1 - Neck, 2 - Torso, 3 - Hands, 4 - Legs, 5 - Feets.
    int attribute; //Attack, Defense...
};

int main()
{
    //Isn't the smartest way to do so...
    Item Sword{
        "Sword", //This item name is 'Short Sword'.
        87, //Cost 87 gold pieces.
        3, //Use this item with hands. :D.
        10 //+10 attack.
    };

    string input; // input is for player commands.
    vector<string> Equipment = { "<Empty>", "<Empty>", "<Empty>", "<Empty>", "<Empty>","<Empty>" }; //Current equipment.
    vector<string> Inventory = {Sword.name}; //Player Inventory.
    string InventorySlots[] = { "Head", "Neck", "Torso", "Hands", "Legs", "Feets" }; //Player parts where items can be equiped.

    while (true){
        cin >> input;
        if (input == "equipment"){
            for (int i = 0; i < 6; i++){
                Normal();
                cout << InventorySlots[i];
                if (Equipment[i] == "<Empty>")
                    Red();
                cout << " " << Equipment[i] << endl << endl;
            }
            Normal();
        }

        if (input == "equip"){
            cout << "What do you want to equip? ";
            cin >> input;
            for (int i = 0; i < Inventory.size(); i++){
                //Search for item player want to equip and equip it in the right place.
                if (input == Inventory[i]){
                    //Inventory[i] = input;
                    //But how to identify what kind of item it is?
                    cout << "Successfully equiped!" << endl;
                }
            }
        }

        if(input == "inventory"){
            for (int i = 0; i < Inventory.size(); i++){
                cout << "______________________________________________________________" << endl;
                cout << "|  " << Inventory[i] << endl;
                cout << "|  Carried items " << Inventory.size() << " / " << 20 << endl;
                cout << "|_____________________________________________________________" << endl;
            }
        }

    }
    system("PAUSE"); // or 'cin.get()'
    return 0;   
}

Boost ASIO SSL get the number of available bytes for read

From this example, the size of the buffer available for reading is 1024 bytes. However, in the boost::asio::buffer() of handle_write(), I can't put that number. The program would give an error:

Write failed: uninitialized

The program would only work if I put exactly the number of bytes sent from the server.

So I tried to use the function available() which I found defined in the include file basic_socket.hpp to tell the number of available bytes to read, but I'm not sure how to call this function. Please assist!

void handle_write(const boost::system::error_code& error, size_t bytes_transferred)
{
    if (!error)
    {
        size_t len = socket_.available();
        boost::asio::async_read(socket_,
                                boost::asio::buffer(reply_, len),
                                boost::bind(&SSLClient::handle_read, this,
                                            boost::asio::placeholders::error,
                                            boost::asio::placeholders::bytes_transferred));
    }
    else
    {
        std::cout << "Write failed: " << error.message() << "\n";
    }
}

The question is: In this example, how can I read all the bytes available?

AMD / ARM alternatives of PDEP Intel BMI2 instruction?

I'm about to implement an algorithm using Intel BMI2 (Bit Manipulation Instruction Set 2) PDEP (parallel bits deposit) instruction. After a short research on the web, it's still unclear whether or not alternative instructions or methods (combination of multiple special instructions) are available on AMD/ARM architectures.

Any suggestions?

how to store a collection of string as key and json as value in c++

I am trying to store a collection of key-value pair in cpp where key will be a string and value will be a json object.

Then I need to access this json object using Key1 For Example

Key1 = "name1" 
Value1 = {name:"Anil Gautam","age":25}
Key2 = "name2" 
Value**strong text** = {name:"Sharan Gupta","age":26}

I want to access

{name:"Anil Gautam","age":25} 

when I input "name1". What Can I possible do to store this kind of data in cpp.