private static int CreateVAO() { int vaoID; GL.GenVertexArrays(1, out vaoID); GL.BindVertexArray(vaoID); vaos.Add(vaoID); return(vaoID); }
private static void BindIndicesBuffer(int[] indices) { int vboID = 0; GL.GenBuffers(1, out vboID); vbos.Add(vboID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, vboID); GL.BufferData(BufferTarget.ElementArrayBuffer, sizeof(int) * indices.Length, indices, BufferUsageHint.StaticDraw); }
protected virtual void Dispose(bool disposing) { if (_disposedValue) { return; } GL.DeleteProgram(_handle); _disposedValue = true; }
protected override void OnLoad(EventArgs e) { GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f); GL.Enable(EnableCap.DepthTest); InitScene(); CursorVisible = false; base.OnLoad(e); }
protected override void OnRenderFrame(FrameEventArgs e) { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); Scene.DrawScene(); GL.BindVertexArray(0); Context.SwapBuffers(); base.OnRenderFrame(e); }
public static void Cleanup() { foreach (int i in vaos) { GL.DeleteVertexArray(i); } foreach (int i in vbos) { GL.DeleteBuffer(i); } }
private static void StoreDataInAttributeList(int attributeIndex, int size, float[] data) { int vboID = 0; GL.GenBuffers(1, out vboID); vbos.Add(vboID); GL.BindBuffer(BufferTarget.ArrayBuffer, vboID); GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * data.Length, data, BufferUsageHint.StaticDraw); GL.VertexAttribPointer(attributeIndex, size, VertexAttribPointerType.Float, false, 0, 0); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); }
protected override void OnUnload(EventArgs e) { GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindVertexArray(0); GL.UseProgram(0); Scene.ClearHandles(); Shader?.Dispose(); LampShader?.Dispose(); base.OnUnload(e); }
private int CreateShader(string shaderPath, ShaderType shaderType) { var shaderId = GL.CreateShader(shaderType); GL.ShaderSource(shaderId, ReadShaderSource(shaderPath)); GL.CompileShader(shaderId); var infoLogVert = GL.GetShaderInfoLog(shaderId); if (infoLogVert != System.String.Empty) { System.Console.WriteLine(infoLogVert); return(-1); } return(shaderId); }
public void Create(string vertexPath, string fragmentPath) { if (_handle >= 0) { throw new InvalidOperationException("Shader is already loaded"); } var vertexShader = CreateShader(vertexPath, ShaderType.VertexShader); var fragmentShader = CreateShader(fragmentPath, ShaderType.FragmentShader); _handle = GL.CreateProgram(); GL.AttachShader(_handle, vertexShader); GL.AttachShader(_handle, fragmentShader); GL.LinkProgram(_handle); GL.DetachShader(_handle, vertexShader); GL.DetachShader(_handle, fragmentShader); GL.DeleteShader(vertexShader); GL.DeleteShader(fragmentShader); }
public void Draw(double deltaTime) { Debug.Clear(); _selectedBlocks.Clear(); _chunkPartManager.CheckInvalidations(_world); _framerate = (float)(1 / deltaTime); GL.ClearColor(new Color4(0.6f, 0.8f, 0.85f, 1f)); GL.ClearDepth(1); GL.DepthFunc(DepthFunction.Less); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.FrontFace(FrontFaceDirection.Ccw); GL.Enable(EnableCap.CullFace); GL.Disable(EnableCap.PolygonSmooth); GL.Hint(HintTarget.PolygonSmoothHint, HintMode.Fastest); var cameraPostition = _player.Position + new Vector3(0, 1.7f, 0); GL.Disable(EnableCap.DepthTest); var skyboxView = Matrix4.CreateRotationY(_player.Rotations.Y) * Matrix4.CreateRotationX(_player.Rotations.X); var skyboxColor = new Vector3(1, 1, 1); _skyboxShader.Bind(); _skyboxShader.SetUniformMatrix4("proj", false, ref _proj); _skyboxShader.SetUniformMatrix4("view", false, ref skyboxView); _skyboxShader.SetUniform3("col", ref skyboxColor); _skybox.BindVao(_skyboxShader); _skybox.Draw(_skyTexture); GL.Enable(EnableCap.DepthTest); try { HitInfo hitInfo; if (_worldManager.CastRay(_world, cameraPostition, _player.EyeForward, 20f, out hitInfo)) { _selectedBlocks.AddAABB(new AABBObjects.AABBObject { Position = new Vector3(hitInfo.X - 0.001f, hitInfo.Y - 0.001f, hitInfo.Z - 0.001f), Position2 = new Vector3(hitInfo.X + 1.001f, hitInfo.Y + 1.001f, hitInfo.Z + 1.001f), Color = new Vector4(0, 0, 0, 0.4f), }); } } catch (Exception e) { Console.WriteLine(e); } var view = Matrix4.CreateTranslation(-cameraPostition) * Matrix4.CreateRotationY(_player.Rotations.Y) * Matrix4.CreateRotationX(_player.Rotations.X); var mv = (view * _proj); mv.Transpose(); _frustum[0].Nums = mv.Row3 + mv.Row0; _frustum[1].Nums = mv.Row3 - mv.Row0; _frustum[2].Nums = mv.Row3 - mv.Row1; _frustum[3].Nums = mv.Row3 + mv.Row1; _frustum[4].Nums = mv.Row2; _frustum[5].Nums = mv.Row3 - mv.Row2; _baseShader.Bind(); _baseShader.SetUniform3("cameraPosition", ref cameraPostition); _baseShader.SetUniformMatrix4("proj", false, ref _proj); _baseShader.SetUniformMatrix4("view", false, ref view); var chunks = _worldManager.GetVisibleChunks(_world, cameraPostition, _frustum); _visibleChunks = chunks.Count; TextureManager.Use(_terrainTexture); foreach (var chunk in chunks) { _chunkManager.Draw(chunk, _baseShader); } TextureManager.Disable(); _baseShader.Unbind(); GL.Enable(EnableCap.Blend); _aabbShader.Bind(); _aabbShader.SetUniformMatrix4("proj", false, ref _proj); _aabbShader.SetUniformMatrix4("view", false, ref view); _selectedBlocks.UpdateData(); _selectedBlocks.BindVao(_aabbShader); _selectedBlocks.Draw(); _aabbShader.Unbind(); GL.Disable(EnableCap.Blend); // START DEBUG DRAWING GL.Disable(EnableCap.DepthTest); _debugShader.Bind(); _debugShader.SetUniformMatrix4("proj", false, ref _proj); _debugShader.SetUniformMatrix4("view", false, ref view); Debug.UpdateData(); Debug.BindVao(_debugShader); Debug.Draw(); _debugShader.Unbind(); GL.Enable(EnableCap.DepthTest); // END DEBUG DRAWING _guiShader.Bind(); GL.Disable(EnableCap.DepthTest); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); _guiShader.SetUniformMatrix4("proj", false, ref _guiProj); view = Matrix4.CreateTranslation(new Vector3(_text.Position) - new Vector3(_width / 2, _height / 2, 0)); _guiShader.SetUniformMatrix4("view", false, ref view); _text.Draw(); TextureManager.Use(_iconsTexture); view = Matrix4.CreateTranslation(-new Vector3(_crosshair.Size.X / 2, _crosshair.Size.Y / 2, 0)); _guiShader.SetUniformMatrix4("view", false, ref view); _crosshair.Draw(); TextureManager.Disable(); GL.Disable(EnableCap.Blend); GL.Enable(EnableCap.DepthTest); _guiShader.Unbind(); }
public int GetAttribLocation(string attribName) { return(GL.GetAttribLocation(_handle, attribName)); }
public void SetMatrix4(string name, Matrix4 data) { var location = GL.GetUniformLocation(_handle, name); GL.UniformMatrix4(location, true, ref data); }
public void SetVector3(string name, Vector3 value) { var location = GL.GetUniformLocation(_handle, name); GL.Uniform3(location, value); }
public void SetFloat(string name, float value) { var location = GL.GetUniformLocation(_handle, name); GL.Uniform1(location, value); }
public void Use() { GL.UseProgram(_handle); }
private static void UnbindVAO() { GL.BindVertexArray(0); }
protected override void OnResize(EventArgs e) { GL.Viewport(0, 0, Width, Height); Camera.AspectRatio = Width / (float)Height; base.OnResize(e); }