public Shader(string vertexPath, string fragmentPath) : base() { string[] reps = vertexPath.Split('/', '\\'); this.Name = reps[reps.Length - 1].Split('.')[0]; string shaderSource = LoadSource(vertexPath); int vertexShader = GL.CreateShader(ShaderType.VertexShader); GL.ShaderSource(vertexShader, shaderSource); CompileShader(vertexShader); shaderSource = LoadSource(fragmentPath); int fragmentShader = GL.CreateShader(ShaderType.FragmentShader); GL.ShaderSource(fragmentShader, shaderSource); CompileShader(fragmentShader); this.Handle = GL.CreateProgram(); GL.AttachShader(Handle, vertexShader); GL.AttachShader(Handle, fragmentShader); LinkProgram(Handle); GL.DetachShader(Handle, vertexShader); GL.DetachShader(Handle, fragmentShader); GL.DeleteShader(fragmentShader); GL.DeleteShader(vertexShader); GL.GetProgram(Handle, GetProgramParameterName.ActiveAttributes, out int nAttribs); Attributes = new ShaderAttributeData[nAttribs]; for (int i = 0; i < nAttribs; i++) { GL.GetActiveAttrib(Handle, i, 16, out int ALength, out int ASize, out ActiveAttribType AType, out string AName); this.Attributes[i] = new ShaderAttributeData(AName, GL.GetAttribLocation(Handle, AName), ASize, ALength, AType); } GL.GetProgram(Handle, GetProgramParameterName.ActiveUniforms, out int nUniforms); Uniforms = new ShaderUniformData[nUniforms]; for (int i = 0; i < nUniforms; i++) { string UName = GL.GetActiveUniform(Handle, i, out int USize, out ActiveUniformType UType); Uniforms[i] = new ShaderUniformData(UName, GL.GetUniformLocation(Handle, UName), USize, UType); } SetAttribute("position", AttributeTypes.Vertice); SetAttribute("uv", AttributeTypes.UV); SetAttribute("normal", AttributeTypes.Normal); Cache.Add(this); }
private Shader(string name, string vert, string frag) : base(name) { // define vertex shader int vertexShader = GL.CreateShader(ShaderType.VertexShader); GL.ShaderSource(vertexShader, vert); CompileShader(vertexShader); // define fragment shader int fragmentShader = GL.CreateShader(ShaderType.FragmentShader); GL.ShaderSource(fragmentShader, frag); CompileShader(fragmentShader); // create program this.Handle = GL.CreateProgram(); // link shaders together and compile the program GL.AttachShader(Handle, vertexShader); GL.AttachShader(Handle, fragmentShader); LinkProgram(Handle); // cleaning GL.DetachShader(Handle, vertexShader); GL.DetachShader(Handle, fragmentShader); GL.DeleteShader(fragmentShader); GL.DeleteShader(vertexShader); GL.GetProgram(Handle, GetProgramParameterName.ActiveAttributes, out int nAttribs); Attributes = new ShaderAttributeData[nAttribs]; for (int i = 0; i < nAttribs; i++) { GL.GetActiveAttrib(Handle, i, 16, out int ALength, out int ASize, out ActiveAttribType AType, out string AName); this.Attributes[i] = new ShaderAttributeData(AName, i, ASize, ALength, AType); } GL.GetProgram(Handle, GetProgramParameterName.ActiveUniforms, out int nUniforms); Uniforms = new ShaderUniformData[nUniforms]; for (int i = 0; i < nUniforms; i++) { string UName = GL.GetActiveUniform(Handle, i, out int USize, out ActiveUniformType UType); Uniforms[i] = new ShaderUniformData(UName, GL.GetUniformLocation(Handle, UName), USize, UType); } this.SetAttribute("position", AttributeTypes.Vertice); this.SetAttribute("uv", AttributeTypes.UV); this.SetAttribute("normal", AttributeTypes.Normal); Cache.Add(this); }
internal bool SetAttribute(string name, AttributeTypes type) { ShaderAttributeData data = null; for (int i = 0; i < Attributes.Length; i++) { if (Attributes[i].Name == name) { data = Attributes[i]; } } if (data == null) { return(false); } GL.EnableVertexAttribArray(data.Location); int size; VertexAttribPointerType ptrType; bool normalized; int stride; int offset; switch (type) { case AttributeTypes.Vertice: { size = 3; ptrType = VertexAttribPointerType.Float; normalized = false; stride = Size * sizeof(float); offset = 0; break; } case AttributeTypes.UV: { size = 2; ptrType = VertexAttribPointerType.Float; normalized = false; stride = Size * sizeof(float); offset = VerticeSize * sizeof(float); break; } case AttributeTypes.Normal: { size = 3; ptrType = VertexAttribPointerType.Float; normalized = false; stride = Size * sizeof(float); offset = (VerticeSize + UVSize) * sizeof(float); break; } default: { size = 0; ptrType = VertexAttribPointerType.UnsignedByte; normalized = false; stride = 0; offset = 0; Debug.Log("Something went wrong.."); break; } } GL.VertexAttribPointer(data.Location, size, ptrType, normalized, stride, offset); return(true); }