Renderer.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package me.hammerle.snuviengine.api;
  2. import java.nio.FloatBuffer;
  3. import org.lwjgl.BufferUtils;
  4. import static org.lwjgl.opengl.GL11.*;
  5. import static org.lwjgl.opengl.GL14.*;
  6. import static org.lwjgl.opengl.GL20.*;
  7. public final class Renderer {
  8. private float width;
  9. private float height;
  10. private int scale;
  11. private final FontRenderer fontRenderer = new FontRenderer();
  12. private final ColorRenderer colorRenderer = new ColorRenderer();
  13. private final DirectTextureRenderer textureRenderer = new DirectTextureRenderer();
  14. // uniform stuff
  15. private final int unifViewMatrix;
  16. private final int unifModelMatrix;
  17. private final MatrixStack modelMatrix = new MatrixStack(20);
  18. private final int unifAmbientLight;
  19. private final int[][] unifLight = new int[32][3];
  20. private final UniformBoolean useTexture;
  21. private final UniformBoolean useColor;
  22. private final UniformBoolean useLight;
  23. private final UniformBoolean useMixColor;
  24. private final int unifMixColorLoc;
  25. private final float[] unifMixColor = new float[]{0.0f, 0.0f, 0.0f, 0.0f};
  26. protected Renderer(int program, int width, int height, WindowView windowView) {
  27. glUseProgram(program);
  28. unifViewMatrix = glGetUniformLocation(program, "viewMatrix");
  29. setSize(width, height, windowView);
  30. unifModelMatrix = glGetUniformLocation(program, "modelMatrix");
  31. updateMatrix();
  32. unifAmbientLight = glGetUniformLocation(program, "ambientLight");
  33. setAmbientLight(1.0f, 1.0f, 1.0f);
  34. for(int index = 0; index < unifLight.length; index++) {
  35. unifLight[index][0] = glGetUniformLocation(program, String.format("lights[%d].color", index));
  36. unifLight[index][1] = glGetUniformLocation(program, String.format("lights[%d].pos", index));
  37. unifLight[index][2] = glGetUniformLocation(program, String.format("lights[%d].strength", index));
  38. setLightColor(index, 0.0f, 0.0f, 0.0f);
  39. setLightLocation(index, 0.0f, 0.0f);
  40. setLightStrength(index, 0.0f);
  41. }
  42. useTexture = new UniformBoolean(program, "useTexture", false);
  43. useColor = new UniformBoolean(program, "useColor", false);
  44. useLight = new UniformBoolean(program, "useLight", false);
  45. useMixColor = new UniformBoolean(program, "useMixColor", false);
  46. unifMixColorLoc = glGetUniformLocation(program, "mixColor");
  47. setMixColor(0.0f, 0.0f, 0.0f, 0.0f);
  48. }
  49. public FontRenderer getFontRenderer() {
  50. return fontRenderer;
  51. }
  52. public ColorRenderer getColorRenderer() {
  53. return colorRenderer;
  54. }
  55. public DirectTextureRenderer getTextureRenderer() {
  56. return textureRenderer;
  57. }
  58. private void updateViewMatrix() {
  59. FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
  60. addToBuffer(buffer, 2.0f / width, 0.0f, 0.0f, 0.0f);
  61. addToBuffer(buffer, 0.0f, -2.0f / height, 0.0f, 0.0f);
  62. addToBuffer(buffer, 0.0f, 0.0f, -1.0f / height, 0.0f);
  63. addToBuffer(buffer, -1.0f, 1.0f, 0.5f, 1.0f);
  64. buffer.flip();
  65. glUniformMatrix4fv(unifViewMatrix, false, buffer);
  66. }
  67. private void addToBuffer(FloatBuffer buffer, float a, float b, float c, float d) {
  68. buffer.put(a);
  69. buffer.put(b);
  70. buffer.put(c);
  71. buffer.put(d);
  72. }
  73. protected void setSize(int width, int height, WindowView windowView) {
  74. scale = windowView.getScale(width, height);
  75. fontRenderer.setScale(windowView.getFontScale(width, height));
  76. this.width = width / scale;
  77. this.height = height / scale;
  78. updateViewMatrix();
  79. }
  80. public int getViewScale() {
  81. return scale;
  82. }
  83. public float getViewWidth() {
  84. return width;
  85. }
  86. public float getViewHeight() {
  87. return height;
  88. }
  89. public void updateMatrix() {
  90. glUniformMatrix4fv(unifModelMatrix, false, modelMatrix.getData());
  91. }
  92. public void pushMatrix() {
  93. modelMatrix.push();
  94. }
  95. public void popMatrix() {
  96. modelMatrix.pop();
  97. }
  98. public void translate(float tx, float ty) {
  99. modelMatrix.translate(tx, ty);
  100. }
  101. public void translateTo(float tx, float ty) {
  102. modelMatrix.translateTo(tx, ty);
  103. }
  104. public void scale(float sx, float sy) {
  105. modelMatrix.scale(sx, sy);
  106. }
  107. public void rotate(float angle) {
  108. modelMatrix.rotate(angle);
  109. }
  110. public void setAmbientLight(float r, float g, float b) {
  111. glUniform3f(unifAmbientLight, r, g, b);
  112. }
  113. private boolean isValidLightIndex(int index) {
  114. return index >= 0 && index < unifLight.length;
  115. }
  116. public void setLightColor(int index, float r, float g, float b) {
  117. if(isValidLightIndex(index)) {
  118. glUniform3f(unifLight[index][0], r, g, b);
  119. }
  120. }
  121. public void setLightLocation(int index, float x, float y) {
  122. if(isValidLightIndex(index)) {
  123. glUniform2f(unifLight[index][1], x, y);
  124. }
  125. }
  126. public void setLightStrength(int index, float strength) {
  127. if(isValidLightIndex(index)) {
  128. glUniform1f(unifLight[index][2], strength);
  129. }
  130. }
  131. public void setTextureEnabled(boolean use) {
  132. useTexture.set(use);
  133. }
  134. public void setColorEnabled(boolean use) {
  135. useColor.set(use);
  136. }
  137. public void setMixColorEnabled(boolean use) {
  138. useMixColor.set(use);
  139. }
  140. public void setMixColor(float r, float g, float b, float a) {
  141. unifMixColor[0] = r;
  142. unifMixColor[1] = g;
  143. unifMixColor[2] = b;
  144. unifMixColor[3] = a;
  145. glUniform4fv(unifMixColorLoc, unifMixColor);
  146. }
  147. public void setLightEnabled(boolean use) {
  148. useLight.set(use);
  149. }
  150. public void enableDepthTest() {
  151. glEnable(GL_DEPTH_TEST);
  152. glDepthFunc(GL_LEQUAL);
  153. }
  154. public void disableDepthTest() {
  155. glDisable(GL_DEPTH_TEST);
  156. }
  157. public void enableBlending() {
  158. glEnable(GL_BLEND);
  159. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  160. glBlendEquation(GL_FUNC_ADD);
  161. }
  162. public void disableBlending() {
  163. glDisable(GL_BLEND);
  164. }
  165. public float round(float f) {
  166. return ((int) (f * scale)) / (float) scale;
  167. }
  168. }