1 module armos.command.generator.glsl;
2 
3 import std.getopt;
4 import std.path;
5 import std.file;
6 
7 // armos g glslsource name.vert --type=defaultglsl
8 // => generate shaders/{name.vert}
9 
10 // armos g glslsource --path=shaders/hoge.vert --type=defaultglsl
11 // => generate shaders/hoge.vert
12 auto generateGlslSource(string[] args){
13     string type; //TODO
14     string path;
15     args.getopt("type|t", &type,
16                 "path|p", &path);
17 
18     string name;
19 
20     string writenPath;
21     if(!path){
22         name = args[0];
23         path = buildPath(getcwd, "shaders", name);
24     }
25     if(!path.isAbsolute){
26         import std.file;
27         path = buildPath(getcwd, path);
28         name = path.baseName;
29     }
30 
31     if(!path.dirName.exists){
32         path.dirName.mkdirRecurse;
33     }
34     string sourceType = path.extension;
35     string sourceText;
36     switch (sourceType) {
37         case ".vert":
38             sourceText = generateVertSource(type);
39             break;
40         case ".geom":
41             sourceText = generateGeomSource(type);
42             break;
43         case ".frag":
44             sourceText = generateFragSource(type);
45             break;
46         default:
47             assert(false);
48     }
49     write(path, sourceText);
50 }
51 
52 
53 // armos g glslsources name --type=defaultglsl
54 // => generate shaders/name/{main.vert, main.geom, main.frag}
55 
56 // armos g glslsources --path=shaders/hoge --type=defaultglsl
57 // => generate shaders/hoge/{main.vert, main.geom, main.frag}
58 auto generateGlslSources(string[] args){
59     string type;
60     string path;
61     bool shouldGenerateNoVert;
62     bool shouldGenerateNoGeom;
63     bool shouldGenerateNoFrag;
64     args.getopt("type|t", &type,
65                 "path|p", &path,
66                 "no-vert", &shouldGenerateNoVert,
67                 "no-geom", &shouldGenerateNoGeom,
68                 "no-frag", &shouldGenerateNoFrag);
69 
70     if(!path){
71         path = buildPath("shaders", args[0]);
72     }
73     if(!shouldGenerateNoVert){
74         args = args.dup ~ "--path=" ~ buildPath(path, "main.vert");
75         generateGlslSource(args);
76     }
77 
78     if(!shouldGenerateNoGeom){
79         args = args.dup ~ "--path=" ~ buildPath(path, "main.geom");
80         generateGlslSource(args);
81     }
82 
83     if(!shouldGenerateNoFrag){
84         args = args.dup ~ "--path=" ~ buildPath(path, "main.frag");
85         generateGlslSource(args);
86     }
87 }
88 
89 private string generateVertSource(in string type){
90     return `#version 330
91 
92 // Enable to load package via glslify
93 // #pragma glslify: noise = require(glsl-noise/simplex/2d)
94 
95 uniform mat4 modelViewMatrix;
96 uniform mat4 projectionMatrix;
97 uniform mat4 modelViewProjectionMatrix;
98 uniform mat4 textureMatrix;
99 
100 in vec4 vertex;
101 // in vec3 normal;
102 // in vec3 tangent;
103 // in vec4 texCoord0;
104 // in vec4 texCoord1;
105 // in vec4 color;
106 
107 // out vec4 f_vertex;
108 // out vec3 f_normal;
109 // out vec3 f_tangent;
110 // out vec4 f_texCoord0;
111 // out vec4 f_texCoord1;
112 // out vec4 f_color;
113 
114 void main(void) {
115     gl_Position = modelViewProjectionMatrix * vertex;
116 
117     // f_vertex = vertex;
118     // f_normal = normal;
119     // f_tangent = tangent;
120     // f_texCoord0 = texCoord0;
121     // f_texCoord1 = texCoord1;
122     // f_color = color;
123 }
124 `;
125 }
126 
127 private string generateGeomSource(in string type){
128     return "";
129 }
130 
131 private string generateFragSource(in string type){
132     return `#version 330
133 
134 // Enable to load package via glslify
135 // #pragma glslify: noise = require(glsl-noise/simplex/2d)
136 
137 uniform mat4 modelViewMatrix;
138 uniform mat4 projectionMatrix;
139 uniform mat4 modelViewProjectionMatrix;
140 uniform mat4 textureMatrix;
141 
142 // in vec4 f_vertex;
143 // in vec3 f_normal;
144 // in vec3 f_tangent;
145 // in vec4 f_texCoord0;
146 // in vec4 f_texCoord1;
147 // in vec4 f_color;
148 
149 out vec4 fragColor;
150 
151 void main(void) {
152     fragColor = vec4(1, 1, 1, 1);
153 }
154 `;
155 }