public void CompileProgram(string vertCode, string fragCode) { if (ProgramID > 0) { TryGL.Call(() => GL.DeleteProgram(ProgramID)); } variableIDs.Clear(); ProgramID = GL.CreateProgram(); CompileShader(vertCode, ShaderType.VertexShader); CompileShader(fragCode, ShaderType.FragmentShader); string info = ""; int statusCode = -1; //TryGL.Call(() => GL.BindAttribLocation(ProgramID, 0, "vertexPosition")); //TryGL.Call(() => GL.BindAttribLocation(ProgramID, 1, "vertexUV")); //TryGL.Call(() => GL.BindAttribLocation(ProgramID, 2, "vertexNormal")); TryGL.Call(() => GL.LinkProgram(ProgramID)); TryGL.Call(() => GL.GetProgramInfoLog(ProgramID, out info)); TryGL.Call(() => GL.GetProgram(ProgramID, GetProgramParameterName.LinkStatus, out statusCode)); if (statusCode != 1) { Console.WriteLine("Failed to link shader program."); Console.WriteLine(info); Console.WriteLine("Status Code: " + statusCode); TryGL.Call(() => GL.DeleteProgram(ProgramID)); ProgramID = 0; throw new Exception("Failed to link shader program"); } Console.WriteLine("Linked shader program."); }
private void CompileShader(string code, ShaderType type) { int shaderID = GL.CreateShader(type); string info = ""; int statusCode = -1; TryGL.Call(() => GL.ShaderSource(shaderID, code)); TryGL.Call(() => GL.CompileShader(shaderID)); TryGL.Call(() => GL.GetShaderInfoLog(shaderID, out info)); TryGL.Call(() => GL.GetShader(shaderID, ShaderParameter.CompileStatus, out statusCode)); if (statusCode != 1) { Console.WriteLine("Failed to compile shader source, type: " + type.ToString()); Console.WriteLine(info); Console.WriteLine("Status Code: " + statusCode); TryGL.Call(() => GL.DeleteShader(shaderID)); TryGL.Call(() => GL.DeleteProgram(ProgramID)); ProgramID = 0; throw new Exception("Failed to compile shader source, type: " + type.ToString()); } Console.WriteLine("Compiled shader, type: " + type.ToString()); TryGL.Call(() => GL.AttachShader(ProgramID, shaderID)); TryGL.Call(() => GL.DeleteShader(shaderID)); }
void onResize(object sender, EventArgs e) { TryGL.Call(() => GL.Viewport(0, 0, gameWindow.Width, gameWindow.Height)); settings.windowWidth.value = gameWindow.Width; settings.windowHeight.value = gameWindow.Height; settings.save(); }
void onLoad(object sender, EventArgs e) { Console.Out.WriteLine("onLoad"); gameWindow.VSync = VSyncMode.On; Console.WriteLine("OpenGL version: {0}", GL.GetString(StringName.Version)); Console.WriteLine("Current GLSL version: {0}", GL.GetString(StringName.ShadingLanguageVersion)); TryGL.Call(() => GL.ClearColor(0.2f, 0.0f, 0.2f, 1.0f)); TryGL.Call(() => GL.Enable(EnableCap.DepthTest)); TryGL.Call(() => GL.Enable(EnableCap.CullFace)); TryGL.Call(() => GL.CullFace(CullFaceMode.Back)); TryGL.Call(() => GL.Enable(EnableCap.Blend)); resetBlending(); TryGL.Call(() => GL.BlendEquation(BlendEquationMode.FuncAdd)); int VAO = GL.GenVertexArray(); GL.BindVertexArray(VAO); shader = new ShaderProgram(); shader.LoadAndCompileProrgam(dirShaders + "world.vert.glsl", dirShaders + "world.frag.glsl"); TextureControl.loadTextures(); heavyThread.Start(); loadedMap = new Map(this, "debugMap"); player = new ActorPlayer(); loadedMap.newActor(player); //loadedMap.newActor(new ActorPlayer(loadedMap.actorMaker)); //loadedMap.newActor(new ActorPlayer(loadedMap.actorMaker)); matrixProjection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver2, (float)gameWindow.Width / (float)gameWindow.Height, NEAR, FAR); matrixModel = Matrix4.Identity; matrixCamera = Matrix4.Identity; player.position.X = 64; player.position.Y = 128; player.position.Z = 64; player.target = player.position; hud.onLoad(); markerModel = ModelControl.getModel("marker"); }
public void drawRender() { //Console.Out.WriteLine("Draw chunk: " + Location.X + ", " + Location.Y); switch (compileState) { case COMP_STATUS.NEEDS_TO_BE_MADE: { compileState = COMP_STATUS.CURRENTLY_MAKING; makeThread.Start(); return; } case COMP_STATUS.NEEDS_TO_BE_COMPILED: { compileRender(); compileState = COMP_STATUS.READY_TO_RENDER; return; } case COMP_STATUS.READY_TO_RENDER: { /*if (this as Chunk != null) { * Console.Out.WriteLine("Draw chunk: {0}, {1} VBO:[{2}, {3}] elCount={4}", (this as Chunk).Location.X, (this as Chunk).Location.Y, VBOIDs[0], VBOIDs[1], elementCount); * }else if (this as Actor != null) { * Console.Out.WriteLine("Draw actor: {0} VBO:[{1}, {2}] elCount={3}", (this as Actor).ID, VBOIDs[0], VBOIDs[1], elementCount); * }*/ TryGL.Call(() => GL.BindBuffer(BufferTarget.ArrayBuffer, VBOIDs[0])); TryGL.Call(() => GL.BindBuffer(BufferTarget.ElementArrayBuffer, VBOIDs[1])); TryGL.Call(() => GL.EnableVertexAttribArray(0)); //Positions TryGL.Call(() => GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vertex.StrideToEnd, Vertex.StrideToPosition)); TryGL.Call(() => GL.EnableVertexAttribArray(1)); //UV TryGL.Call(() => GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, Vertex.StrideToEnd, Vertex.StrideToUV)); TryGL.Call(() => GL.EnableVertexAttribArray(2)); //Normals TryGL.Call(() => GL.VertexAttribPointer(2, 3, VertexAttribPointerType.Float, false, Vertex.StrideToEnd, Vertex.StrideToNormal)); TryGL.Call(() => GL.DrawElements(PrimitiveType.Triangles, elementCount, DrawElementsType.UnsignedInt, (IntPtr)null)); //GL.BindBuffer(BufferTarget.ArrayBuffer, 0); //GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); return; } case COMP_STATUS.NEEDS_TO_BE_REMOVED: { cleanupRender(); return; } } }
internal void make() { id = GL.GenTexture(); TryGL.Call(() => GL.BindTexture(TextureTarget.Texture2DArray, id)); TryGL.Call(() => GL.TexStorage3D(TextureTarget3d.Texture2DArray, 1, SizedInternalFormat.Rgba8, size, size, names.Count)); TryGL.Call(() => GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear)); TryGL.Call(() => GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear)); TryGL.Call(() => GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat)); TryGL.Call(() => GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat)); if (subdivide) { coords = new Vector3[names.Count, scale, scale, 4]; for (int index = 0; index < names.Count; index++) { loadTexture(Game.dirTextures + folder + "/" + names[index] + ".png", index); for (int i = 0; i < scale; i++) { for (int j = 0; j < scale; j++) { float x1 = i * (1.0f / scale); float y1 = j * (1.0f / scale); float x2 = (i + 1) * (1.0f / scale); float y2 = (j + 1) * (1.0f / scale); coords[index, i, j, 0] = new Vector3(x1, y1, index); coords[index, i, j, 1] = new Vector3(x2, y1, index); coords[index, i, j, 2] = new Vector3(x1, y2, index); coords[index, i, j, 3] = new Vector3(x2, y2, index); } } } } else { coords = new Vector3[names.Count, 1, 1, 4]; for (int index = 0; index < names.Count; index++) { loadTexture(Game.dirTextures + folder + "/" + names[index] + ".png", index); coords[index, 0, 0, 0] = new Vector3(0, 0, index); coords[index, 0, 0, 1] = new Vector3(1, 0, index); coords[index, 0, 0, 2] = new Vector3(0, 1, index); coords[index, 0, 0, 3] = new Vector3(1, 1, index); } } }
void onRenderFrame(object sender, FrameEventArgs e) { FPS = 1.0 / e.Time; TryGL.Call(() => GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit)); shader.use(); GL.Uniform1(shader.GetUniformID("light.directional"), 1); GL.Uniform3(shader.GetUniformID("light.position"), sunPosition); GL.Uniform3(shader.GetUniformID("light.color"), sunColor); //matrixCamera = Matrix4.CreateTranslation(0.0f, 0.0f, 0.0f); matrixCamera = Matrix4.CreateTranslation(-camPos.X, -camPos.Y, -camPos.Z); matrixCamera *= Matrix4.CreateRotationY(camAngle.Phi); matrixCamera *= Matrix4.CreateRotationX(camAngle.Theta); resetColor(); loadedMap.drawActors(this); loadedMap.drawChunks(this); if (markerCooldown > 0) { GL.Disable(EnableCap.DepthTest); setColor(highlightColor, true); matrixModel = Matrix4.CreateTranslation(editor.active ? editor.focusPosition : player.target); setModel(); markerModel.drawRender(); GL.Enable(EnableCap.DepthTest); resetColor(); resetBlending(); markerCooldown--; } gameWindow.SwapBuffers(); rayWasUpdated = false; }
public void loadTexture(string path, int index) { if (File.Exists(path)) { using (Bitmap bmp = new Bitmap(path)) { BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); TryGL.Call(() => GL.TexSubImage3D(TextureTarget.Texture2DArray, 0, 0, 0, index, bmp.Width, bmp.Height, 1, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmpData.Scan0)); bmp.UnlockBits(bmpData); bmp.Dispose(); Console.WriteLine("Texture loaded: {0}", path); } } else { Console.WriteLine("{0} does not exist.", path); } }
//HORRIBLY SLOW: /*public void tryToAdd(ref Vector3 p, ref Vector3 normal, ref Vector2 UV, ref Dictionary<Vertex, uint> vert, ref List<uint> ind, ref uint nextI) { * Vertex v = new Vertex(p, UV, normal); * if (!vert.ContainsKey(v)) { * vert.Add(v, nextI); * nextI++; * } * ind.Add(vert[v]); * }*/ public void compileRender() { //Console.WriteLine("(Render) Array creation"); Vertex[] vertices = vert.ToArray(); uint[] indecies = ind.ToArray(); elementCount = ind.Count; Console.Out.WriteLine("(Render) Vert Count = " + vert.Count); Console.Out.WriteLine("(Render) Ind Count = " + ind.Count); //Console.WriteLine("(Render) Opengl Chunk compilation calls"); if (VBOIDs == null) { VBOIDs = new uint[2]; TryGL.Call(() => GL.GenBuffers(2, VBOIDs)); Console.WriteLine("(Render) New Buffers: [{0}, {1}]", VBOIDs[0], VBOIDs[1]); } GL.BindBuffer(BufferTarget.ArrayBuffer, VBOIDs[0]); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(Vertex.StrideToEnd * vertices.Length), vertices, BufferUsageHint.StaticDraw); //GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindBuffer(BufferTarget.ElementArrayBuffer, VBOIDs[1]); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(sizeof(uint) * indecies.Length), indecies, BufferUsageHint.StaticDraw); //GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); //Console.WriteLine("(Render) done"); if (!keepVerts) { vert = null; ind = null; } }
public void additiveBlending() { TryGL.Call(() => GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One)); }
public void resetBlending() { TryGL.Call(() => GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha)); }
public void use() { TryGL.Call(() => GL.UseProgram(ProgramID)); }