app.d 24 KB

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