Abstract picking renderer.

This commit is contained in:
UnknownShadow200 2015-06-28 13:51:35 +10:00
parent 589a9e0beb
commit 43fea6708f
6 changed files with 138 additions and 101 deletions

View file

@ -22,7 +22,7 @@ namespace DefaultPlugin {
public override IEnumerable<PluginModule> Modules {
get {
return new List<PluginModule>() {
return new [] {
new PluginModule( PluginModuleType.Command, typeof( CommandsCommand ) ),
new PluginModule( PluginModuleType.Command, typeof( HelpCommand ) ),
new PluginModule( PluginModuleType.Command, typeof( EnvCommand ) ),
@ -51,6 +51,7 @@ namespace DefaultPlugin {
new PluginModule( PluginModuleType.NetworkProcessor, typeof( ClassicNetworkProcessor ) ),
new PluginModule( PluginModuleType.BlockInfo, typeof( ClassicBlockInfo ) ),
new PluginModule( PluginModuleType.PickingRenderer, typeof( StandardPickingRenderer ) ),
};
}
}

View file

@ -58,6 +58,7 @@
<Compile Include="Network\GZipHeaderReader.cs" />
<Compile Include="Network\ClassicNetwork.cs" />
<Compile Include="Network\ClassicNetwork.Wom.cs" />
<Compile Include="Renderers\PickingRenderer.cs" />
<Compile Include="Shaders\GrayscaleFilter.cs" />
<Compile Include="StandardChunkMeshBuilder.cs" />
<Compile Include="DefaultCommands.cs" />

View file

@ -0,0 +1,107 @@
using System;
using ClassicalSharp;
using ClassicalSharp.GraphicsAPI;
using ClassicalSharp.Renderers;
using OpenTK;
namespace DefaultPlugin {
public sealed class StandardPickingRenderer : PickingRenderer {
int vb;
PickingShader shader;
public StandardPickingRenderer( Game window ) : base( window ) {
this.window = window;
graphics = window.Graphics;
}
public override void Init() {
vb = graphics.CreateDynamicVb( Vector3.SizeInBytes, verticesCount );
shader = new PickingShader();
shader.Init( graphics );
}
int index = 0;
const int verticesCount = 24 * ( 3 * 2 );
Vector3[] vertices = new Vector3[verticesCount];
const float size = 0.0625f;
const float offset = 0.01f;
public override void Render( double delta, PickedPos pickedPos ) {
index = 0;
shader.Bind();
shader.UpdateFogAndMVPState( ref window.MVP );
Vector3 p1 = pickedPos.Min - new Vector3( offset, offset, offset );
Vector3 p2 = pickedPos.Max + new Vector3( offset, offset, offset );
// bottom face
DrawYPlane( p1.Y, p1.X, p1.Z, p1.X + size, p2.Z );
DrawYPlane( p1.Y, p2.X, p1.Z, p2.X - size, p2.Z );
DrawYPlane( p1.Y, p1.X, p1.Z, p2.X, p1.Z + size );
DrawYPlane( p1.Y, p1.X, p2.Z, p2.X, p2.Z - size );
// top face
DrawYPlane( p2.Y, p1.X, p1.Z, p1.X + size, p2.Z );
DrawYPlane( p2.Y, p2.X, p1.Z, p2.X - size, p2.Z );
DrawYPlane( p2.Y, p1.X, p1.Z, p2.X, p1.Z + size );
DrawYPlane( p2.Y, p1.X, p2.Z, p2.X, p2.Z - size );
// left face
DrawXPlane( p1.X, p1.Z, p1.Y, p1.Z + size, p2.Y );
DrawXPlane( p1.X, p2.Z, p1.Y, p2.Z - size, p2.Y );
DrawXPlane( p1.X, p1.Z, p1.Y, p2.Z, p1.Y + size );
DrawXPlane( p1.X, p1.Z, p2.Y, p2.Z, p2.Y - size );
// right face
DrawXPlane( p2.X, p1.Z, p1.Y, p1.Z + size, p2.Y );
DrawXPlane( p2.X, p2.Z, p1.Y, p2.Z - size, p2.Y );
DrawXPlane( p2.X, p1.Z, p1.Y, p2.Z, p1.Y + size );
DrawXPlane( p2.X, p1.Z, p2.Y, p2.Z, p2.Y - size );
// front face
DrawZPlane( p1.Z, p1.X, p1.Y, p1.X + size, p2.Y );
DrawZPlane( p1.Z, p2.X, p1.Y, p2.X - size, p2.Y );
DrawZPlane( p1.Z, p1.X, p1.Y, p2.X, p1.Y + size );
DrawZPlane( p1.Z, p1.X, p2.Y, p2.X, p2.Y - size );
// back face
DrawZPlane( p2.Z, p1.X, p1.Y, p1.X + size, p2.Y );
DrawZPlane( p2.Z, p2.X, p1.Y, p2.X - size, p2.Y );
DrawZPlane( p2.Z, p1.X, p1.Y, p2.X, p1.Y + size );
DrawZPlane( p2.Z, p1.X, p2.Y, p2.X, p2.Y - size );
shader.DrawDynamic( DrawMode.Triangles, Vector3.SizeInBytes, vb, vertices, verticesCount );
}
public override void Dispose() {
graphics.DeleteDynamicVb( vb );
}
void DrawXPlane( float x, float z1, float y1, float z2, float y2 ) {
vertices[index++] = new Vector3( x, y1, z1 );
vertices[index++] = new Vector3( x, y2, z1 );
vertices[index++] = new Vector3( x, y2, z2 );
vertices[index++] = new Vector3( x, y2, z2 );
vertices[index++] = new Vector3( x, y1, z2 );
vertices[index++] = new Vector3( x, y1, z1 );
}
void DrawZPlane( float z, float x1, float y1, float x2, float y2 ) {
vertices[index++] = new Vector3( x1, y1, z );
vertices[index++] = new Vector3( x1, y2, z );
vertices[index++] = new Vector3( x2, y2, z );
vertices[index++] = new Vector3( x2, y2, z );
vertices[index++] = new Vector3( x2, y1, z );
vertices[index++] = new Vector3( x1, y1, z );
}
void DrawYPlane( float y, float x1, float z1, float x2, float z2 ) {
vertices[index++] = new Vector3( x1, y, z1 );
vertices[index++] = new Vector3( x1, y, z2 );
vertices[index++] = new Vector3( x2, y, z2 );
vertices[index++] = new Vector3( x2, y, z2 );
vertices[index++] = new Vector3( x2, y, z1 );
vertices[index++] = new Vector3( x1, y, z1 );
}
}
}

View file

@ -49,6 +49,7 @@ namespace ClassicalSharp {
public SelectionManager SelectionManager;
public ParticleManager ParticleManager;
public PickingRenderer Picking;
public List<Type> PickingRendererTypes = new List<Type>();
public PickedPos SelectedPos = new PickedPos();
public ModelCache ModelCache;
public string skinServer, chatInInputBuffer;
@ -140,25 +141,30 @@ namespace ClassicalSharp {
SelectionManager = new SelectionManager( this );
SelectionManager.Init();
ParticleManager = new ParticleManager( this );
ParticleManager.Init();
Picking = new PickingRenderer( this );
ParticleManager.Init();
MapBordersRenderer = Utils.New<MapBordersRenderer>( MapBordersRendererTypes[0], this );
MapBordersRenderer = DefaultImpl<MapBordersRenderer>( MapBordersRendererTypes );
MapBordersRenderer.Init();
EnvRenderer = Utils.New<EnvRenderer>( EnvRendererTypes[0], this );
EnvRenderer = DefaultImpl<EnvRenderer>( EnvRendererTypes );
EnvRenderer.Init();
WeatherRenderer = Utils.New<WeatherRenderer>( WeatherRendererTypes[0], this );
WeatherRenderer = DefaultImpl<WeatherRenderer>( WeatherRendererTypes );
WeatherRenderer.Init();
MapRenderer = Utils.New<MapRenderer>( MapRendererTypes[0], this );
MapRenderer.Init();
MapRenderer = DefaultImpl<MapRenderer>( MapRendererTypes );
MapRenderer.Init();
Picking = DefaultImpl<PickingRenderer>( PickingRendererTypes );
Picking.Init();
string connectString = "Connecting to " + IPAddress + ":" + Port + "..";
SetNewScreen( new LoadingMapScreen( this, connectString, "Reticulating splines" ) );
Network = Utils.New<NetworkProcessor>( NetworkProcessorTypes[0], this );
Network = DefaultImpl<NetworkProcessor>( NetworkProcessorTypes );
Network.Connect( IPAddress, Port );
}
T DefaultImpl<T>( List<Type> candidates ) {
return Utils.New<T>( candidates[0], this );
}
public void SetViewDistance( int distance ) {
ViewDistance = distance;
Utils.LogDebug( "setting view distance to: " + distance );
@ -224,7 +230,9 @@ namespace ClassicalSharp {
RenderPlayers( e.Time, t );
ParticleManager.Render( e.Time, t );
Camera.GetPickedBlock( SelectedPos ); // TODO: only pick when necessary
Picking.Render( e.Time );
if( SelectedPos.Valid ) {
Picking.Render( e.Time, SelectedPos );
}
EnvRenderer.Render( e.Time );
MapRenderer.Render( e.Time );
WeatherRenderer.Render( e.Time );

View file

@ -73,6 +73,10 @@ namespace ClassicalSharp.Plugins {
case PluginModuleType.BlockInfo:
game.BlockInfoTypes.Add( module.Type );
break;
case PluginModuleType.PickingRenderer:
game.PickingRendererTypes.Add( module.Type );
break;
}
}
}
@ -87,6 +91,7 @@ namespace ClassicalSharp.Plugins {
PostProcessingShader,
NetworkProcessor,
BlockInfo,
PickingRenderer,
}
public class PluginModule {

View file

@ -4,105 +4,20 @@ using OpenTK;
namespace ClassicalSharp.Renderers {
public class PickingRenderer {
public abstract class PickingRenderer {
Game window;
OpenGLApi graphics;
int vb;
PickingShader shader;
protected Game window;
protected OpenGLApi graphics;
public PickingRenderer( Game window ) {
this.window = window;
graphics = window.Graphics;
vb = graphics.CreateDynamicVb( Vector3.SizeInBytes, verticesCount );
shader = new PickingShader();
shader.Init( graphics );
}
FastColour col = FastColour.White;
int index = 0;
const int verticesCount = 24 * ( 3 * 2 );
Vector3[] vertices = new Vector3[verticesCount];
const float size = 0.0625f;
const float offset = 0.01f;
public abstract void Init();
public void Render( double delta ) {
index = 0;
PickedPos pickedPos = window.SelectedPos;
if( pickedPos.Valid ) {
shader.Bind();
shader.UpdateFogAndMVPState( ref window.MVP );
Vector3 p1 = pickedPos.Min - new Vector3( offset, offset, offset );
Vector3 p2 = pickedPos.Max + new Vector3( offset, offset, offset );
// bottom face
DrawYPlane( p1.Y, p1.X, p1.Z, p1.X + size, p2.Z );
DrawYPlane( p1.Y, p2.X, p1.Z, p2.X - size, p2.Z );
DrawYPlane( p1.Y, p1.X, p1.Z, p2.X, p1.Z + size );
DrawYPlane( p1.Y, p1.X, p2.Z, p2.X, p2.Z - size );
// top face
DrawYPlane( p2.Y, p1.X, p1.Z, p1.X + size, p2.Z );
DrawYPlane( p2.Y, p2.X, p1.Z, p2.X - size, p2.Z );
DrawYPlane( p2.Y, p1.X, p1.Z, p2.X, p1.Z + size );
DrawYPlane( p2.Y, p1.X, p2.Z, p2.X, p2.Z - size );
// left face
DrawXPlane( p1.X, p1.Z, p1.Y, p1.Z + size, p2.Y );
DrawXPlane( p1.X, p2.Z, p1.Y, p2.Z - size, p2.Y );
DrawXPlane( p1.X, p1.Z, p1.Y, p2.Z, p1.Y + size );
DrawXPlane( p1.X, p1.Z, p2.Y, p2.Z, p2.Y - size );
// right face
DrawXPlane( p2.X, p1.Z, p1.Y, p1.Z + size, p2.Y );
DrawXPlane( p2.X, p2.Z, p1.Y, p2.Z - size, p2.Y );
DrawXPlane( p2.X, p1.Z, p1.Y, p2.Z, p1.Y + size );
DrawXPlane( p2.X, p1.Z, p2.Y, p2.Z, p2.Y - size );
// front face
DrawZPlane( p1.Z, p1.X, p1.Y, p1.X + size, p2.Y );
DrawZPlane( p1.Z, p2.X, p1.Y, p2.X - size, p2.Y );
DrawZPlane( p1.Z, p1.X, p1.Y, p2.X, p1.Y + size );
DrawZPlane( p1.Z, p1.X, p2.Y, p2.X, p2.Y - size );
// back face
DrawZPlane( p2.Z, p1.X, p1.Y, p1.X + size, p2.Y );
DrawZPlane( p2.Z, p2.X, p1.Y, p2.X - size, p2.Y );
DrawZPlane( p2.Z, p1.X, p1.Y, p2.X, p1.Y + size );
DrawZPlane( p2.Z, p1.X, p2.Y, p2.X, p2.Y - size );
shader.DrawDynamic( DrawMode.Triangles, Vector3.SizeInBytes, vb, vertices, verticesCount );
}
}
public abstract void Render( double delta, PickedPos pos );
public void Dispose() {
graphics.DeleteDynamicVb( vb );
}
void DrawXPlane( float x, float z1, float y1, float z2, float y2 ) {
vertices[index++] = new Vector3( x, y1, z1 );
vertices[index++] = new Vector3( x, y2, z1 );
vertices[index++] = new Vector3( x, y2, z2 );
vertices[index++] = new Vector3( x, y2, z2 );
vertices[index++] = new Vector3( x, y1, z2 );
vertices[index++] = new Vector3( x, y1, z1 );
}
void DrawZPlane( float z, float x1, float y1, float x2, float y2 ) {
vertices[index++] = new Vector3( x1, y1, z );
vertices[index++] = new Vector3( x1, y2, z );
vertices[index++] = new Vector3( x2, y2, z );
vertices[index++] = new Vector3( x2, y2, z );
vertices[index++] = new Vector3( x2, y1, z );
vertices[index++] = new Vector3( x1, y1, z );
}
void DrawYPlane( float y, float x1, float z1, float x2, float z2 ) {
vertices[index++] = new Vector3( x1, y, z1 );
vertices[index++] = new Vector3( x1, y, z2 );
vertices[index++] = new Vector3( x2, y, z2 );
vertices[index++] = new Vector3( x2, y, z2 );
vertices[index++] = new Vector3( x2, y, z1 );
vertices[index++] = new Vector3( x1, y, z1 );
}
public abstract void Dispose();
}
}