app.d 9.5 KB

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