Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

131 řádky
4.0 KiB

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