app.d 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. class Tester {
  9. Unique!(Window) _window;
  10. bool _keepRunning = true;
  11. this() {
  12. this._window = initThree();
  13. this._window.onKey.connect!"_onKey"(this);
  14. this._window.onClose.connect!"_onClose"(this);
  15. }
  16. ~this() {
  17. //_window.destroy();
  18. deinitThree();
  19. }
  20. void run() {
  21. double time = 0, dt; // Current time and enlapsed time
  22. double turn = 0; // Model turn counter
  23. double speed = 0.3; // Model rotation speed
  24. int wire = 0; // Draw model in wireframe?
  25. uint frameCount = 0;
  26. double fps = 0;
  27. float bgColor[3] = [0.1f, 0.2f, 0.4f]; // Background color
  28. ubyte cubeColor[4] = [255, 0, 0, 128]; // Model color (32bits RGBA)
  29. TwWindowSize(640, 480);
  30. // Create a tweak bar
  31. auto bar = TwNewBar("TweakBar");
  32. TwDefine(" GLOBAL help='This example shows how to integrate AntTweakBar with GLFW and OpenGL.' "); // Message added to the help bar.
  33. // Add 'speed' to 'bar': it is a modifable (RW) variable of type TW_TYPE_DOUBLE. Its key shortcuts are [s] and [S].
  34. TwAddVarRW(bar, "speed", TW_TYPE_DOUBLE, &speed,
  35. " label='Rot speed' min=0 max=2 step=0.01 keyIncr=s keyDecr=S help='Rotation speed (turns/second)' ");
  36. // Add 'wire' to 'bar': it is a modifable variable of type TW_TYPE_BOOL32 (32 bits boolean). Its key shortcut is [w].
  37. TwAddVarRW(bar, "wire", TW_TYPE_BOOL32, &wire,
  38. " label='Wireframe mode' key=CTRL+w help='Toggle wireframe display mode.' ");
  39. // Add 'time' to 'bar': it is a read-only (RO) variable of type TW_TYPE_DOUBLE, with 1 precision digit
  40. TwAddVarRO(bar, "time", TW_TYPE_DOUBLE, &time, " label='Time' precision=1 help='Time (in seconds).' ");
  41. TwAddVarRO(bar, "frameCount", TW_TYPE_UINT32, &frameCount, " label='FrameCount' precision=1 help='FrameCount (in counts).' ");
  42. TwAddVarRO(bar, "fps", TW_TYPE_DOUBLE, &fps, " label='fps' precision=1 help='fps (in fps).' ");
  43. // Add 'bgColor' to 'bar': it is a modifable variable of type TW_TYPE_COLOR3F (3 floats color)
  44. TwAddVarRW(bar, "bgColor", TW_TYPE_COLOR3F, &bgColor, " label='Background color' ");
  45. // Add 'cubeColor' to 'bar': it is a modifable variable of type TW_TYPE_COLOR32 (32 bits color) with alpha
  46. TwAddVarRW(bar, "cubeColor", TW_TYPE_COLOR32, &cubeColor,
  47. " label='Cube color' alpha help='Color and transparency of the cube.' ");
  48. //-----------------
  49. // Create Mesh
  50. Vector3f vertices[3] = [
  51. Vector3f(-1.0f, -1.0f, 0.0f),
  52. Vector3f( 1.0f, -1.0f, 0.0f),
  53. Vector3f( 0.0f, 1.0f, 0.0f)
  54. ];
  55. Unique!(VertexArrayObject) vao = new VertexArrayObject();
  56. vao.bind();
  57. Unique!(VertexBufferObject!(VertexBufferObjectTarget.Array)) vbo = new VertexBufferObject!(VertexBufferObjectTarget.Array);
  58. vbo.bind();
  59. GLuint attribIndex = 0;
  60. glBufferData(GL_ARRAY_BUFFER, cast(ptrdiff_t)(vertices.sizeof) , vertices.ptr, GL_STATIC_DRAW);
  61. glEnableVertexAttribArray(attribIndex);
  62. glVertexAttribPointer(attribIndex, 3, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
  63. vbo.unbind();
  64. vao.unbind();
  65. //-----------------
  66. // Create Shaders
  67. enum vertexShaderSource = "
  68. #version 420 core
  69. layout(location = 0) in vec3 in_position;
  70. //layout(location = 1) in vec3 in_normal;
  71. //layout(location = 2) in vec2 in_texcoord;
  72. //layout(location = 3) in vec3 in_color;
  73. out gl_PerVertex
  74. {
  75. vec4 gl_Position;
  76. };
  77. void main()
  78. {
  79. gl_Position = vec4(0.5 * in_position.x, 0.5 * in_position.y, in_position.z, 1.0);
  80. }
  81. ";
  82. enum fragmentShaderSource = "
  83. #version 420 core
  84. out vec4 FragColor;
  85. void main()
  86. {
  87. FragColor = vec4(1.0, 0.0, 0.0, 1.0);
  88. }
  89. ";
  90. Unique!(Shader!(ShaderType.Vertex)) vertexShader = new Shader!(ShaderType.Vertex)(vertexShaderSource);
  91. Unique!(Shader!(ShaderType.Fragment)) fragmentShader = new Shader!(ShaderType.Fragment)(fragmentShaderSource);
  92. assert(vertexShader.isLinked, vertexShader.infoLog());
  93. assert(fragmentShader.isLinked, fragmentShader.infoLog());
  94. Unique!(ShaderPipeline) shaderPipeline = new ShaderPipeline();
  95. shaderPipeline.bind();
  96. shaderPipeline.use(vertexShader);
  97. shaderPipeline.use(fragmentShader);
  98. assert(shaderPipeline.isValidProgramPipeline, shaderPipeline.infoLog());
  99. shaderPipeline.unbind();
  100. //-----------------
  101. // Render Loop
  102. glfwSetTime(0);
  103. while(this._keepRunning) {
  104. this._window.clear(0, 0, 0.5, 1);
  105. shaderPipeline.bind();
  106. vao.bind();
  107. vbo.bind();
  108. glDrawArrays(GL_TRIANGLES, 0, 3);
  109. vbo.unbind();
  110. vao.unbind();
  111. shaderPipeline.unbind();
  112. TwDraw();
  113. this._window.swapBuffers();
  114. ++frameCount;
  115. if(frameCount % 100 == 0) {
  116. updateWindows();
  117. time = glfwGetTime();
  118. fps = cast(double)frameCount / time;
  119. }
  120. }
  121. }
  122. void stop() {
  123. this._keepRunning = false;
  124. }
  125. void _onKey(Window window, Key key, ScanCode scanCode, KeyAction action, KeyMod keyMod) {
  126. if(window is this._window.opDot() && action == KeyAction.Pressed) {
  127. if(key == Key.Escape) {
  128. this.stop();
  129. }
  130. }
  131. }
  132. void _onClose(Window window) {
  133. this.stop();
  134. }
  135. }
  136. void main() {
  137. Unique!(Tester) tester = new Tester();
  138. tester.run();
  139. }