app.d 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import std.stdio;
  2. import three;
  3. import std.typecons;
  4. import derelict.opengl3.gl3;
  5. import three.gl.util;
  6. import derelict.anttweakbar.anttweakbar;
  7. import derelict.glfw3.glfw3;
  8. import derelict.assimp3.assimp;
  9. import std.string;
  10. struct Vertex {
  11. float x, y, z;
  12. }
  13. struct Normal {
  14. float x, y, z;
  15. }
  16. struct UV {
  17. float u, v;
  18. }
  19. struct Color {
  20. float r, g, b, a;
  21. }
  22. struct Vector {
  23. float x, y, z;
  24. }
  25. struct Quaternion {
  26. float x, y, z, w;
  27. }
  28. final class Mesh {
  29. public:
  30. Vertex[] vertexData;
  31. Normal[] normalData;
  32. UV[] textureData;
  33. Color[] colorData;
  34. VertexArrayObject vao;
  35. VertexBufferObject!(VertexBufferObjectTarget.Array) vboVertexData;
  36. VertexBufferObject!(VertexBufferObjectTarget.Array) vboNormalData;
  37. VertexBufferObject!(VertexBufferObjectTarget.Array) vboTextureData;
  38. VertexBufferObject!(VertexBufferObjectTarget.Array) vboColorData;
  39. this(string filePath) {
  40. writeln("loading scene: ", filePath);
  41. auto scene = aiImportFile(filePath.toStringz(), aiProcess_Triangulate);
  42. writeln("meshes: ", scene.mNumMeshes);
  43. for(uint m = 0; m < scene.mNumMeshes; ++m) {
  44. const(aiMesh*) mesh = scene.mMeshes[m];
  45. assert(mesh !is null);
  46. writeln("mesh[", m, "] faces : ", mesh.mNumFaces);
  47. for (uint f = 0; f < mesh.mNumFaces; ++f) {
  48. const(aiFace*) face = &mesh.mFaces[f];
  49. assert(face !is null);
  50. for(uint v = 0; v < 3; ++v) {
  51. aiVector3D p, n, uv;
  52. assert(face.mNumIndices > v);
  53. uint vertex = face.mIndices[v];
  54. assert(mesh.mNumVertices > vertex);
  55. p = mesh.mVertices[vertex];
  56. n = mesh.mNormals[vertex];
  57. // check if the mesh has texture coordinates
  58. if(mesh.mTextureCoords[0] !is null) {
  59. uv = mesh.mTextureCoords[0][vertex];
  60. }
  61. vertexData ~= Vertex(p.x, p.y, p.z);
  62. normalData ~= Normal(n.x, n.y, n.z);
  63. textureData ~= UV(uv.x, uv.y);
  64. colorData ~= Color(n.x, n.y, n.z, 1.0f);
  65. }
  66. }
  67. }
  68. writeln("unloading scene: ", filePath);
  69. aiReleaseImport(scene);
  70. //-----------------------------
  71. // upload
  72. writeln("uploading mesh: ", filePath);
  73. vao = new VertexArrayObject();
  74. vao.bind();
  75. writeln("vertex data");
  76. vboVertexData = new VertexBufferObject!(VertexBufferObjectTarget.Array);
  77. vboVertexData.bind();
  78. GLuint attribIndex = 0;
  79. glBufferData(GL_ARRAY_BUFFER, cast(ptrdiff_t)(Vertex.sizeof * vertexData.length) , vertexData.ptr, GL_STATIC_DRAW);
  80. glEnableVertexAttribArray(attribIndex);
  81. glVertexAttribPointer(attribIndex, 3, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
  82. vboVertexData.unbind();
  83. writeln("normal data");
  84. vboNormalData = new VertexBufferObject!(VertexBufferObjectTarget.Array);
  85. vboNormalData.bind();
  86. attribIndex = 1;
  87. glBufferData(GL_ARRAY_BUFFER, cast(ptrdiff_t)(Normal.sizeof * normalData.length) , normalData.ptr, GL_STATIC_DRAW);
  88. glEnableVertexAttribArray(attribIndex);
  89. glVertexAttribPointer(attribIndex, 3, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
  90. vboNormalData.unbind();
  91. writeln("uv data");
  92. vboTextureData = new VertexBufferObject!(VertexBufferObjectTarget.Array);
  93. vboTextureData.bind();
  94. attribIndex = 2;
  95. glBufferData(GL_ARRAY_BUFFER, cast(ptrdiff_t)(UV.sizeof * textureData.length) , textureData.ptr, GL_STATIC_DRAW);
  96. glEnableVertexAttribArray(attribIndex);
  97. glVertexAttribPointer(attribIndex, 2, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
  98. vboTextureData.unbind();
  99. writeln("color data");
  100. vboColorData = new VertexBufferObject!(VertexBufferObjectTarget.Array);
  101. vboColorData.bind();
  102. attribIndex = 3;
  103. glBufferData(GL_ARRAY_BUFFER, cast(ptrdiff_t)(Color.sizeof * colorData.length) , colorData.ptr, GL_STATIC_DRAW);
  104. glEnableVertexAttribArray(attribIndex);
  105. glVertexAttribPointer(attribIndex, 4, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
  106. vboColorData.unbind();
  107. vao.unbind();
  108. writeln("done");
  109. }
  110. }
  111. void setupTweakbar() {
  112. writeln("creating TweakBar");
  113. double time = 0, dt; // Current time and enlapsed time
  114. double turn = 0; // Model turn counter
  115. double speed = 0.3; // Model rotation speed
  116. int wire = 0; // Draw model in wireframe?
  117. uint frameCount = 0;
  118. double fps = 0;
  119. float bgColor[3] = [0.1f, 0.2f, 0.4f]; // Background color
  120. ubyte cubeColor[4] = [255, 0, 0, 128]; // Model color (32bits RGBA)
  121. auto quat = Quaternion(0,0,0,1);
  122. // auto w = this._window.getBounds()[2];
  123. // auto h = this._window.getBounds()[3];
  124. TwWindowSize(1600, 900);
  125. // Create a tweak bar
  126. auto bar = TwNewBar("TweakBar");
  127. TwDefine(" GLOBAL help='This example shows how to integrate AntTweakBar with GLFW and OpenGL.' "); // Message added to the help bar.
  128. // Add 'speed' to 'bar': it is a modifable (RW) variable of type TW_TYPE_DOUBLE. Its key shortcuts are [s] and [S].
  129. TwAddVarRW(bar, "speed", TW_TYPE_DOUBLE, &speed, " label='Rot speed' min=0 max=2 step=0.01 keyIncr=s keyDecr=S help='Rotation speed (turns/second)' ");
  130. // Add 'wire' to 'bar': it is a modifable variable of type TW_TYPE_BOOL32 (32 bits boolean). Its key shortcut is [w].
  131. TwAddVarRW(bar, "wire", TW_TYPE_BOOL32, &wire, " label='Wireframe mode' key=CTRL+w help='Toggle wireframe display mode.' ");
  132. // Add 'time' to 'bar': it is a read-only (RO) variable of type TW_TYPE_DOUBLE, with 1 precision digit
  133. TwAddVarRO(bar, "time", TW_TYPE_DOUBLE, &time, " label='Time' precision=1 help='Time (in seconds).' ");
  134. //
  135. TwAddVarRO(bar, "frameCount", TW_TYPE_UINT32, &frameCount, " label='FrameCount' precision=1 help='FrameCount (in counts).' ");
  136. TwAddVarRO(bar, "fps", TW_TYPE_DOUBLE, &fps, " label='fps' precision=1 help='fps (in fps).' ");
  137. // Add 'bgColor' to 'bar': it is a modifable variable of type TW_TYPE_COLOR3F (3 floats color)
  138. TwAddVarRW(bar, "bgColor", TW_TYPE_COLOR3F, &bgColor, " label='Background color' ");
  139. // Add 'cubeColor' to 'bar': it is a modifable variable of type TW_TYPE_COLOR32 (32 bits color) with alpha
  140. TwAddVarRW(bar, "cubeColor", TW_TYPE_COLOR32, &cubeColor, " label='Cube color' alpha help='Color and transparency of the cube.' ");
  141. //
  142. // TwAddVarRW(bar, "quaternion", TW_TYPE_QUAT4F, &quat, " label='Cube color' alpha help='Color and transparency of the cube.' ");
  143. }
  144. //-----------------
  145. // Create Shaders
  146. enum vertexShaderSource = "
  147. #version 420 core
  148. layout(location = 0) in vec3 in_position;
  149. layout(location = 1) in vec3 in_normal;
  150. layout(location = 2) in vec2 in_texcoord;
  151. layout(location = 3) in vec4 in_color;
  152. out vec4 v_color;
  153. out gl_PerVertex
  154. {
  155. vec4 gl_Position;
  156. };
  157. void main()
  158. {
  159. gl_Position = vec4(0.005 * in_position.x, 0.005 * in_position.y, 0.005* in_position.z, 1.0);
  160. v_color = in_color;
  161. }
  162. ";
  163. enum fragmentShaderSource = "
  164. #version 420 core
  165. in vec4 v_color;
  166. out vec4 FragColor;
  167. void main()
  168. {
  169. FragColor = v_color;
  170. }
  171. ";
  172. auto createShaderPipeline(Shader!(ShaderType.Vertex) vertexShader, Shader!(ShaderType.Fragment) fragmentShader) {
  173. assert(vertexShader.isLinked, vertexShader.infoLog());
  174. assert(fragmentShader.isLinked, fragmentShader.infoLog());
  175. writeln("a: ");
  176. auto shaderPipeline = new ShaderPipeline();
  177. shaderPipeline.bind();
  178. shaderPipeline.use(vertexShader);
  179. shaderPipeline.use(fragmentShader);
  180. writeln("b: ");
  181. assert(shaderPipeline.isValidProgramPipeline, shaderPipeline.infoLog());
  182. shaderPipeline.unbind();
  183. writeln("c: ");
  184. return shaderPipeline;
  185. }
  186. class Tester {
  187. Window _window;
  188. bool _keepRunning = true;
  189. this() {
  190. this._window = initThree();
  191. this._window.onKey.connect!"_onKey"(this);
  192. this._window.onClose.connect!"_onClose"(this);
  193. }
  194. ~this() {
  195. import core.memory;
  196. writeln("GC.collect: ");
  197. GC.collect();
  198. //Collect window _AFTER_ everything else
  199. this._window = null;
  200. GC.collect();
  201. deinitThree();
  202. }
  203. void run() {
  204. auto mesh = new Mesh("C:/Coding/models/Collada/duck.dae");
  205. setupTweakbar();
  206. writeln("creating shaders: ");
  207. auto vertexShader = new Shader!(ShaderType.Vertex)(vertexShaderSource);
  208. auto fragmentShader = new Shader!(ShaderType.Fragment)(fragmentShaderSource);
  209. writeln("creating shader pipeline: ");
  210. auto shaderPipeline = createShaderPipeline(vertexShader, fragmentShader);
  211. writeln("connectiong window callbacks: ");
  212. this._window.onSize.connect!"onSize"(this);
  213. this._window.onPosition.connect!"onPosition"(this);
  214. this._window.onButton.connect!"onButton"(this);
  215. this._window.onCursorPos.connect!"onCursorPos"(this);
  216. writeln("begin render loop: ");
  217. //-----------------
  218. // Render Loop
  219. glfwSetTime(0);
  220. while(this._keepRunning) {
  221. this._window.clear(0, 0, 0.5, 1);
  222. shaderPipeline.bind();
  223. mesh.vao.bind();
  224. //vboVertexData.bind();
  225. glDrawArrays(GL_TRIANGLES, 0, mesh.vertexData.length);
  226. //vbo.unbind();
  227. mesh.vao.unbind();
  228. shaderPipeline.unbind();
  229. TwDraw();
  230. this._window.swapBuffers();
  231. updateWindows();
  232. // ++frameCount;
  233. // //if(frameCount % 100 == 0) {
  234. // time = glfwGetTime();
  235. // fps = cast(double)frameCount / time;
  236. // //}
  237. }
  238. }
  239. void onSize(Window window, int width, int height) {
  240. TwWindowSize(width, height);
  241. }
  242. void onPosition(Window window, int x, int y) {
  243. }
  244. void onButton(Window window , int button, ButtonAction action) {
  245. TwMouseAction twaction = action == ButtonAction.Pressed ? TW_MOUSE_PRESSED : TW_MOUSE_RELEASED;
  246. TwMouseButtonID twbutton;
  247. switch(button) {
  248. default:
  249. case GLFW_MOUSE_BUTTON_LEFT: twbutton = TW_MOUSE_LEFT; break;
  250. case GLFW_MOUSE_BUTTON_RIGHT: twbutton = TW_MOUSE_RIGHT; break;
  251. case GLFW_MOUSE_BUTTON_MIDDLE: twbutton = TW_MOUSE_MIDDLE; break;
  252. }
  253. TwMouseButton(twaction, twbutton);
  254. }
  255. void onCursorPos(Window window, double x, double y) {
  256. TwMouseMotion(cast(int)x, this._window.getBounds()[3] - cast(int)y);
  257. }
  258. void stop() {
  259. this._keepRunning = false;
  260. }
  261. void _onKey(Window window, Key key, ScanCode scanCode, KeyAction action, KeyMod keyMod) {
  262. if(window is this._window && action == KeyAction.Pressed) {
  263. if(key == Key.Escape) {
  264. this.stop();
  265. }
  266. }
  267. }
  268. void _onClose(Window window) {
  269. this.stop();
  270. }
  271. }
  272. void main() {
  273. Unique!(Tester) tester = new Tester();
  274. tester.run();
  275. }