1 /**
2 Copyright: Copyright (c) 2013-2016 Andrey Penechko.
3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 
7 module anchovy.oglrenderer;
8 
9 import derelict.opengl3.gl3;
10 
11 
12 import dlib.math.vector;
13 import anchovy.irenderer;
14 import anchovy.iwindow;
15 import anchovy.shaderprogram;
16 import anchovy.texture;
17 
18 
19 class Vbo
20 {
21 	this(uint bufferUsage, uint target = GL_ARRAY_BUFFER)
22 	{
23 		this.bufferUsage = bufferUsage;
24 		this.target = target;
25 		glGenBuffers(1, &handle);
26 	}
27 
28 	void close()
29 	{
30 		glDeleteBuffers(1, &handle);
31 	}
32 
33 	ref const(void[]) data(void[] newData) @property
34 	{
35 		_data = newData;
36 		glBindBuffer(target, handle);
37 		glBufferData(target, _data.length, _data.ptr, bufferUsage);
38 		glBindBuffer(target, 0);
39 		return _data;
40 	}
41 
42 	ref const(void[]) data() @property
43 	{
44 		return _data;
45 	}
46 
47 	void bind()
48 	{
49 		glBindBuffer(target, handle);
50 	}
51 
52 	void unbind()
53 	{
54 		glBindBuffer(target, 0);
55 	}
56 
57 	uint handle;
58 	uint bufferUsage;
59 	uint target;
60 	void[] _data;
61 }
62 
63 class Vao
64 {
65 	this()
66 	{
67 		glGenVertexArrays(1, &handle);
68 	}
69 	void close()
70 	{
71 		glDeleteVertexArrays(1, &handle);
72 	}
73 	void bind()
74 	{
75 		glBindVertexArray(handle);
76 	}
77 
78 	static void unbind()
79 	{
80 		glBindVertexArray(0);
81 	}
82 	uint handle;
83 }
84 
85 class OglRenderer : IRenderer
86 {
87 private:
88 	ShaderProgram[] shaders;
89 
90 	IWindow	window;
91 
92 public:
93 	this(IWindow window)
94 	{
95 		this.window = window;
96 	}
97 
98 	override void enableAlphaBlending()
99 	{
100 		glEnable(GL_BLEND);
101 		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
102 	}
103 
104 	override void disableAlphaBlending()
105 	{
106 		glDisable(GL_BLEND);
107 	}
108 
109 	override void setClearColor(ubyte r, ubyte g, ubyte b, ubyte a = 255)
110 	{
111 		glClearColor(cast(float)r/255, cast(float)g/255, cast(float)b/255, cast(float)a/255);
112 	}
113 
114 	override Texture createTexture(string filename)
115 	{
116 		import dlib.image.io.io : loadImage;
117 		import dlib.image.image : SuperImage, ImageRGBA8, convert;
118 		SuperImage image = loadImage(filename);
119 		SuperImage convertedImage = convert!ImageRGBA8(image);
120 		Texture tex = new Texture(convertedImage, TextureTarget.target2d, TextureFormat.rgba);
121 		return tex;
122 	}
123 
124 	override ShaderProgram createShaderProgram(string vertexSource, string fragmentSource)
125 	{
126 		ShaderProgram newProgram = new ShaderProgram(vertexSource, fragmentSource);
127 		if (!newProgram.compile) throw new Exception(newProgram.errorLog);
128 		shaders ~= newProgram;
129 		return newProgram;
130 	}
131 
132 	override uvec2 framebufferSize() @property
133 	{
134 		return window.framebufferSize();
135 	}
136 
137 	override void flush()
138 	{
139 		window.swapBuffers;
140 	}
141 
142 	override void close()
143 	{
144 		foreach(shader; shaders)
145 		{
146 			shader.close;
147 		}
148 		shaders = null;
149 	}
150 }