1 module d2d.rendering.irendertarget;
2 
3 import d2d;
4 
5 /// Interface for containers being able to draw elements.
6 interface IRenderTarget
7 {
8 	/// Set active container.
9 	void bind();
10 	/// Resize the container texture to the new width and height.
11 	void resize(int width, int height);
12 	/// Create a container texture in the given resolution.
13 	void create(int width, int height);
14 
15 	/// Clears the container texture by calling `bind()` -> `glClearColor(r, g, b, 1)` -> `glClear(GL_COLOR_BUFFER_BIT)`.
16 	final void clear(float r, float g, float b)
17 	{
18 		bind();
19 		glClearColor(r, g, b, 1);
20 		glClear(GL_COLOR_BUFFER_BIT);
21 	}
22 
23 	/// Draws drawable using optional shader onto this. This will call `drawable.draw(this, shader);`
24 	/// If shader is `null`, `ShaderProgram.default` is gonna be used.
25 	final void draw(IDrawable drawable, ShaderProgram shader = null)
26 	{
27 		bind();
28 		drawable.draw(this, shader);
29 	}
30 
31 	/// Draws raw geometry to the container texture using an optional shader.
32 	/// If shader is null, ShaderProgram.default is gonna be used.
33 	final void draw(Mesh mesh, ShaderProgram shader = null)
34 	{
35 		draw(mesh.renderable, shader);
36 	}
37 
38 	/// Draws raw geometry to the container texture using an optional shader.
39 	/// If shader is null, ShaderProgram.default is gonna be used.
40 	final void draw(RenderableMesh mesh, ShaderProgram shader = null)
41 	{
42 		bind();
43 		if (shader is null)
44 			shader = ShaderProgram.defaultShader;
45 
46 		shader.bind();
47 		shader.set("transform", matrixStack.top);
48 		shader.set("projection", projectionStack.top);
49 
50 		glBindVertexArray(mesh.bufferID);
51 
52 		if (mesh.indexed)
53 			glDrawElements(mesh.primitiveType, mesh.count, mesh.indexType,
54 					cast(void*)(mesh.start * mesh.indexStride));
55 		else
56 			glDrawArrays(mesh.primitiveType, mesh.start, mesh.count);
57 	}
58 
59 	/// Returns the result of the container texture.
60 	@property Texture texture();
61 }