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.

126 lines
4.2 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. String vertex_out = Shader::GetProgramOutVariableLocal(ShaderProgram::Vertex);
  40. String pixel_out = Shader::GetProgramOutVariableLocal(ShaderProgram::Pixel);
  41. String in_position = Shader::GetVariablePrefix(ShaderVariable::Attribute) + "position";
  42. String in_color = Shader::GetVariablePrefix(ShaderVariable::Attribute) + "color";
  43. String pass_color = Shader::GetVariablePrefix(ShaderVariable::Varying) + "color";
  44. nothing_vertex.Add(ShaderVariable::Attribute, "vec3", "position");
  45. nothing_vertex.Add(ShaderVariable::Attribute, "vec4", "color");
  46. nothing_vertex.Add(ShaderVariable::Varying, "vec4", "color");
  47. nothing_vertex.Add(ShaderVariable::InOut, "vec4", vertex_out);
  48. nothing_vertex.SetMainCode(
  49. String(" ") + pass_color + " = " + in_color + ";\n" +
  50. String(" ") + vertex_out + " = vec4(" + in_position + ", 0.f);\n"
  51. );
  52. red_pixel.Add(ShaderVariable::Varying, "vec4", "color");
  53. red_pixel.Add(ShaderVariable::InOut, "vec4", pixel_out);
  54. red_pixel.Add(ShaderVariable::InOut, "vec4", "ambient");
  55. red_pixel.SetMainCode(
  56. String(" ") + pixel_out + " = " + pass_color + ";\n" +
  57. String(" ") + pixel_out + ".r = 1.0;\n" +
  58. String(" ") + "ambient = vec4(1.0);\n"
  59. );
  60. green_pixel.Add(ShaderVariable::Varying, "vec4", "color");
  61. green_pixel.Add(ShaderVariable::InOut, "vec4", pixel_out);
  62. green_pixel.Add(ShaderVariable::InOut, "vec4", "ambient");
  63. green_pixel.SetMainCode(
  64. String(" ") + pixel_out + " = " + pass_color + ";\n" +
  65. String(" ") + pixel_out + ".g = 1.0;\n" +
  66. String(" ") + "ambient.r = 0.0;\n"
  67. );
  68. blue_pixel.Add(ShaderVariable::Varying, "vec4", "color");
  69. blue_pixel.Add(ShaderVariable::InOut, "vec4", pixel_out);
  70. blue_pixel.Add(ShaderVariable::InOut, "vec4", "ambient");
  71. blue_pixel.SetCustomCode(
  72. String("void SetAmbient(inout vec4 ambient)\n{\n ambient = vec4(1.0, 1.0, 1.0, 1.0);\n}"));
  73. blue_pixel.SetMainCode(
  74. String(" ") + pixel_out + " = " + pass_color + ";\n" +
  75. String(" ") + pixel_out + ".b = 1.0;\n" +
  76. String(" ") + "SetAmbient(ambient);\n" +
  77. String(" ") + pixel_out + " *= ambient;\n"
  78. );
  79. builder << ShaderProgram::Vertex
  80. << &nothing_vertex
  81. << ShaderProgram::Pixel
  82. << &red_pixel
  83. << &green_pixel
  84. << &blue_pixel;
  85. builder.Build(code);
  86. file.WriteString(code);
  87. //code = file.ReadString();
  88. file.Close();
  89. Shader* shader = Shader::Create(builder.GetName(), code);
  90. Shader::Destroy(shader);
  91. Ticker::Shutdown();
  92. }
  93. };
  94. int main(int argc, char **argv)
  95. {
  96. System::Init(argc, argv);
  97. Application app("Tutorial 13: Shader Builder", ivec2(1280, 720), 60.0f);
  98. new ShaderBuilderDemo();
  99. app.Run();
  100. return EXIT_SUCCESS;
  101. }