Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. /*
  2. *
  3. * Project Zero by EnZaCode
  4. *
  5. *
  6. */
  7.  
  8. package de.flori2007.eztools.shader;
  9.  
  10. import net.minecraft.client.Minecraft;
  11. import net.minecraft.client.gui.ScaledResolution;
  12. import org.jetbrains.annotations.NotNull;
  13.  
  14. import static org.lwjgl.opengl.GL11.*;
  15. import static org.lwjgl.opengl.GL20.*;
  16.  
  17.  
  18. public class EZ_ShaderAPI {
  19.  
  20. private static final String VERTEX_SHADER = "#version 130\n" +
  21. "\n" +
  22. "void main() {\n" +
  23. " gl_TexCoord[0] = gl_MultiTexCoord0;\n" +
  24. " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" +
  25. "}";
  26. private Minecraft mc = Minecraft.getMinecraft();
  27. private int program;
  28. private long startTime;
  29.  
  30. public EZ_ShaderAPI(@NotNull String fragment) {
  31. program = glCreateProgram();
  32. startTime = System.currentTimeMillis();
  33. initShader(fragment);
  34. }
  35.  
  36. private void initShader(@NotNull String frag) {
  37. int vertex = glCreateShader(GL_VERTEX_SHADER), fragment = glCreateShader(GL_FRAGMENT_SHADER);
  38. glShaderSource(vertex, VERTEX_SHADER);
  39. glShaderSource(fragment, frag);
  40. glValidateProgram(program);
  41. glCompileShader(vertex);
  42. glCompileShader(fragment);
  43. glAttachShader(program, vertex);
  44. glAttachShader(program, fragment);
  45. glLinkProgram(program);
  46. }
  47.  
  48. public void renderFirst() {
  49. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  50. glUseProgram(program);
  51. }
  52.  
  53. public void renderSecond() {
  54. glEnable(GL_BLEND);
  55. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  56. ScaledResolution sr = new ScaledResolution(mc);
  57. glBegin(GL_QUADS);
  58. glTexCoord2d(0, 1);
  59. glVertex2d(0, 0);
  60. glTexCoord2d(0, 0);
  61. glVertex2d(0, sr.getScaledHeight());
  62. glTexCoord2d(1, 0);
  63. glVertex2d(sr.getScaledWidth(), sr.getScaledHeight());
  64. glTexCoord2d(1, 1);
  65. glVertex2d(sr.getScaledWidth(), 0);
  66. glEnd();
  67. glUseProgram(0);
  68. }
  69.  
  70. public void bind() {
  71. glUseProgram(program);
  72. }
  73.  
  74. public int getProgram() {
  75. return program;
  76. }
  77.  
  78. @NotNull
  79. public EZ_ShaderAPI uniform1i(@NotNull String loc, int i) {
  80. glUniform1i(glGetUniformLocation(program, loc), i);
  81. return this;
  82. }
  83.  
  84. @NotNull
  85. public EZ_ShaderAPI uniform2i(@NotNull String loc, int i, int i1) {
  86. glUniform2i(glGetUniformLocation(program, loc), i, i1);
  87. return this;
  88. }
  89.  
  90. @NotNull
  91. public EZ_ShaderAPI uniform3i(@NotNull String loc, int i, int i1, int i2) {
  92. glUniform3i(glGetUniformLocation(program, loc), i, i1, i2);
  93. return this;
  94. }
  95.  
  96. @NotNull
  97. public EZ_ShaderAPI uniform4i(@NotNull String loc, int i, int i1, int i2, int i3) {
  98. glUniform4i(glGetUniformLocation(program, loc), i, i1, i2, i3);
  99. return this;
  100. }
  101.  
  102. @NotNull
  103. public EZ_ShaderAPI uniform1f(@NotNull String loc, float f) {
  104. glUniform1f(glGetUniformLocation(program, loc), f);
  105. return this;
  106. }
  107.  
  108. @NotNull
  109. public EZ_ShaderAPI uniform2f(@NotNull String loc, float f, float f1) {
  110. glUniform2f(glGetUniformLocation(program, loc), f, f1);
  111. return this;
  112. }
  113.  
  114. @NotNull
  115. public EZ_ShaderAPI uniform3f(@NotNull String loc, float f, float f1, float f2) {
  116. glUniform3f(glGetUniformLocation(program, loc), f, f1, f2);
  117. return this;
  118. }
  119.  
  120. @NotNull
  121. public EZ_ShaderAPI uniform4f(@NotNull String loc, float f, float f1, float f2, float f3) {
  122. glUniform4f(glGetUniformLocation(program, loc), f, f1, f2, f3);
  123. return this;
  124. }
  125.  
  126. @NotNull
  127. public EZ_ShaderAPI uniform1b(@NotNull String loc, boolean b) {
  128. glUniform1i(glGetUniformLocation(program, loc), b ? 1 : 0);
  129. return this;
  130. }
  131.  
  132. public void addDefaultUniforms() {
  133. glUniform2f(glGetUniformLocation(program, "resolution"), mc.displayWidth, mc.displayHeight);
  134. float time = (System.currentTimeMillis() - this.startTime) / 1000f;
  135. glUniform1f(glGetUniformLocation(program, "time"), time);
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement