Exemplo n.º 1
0
/* compile shaders and return their ids in a 2-vector */
glm::ivec2 loadShaders(std::string vertFile, std::string fragFile)
{

    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    /* read shader files into strings */
    std::ifstream vfile(vertFile.c_str());
    std::ifstream ffile(fragFile.c_str());

    std::ostringstream vbuffer, fbuffer;
    vbuffer << vfile.rdbuf();
    fbuffer << ffile.rdbuf();
    
    std::string vertexString(vbuffer.str());
    std::string fragmentString(fbuffer.str());

    /* convert strings to c_strings and set as shader source */
    char *vertexCString, *fragmentCString;
    vertexCString = new char[vertexString.length() + 1];
    fragmentCString = new char[fragmentString.length() + 1];
    strcpy(vertexCString, vertexString.c_str());
    strcpy(fragmentCString, fragmentString.c_str());

    glShaderSource(vertexShader, 1, (const char**)&vertexCString, 0);
    glShaderSource(fragmentShader, 1, (const char**)&fragmentCString, 0);

    delete vertexCString;
    delete fragmentCString;

    /* compile shaders and check for errors */
    GLint status;
    glCompileShader(vertexShader);
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
    if(status != GL_TRUE)
    {
        char log[2048];
        int len;
        glGetShaderInfoLog(vertexShader, 2048, (GLsizei*)&len, log);
        fprintf(stderr, "Vertex Shader: %s", log);
        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);
        exit(1);
    }

    glCompileShader(fragmentShader);
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &status);
    if(status != GL_TRUE)
    {
        char log[2048];
        int len;
        glGetShaderInfoLog(fragmentShader, 2048, (GLsizei*)&len, log);
        fprintf(stderr, "Fragment Shader: %s", log);
        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);
        exit(1);
    }

    return glm::ivec2(vertexShader, fragmentShader);
}
Exemplo n.º 2
0
static inline void dumpTextBoxes(Vector<SVGInlineTextBox*>& boxes)
{
    unsigned boxCount = boxes.size();
    fprintf(stderr, "Dumping all text fragments in text sub tree, %i boxes\n", boxCount);

    for (unsigned boxPosition = 0; boxPosition < boxCount; ++boxPosition) {
        SVGInlineTextBox* textBox = boxes.at(boxPosition);
        Vector<SVGTextFragment>& fragments = textBox->textFragments();
        fprintf(stderr, "-> Box %i: Dumping text fragments for SVGInlineTextBox, textBox=%p, textRenderer=%p\n", boxPosition, textBox, textBox->textRenderer());
        fprintf(stderr, "        textBox properties, start=%i, len=%i, box direction=%i\n", textBox->start(), textBox->len(), textBox->direction());
        fprintf(stderr, "   textRenderer properties, textLength=%i\n", textBox->textRenderer()->textLength());

        const UChar* characters = textBox->textRenderer()->characters();

        unsigned fragmentCount = fragments.size();
        for (unsigned i = 0; i < fragmentCount; ++i) {
            SVGTextFragment& fragment = fragments.at(i);
            String fragmentString(characters + fragment.characterOffset, fragment.length);
            fprintf(stderr, "    -> Fragment %i, x=%lf, y=%lf, width=%lf, height=%lf, characterOffset=%i, length=%i, characters='%s'\n"
                          , i, fragment.x, fragment.y, fragment.width, fragment.height, fragment.characterOffset, fragment.length, fragmentString.utf8().data());
        }
    }
}
Exemplo n.º 3
0
GLuint engine::loadshaders(std::string vertfile, std::string fragfile,
        std::vector<std::string> in_attributes,
        std::vector<std::string> out_attributes)
{
    /* init glew and load compiled shaders */
    glewInit();
    if(!glewIsSupported("GL_VERSION_2_0 GL_ARB_multitexture GL_EXT_framebuffer_object")) 
    {
        fprintf(stderr, "Required OpenGL extensions missing\n");
        return -1;
    }

    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    /* read shader files into strings */
    std::ifstream vfile(vertfile.c_str());
    std::ifstream ffile(fragfile.c_str());

    if(!vfile)
    {
        std::cout << vertfile << " does not exist!\n";
        exit(1);
    }
    if(!ffile)
    {
        std::cout << fragfile << " does not exist!\n";
        exit(1);
    }

    std::ostringstream vbuffer, fbuffer;
    vbuffer << vfile.rdbuf();
    fbuffer << ffile.rdbuf();
    
    std::string vertexString(vbuffer.str());
    std::string fragmentString(fbuffer.str());

    /* convert strings to c_strings and set as shader source */
    char *vertexCString, *fragmentCString;
    vertexCString = new char[vertexString.length() + 1];
    fragmentCString = new char[fragmentString.length() + 1];
    strcpy(vertexCString, vertexString.c_str());
    strcpy(fragmentCString, fragmentString.c_str());

    glShaderSource(vertexShader, 1, (const char**)&vertexCString, 0);
    glShaderSource(fragmentShader, 1, (const char**)&fragmentCString, 0);

    delete vertexCString;
    delete fragmentCString;

    /* compile shaders and check for errors */
    GLint status;
    glCompileShader(vertexShader);
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
    if(status != GL_TRUE)
    {
        char log[2048];
        int len;
        glGetShaderInfoLog(vertexShader, 2048, (GLsizei*)&len, log);
        fprintf(stderr, "Vertex Shader: %s", log);
        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);
        exit(1);
    }

    glCompileShader(fragmentShader);
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &status);
    if(status != GL_TRUE)
    {
        char log[2048];
        int len;
        glGetShaderInfoLog(fragmentShader, 2048, (GLsizei*)&len, log);
        fprintf(stderr, "Fragment Shader: %s", log);
        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);
        exit(1);
    }

    /* create program, bind attributes, and link program */
    int program = glCreateProgram();
    glAttachShader(program, vertexShader);
    glAttachShader(program, fragmentShader);
    int num_in_attributes = in_attributes.size();
    for(int i = 0; i < num_in_attributes; i++)
    {
        glBindAttribLocation(program, i, in_attributes.at(i).c_str());
    }
    int num_out_attributes = out_attributes.size();
    for(int i = 0; i < num_out_attributes; i++)
    {
        glBindFragDataLocation(program, i, out_attributes.at(i).c_str());
    }
    glLinkProgram(program);

    GLint pstatus;
    glGetProgramiv(program, GL_LINK_STATUS, &pstatus);
    if(pstatus != GL_TRUE)
    {
        char log[2048];
        int len;
        glGetProgramInfoLog(program, 2048, (GLsizei*)&len, log);
        fprintf(stderr, "%s", log);
        glDeleteProgram(program);
        return -1;
    }

    return program;
}