app.d 9.8 KB

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