public MagicBackground() { shader = new Shader(); float v = Shader.Version; shader.LoadVertexFile(Path.Combine("Effects", v < 3.3f ? "v12" : "v33", "magic_background.vert")); shader.LoadFragmentFile(Path.Combine("Effects", v < 3.3f ? "v12" : "v33", "magic_background.frag")); shader.Link(); buffer = new VertexBuffer(4); buffer.DeclareNextAttribute("vposition", 2); buffer.DeclareNextAttribute("vtexcoord", 2); buffer.DeclareNextAttribute("vcolor", 4); buffer.Attach(shader); buffer.AddVertex(-1, -1, 0, 0, 1f, 1f, 1f, 1f); buffer.AddVertex(1, -1, 1, 0, 1f, 1f, 1f, 1f); buffer.AddVertex(1, 1, 1, 1, 1, 1, 1, 1); buffer.AddVertex(-1, 1, 0, 1, 1, 1, 1, 1); buffer.AddIndices(0, 1, 2, 0, 2, 3); buffer.UpdateBuffer(); }
public void Init() { blood_blend_shader = new Shader(); float v = Shader.Version; blood_blend_shader.LoadVertexFile(Path.Combine("Effects", v < 3.3f ? "v12" : "v33", "blood_blend.vert")); blood_blend_shader.LoadFragmentFile(Path.Combine("Effects", v < 3.3f ? "v12" : "v33", "blood_blend.frag")); blood_blend_shader.Link(); blend_buffer = new VertexBuffer(4); blend_buffer.DeclareNextAttribute("vposition", 2); blend_buffer.DeclareNextAttribute("vtexcoord", 2); blend_buffer.DeclareNextAttribute("vcolor", 4); blend_buffer.Attach(blood_blend_shader); blend_buffer.AddVertex(-1, -1, 0, 0, 1f, 1f, 1f, 1f); blend_buffer.AddVertex(1, -1, 1, 0, 1f, 1f, 1f, 1f); blend_buffer.AddVertex(1, 1, 1, 1, 1, 1, 1, 1); blend_buffer.AddVertex(-1, 1, 0, 1, 1, 1, 1, 1); blend_buffer.AddIndices(0, 1, 2, 0, 2, 3); blend_buffer.UpdateBuffer(); }
public void Attach(Shader shader) { if (!Bind()) return; GL.BindBuffer(BufferTarget.ArrayBuffer, VertexDataBufferObject); shader.Use(); int off = 0; for (int i = 0; i < declarations.Count; i++) { int location = GL.GetAttribLocation(shader.Handle, declarations[i].name); if (location != -1) { GL.VertexAttribPointer(location, declarations[i].elements, VertexAttribPointerType.Float, false, stride * sizeof(float), off); GL.EnableVertexAttribArray(location); } off += declarations[i].elements * sizeof(float); } GL.BindBuffer(BufferTarget.ElementArrayBuffer, IndexDataBufferObject); }
private static void LoadMaterials(XmlNode main) { XmlNode material = main.SelectSingleNode("Materials"); XmlNodeList materials = material.SelectNodes("Material"); string basePath = material.Attributes["basePath"].Value; foreach (XmlNode item in materials) { string name = item.Attributes["gameName"].Value; string vshader = item.Attributes["vshader"].Value; string fshader = item.Attributes["fshader"].Value; XmlNodeList @params = item.SelectNodes("Param"); Shader shader = new Shader(); shader.LoadVertexFile(basePath + "//" + vshader); shader.LoadFragmentFile(basePath + "//" + fshader); shader.Link(); Material mat = new Material(shader); foreach (XmlNode param in @params) { string paramName = param.Attributes["name"].Value; string paramType = param.Attributes["type"].Value; string paramValue = param.Attributes["value"].Value; mat.SetParameter(paramName, paramType, paramValue); } _materials[name] = mat; } }
public SpriteBatch() { _batchItemList = new List<BatchItem>(256); _freeBatchItemQueue = new Queue<BatchItem>(256); vbuffer = new VertexBuffer(1024); defaultShader = new Shader(); if (Shader.Version < 3.3f) { defaultShader.LoadVertexSource(@"#version 120 uniform mat4 projection, view; attribute vec2 vposition; attribute vec4 vcolor; attribute vec2 vtexcoord; varying vec4 fcolor; varying vec2 ftexcoord; void main(void) { fcolor = vcolor; ftexcoord = vtexcoord; gl_Position = projection * view * vec4(vposition, 0, 1); }"); defaultShader.LoadFragmentSource(@"#version 120 uniform sampler2D colorTexture; varying vec4 fcolor; varying vec2 ftexcoord; void main(void) { gl_FragColor = texture2D(colorTexture, ftexcoord) * fcolor; }"); } else { defaultShader.LoadVertexSource(@"#version 330 core uniform mat4 projection, view; in vec2 vposition; in vec4 vcolor; in vec2 vtexcoord; out vec4 fcolor; out vec2 ftexcoord; void main(void) { fcolor = vcolor; ftexcoord = vtexcoord; gl_Position = projection * view * vec4(vposition, 0, 1); }"); defaultShader.LoadFragmentSource(@"#version 330 core uniform sampler2D colorTexture; in vec4 fcolor; in vec2 ftexcoord; out vec4 color; void main(void) { color = texture(colorTexture, ftexcoord) * fcolor; }"); } defaultShader.Link(); BindShader(this.defaultShader, "vposition", "vcolor", "vtexcoord", "projection", "view"); int[] p = new int[4]; GL.GetInteger(GetPName.Viewport, p); proj = Matrix4.CreateOrthographicOffCenter(p[0], p[2], p[3], p[1], 1, -1); GL.GenTextures(1, out pixelTex); GL.BindTexture(TextureTarget.Texture2D, pixelTex); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 0); float[] pix = new float[] { 1.0f, 1.0f, 1.0f, 1.0f }; GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, 1, 1, 0, PixelFormat.Bgra, PixelType.Float, pix); }
public void End() { int pr; GL.GetInteger(GetPName.CurrentProgram,out pr); if(current == null) { current = defaultShader; current.Use(); } if (current.Program != pr) current.Use(); current.SetUniform(proj_uniform_loc, ref proj); current.SetUniform(view_uniform_loc, ref trans); // nothing to do if (_batchItemList.Count == 0) return; vbuffer.Bind(); vbuffer.UpdateVertexBuffer(); vbuffer.UpdateIndexBuffer(); int texID = -1; BeginMode mode = BeginMode.Triangles; float lineWidth; float pointSize; GL.GetFloat(GetPName.LineWidth, out lineWidth); GL.GetFloat(GetPName.PointSize, out pointSize); int offset = 0, count = 0; foreach (BatchItem item in _batchItemList) { if (item.texture != texID || item.mode != mode || lineWidth != item.lineWidth || pointSize != item.pointSize) { FlushBuffer(mode, offset, count); offset += count; count = 0; texID = item.texture; mode = item.mode; lineWidth = item.lineWidth; pointSize = item.pointSize; GL.BindTexture(TextureTarget.Texture2D, texID); GL.LineWidth(item.lineWidth); GL.PointSize(item.pointSize); } count += item.count; _freeBatchItemQueue.Enqueue(item); } // flush the remaining vertexArray data FlushBuffer(mode, offset, count); vbuffer.ClearBuffer(); _batchItemList.Clear(); }
public void BindShader(Shader shader, string positionAttrib, string colorAttrib, string texcoordAttrib, string projectionUniform, string viewUniform) { vbuffer.ClearAttributeDeclarations(); vbuffer.DeclareNextAttribute(positionAttrib, 2); vbuffer.DeclareNextAttribute(colorAttrib, 4); vbuffer.DeclareNextAttribute(texcoordAttrib, 2); vbuffer.Attach(shader); proj_uniform_loc = GL.GetUniformLocation(shader.Program, projectionUniform); view_uniform_loc = GL.GetUniformLocation(shader.Program, viewUniform); shader.Use(); current = shader; }
internal Material(Shader shader) { Shader = shader; }
public void BindShader(Shader shader, string positionAttrib, string colorAttrib, string texcoordAttrib) { vbuffer.ClearAttributeDeclarations(); vbuffer.DeclareNextAttribute(positionAttrib, 2); vbuffer.DeclareNextAttribute(colorAttrib, 4); vbuffer.DeclareNextAttribute(texcoordAttrib, 2); vbuffer.Attach(shader); shader.Use(); }
public void ResetShader(Shader shader = null) { BindShader(shader == null ? defaultMaterial.Shader : shader, "vposition", "vcolor", "vtexcoord"); }