public void DataToVBO(Vertex[] vertices, ushort[] indices, VertexMode mode) { if (numOfIndices > 0) { Dispose(); } int size; vertexFlags = mode; numOfIndices = indices.Length; Vertex.Size = BlittableValueType.StrideOf(vertices); GL.GenBuffers(1, out vertexID); GL.BindBuffer(BufferTarget.ArrayBuffer, vertexID); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * Vertex.Size), vertices, usage); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size); if (vertices.Length * BlittableValueType.StrideOf(vertices) != size) { Log.Error("DataToVBO: Vertex data not uploaded correctly"); } GL.GenBuffers(1, out indexID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexID); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(ushort)), indices, usage); GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size); if (indices.Length * sizeof(short) != size) { throw new ApplicationException("DataToVBO: Element data not uploaded correctly"); } Shader = GLSLShader.Load(); if (Shader != null) { if (Settings.UseGL3) { GL.GenVertexArrays(1, out vaoID); GL.BindVertexArray(vaoID); } GL.BindBuffer(BufferTarget.ArrayBuffer, vertexID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexID); Shader.SetAttributes(); if (Settings.UseGL3) { GL.BindVertexArray(0); } } GLExt.CheckGLError("DataToVBO"); }
void CreateShaders(string shaderFileName, string shaderStr) { int statusCode; string info; string flags = ""; if (shaderFileName.Contains(":")) // jos flagit (esim :TEXTURE) { int start = shaderFileName.IndexOf(':'); flags = shaderFileName.Substring(start, shaderFileName.Length - start); } // jos shader jo ladattu, käytetään sitä if (shaders.ContainsKey(shaderFileName)) { ProgramID = shaders[shaderFileName].ProgramID; vertexObject = shaders[shaderFileName].vertexObject; fragmentObject = shaders[shaderFileName].fragmentObject; return; } Log.WriteLine("Shader: " + shaderFileName); shaders.Add(shaderFileName, this); if (shaderStr.Contains("[SETUP]") == false) { shaderStr = "[SETUP]\n" + shaderStr; } int s = shaderStr.IndexOf("[SETUP]") + 8; int v = shaderStr.IndexOf("[VERTEX]") + 9; int f = shaderStr.IndexOf("[FRAGMENT]") + 11; string set = shaderStr.Substring(s, v - s - 9); string vs = shaderStr.Substring(v, f - v - 11); string fs = shaderStr.Substring(f, shaderStr.Length - f); // käy flagsit läpi, #define flags string[] flag = flags.Split(':'); for (int q = 0; q < flag.Length; q++) { if (flag[q].Length > 0) { set += "\n#define " + flag[q]; } } // käy [SETUP] blokki läpi, aseta oikeat definet string[] setup = set.Split('\n'); for (int q = 0; q < setup.Length; q++) { if (setup[q].StartsWith("//")) { continue; // skippaa kommentit } else if (setup[q].StartsWith("#define")) { vs = setup[q] + "\n" + vs; fs = setup[q] + "\n" + fs; } } // konvaa gl2:n glsl koodit gl3:sen glsl:ksi if (Settings.UseGL3) { string gl3 = @"#version 130 precision highp float; "; vs = gl3 + vs; vs = vs.Replace("attribute ", "in "); vs = vs.Replace("varying ", "out "); fs = gl3 + "out vec4 glFragColor;\n" + fs; fs = fs.Replace("gl_FragColor", "glFragColor"); fs = fs.Replace("varying ", "in "); fs = fs.Replace("texture2D", "texture"); fs = fs.Replace("shadow2DProj", "textureProj"); } #if DEBUG #if SHOWSHADERS System.Console.WriteLine("----VS------"); string[] lines = vs.Split('\n'); for (int q = 0; q < lines.Length; q++) { System.Console.WriteLine("" + (q + 1) + ": " + lines[q]); } System.Console.WriteLine("----FS------"); lines = fs.Split('\n'); for (int q = 0; q < lines.Length; q++) { System.Console.WriteLine("" + (q + 1) + ": " + lines[q]); } System.Console.Write("\n"); #endif #endif vertexObject = GL.CreateShader(ShaderType.VertexShader); fragmentObject = GL.CreateShader(ShaderType.FragmentShader); // Compile vertex shader GL.ShaderSource(vertexObject, vs); GL.CompileShader(vertexObject); GL.GetShaderInfoLog(vertexObject, out info); GL.GetShader(vertexObject, ShaderParameter.CompileStatus, out statusCode); if (statusCode != 1) { Log.Error(shaderFileName + ":\n" + info); } else { Log.WriteLine(info); } if (info.IndexOf('.') > 0) { Log.WriteLine(info.Substring(0, info.IndexOf('.'))); } // Compile fragment shader GL.ShaderSource(fragmentObject, fs); GL.CompileShader(fragmentObject); GL.GetShaderInfoLog(fragmentObject, out info); GL.GetShader(fragmentObject, ShaderParameter.CompileStatus, out statusCode); if (statusCode != 1) { Log.Error(shaderFileName + ":\n" + info); } else { Log.WriteLine(info); } if (info.IndexOf('.') > 0) { Log.WriteLine(info.Substring(0, info.IndexOf('.'))); } ProgramID = GL.CreateProgram(); GL.AttachShader(ProgramID, fragmentObject); GL.AttachShader(ProgramID, vertexObject); GL.LinkProgram(ProgramID); Log.WriteLine(GL.GetProgramInfoLog(ProgramID)); if (GL.GetProgramInfoLog(ProgramID).Contains("error")) { Log.Error("GLSL compiling error."); } GLExt.CheckGLError("GLSL"); }