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.
 
 
 

131 lines
3.8 KiB

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