5

The Setup

Microsoft Visual Studio Professional 2015, running on Windows 10 Pro
Unity 5.3.1f1 (x64)
The project is built upon an example project provided by Unity on their site. The project can be found here.


That which needs to be done

I'm looking into creating a opengl plugin (in the form of a dll) for use with Unity. In their example code the vertex and fragment shaders are hard coded in the code like so:

#define VPROG_SRC(ver, attr, varying)                               \
    ver                                                             \
    attr " highp vec3 pos;\n"                                       \
    attr " lowp vec4 color;\n"                                      \
    "\n"                                                            \
    varying " lowp vec4 ocolor;\n"                                  \
    "\n"                                                            \
    "uniform highp mat4 worldMatrix;\n"                             \
    "uniform highp mat4 projMatrix;\n"                              \
    "\n"                                                            \
    "void main()\n"                                                 \
    "{\n"                                                           \
    "   gl_Position = (projMatrix * worldMatrix) * vec4(pos,1);\n"  \
    "   ocolor = color;\n"                                          \
    "}\n"                                                           \

I don't like this approach and I don't need it's flexibility. So I wanted to put each shader in their own file and load the file using the following piece of code.

const char* ShaderLoader::ReadShader(char *filename)
{
    std::string shaderCode;
    std::ifstream file(filename, std::ios::in);

    if (!file.good())
    {
        std::cout << "Can't read file " << filename << std::endl;
        return nullptr;
    }

    file.seekg(0, std::ios::end);
    shaderCode.resize((unsigned int)file.tellg());
    file.seekg(0, std::ios::beg);
    file.read(&shaderCode[0], shaderCode.size());
    file.close();

    return shaderCode.c_str();
}

The Problem

The output of my project is a .dll file and I wanted my two shaders included in that file, where they could be loaded. Maybe this is not possible, I don't know.
I've added the shaders to the project. I've also right clicked each file and chosen Include In Project.

I don't where the two files end up. Are they included in the dll file and if so what path do I need to pass to ShaderLoader::ReadShader to read them?

I've tried to figure that out by using GetModuleFileName and FindFirstFile but to no success.

So ultimately the question is: Is it possible to include text files in dll libraries built with Visual Studio and if so how do I go about it?

Community
  • 1
  • 1
Bjorninn
  • 4,027
  • 4
  • 27
  • 39
  • Is that an answer to the problem of reading the file from a dll? http://stackoverflow.com/questions/4344486/how-to-load-text-file-from-resources-in-vc –  Mar 14 '16 at 19:02
  • Maybe, that pertains to reading an external text document. I was hoping to do it with an internal document. Include it with the built dll. I'm still unsure if that is possible or not. – Bjorninn Mar 14 '16 at 19:13

1 Answers1

2

Of course it will work to read the file from the DLL, but not using your code. The problem here is that you return a pointer to data that will disappear when the function returns.

The variable shaderCode is a local variable inside the ReadShader function, so it will of course be destructed once the function returns. That destruction includes freeing memory used for the contained string, so the pointer you return is pointing to data no longer allocated and trying to use that data will lead to undefined behavior.

Instead return the actual std::string object and get the pointer when actually needed. Alternatively (but not really something I recommend) allocate memory yourself and copy the string into the memory, and return a pointer to that.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks for the pointer! but that's not my problem right now. Right now I have no idea how to reference the possibly included text files. It simply always fails on !file.good(). – Bjorninn Mar 14 '16 at 13:53
  • 1
    Try using the the full path. – drescherjm Mar 14 '16 at 14:07
  • 1
    And what would the full path be? This is a dll file which will be added to a Unity project that will then be built and stored god know where. Also how do you reference a file that's stored inside a dll with a full path? – Bjorninn Mar 14 '16 at 14:12