1 module Engine.Renderer;
2 
3 import Engine.Batch;
4 import derelict.opengl3.gl;
5 import Engine.Core;
6 
7 alias void function(Batch batch) Renderer;
8 
9 Renderer defaultRenderer = &_defaultRenderer;
10 
11 private static const final void _defaultRenderer(Batch batch) {
12 	auto shader = batch.material.shader;
13 	shader.Use();
14 
15 	auto vertexAttrib = shader.Attributes["vertex"];
16 	auto uvAttrib = shader.Attributes["uv"];
17 	auto colorAttrib = shader.Attributes["color"];
18 	auto modelsAttrib = shader.Attributes["models"];
19 
20 	glBindBuffer(GL_ARRAY_BUFFER, batch.vertex);
21 	glEnableVertexAttribArray(vertexAttrib.location);
22 	glVertexAttribPointer(
23 						  vertexAttrib.location,    // attribute    
24 						  3,                  // size
25 						  GL_FLOAT,           // type
26 						  GL_FALSE,           // normalized?
27 						  0,                  // stride
28 						  null                // array buffer offset
29 							  );	
30 	glBindBuffer(GL_ARRAY_BUFFER, batch.uv);
31 	glEnableVertexAttribArray(uvAttrib.location);
32 	glVertexAttribPointer(
33 						  uvAttrib.location,       // attribute 
34 						  2,                  // size
35 						  GL_FLOAT,           // type
36 						  GL_FALSE,           // normalized?
37 						  0,                  // stride
38 						  null                // array buffer offset
39 							  );	
40 	glBindBuffer(GL_ARRAY_BUFFER, batch.color);
41 	glEnableVertexAttribArray(colorAttrib.location);
42 	glVertexAttribPointer(
43 						  colorAttrib.location,        // attribute 
44 						  4,                  // size
45 						  GL_FLOAT,           // type
46 						  GL_FALSE,           // normalized?
47 						  0,                  // stride
48 						  null                // array buffer offset
49 							  );	
50 
51 	glBindBuffer(GL_ARRAY_BUFFER, batch.matrix);
52 	glEnableVertexAttribArray(modelsAttrib.location);
53 	glVertexAttribPointer(
54 						  modelsAttrib.location,        // attribute 
55 						  3,                  // size
56 						  GL_FLOAT,           // type
57 						  GL_FALSE,           // normalized?
58 						  3*3*4,                  // stride
59 						  null                // array buffer offset
60 							  );	
61 	glEnableVertexAttribArray(modelsAttrib.location+1);
62 	glVertexAttribPointer(
63 						  modelsAttrib.location+1,        // attribute 
64 						  3,                  // size
65 						  GL_FLOAT,           // type
66 						  GL_FALSE,           // normalized?
67 						  3*3*4,                 // stride
68 						  cast(void*)(3*4)                // array buffer offset
69 							  );
70 	glEnableVertexAttribArray(modelsAttrib.location+2);
71 	glVertexAttribPointer(
72 						  modelsAttrib.location+2,        // attribute
73 						  3,                  // size
74 						  GL_FLOAT,           // type
75 						  GL_FALSE,           // normalized?
76 						  3*3*4,                  // stride
77 						  cast(void*)(6*4)               // array buffer offset
78 							  );
79 
80 
81 
82 	batch.material.texture.Bind();
83 	glActiveTexture(GL_TEXTURE0);
84 	shader.Attributes["texture"].Set(0);
85 	shader.Attributes["projection"].Set(Core.camera.Projection());
86 	shader.Attributes["view"].Set(Core.camera.View());
87 	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, batch.index);
88 	glDrawElements(GL_TRIANGLES, batch.totalIndecies, GL_UNSIGNED_INT, null);
89 }