Added RenderGroup and Camera
This commit is contained in:
@@ -53,6 +53,8 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="Buffers\Buffer.cs" />
|
||||
<Compile Include="Buffers\VertexDataInfo.cs" />
|
||||
<Compile Include="Render\Camera.cs" />
|
||||
<Compile Include="Render\RenderGroup.cs" />
|
||||
<Compile Include="Wrappers\BufferWrap.cs" />
|
||||
<Compile Include="GLObject.cs" />
|
||||
<Compile Include="Wrappers\ProgramWrap.cs" />
|
||||
|
||||
54
Diamond/Render/Camera.cs
Normal file
54
Diamond/Render/Camera.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using OpenTK;
|
||||
|
||||
namespace Diamond.Render
|
||||
{
|
||||
public class Camera
|
||||
{
|
||||
private Vector3 _position = Vector3.Zero;
|
||||
private Vector3 _target = -Vector3.One;
|
||||
private Vector3 _up = Vector3.UnitZ;
|
||||
|
||||
public Matrix4 View;
|
||||
public Matrix4 Projection;
|
||||
|
||||
public Vector3 Position
|
||||
{
|
||||
set
|
||||
{
|
||||
_position = value;
|
||||
UpdateView();
|
||||
}
|
||||
get => _position;
|
||||
}
|
||||
|
||||
public Vector3 Target
|
||||
{
|
||||
set
|
||||
{
|
||||
_target = value;
|
||||
UpdateView();
|
||||
}
|
||||
get => _target;
|
||||
}
|
||||
|
||||
public Vector3 Up
|
||||
{
|
||||
set
|
||||
{
|
||||
_up = value;
|
||||
UpdateView();
|
||||
}
|
||||
get => _up;
|
||||
}
|
||||
|
||||
private void UpdateView()
|
||||
{
|
||||
View = Matrix4.LookAt(_position, _target, _up);
|
||||
}
|
||||
}
|
||||
}
|
||||
46
Diamond/Render/RenderGroup.cs
Normal file
46
Diamond/Render/RenderGroup.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using Diamond.Buffers;
|
||||
using Diamond.Shaders;
|
||||
using Diamond.Textures;
|
||||
using Diamond.Util;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
|
||||
namespace Diamond.Render
|
||||
{
|
||||
public class RenderGroup
|
||||
{
|
||||
public SubArray<TileData> Tiles;
|
||||
public Mesh<ObjVertex> Mesh;
|
||||
|
||||
public Buffer<TileData> TileBuffer;
|
||||
public Buffer<ObjVertex> MeshBuffer;
|
||||
|
||||
public Program Program;
|
||||
public Texture Texture;
|
||||
|
||||
public Camera Camera;
|
||||
|
||||
public void Draw()
|
||||
{
|
||||
Program.Use();
|
||||
|
||||
TileBuffer.PointTo(Program);
|
||||
MeshBuffer.PointTo(Program);
|
||||
|
||||
Texture.Bind(0);
|
||||
|
||||
var texLoc = Program.UniformLocation("tex");
|
||||
var viewLoc = Program.UniformLocation("view");
|
||||
var projLoc = Program.UniformLocation("proj");
|
||||
|
||||
if (texLoc.HasValue)
|
||||
GL.Uniform1(texLoc.Value, 0);
|
||||
if (viewLoc.HasValue)
|
||||
GL.UniformMatrix4(viewLoc.Value, false, ref Camera.View);
|
||||
if (projLoc.HasValue)
|
||||
GL.UniformMatrix4(projLoc.Value, false, ref Camera.Projection);
|
||||
|
||||
Mesh.DrawInstanced(Tiles);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Diamond.Buffers;
|
||||
using Diamond.Render;
|
||||
using Diamond.Shaders;
|
||||
using Diamond.Textures;
|
||||
using Diamond.Util;
|
||||
@@ -27,6 +28,8 @@ namespace hexworld
|
||||
private Buffer<ObjVertex> _meshBuffer;
|
||||
private Buffer<TileData> _tileBuffer;
|
||||
|
||||
private RenderGroup _renderGroup;
|
||||
|
||||
protected override void OnClosed(EventArgs e)
|
||||
{
|
||||
_texPgm?.Dispose();
|
||||
@@ -45,8 +48,7 @@ namespace hexworld
|
||||
|
||||
private Mesh<ObjVertex> _cubeMesh;
|
||||
|
||||
private Matrix4 _view;
|
||||
private Matrix4 _proj;
|
||||
private Camera _camera;
|
||||
|
||||
private double _time;
|
||||
|
||||
@@ -100,6 +102,19 @@ namespace hexworld
|
||||
BufferUsageHint.DynamicDraw, "tile");
|
||||
_meshBuffer = Buffer.FromData(cubeMesh.Vertices.ToArray(), BufferTarget.ArrayBuffer,
|
||||
BufferUsageHint.StaticDraw, "mesh");
|
||||
|
||||
_camera = new Camera();
|
||||
|
||||
_renderGroup = new RenderGroup()
|
||||
{
|
||||
Camera = _camera,
|
||||
Mesh = _cubeMesh,
|
||||
MeshBuffer = _meshBuffer,
|
||||
Program = _texPgm,
|
||||
Texture = _grassTex,
|
||||
TileBuffer = _tileBuffer,
|
||||
Tiles = _floorTiles
|
||||
};
|
||||
}
|
||||
|
||||
protected override void OnUpdateFrame(FrameEventArgs e)
|
||||
@@ -108,9 +123,9 @@ namespace hexworld
|
||||
|
||||
_time += e.Time;
|
||||
|
||||
_view = Matrix4.CreateRotationZ((float) _time / 3) *
|
||||
_camera.View = Matrix4.CreateRotationZ((float) _time / 3) *
|
||||
Matrix4.LookAt(10 * Vector3.One, Vector3.Zero, Vector3.UnitZ);
|
||||
_proj = Matrix4.CreateOrthographic(Width / 100f, Height / 100f, -100, 100);
|
||||
_camera.Projection = Matrix4.CreateOrthographic(Width / 100f, Height / 100f, -100, 100);
|
||||
}
|
||||
|
||||
protected override void OnRenderFrame(FrameEventArgs e)
|
||||
@@ -127,29 +142,7 @@ namespace hexworld
|
||||
GL.Enable(EnableCap.Blend);
|
||||
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
|
||||
|
||||
|
||||
if (_texPgm != null)
|
||||
{
|
||||
_texPgm.Use();
|
||||
|
||||
_meshBuffer.PointTo(_texPgm);
|
||||
_tileBuffer.PointTo(_texPgm);
|
||||
|
||||
_grassTex.Bind(0);
|
||||
|
||||
var texLoc = _texPgm.UniformLocation("tex");
|
||||
var viewLoc = _texPgm.UniformLocation("view");
|
||||
var projLoc = _texPgm.UniformLocation("proj");
|
||||
|
||||
if (texLoc.HasValue)
|
||||
GL.Uniform1(texLoc.Value, 0);
|
||||
if (viewLoc.HasValue)
|
||||
GL.UniformMatrix4(viewLoc.Value, false, ref _view);
|
||||
if (projLoc.HasValue)
|
||||
GL.UniformMatrix4(projLoc.Value, false, ref _proj);
|
||||
|
||||
_cubeMesh.DrawInstanced(_floorTiles);
|
||||
}
|
||||
_renderGroup.Draw();
|
||||
|
||||
SwapBuffers();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user