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 4.1 KiB

пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Lol Engine — Shader builder tutorial
  3. //
  4. // Copyright © 2002—2015 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  5. // © 2012—2019 Sam Hocevar <sam@hocevar.net>
  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 <cstdio>
  19. #include <string>
  20. using namespace lol;
  21. class ShaderBuilderDemo : public WorldEntity
  22. {
  23. public:
  24. ShaderBuilderDemo()
  25. {
  26. }
  27. virtual void tick_game(float seconds)
  28. {
  29. WorldEntity::tick_game(seconds);
  30. }
  31. virtual void tick_draw(float seconds, Scene &scene)
  32. {
  33. WorldEntity::tick_draw(seconds, scene);
  34. File file;
  35. file.Open("13_shader_builder_export.txt", FileAccess::Write);
  36. //file.Open("13_shader_builder_export.txt", FileAccess::Read);
  37. std::string code;
  38. ShaderBuilder builder("red_blue_green", "120");
  39. ShaderBlock nothing_vertex("NothingVertex");
  40. ShaderBlock red_pixel("RedPixel");
  41. ShaderBlock green_pixel("GreenPixel");
  42. ShaderBlock blue_pixel("BluePixel");
  43. ShaderVar out_vertex = ShaderVar::GetShaderOut(ShaderProgram::Vertex);
  44. ShaderVar out_pixel = ShaderVar::GetShaderOut(ShaderProgram::Pixel);
  45. ShaderVar in_position = ShaderVar(ShaderVariable::Attribute, ShaderVariableType::Vec3, "position");
  46. ShaderVar in_color = ShaderVar(ShaderVariable::Attribute, ShaderVariableType::Vec4, "color");
  47. ShaderVar pass_color = ShaderVar(ShaderVariable::Varying, ShaderVariableType::Vec4, "color");
  48. nothing_vertex << in_position
  49. << in_color
  50. << pass_color;
  51. nothing_vertex.AddVar(out_vertex);
  52. nothing_vertex.SetMainCode(
  53. pass_color.tostring() + " = " + in_color.tostring() + ";\n" +
  54. out_vertex.tostring() + " = vec4(" + in_position.tostring() + ", 0.f);\n"
  55. );
  56. ShaderVar ambient = ShaderVar(ShaderVariable::InOut, ShaderVariableType::Vec4, "ambient");
  57. red_pixel.AddVar(pass_color);
  58. red_pixel.AddVar(out_pixel);
  59. red_pixel.AddVar(ambient);
  60. red_pixel.SetMainCode(
  61. out_pixel.tostring() + " = " + pass_color.tostring() + ";\n" +
  62. out_pixel.tostring() + ".r = 1.0;\n"
  63. "ambient = vec4(1.0);\n"
  64. );
  65. green_pixel.AddVar(pass_color);
  66. green_pixel.AddVar(out_pixel);
  67. green_pixel.AddVar(ambient);
  68. green_pixel.SetMainCode(
  69. out_pixel.tostring() + " = " + pass_color.tostring() + ";\n" +
  70. out_pixel.tostring() + ".g = 1.0;\n"
  71. "ambient.r = 0.0;\n"
  72. );
  73. blue_pixel.AddVar(pass_color);
  74. blue_pixel.AddVar(out_pixel);
  75. blue_pixel.AddVar(ambient);
  76. blue_pixel.SetCustomCode(
  77. "void SetAmbient(inout vec4 ambient)\n"
  78. "{\n"
  79. " ambient = vec4(1.0, 1.0, 1.0, 1.0);\n"
  80. "}\n");
  81. blue_pixel.SetMainCode(
  82. out_pixel.tostring() + " = " + pass_color.tostring() + ";\n" +
  83. out_pixel.tostring() + ".b = 1.0;\n"
  84. "SetAmbient(ambient);\n" +
  85. out_pixel.tostring() + " *= ambient;\n"
  86. );
  87. builder << ShaderProgram::Vertex
  88. << nothing_vertex
  89. << ShaderProgram::Pixel
  90. << red_pixel
  91. << green_pixel
  92. << blue_pixel;
  93. code = builder.Build();
  94. file.Write(code);
  95. //code = file.ReadString();
  96. file.Close();
  97. std::shared_ptr<Shader> shader = Shader::Create(builder.GetName(), code);
  98. shader = nullptr;
  99. Ticker::Shutdown();
  100. }
  101. };
  102. int main(int argc, char **argv)
  103. {
  104. sys::init(argc, argv);
  105. Application app("Tutorial 13: Shader Builder", ivec2(1280, 720), 60.0f);
  106. new ShaderBuilderDemo();
  107. app.Run();
  108. return EXIT_SUCCESS;
  109. }