app.d 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import std.stdio;
  2. import three;
  3. import std.typecons;
  4. import derelict.opengl3.gl3;
  5. import three.gl.util;
  6. class Tester {
  7. Unique!(Window) _window;
  8. bool _keepRunning = true;
  9. this() {
  10. this._window = initThree();
  11. this._window.onKey.connect!"_onKey"(this);
  12. this._window.onClose.connect!"_onClose"(this);
  13. }
  14. ~this() {
  15. //_window.destroy();
  16. deinitThree();
  17. }
  18. void run() {
  19. //-----------------
  20. // Create Mesh
  21. Vector3f vertices[3] = [
  22. Vector3f(-1.0f, -1.0f, 0.0f),
  23. Vector3f( 1.0f, -1.0f, 0.0f),
  24. Vector3f( 0.0f, 1.0f, 0.0f)
  25. ];
  26. Unique!(VertexArrayObject) vao = new VertexArrayObject();
  27. vao.bind();
  28. Unique!(VertexBufferObject!(VertexBufferObjectTarget.Array)) vbo = new VertexBufferObject!(VertexBufferObjectTarget.Array);
  29. vbo.bind();
  30. GLuint attribIndex = 0;
  31. glBufferData(GL_ARRAY_BUFFER, cast(ptrdiff_t)(vertices.sizeof) , vertices.ptr, GL_STATIC_DRAW);
  32. glEnableVertexAttribArray(attribIndex);
  33. glVertexAttribPointer(attribIndex, 3, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
  34. vbo.unbind();
  35. vao.unbind();
  36. //-----------------
  37. // Create Shaders
  38. enum vertexShaderSource = "
  39. #version 420 core
  40. layout(location = 0) in vec3 in_position;
  41. //layout(location = 1) in vec3 in_normal;
  42. //layout(location = 2) in vec2 in_texcoord;
  43. //layout(location = 3) in vec3 in_color;
  44. out gl_PerVertex
  45. {
  46. vec4 gl_Position;
  47. };
  48. void main()
  49. {
  50. gl_Position = vec4(0.5 * in_position.x, 0.5 * in_position.y, in_position.z, 1.0);
  51. }
  52. ";
  53. enum fragmentShaderSource = "
  54. #version 420 core
  55. out vec4 FragColor;
  56. void main()
  57. {
  58. FragColor = vec4(1.0, 0.0, 0.0, 1.0);
  59. }
  60. ";
  61. Unique!(Shader!(ShaderType.Vertex)) vertexShader = new Shader!(ShaderType.Vertex)(vertexShaderSource);
  62. Unique!(Shader!(ShaderType.Fragment)) fragmentShader = new Shader!(ShaderType.Fragment)(fragmentShaderSource);
  63. assert(vertexShader.isLinked, vertexShader.infoLog());
  64. assert(fragmentShader.isLinked, fragmentShader.infoLog());
  65. Unique!(ShaderPipeline) shaderPipeline = new ShaderPipeline();
  66. shaderPipeline.bind();
  67. shaderPipeline.use(vertexShader);
  68. shaderPipeline.use(fragmentShader);
  69. assert(shaderPipeline.isValidProgramPipeline, shaderPipeline.infoLog());
  70. shaderPipeline.unbind();
  71. //-----------------
  72. // Render Loop
  73. while(this._keepRunning) {
  74. updateWindows();
  75. this._window.clear(0, 0, 0.5, 1);
  76. shaderPipeline.bind();
  77. vao.bind();
  78. vbo.bind();
  79. glDrawArrays(GL_TRIANGLES, 0, 3);
  80. vbo.unbind();
  81. vao.unbind();
  82. shaderPipeline.unbind();
  83. this._window.swapBuffers();
  84. }
  85. }
  86. void stop() {
  87. this._keepRunning = false;
  88. }
  89. void _onKey(Window window, Key key, ScanCode scanCode, KeyAction action, KeyMod keyMod) {
  90. if(window is this._window.opDot() && action == KeyAction.Pressed) {
  91. if(key == Key.Escape) {
  92. this.stop();
  93. }
  94. }
  95. }
  96. void _onClose(Window window) {
  97. this.stop();
  98. }
  99. }
  100. void main() {
  101. Unique!(Tester) tester = new Tester();
  102. tester.run();
  103. }