You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

13_shader_builder.cpp 3.9 KiB

преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // Lol Engine — Shader builder tutorial
  3. //
  4. // Copyright © 2012—2015 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #if HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include <lol/engine.h>
  16. #include "loldebug.h"
  17. #include <cstdio>
  18. using namespace lol;
  19. #define Line(x) ((x) + "\n")
  20. class ShaderBuilderDemo : public WorldEntity
  21. {
  22. public:
  23. ShaderBuilderDemo()
  24. {
  25. }
  26. virtual void TickGame(float seconds)
  27. {
  28. WorldEntity::TickGame(seconds);
  29. }
  30. virtual void TickDraw(float seconds, Scene &scene)
  31. {
  32. WorldEntity::TickDraw(seconds, scene);
  33. File file;
  34. file.Open("13_shader_builder_export.txt", FileAccess::Write);
  35. //file.Open("13_shader_builder_export.txt", FileAccess::Read);
  36. String code;
  37. ShaderBuilder builder("red_blue_green", "120");
  38. ShaderBlock nothing_vertex("NothingVertex");
  39. ShaderBlock red_pixel("RedPixel");
  40. ShaderBlock green_pixel("GreenPixel");
  41. ShaderBlock blue_pixel("BluePixel");
  42. ShaderVar out_vertex = ShaderVar::GetShaderOut(ShaderProgram::Vertex);
  43. ShaderVar out_pixel = ShaderVar::GetShaderOut(ShaderProgram::Pixel);
  44. ShaderVar in_position = ShaderVar(ShaderVariable::Attribute, ShaderVariableType::Vec3, "position");
  45. ShaderVar in_color = ShaderVar(ShaderVariable::Attribute, ShaderVariableType::Vec4, "color");
  46. ShaderVar pass_color = ShaderVar(ShaderVariable::Varying, ShaderVariableType::Vec4, "color");
  47. nothing_vertex << in_position
  48. << in_color
  49. << pass_color;
  50. nothing_vertex.AddVar(out_vertex);
  51. nothing_vertex.SetMainCode(String() +
  52. Line(pass_color + " = " + in_color + ";") +
  53. Line(out_vertex + " = vec4(" + in_position + ", 0.f);")
  54. );
  55. ShaderVar ambient = ShaderVar(ShaderVariable::InOut, ShaderVariableType::Vec4, "ambient");
  56. red_pixel.AddVar(pass_color);
  57. red_pixel.AddVar(out_pixel);
  58. red_pixel.AddVar(ambient);
  59. red_pixel.SetMainCode(String() +
  60. out_pixel + " = " + pass_color + ";\n" +
  61. out_pixel + ".r = 1.0;\n" +
  62. "ambient = vec4(1.0);\n"
  63. );
  64. green_pixel.AddVar(pass_color);
  65. green_pixel.AddVar(out_pixel);
  66. green_pixel.AddVar(ambient);
  67. green_pixel.SetMainCode(String() +
  68. out_pixel + " = " + pass_color + ";\n" +
  69. out_pixel + ".g = 1.0;\n" +
  70. "ambient.r = 0.0;\n"
  71. );
  72. blue_pixel.AddVar(pass_color);
  73. blue_pixel.AddVar(out_pixel);
  74. blue_pixel.AddVar(ambient);
  75. blue_pixel.SetCustomCode(String() +
  76. "void SetAmbient(inout vec4 ambient)\n" +
  77. "{\n" +
  78. " ambient = vec4(1.0, 1.0, 1.0, 1.0);\n" +
  79. "}\n");
  80. blue_pixel.SetMainCode(String() +
  81. out_pixel + " = " + pass_color + ";\n" +
  82. out_pixel + ".b = 1.0;\n" +
  83. "SetAmbient(ambient);\n" +
  84. out_pixel + " *= ambient;\n"
  85. );
  86. builder << ShaderProgram::Vertex
  87. << nothing_vertex
  88. << ShaderProgram::Pixel
  89. << red_pixel
  90. << green_pixel
  91. << blue_pixel;
  92. builder.Build(code);
  93. file.WriteString(code);
  94. //code = file.ReadString();
  95. file.Close();
  96. Shader* shader = Shader::Create(builder.GetName(), code);
  97. Shader::Destroy(shader);
  98. Ticker::Shutdown();
  99. }
  100. };
  101. int main(int argc, char **argv)
  102. {
  103. System::Init(argc, argv);
  104. Application app("Tutorial 13: Shader Builder", ivec2(1280, 720), 60.0f);
  105. new ShaderBuilderDemo();
  106. app.Run();
  107. return EXIT_SUCCESS;
  108. }