app.d 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. import std.stdio;
  2. import std.typecons;
  3. import three;
  4. import std.experimental.logger;
  5. import three.gl.util;
  6. import three.window;
  7. import three.viewport;
  8. import three.camera;
  9. import three.renderTarget;
  10. import three.scene;
  11. import three.renderer;
  12. void main() {
  13. Window window;
  14. Viewport viewport;
  15. Scene scene;
  16. Camera camera;
  17. RenderTarget renderTarget;
  18. // OpenGlTiledDeferredRenderer renderer;
  19. Renderer renderer;
  20. bool keepRunning = true;
  21. //------------------------------------------------
  22. // Load and Construct Rendering Pipeline
  23. //------------------------------------------------
  24. DerelictGL3.load();
  25. DerelictGLFW3.load();
  26. DerelictFI.load();
  27. // DerelictFT.load();
  28. DerelictASSIMP3.load();
  29. DerelictAntTweakBar.load();
  30. if(!glfwInit()) throw new Exception("Initialising GLFW failed"); scope(exit) glfwTerminate();
  31. window.construct("Three.d", 1600, 900); scope(exit) window.destruct();
  32. try {
  33. GLVersion glVersion = DerelictGL3.reload();
  34. import std.conv : to;
  35. writeln("Reloaded OpenGL Version: ", to!string(glVersion));
  36. } catch(Exception e) {
  37. writeln("Reloading OpenGl failed: " ~ e.msg);
  38. }
  39. // static FT_Library _s_freeTypeLibrary
  40. // if(!FT_Init_FreeType(&_s_freeTypeLibrary)) throw new Exception("Initialising FreeType failed"); scope(exit) FT_Done_FreeType(_s_freeTypeLibrary);
  41. if(TwInit(TW_OPENGL_CORE, null) == 0) throw new Exception("Initialising AntTweakBar failed"); scope(exit) TwTerminate();
  42. viewport.construct(); scope(exit) viewport.destruct();
  43. scene.construct(); scope(exit) scene.destruct();
  44. camera.construct(); scope(exit) camera.destruct();
  45. renderTarget.construct(window.width, window.height); scope(exit) renderTarget.destruct();
  46. renderer.construct(window.width, window.height); scope(exit) renderer.destruct();
  47. //------------------------------------------------
  48. // Create Scene
  49. //------------------------------------------------
  50. ModelData modelData = loadModelData("C:/Coding/models/Collada/duck.dae");
  51. //------------------------------------------------
  52. // Generate TweakBar
  53. //------------------------------------------------
  54. TwWindowSize(window.width, window.height);
  55. auto tweakBar = TwNewBar("TweakBar");
  56. //------------------------------------------------
  57. // Connect Window callbacks
  58. //------------------------------------------------
  59. window.onKey = (ref Window rWindow, Key key, ScanCode scanCode, KeyAction action, KeyMod keyMod) {
  60. if(window is window && action == KeyAction.Pressed) {
  61. if(key == Key.Escape) {
  62. keepRunning = false;
  63. }
  64. }
  65. };
  66. window.onClose = (ref Window rWindow) {
  67. keepRunning = false;
  68. };
  69. window.onSize = (ref Window rWindow, int width, int height) {
  70. TwWindowSize(width, height);
  71. };
  72. window.onPosition = (ref Window rWindow, int x, int y) {
  73. };
  74. window.onButton = (ref Window rWindow , int button, ButtonAction action) {
  75. TwMouseAction twaction = action == ButtonAction.Pressed ? TW_MOUSE_PRESSED : TW_MOUSE_RELEASED;
  76. TwMouseButtonID twbutton;
  77. switch(button) {
  78. default:
  79. case GLFW_MOUSE_BUTTON_LEFT: twbutton = TW_MOUSE_LEFT; break;
  80. case GLFW_MOUSE_BUTTON_RIGHT: twbutton = TW_MOUSE_RIGHT; break;
  81. case GLFW_MOUSE_BUTTON_MIDDLE: twbutton = TW_MOUSE_MIDDLE; break;
  82. }
  83. TwMouseButton(twaction, twbutton);
  84. };
  85. window.onCursorPos = (ref Window rWindow, double x, double y) {
  86. TwMouseMotion(cast(int)x, window.height - cast(int)y);
  87. };
  88. //------------------------------------------------
  89. // Main Loop
  90. //------------------------------------------------
  91. ulong frameCount = 0;
  92. glfwSetTime(0);
  93. while(keepRunning) {
  94. window.pollEvents();
  95. window.makeAktiveRenderWindow();
  96. glCheck!glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  97. glCheck!glClearDepth(1.0f);
  98. glCheck!glClearColor(0, 0.3, 0, 1);
  99. renderer.renderOneFrame(scene, camera, renderTarget, viewport);
  100. TwDraw();
  101. debug{ renderer.blitGBufferToScreen(); }
  102. window.swapBuffers();
  103. ++frameCount;
  104. if(frameCount % 1000 == 0) {
  105. auto fps = cast(double)frameCount / glfwGetTime();
  106. log("FPS: ", fps);
  107. frameCount = 0;
  108. glfwSetTime(0);
  109. }
  110. }
  111. }
  112. /+++++++
  113. //======================================================================================================================
  114. //
  115. //======================================================================================================================
  116. struct Slice(T) {
  117. T* data;
  118. size_t length;
  119. }
  120. //======================================================================================================================
  121. //
  122. //======================================================================================================================
  123. struct PersistentlyMappedBuffer(T) {
  124. GLuint bo;
  125. GLenum target;
  126. Slice!T data;
  127. alias Atom = T;
  128. }
  129. void create(T)(out PersistentlyMappedBuffer!T pmb, GLuint count, GLenum target, GLbitfield createFlags, GLbitfield mapFlags) {
  130. pmb.target = target;
  131. glCheck!glGenBuffers(1, &pmb.bo);
  132. glCheck!glBindBuffer(target, pmb.bo);
  133. glCheck!glBufferStorage(target, T.sizeof * count, null, createFlags);
  134. pmb.data.data = cast(T*)glMapBufferRange(target, 0, T.sizeof * count, mapFlags);
  135. pmb.data.length = count;
  136. if (!pmb.data.data) {
  137. throw new Exception("glMapBufferRange failed, probable bug.");
  138. }
  139. }
  140. void destroy(T)(ref PersistentlyMappedBuffer!T pmb) {
  141. glCheck!glBindBuffer(pmb.target, mName);
  142. glCheck!glUnmapBuffer(pmb.target);
  143. glCheck!glDeleteBuffers(1, &mName);
  144. pmb = GlPersistentlyMappedBuffer!T.init;
  145. }
  146. //
  147. ////======================================================================================================================
  148. ////
  149. ////======================================================================================================================
  150. //struct GlCircularBufferView(T) {
  151. // GlPersistentlyMappedBuffer!T _buffer;
  152. // GLsizeiptr _head;
  153. //}
  154. //======================================================================================================================
  155. //
  156. //======================================================================================================================
  157. struct DrawArraysIndirectCommand {
  158. GLuint vertexCount;
  159. GLuint instanceCount;
  160. GLuint firstVertex;
  161. GLuint baseInstance;
  162. }
  163. struct DrawElementsIndirectCommand {
  164. GLuint count;
  165. GLuint instanceCount;
  166. GLuint firstIndex;
  167. GLuint baseVertex;
  168. GLuint baseInstance;
  169. }
  170. struct PerInstanceRenderData {
  171. }
  172. struct MeshDataRef {
  173. }
  174. struct PerInstanceDataRef {
  175. }
  176. struct Mesh {
  177. size_t meshDataRef;
  178. InstanceData[] instanceData;
  179. }
  180. struct MeshInstance {
  181. size_t meshRef;
  182. size_t meshInstanceRef;
  183. }
  184. struct Renderer {
  185. MeshData[] meshData;
  186. Mesh[] meshes;
  187. MeshInstance[] meshInstances;
  188. PersistentlyMappedBuffer!DrawElementsIndirectCommand commandBuffer;
  189. PersistentlyMappedBuffer!TransformationMatrix perInstanceBuffer;
  190. void renderOneFrame(Instances instances) {
  191. auto instanceCount = 0;
  192. // write draw commands
  193. DrawElementsIndirectCommand* cmds = commandBuffer.reserve(instanceCount);
  194. foreach(size_t i = 0; i < instanceCount; ++i) {
  195. DrawElementsIndirectCommand* cmd = &cmds[u];
  196. cmd->count = mIndexCount;
  197. cmd->instanceCount = 1;
  198. cmd->firstIndex = 0;
  199. cmd->baseVertex = 0;
  200. cmd->baseInstance = mUseShaderDrawParameters ? 0 : u;
  201. }
  202. // write per instance
  203. perInstanceBuffer.reserve(instances.length);
  204. foreach() {
  205. }
  206. glCheck!glMultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_SHORT, mCommands.GetHeadOffset(), xformCount, 0);
  207. }
  208. }
  209. /++++
  210. struct GlPeristentBuffer {
  211. }
  212. struct GlRingBuffer {
  213. GLuint bo;
  214. }
  215. struct ParamPerInstanceRingBuffer {
  216. GLuint bo;
  217. }
  218. struct Renderer {
  219. CommandRingBuffer commandRb;
  220. ParamPerInstanceRingBuffer paramPerInstanceRb;
  221. }
  222. struct MeshStorageManager(MeshLayout = DefaultVertexLayout) {
  223. struct Slice {
  224. size_t offset;
  225. size_t length;
  226. }
  227. struct MeshStorage {
  228. Slice vertexSlice;
  229. Slice indexSlice;
  230. }
  231. GLuint vao;
  232. GLuint vbo;
  233. GLuint ibo;
  234. MeshStorage[] meshes;
  235. }
  236. struct MeshInstance {
  237. }
  238. struct MappedPersistentBuffer {
  239. GLuint vbo;
  240. void* data;
  241. }
  242. void create(out MappedPersistentBuffer mpb, GLsizeiptr bufferSize) {
  243. glCheck!glGenVertexArrays(1, &mpb.vbo);
  244. GLbitfield mapFlags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
  245. GLbitfield createFlags = mapFlags | GL_MAP_DYNAMIC_STORAGE_BIT;
  246. glCheck!glBindBuffer(GL_ARRAY_BUFFER, mpb.vbo);
  247. glCheck!glBufferStorage(GL_ARRAY_BUFFER, bufferSize, null, createFlags);
  248. mpb.data = glCheck!glMapBufferRange(GL_ARRAY_BUFFER, 0, bufferSize, mapFlags);
  249. }
  250. ++++/
  251. //======================================================================================================================
  252. //
  253. //======================================================================================================================
  254. struct SOAVector3 {
  255. SoA!float x;
  256. SoA!float y;
  257. SoA!float z;
  258. }
  259. struct SOAQuaternion {
  260. SoA!float x;
  261. SoA!float y;
  262. SoA!float z;
  263. SoA!float w;
  264. }
  265. //======================================================================================================================
  266. //
  267. //======================================================================================================================
  268. void main() {
  269. Window window;
  270. Viewport viewport;
  271. Scene scene;
  272. Camera camera;
  273. RenderTarget renderTarget;
  274. OpenGlTiledDeferredRenderer renderer;
  275. bool keepRunning = true;
  276. //------------------------------------------------
  277. // Load and Construct Rendering Pipeline
  278. //------------------------------------------------
  279. DerelictGL3.load();
  280. DerelictGLFW3.load();
  281. DerelictFI.load();
  282. // DerelictFT.load();
  283. DerelictASSIMP3.load();
  284. DerelictAntTweakBar.load();
  285. if(!glfwInit()) throw new Exception("Initialising GLFW failed"); scope(exit) glfwTerminate();
  286. window.construct("Three.d", 1600, 900); scope(exit) window.destruct();
  287. try {
  288. GLVersion glVersion = DerelictGL3.reload();
  289. import std.conv : to;
  290. writeln("Reloaded OpenGL Version: ", to!string(glVersion));
  291. } catch(Exception e) {
  292. writeln("Reloading OpenGl failed: " ~ e.msg);
  293. }
  294. // static FT_Library _s_freeTypeLibrary
  295. // if(!FT_Init_FreeType(&_s_freeTypeLibrary)) throw new Exception("Initialising FreeType failed"); scope(exit) FT_Done_FreeType(_s_freeTypeLibrary);
  296. if(TwInit(TW_OPENGL_CORE, null) == 0) throw new Exception("Initialising AntTweakBar failed"); scope(exit) TwTerminate();
  297. viewport.construct(); scope(exit) window.destruct();
  298. scene.construct(); scope(exit) window.destruct();
  299. camera.construct(); scope(exit) window.destruct();
  300. renderTarget.construct(window.width, window.height); scope(exit) renderTarget.destruct();
  301. renderer.construct(window.width, window.height); scope(exit) renderer.destruct();
  302. //############################
  303. //############################
  304. //############################
  305. //############################
  306. GlPersistentlyMappedBuffer!DefaultVertexData vertexBuffer;
  307. vertexBuffer.create(1024, GL_ARRAY_BUFFER, 0, 0);
  308. //############################
  309. //############################
  310. //############################
  311. //############################
  312. //------------------------------------------------
  313. // Create Scene
  314. //------------------------------------------------
  315. scene.mesh.loadModel("C:/Coding/models/Collada/duck.dae");
  316. //------------------------------------------------
  317. // Generate TweakBar
  318. //------------------------------------------------
  319. TwWindowSize(window.width, window.height);
  320. auto tweakBar = TwNewBar("TweakBar");
  321. //------------------------------------------------
  322. // Connect Window callbacks
  323. //------------------------------------------------
  324. window.onKey = (ref Window rWindow, Key key, ScanCode scanCode, KeyAction action, KeyMod keyMod) {
  325. if(window is window && action == KeyAction.Pressed) {
  326. if(key == Key.Escape) {
  327. keepRunning = false;
  328. }
  329. }
  330. };
  331. window.onClose = (ref Window rWindow) {
  332. keepRunning = false;
  333. };
  334. window.onSize = (ref Window rWindow, int width, int height) {
  335. TwWindowSize(width, height);
  336. };
  337. window.onPosition = (ref Window rWindow, int x, int y) {
  338. };
  339. window.onButton = (ref Window rWindow , int button, ButtonAction action) {
  340. TwMouseAction twaction = action == ButtonAction.Pressed ? TW_MOUSE_PRESSED : TW_MOUSE_RELEASED;
  341. TwMouseButtonID twbutton;
  342. switch(button) {
  343. default:
  344. case GLFW_MOUSE_BUTTON_LEFT: twbutton = TW_MOUSE_LEFT; break;
  345. case GLFW_MOUSE_BUTTON_RIGHT: twbutton = TW_MOUSE_RIGHT; break;
  346. case GLFW_MOUSE_BUTTON_MIDDLE: twbutton = TW_MOUSE_MIDDLE; break;
  347. }
  348. TwMouseButton(twaction, twbutton);
  349. };
  350. window.onCursorPos = (ref Window rWindow, double x, double y) {
  351. TwMouseMotion(cast(int)x, window.height - cast(int)y);
  352. };
  353. //------------------------------------------------
  354. // Main Loop
  355. //------------------------------------------------
  356. ulong frameCount = 0;
  357. glfwSetTime(0);
  358. while(keepRunning) {
  359. window.pollEvents();
  360. window.makeAktiveRenderWindow();
  361. glCheck!glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  362. glCheck!glClearDepth(1.0f);
  363. glCheck!glClearColor(0, 0.3, 0, 1);
  364. /*
  365. foreach(renderTarget) //framebuffer
  366. foreach(pass)
  367. foreach(material) //shaders
  368. foreach(materialInstance) //textures
  369. foreach(vertexFormat) //vertex buffers
  370. foreach(object) {
  371. write uniform data;
  372. glDrawElementsBaseVertex
  373. }
  374. */
  375. renderer.renderOneFrame(scene, camera, renderTarget, viewport);
  376. TwDraw();
  377. debug{ renderer.blitGBufferToScreen(); }
  378. window.swapBuffers();
  379. ++frameCount;
  380. if(frameCount % 1000 == 0) {
  381. auto fps = cast(double)frameCount / glfwGetTime();
  382. log("FPS: ", fps);
  383. frameCount = 0;
  384. glfwSetTime(0);
  385. }
  386. }
  387. }
  388. /+++
  389. struct Vertex {
  390. float x, y, z;
  391. }
  392. struct Normal {
  393. float x, y, z;
  394. }
  395. struct UV {
  396. float u, v;
  397. }
  398. final class Mesh {
  399. public:
  400. Vertex[] vertexData;
  401. Normal[] normalData;
  402. UV[] textureData;
  403. RGBAf[] colorData;
  404. VertexArrayObject vao;
  405. VertexBufferObject!(VertexBufferObjectTarget.Array) vboVertexData;
  406. VertexBufferObject!(VertexBufferObjectTarget.Array) vboNormalData;
  407. VertexBufferObject!(VertexBufferObjectTarget.Array) vboTextureData;
  408. VertexBufferObject!(VertexBufferObjectTarget.Array) vboColorData;
  409. this(string filePath) {
  410. writeln("loading scene: ", filePath);
  411. auto scene = aiImportFile(filePath.toStringz(), aiProcess_Triangulate);
  412. writeln("meshes: ", scene.mNumMeshes);
  413. for(uint m = 0; m < scene.mNumMeshes; ++m) {
  414. const(aiMesh*) mesh = scene.mMeshes[m];
  415. assert(mesh !is null);
  416. writeln("mesh[", m, "] faces : ", mesh.mNumFaces);
  417. for (uint f = 0; f < mesh.mNumFaces; ++f) {
  418. const(aiFace*) face = &mesh.mFaces[f];
  419. assert(face !is null);
  420. for(uint v = 0; v < 3; ++v) {
  421. aiVector3D p, n, uv;
  422. assert(face.mNumIndices > v);
  423. uint vertex = face.mIndices[v];
  424. assert(mesh.mNumVertices > vertex);
  425. p = mesh.mVertices[vertex];
  426. n = mesh.mNormals[vertex];
  427. // check if the mesh has texture coordinates
  428. if(mesh.mTextureCoords[0] !is null) {
  429. uv = mesh.mTextureCoords[0][vertex];
  430. }
  431. vertexData ~= Vertex(p.x, p.y, p.z);
  432. normalData ~= Normal(n.x, n.y, n.z);
  433. textureData ~= UV(uv.x, uv.y);
  434. colorData ~= RGBAf(n.x, n.y, n.z, 1.0f);
  435. }
  436. }
  437. }
  438. writeln("unloading scene: ", filePath);
  439. aiReleaseImport(scene);
  440. //-----------------------------
  441. // upload
  442. writeln("uploading mesh: ", filePath);
  443. vao = new VertexArrayObject();
  444. vao.bind();
  445. writeln("vertex data");
  446. vboVertexData = new VertexBufferObject!(VertexBufferObjectTarget.Array);
  447. vboVertexData.bind();
  448. GLuint attribIndex = 0;
  449. glBufferData(GL_ARRAY_BUFFER, cast(ptrdiff_t)(Vertex.sizeof * vertexData.length) , vertexData.ptr, GL_STATIC_DRAW);
  450. glEnableVertexAttribArray(attribIndex);
  451. glVertexAttribPointer(attribIndex, 3, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
  452. vboVertexData.unbind();
  453. writeln("normal data");
  454. vboNormalData = new VertexBufferObject!(VertexBufferObjectTarget.Array);
  455. vboNormalData.bind();
  456. attribIndex = 1;
  457. glBufferData(GL_ARRAY_BUFFER, cast(ptrdiff_t)(Normal.sizeof * normalData.length) , normalData.ptr, GL_STATIC_DRAW);
  458. glEnableVertexAttribArray(attribIndex);
  459. glVertexAttribPointer(attribIndex, 3, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
  460. vboNormalData.unbind();
  461. writeln("uv data");
  462. vboTextureData = new VertexBufferObject!(VertexBufferObjectTarget.Array);
  463. vboTextureData.bind();
  464. attribIndex = 2;
  465. glBufferData(GL_ARRAY_BUFFER, cast(ptrdiff_t)(UV.sizeof * textureData.length) , textureData.ptr, GL_STATIC_DRAW);
  466. glEnableVertexAttribArray(attribIndex);
  467. glVertexAttribPointer(attribIndex, 2, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
  468. vboTextureData.unbind();
  469. writeln("color data");
  470. vboColorData = new VertexBufferObject!(VertexBufferObjectTarget.Array);
  471. vboColorData.bind();
  472. attribIndex = 3;
  473. glBufferData(GL_ARRAY_BUFFER, cast(ptrdiff_t)(RGBAf.sizeof * colorData.length) , colorData.ptr, GL_STATIC_DRAW);
  474. glEnableVertexAttribArray(attribIndex);
  475. glVertexAttribPointer(attribIndex, 4, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
  476. vboColorData.unbind();
  477. vao.unbind();
  478. writeln("done");
  479. }
  480. }
  481. void setupTweakbar() {
  482. writeln("creating TweakBar");
  483. double time = 0, dt; // Current time and enlapsed time
  484. double turn = 0; // Model turn counter
  485. double speed = 0.3; // Model rotation speed
  486. int wire = 0; // Draw model in wireframe?
  487. uint frameCount = 0;
  488. double fps = 0;
  489. float bgColor[3] = [0.1f, 0.2f, 0.4f]; // Background color
  490. ubyte cubeColor[4] = [255, 0, 0, 128]; // Model color (32bits RGBA)
  491. auto quat = Quaternionf(0,0,0,1);
  492. // auto w = this._window.getBounds()[2];
  493. // auto h = this._window.getBounds()[3];
  494. TwWindowSize(1600, 900);
  495. // // Create a tweak bar
  496. // auto bar = TwNewBar("TweakBar");
  497. // TwDefine(" GLOBAL help='This example shows how to integrate AntTweakBar with GLFW and OpenGL.' "); // Message added to the help bar.
  498. // // Add 'speed' to 'bar': it is a modifable (RW) variable of type TW_TYPE_DOUBLE. Its key shortcuts are [s] and [S].
  499. // 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)' ");
  500. // // Add 'wire' to 'bar': it is a modifable variable of type TW_TYPE_BOOL32 (32 bits boolean). Its key shortcut is [w].
  501. // TwAddVarRW(bar, "wire", TW_TYPE_BOOL32, &wire, " label='Wireframe mode' key=CTRL+w help='Toggle wireframe display mode.' ");
  502. // // Add 'time' to 'bar': it is a read-only (RO) variable of type TW_TYPE_DOUBLE, with 1 precision digit
  503. // TwAddVarRO(bar, "time", TW_TYPE_DOUBLE, &time, " label='Time' precision=1 help='Time (in seconds).' ");
  504. // //
  505. // TwAddVarRO(bar, "frameCount", TW_TYPE_UINT32, &frameCount, " label='FrameCount' precision=1 help='FrameCount (in counts).' ");
  506. // TwAddVarRO(bar, "fps", TW_TYPE_DOUBLE, &fps, " label='fps' precision=1 help='fps (in fps).' ");
  507. // // Add 'bgColor' to 'bar': it is a modifable variable of type TW_TYPE_COLOR3F (3 floats color)
  508. // TwAddVarRW(bar, "bgColor", TW_TYPE_COLOR3F, &bgColor, " label='Background color' ");
  509. // // Add 'cubeColor' to 'bar': it is a modifable variable of type TW_TYPE_COLOR32 (32 bits color) with alpha
  510. // TwAddVarRW(bar, "cubeColor", TW_TYPE_COLOR32, &cubeColor, " label='Cube color' alpha help='Color and transparency of the cube.' ");
  511. // //
  512. // TwAddVarRW(bar, "quaternion", TW_TYPE_QUAT4F, &quat, " label='Cubde color' alpha help='Color anwdwd transparency of the cube.' ");
  513. }
  514. //-----------------
  515. // Create Shaders
  516. enum vertexShaderSource = "
  517. #version 420 core
  518. layout(location = 0) in vec3 in_position;
  519. layout(location = 1) in vec3 in_normal;
  520. layout(location = 2) in vec2 in_texcoord;
  521. layout(location = 3) in vec4 in_color;
  522. out vec4 v_color;
  523. out gl_PerVertex
  524. {
  525. vec4 gl_Position;
  526. };
  527. void main()
  528. {
  529. gl_Position = vec4(0.005 * in_position.x, 0.005 * in_position.y, 0.005* in_position.z, 1.0);
  530. v_color = in_color;
  531. }
  532. ";
  533. enum fragmentShaderSource = "
  534. #version 420 core
  535. in vec4 v_color;
  536. out vec4 FragColor;
  537. void main()
  538. {
  539. FragColor = v_color;
  540. }
  541. ";
  542. auto createShaderPipeline(Shader!(ShaderType.Vertex) vertexShader, Shader!(ShaderType.Fragment) fragmentShader) {
  543. assert(vertexShader.isLinked, vertexShader.infoLog());
  544. assert(fragmentShader.isLinked, fragmentShader.infoLog());
  545. writeln("a: ");
  546. auto shaderPipeline = new ShaderPipeline();
  547. shaderPipeline.bind();
  548. shaderPipeline.use(vertexShader);
  549. shaderPipeline.use(fragmentShader);
  550. writeln("b: ");
  551. assert(shaderPipeline.isValidProgramPipeline, shaderPipeline.infoLog());
  552. shaderPipeline.unbind();
  553. writeln("c: ");
  554. return shaderPipeline;
  555. }
  556. class Tester {
  557. Window _window;
  558. bool _keepRunning = true;
  559. this() {
  560. this._window = initThree();
  561. this._window.onKey.connect!"_onKey"(this);
  562. this._window.onClose.connect!"_onClose"(this);
  563. }
  564. ~this() {
  565. import core.memory;
  566. writeln("GC.collect: ");
  567. GC.collect();
  568. //Collect window _AFTER_ everything else
  569. this._window = null;
  570. GC.collect();
  571. deinitThree();
  572. }
  573. void run() {
  574. RGBAf rgg;
  575. auto mesh = new Mesh("C:/Coding/models/Collada/duck.dae");
  576. setupTweakbar();
  577. writeln("creating shaders: ");
  578. auto vertexShader = new Shader!(ShaderType.Vertex)(vertexShaderSource);
  579. auto fragmentShader = new Shader!(ShaderType.Fragment)(fragmentShaderSource);
  580. writeln("creating shader pipeline: ");
  581. auto shaderPipeline = createShaderPipeline(vertexShader, fragmentShader);
  582. writeln("connectiong window callbacks: ");
  583. this._window.onSize.connect!"onSize"(this);
  584. this._window.onPosition.connect!"onPosition"(this);
  585. this._window.onButton.connect!"onButton"(this);
  586. this._window.onCursorPos.connect!"onCursorPos"(this);
  587. writeln("begin render loop: ");
  588. //-----------------
  589. // Render Loop
  590. glfwSetTime(0);
  591. while(this._keepRunning) {
  592. this._window.clear(0, 0, 0.5, 1);
  593. shaderPipeline.bind();
  594. mesh.vao.bind();
  595. //vboVertexData.bind();
  596. glDrawArrays(GL_TRIANGLES, 0, mesh.vertexData.length);
  597. //vbo.unbind();
  598. mesh.vao.unbind();
  599. shaderPipeline.unbind();
  600. TwDraw();
  601. this._window.swapBuffers();
  602. updateWindows();
  603. // ++frameCount;
  604. // //if(frameCount % 100 == 0) {
  605. // time = glfwGetTime();
  606. // fps = cast(double)frameCount / time;
  607. // //}
  608. }
  609. }
  610. void onSize(Window window, int width, int height) {
  611. TwWindowSize(width, height);
  612. }
  613. void onPosition(Window window, int x, int y) {
  614. }
  615. void onButton(Window window , int button, ButtonAction action) {
  616. TwMouseAction twaction = action == ButtonAction.Pressed ? TW_MOUSE_PRESSED : TW_MOUSE_RELEASED;
  617. TwMouseButtonID twbutton;
  618. switch(button) {
  619. default:
  620. case GLFW_MOUSE_BUTTON_LEFT: twbutton = TW_MOUSE_LEFT; break;
  621. case GLFW_MOUSE_BUTTON_RIGHT: twbutton = TW_MOUSE_RIGHT; break;
  622. case GLFW_MOUSE_BUTTON_MIDDLE: twbutton = TW_MOUSE_MIDDLE; break;
  623. }
  624. TwMouseButton(twaction, twbutton);
  625. }
  626. void onCursorPos(Window window, double x, double y) {
  627. TwMouseMotion(cast(int)x, this._window.getBounds()[3] - cast(int)y);
  628. }
  629. void stop() {
  630. this._keepRunning = false;
  631. }
  632. void _onKey(Window window, Key key, ScanCode scanCode, KeyAction action, KeyMod keyMod) {
  633. if(window is this._window && action == KeyAction.Pressed) {
  634. if(key == Key.Escape) {
  635. this.stop();
  636. }
  637. }
  638. }
  639. void _onClose(Window window) {
  640. this.stop();
  641. }
  642. }
  643. class InputHandler {
  644. private:
  645. Window _window;
  646. OpenGlRenderer _renderer;
  647. public:
  648. this(Window window, OpenGlRenderer renderer) {
  649. _window = window;
  650. _renderer = renderer;
  651. _window.onKey.connect!"_onKey"(this);
  652. _window.onClose.connect!"_onClose"(this);
  653. _window.onSize.connect!"_onSize"(this);
  654. _window.onPosition.connect!"_onPosition"(this);
  655. _window.onButton.connect!"_onButton"(this);
  656. _window.onCursorPos.connect!"_onCursorPos"(this);
  657. }
  658. private:
  659. void _onKey(Window window, Key key, ScanCode scanCode, KeyAction action, KeyMod keyMod) {
  660. if(window is _window && action == KeyAction.Pressed) {
  661. if(key == Key.Escape) {
  662. _renderer.stop();
  663. }
  664. }
  665. }
  666. void _onClose(Window window) {
  667. _renderer.stop();
  668. }
  669. void _onSize(Window window, int width, int height) {
  670. }
  671. void _onPosition(Window window, int x, int y) {
  672. }
  673. void _onButton(Window window , int button, ButtonAction action) {
  674. }
  675. void _onCursorPos(Window window, double x, double y) {
  676. }
  677. }
  678. void main() {
  679. auto window = initThree();
  680. {
  681. OpenGlRenderer renderer = new OpenGlRenderer(window);
  682. InputHandler inputHandler = new InputHandler(window, renderer);
  683. renderer.run();
  684. }
  685. import core.memory;
  686. GC.collect();
  687. //Collect window _AFTER_ everything else
  688. this._window = null;
  689. GC.collect();
  690. deinitThree();
  691. }
  692. +++/
  693. +++++++/