1 module polyplex.core.render.gl;
2 public import polyplex.core.render.gl.batch;
3 public import polyplex.core.render.gl.debug2d;
4 public import polyplex.core.render.gl.renderbuf;
5 import polyplex.core.render;
6 static import win = polyplex.core.window;
7 import polyplex.core.color;
8 import polyplex.utils;
9 import polyplex.math;
10 import polyplex;
11 
12 import bindbc.sdl;
13 import bindbc.opengl;
14 import bindbc.opengl.gl;
15 import std.conv;
16 import std.stdio;
17 
18 public import polyplex.core.render.gl.shader;
19 
20 // TODO: Remove SDL dependency from this.
21 
22 public class GlRenderer : BackendRenderer {
23 
24 	private Rectangle scissorRect;
25 
26 	this(win.Window parent) { super(parent); }
27 
28 	public override void Init() {
29 		// Create the neccesary rendering backend.
30 		Window.CreateContext(GraphicsBackend.OpenGL);
31 		
32 		loadOpenGL();
33 
34 		GlSpriteBatch.InitializeSpritebatch();
35 		GlDebugging2D.PrepDebugging();
36 		
37 		//Default settings for sprite clamping and wrapping
38 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
39 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
40 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
41 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
42 
43 		Logger.Info("OpenGL version: {0}", to!string(glGetString(GL_VERSION)));
44 		glEnable (GL_BLEND);
45 		glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
46 		
47 		// TODO: reimplement this.
48 		//Crash if system has unsupported opengl version.
49 		//if (glver < GLVersion.gl30) throw new Error("Sorry, your graphics card does not support Open GL 3 or above.");
50 		Logger.Info("OpenGL initialized...");
51 	}
52 	
53 	public override @property Rectangle ScissorRectangle() {
54 		return scissorRect;
55 	}
56 
57 	public override @property void ScissorRectangle(Rectangle rect) {
58 		scissorRect = rect;
59 		glScissor(
60 			scissorRect.X, 
61 			(Renderer.Window.ClientBounds.Height-scissorRect.Y)-scissorRect.Height, 
62 			scissorRect.Width, 
63 			scissorRect.Height);
64 	}
65 
66 	public override @property VSyncState VSync() {
67 		return Window.VSync;
68 	}
69 
70 	public override @property void VSync(VSyncState state) {
71 		Window.VSync = state;
72 	}
73 
74 	public override void AdjustViewport() {
75 		glViewport(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height);
76 	}
77 
78 	public override void ClearColor(Color color) {
79 		glClearColor(color.Rf, color.Gf, color.Bf, color.Af);
80 		glClear(GL_COLOR_BUFFER_BIT);
81 		ClearDepth();
82 	}
83 
84 	public override void ClearDepth() {
85 		glClear(GL_DEPTH_BUFFER_BIT);
86 	}
87 
88 	public override void SwapBuffers() {
89 		Window.SwapBuffers();
90 	}
91 
92 	public override Shader CreateShader(ShaderCode code) {
93 		return new GLShader(code);
94 	}
95 }