1
1

app.d 24 KB

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