summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/shaders.cc
blob: dc8936fa4475dbcd02472b6e784e86ef4e67746c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// shader handling
// Author: Supakorn "Jamie" Rassameemasmuang

#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <unordered_set>

#include <iostream>

#include "common.h"
#ifdef HAVE_GL

#include "shaders.h"

GLuint createShaders(GLchar const* src, int shaderType)
{
    GLuint shader = glCreateShader(shaderType);
    glShaderSource(shader, 1, &src, nullptr);
    glCompileShader(shader);
    
    GLint status;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &status);

    if (status != GL_TRUE)
    {
        GLint length; 

        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);

        std::vector<GLchar> msg(length);

        glGetShaderInfoLog(shader, length, &length, msg.data());

        for(GLchar const& cha : msg)
        {
            std::cerr << cha;
        }

        std::cerr << std::endl << "GL Compile error" << std::endl;
        std::cerr << src << std::endl;
        throw 1; 
    }
    return shader;
}

GLuint createShaderFile(std::string file, int shaderType, size_t Nlights,
                        size_t Nmaterials,  bool explicitcolor)
{
    std::ifstream shaderFile;
    shaderFile.open(file);
    std::stringstream shaderSrc;

    shaderSrc << "#version 130" << "\r\n";
    shaderSrc << "#extension GL_ARB_uniform_buffer_object : enable"
              << "\r\n";
    shaderSrc << "#extension GL_ARB_shading_language_packing : enable"
              << "\r\n";

    
    if(explicitcolor)
      shaderSrc << "#define EXPLICIT_COLOR" << "\r\n";
    
    shaderSrc << "const int Nlights=" << Nlights << ";\r\n";
    shaderSrc << "const int Nmaterials=" << Nmaterials << ";\r\n";

    if (shaderFile)
    {
        shaderSrc << shaderFile.rdbuf();
        shaderFile.close();
    }
    else
    {
        throw 1;
    }

    return createShaders(shaderSrc.str().data(), shaderType);
}
#endif