Renderer.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (c) 2009-2012 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package com.jme3.renderer;
  33. import com.jme3.light.LightList;
  34. import com.jme3.material.RenderState;
  35. import com.jme3.math.ColorRGBA;
  36. import com.jme3.math.Matrix4f;
  37. import com.jme3.scene.Mesh;
  38. import com.jme3.scene.VertexBuffer;
  39. import com.jme3.shader.Shader;
  40. import com.jme3.shader.Shader.ShaderSource;
  41. import com.jme3.texture.FrameBuffer;
  42. import com.jme3.texture.Image;
  43. import com.jme3.texture.Texture;
  44. import com.jme3.util.NativeObject;
  45. import java.nio.ByteBuffer;
  46. import java.util.EnumSet;
  47. /**
  48. * The <code>Renderer</code> is responsible for taking rendering commands and
  49. * executing them on the underlying video hardware.
  50. *
  51. * @author Kirill Vainer
  52. */
  53. public interface Renderer {
  54. /**
  55. * Get the capabilities of the renderer.
  56. * @return The capabilities of the renderer.
  57. */
  58. public EnumSet<Caps> getCaps();
  59. /**
  60. * The statistics allow tracking of how data
  61. * per frame, such as number of objects rendered, number of triangles, etc.
  62. * These are updated when the Renderer's methods are used, make sure
  63. * to call {@link Statistics#clearFrame() } at the appropriate time
  64. * to get accurate info per frame.
  65. */
  66. public Statistics getStatistics();
  67. /**
  68. * Invalidates the current rendering state. Should be called after
  69. * the GL state was changed manually or through an external library.
  70. */
  71. public void invalidateState();
  72. /**
  73. * Clears certain channels of the currently bound framebuffer.
  74. *
  75. * @param color True if to clear colors (RGBA)
  76. * @param depth True if to clear depth/z
  77. * @param stencil True if to clear stencil buffer (if available, otherwise
  78. * ignored)
  79. */
  80. public void clearBuffers(boolean color, boolean depth, boolean stencil);
  81. /**
  82. * Sets the background (aka clear) color.
  83. *
  84. * @param color The background color to set
  85. */
  86. public void setBackgroundColor(ColorRGBA color);
  87. /**
  88. * Applies the given {@link RenderState}, making the necessary
  89. * GL calls so that the state is applied.
  90. */
  91. public void applyRenderState(RenderState state);
  92. /**
  93. * Set the range of the depth values for objects. All rendered
  94. * objects will have their depth clamped to this range.
  95. *
  96. * @param start The range start
  97. * @param end The range end
  98. */
  99. public void setDepthRange(float start, float end);
  100. /**
  101. * Called when a new frame has been rendered.
  102. */
  103. public void onFrame();
  104. /**
  105. * Set the world matrix to use. Does nothing if the Renderer is
  106. * shader based.
  107. *
  108. * @param worldMatrix World matrix to use.
  109. */
  110. public void setWorldMatrix(Matrix4f worldMatrix);
  111. /**
  112. * Sets the view and projection matrices to use. Does nothing if the Renderer
  113. * is shader based.
  114. *
  115. * @param viewMatrix The view matrix to use.
  116. * @param projMatrix The projection matrix to use.
  117. */
  118. public void setViewProjectionMatrices(Matrix4f viewMatrix, Matrix4f projMatrix);
  119. /**
  120. * Set the viewport location and resolution on the screen.
  121. *
  122. * @param x The x coordinate of the viewport
  123. * @param y The y coordinate of the viewport
  124. * @param width Width of the viewport
  125. * @param height Height of the viewport
  126. */
  127. public void setViewPort(int x, int y, int width, int height);
  128. /**
  129. * Specifies a clipping rectangle.
  130. * For all future rendering commands, no pixels will be allowed
  131. * to be rendered outside of the clip rectangle.
  132. *
  133. * @param x The x coordinate of the clip rect
  134. * @param y The y coordinate of the clip rect
  135. * @param width Width of the clip rect
  136. * @param height Height of the clip rect
  137. */
  138. public void setClipRect(int x, int y, int width, int height);
  139. /**
  140. * Clears the clipping rectangle set with
  141. * {@link #setClipRect(int, int, int, int) }.
  142. */
  143. public void clearClipRect();
  144. /**
  145. * Set lighting state.
  146. * Does nothing if the renderer is shader based.
  147. * The lights should be provided in world space.
  148. * Specify <code>null</code> to disable lighting.
  149. *
  150. * @param lights The light list to set.
  151. */
  152. public void setLighting(LightList lights);
  153. /**
  154. * Sets the shader to use for rendering.
  155. * If the shader has not been uploaded yet, it is compiled
  156. * and linked. If it has been uploaded, then the
  157. * uniform data is updated and the shader is set.
  158. *
  159. * @param shader The shader to use for rendering.
  160. */
  161. public void setShader(Shader shader);
  162. /**
  163. * Deletes a shader. This method also deletes
  164. * the attached shader sources.
  165. *
  166. * @param shader Shader to delete.
  167. */
  168. public void deleteShader(Shader shader);
  169. /**
  170. * Deletes the provided shader source.
  171. *
  172. * @param source The ShaderSource to delete.
  173. */
  174. public void deleteShaderSource(ShaderSource source);
  175. /**
  176. * Copies contents from src to dst, scaling if necessary.
  177. */
  178. public void copyFrameBuffer(FrameBuffer src, FrameBuffer dst);
  179. /**
  180. * Copies contents from src to dst, scaling if necessary.
  181. * set copyDepth to false to only copy the color buffers.
  182. */
  183. public void copyFrameBuffer(FrameBuffer src, FrameBuffer dst, boolean copyDepth);
  184. /**
  185. * Sets the framebuffer that will be drawn to.
  186. */
  187. public void setFrameBuffer(FrameBuffer fb);
  188. /**
  189. * Set the framebuffer that will be set instead of the main framebuffer
  190. * when a call to setFrameBuffer(null) is made.
  191. *
  192. * @param fb
  193. */
  194. public void setMainFrameBufferOverride(FrameBuffer fb);
  195. /**
  196. * Reads the pixels currently stored in the specified framebuffer
  197. * into the given ByteBuffer object.
  198. * Only color pixels are transferred, the format is BGRA with 8 bits
  199. * per component. The given byte buffer should have at least
  200. * fb.getWidth() * fb.getHeight() * 4 bytes remaining.
  201. *
  202. * @param fb The framebuffer to read from
  203. * @param byteBuf The bytebuffer to transfer color data to
  204. */
  205. public void readFrameBuffer(FrameBuffer fb, ByteBuffer byteBuf);
  206. /**
  207. * Deletes a framebuffer and all attached renderbuffers
  208. */
  209. public void deleteFrameBuffer(FrameBuffer fb);
  210. /**
  211. * Sets the texture to use for the given texture unit.
  212. */
  213. public void setTexture(int unit, Texture tex);
  214. /**
  215. * Deletes a texture from the GPU.
  216. */
  217. public void deleteImage(Image image);
  218. /**
  219. * Uploads a vertex buffer to the GPU.
  220. *
  221. * @param vb The vertex buffer to upload
  222. */
  223. public void updateBufferData(VertexBuffer vb);
  224. /**
  225. * Deletes a vertex buffer from the GPU.
  226. * @param vb The vertex buffer to delete
  227. */
  228. public void deleteBuffer(VertexBuffer vb);
  229. /**
  230. * Renders <code>count</code> meshes, with the geometry data supplied.
  231. * The shader which is currently set with <code>setShader</code> is
  232. * responsible for transforming the input verticies into clip space
  233. * and shading it based on the given vertex attributes.
  234. * The int variable gl_InstanceID can be used to access the current
  235. * instance of the mesh being rendered inside the vertex shader.
  236. *
  237. * @param mesh The mesh to render
  238. * @param lod The LOD level to use, see {@link Mesh#setLodLevels(com.jme3.scene.VertexBuffer[]) }.
  239. * @param count Number of mesh instances to render
  240. */
  241. public void renderMesh(Mesh mesh, int lod, int count);
  242. /**
  243. * Resets all previously used {@link NativeObject Native Objects} on this Renderer.
  244. * The state of the native objects is reset in such way, that using
  245. * them again will cause the renderer to reupload them.
  246. * Call this method when you know the GL context is going to shutdown.
  247. *
  248. * @see NativeObject#resetObject()
  249. */
  250. public void resetGLObjects();
  251. /**
  252. * Deletes all previously used {@link NativeObject Native Objects} on this Renderer, and
  253. * then resets the native objects.
  254. *
  255. * @see #resetGLObjects()
  256. * @see NativeObject#deleteObject(com.jme3.renderer.Renderer)
  257. */
  258. public void cleanup();
  259. /**
  260. * Sets the alpha to coverage state.
  261. * <p>
  262. * When alpha coverage and multi-sampling is enabled,
  263. * each pixel will contain alpha coverage in all
  264. * of its subsamples, which is then combined when
  265. * other future alpha-blended objects are rendered.
  266. * </p>
  267. * <p>
  268. * Alpha-to-coverage is useful for rendering transparent objects
  269. * without having to worry about sorting them.
  270. * </p>
  271. */
  272. public void setAlphaToCoverage(boolean value);
  273. }