Renderer.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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) {
  27. glUseProgram(program);
  28. unifViewMatrix = glGetUniformLocation(program, "viewMatrix");
  29. setSize(width, height);
  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. setSize(width, height);
  49. }
  50. public FontRenderer getFontRenderer() {
  51. return fontRenderer;
  52. }
  53. public ColorRenderer getColorRenderer() {
  54. return colorRenderer;
  55. }
  56. public DirectTextureRenderer getTextureRenderer() {
  57. return textureRenderer;
  58. }
  59. private void updateViewMatrix() {
  60. FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
  61. addToBuffer(buffer, 2.0f / width, 0.0f, 0.0f, 0.0f);
  62. addToBuffer(buffer, 0.0f, -2.0f / height, 0.0f, 0.0f);
  63. addToBuffer(buffer, 0.0f, 0.0f, -1.0f / height, 0.0f);
  64. addToBuffer(buffer, -1.0f, 1.0f, 0.5f, 1.0f);
  65. buffer.flip();
  66. glUniformMatrix4fv(unifViewMatrix, false, buffer);
  67. }
  68. private void addToBuffer(FloatBuffer buffer, float a, float b, float c, float d) {
  69. buffer.put(a);
  70. buffer.put(b);
  71. buffer.put(c);
  72. buffer.put(d);
  73. }
  74. protected void setSize(int width, int height) {
  75. scale = 1;
  76. while(width / (scale + 1) >= 400 && height / (scale + 1) >= 300) {
  77. scale++;
  78. }
  79. fontRenderer.setScale(scale);
  80. this.width = width / scale;
  81. this.height = height / scale;
  82. updateViewMatrix();
  83. }
  84. public int getViewScale() {
  85. return scale;
  86. }
  87. public float getViewWidth() {
  88. return width;
  89. }
  90. public float getViewHeight() {
  91. return height;
  92. }
  93. public void updateMatrix() {
  94. glUniformMatrix4fv(unifModelMatrix, false, modelMatrix.getData());
  95. }
  96. public void pushMatrix() {
  97. modelMatrix.push();
  98. }
  99. public void popMatrix() {
  100. modelMatrix.pop();
  101. }
  102. public void translate(float tx, float ty) {
  103. modelMatrix.translate(tx, ty);
  104. }
  105. public void translateTo(float tx, float ty) {
  106. modelMatrix.translateTo(tx, ty);
  107. }
  108. public void scale(float sx, float sy) {
  109. modelMatrix.scale(sx, sy);
  110. }
  111. public void rotate(float angle) {
  112. modelMatrix.rotate(angle);
  113. }
  114. public void setAmbientLight(float r, float g, float b) {
  115. glUniform3f(unifAmbientLight, r, g, b);
  116. }
  117. private boolean isValidLightIndex(int index) {
  118. return index >= 0 && index < unifLight.length;
  119. }
  120. public void setLightColor(int index, float r, float g, float b) {
  121. if(isValidLightIndex(index)) {
  122. glUniform3f(unifLight[index][0], r, g, b);
  123. }
  124. }
  125. public void setLightLocation(int index, float x, float y) {
  126. if(isValidLightIndex(index)) {
  127. glUniform2f(unifLight[index][1], x, y);
  128. }
  129. }
  130. public void setLightStrength(int index, float strength) {
  131. if(isValidLightIndex(index)) {
  132. glUniform1f(unifLight[index][2], strength);
  133. }
  134. }
  135. public void setTextureEnabled(boolean use) {
  136. useTexture.set(use);
  137. }
  138. public void setColorEnabled(boolean use) {
  139. useColor.set(use);
  140. }
  141. public void setMixColorEnabled(boolean use) {
  142. useMixColor.set(use);
  143. }
  144. public void setMixColor(float r, float g, float b, float a) {
  145. unifMixColor[0] = r;
  146. unifMixColor[1] = g;
  147. unifMixColor[2] = b;
  148. unifMixColor[3] = a;
  149. glUniform4fv(unifMixColorLoc, unifMixColor);
  150. }
  151. public void setLightEnabled(boolean use) {
  152. useLight.set(use);
  153. }
  154. public void enableDepthTest() {
  155. glEnable(GL_DEPTH_TEST);
  156. glDepthFunc(GL_LEQUAL);
  157. }
  158. public void disableDepthTest() {
  159. glDisable(GL_DEPTH_TEST);
  160. }
  161. public void enableBlending() {
  162. glEnable(GL_BLEND);
  163. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  164. glBlendEquation(GL_FUNC_ADD);
  165. }
  166. public void disableBlending() {
  167. glDisable(GL_BLEND);
  168. }
  169. public float round(float f) {
  170. return ((int) (f * scale)) / (float) scale;
  171. }
  172. }